-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
164 lines (133 loc) · 6.37 KB
/
Copy pathstreamlit_app.py
File metadata and controls
164 lines (133 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import streamlit as st
# Initialize session state variables at the top level
if 'key' not in st.session_state:
st.session_state.key = 'value'
if "clicked" not in st.session_state:
st.session_state.clicked = False
if "reviews" not in st.session_state:
st.session_state.reviews = None
from streamlit_lottie import st_lottie
import datetime
import pandas as pd
from transformers import pipeline
from src.utils import app_store_reviews, generate_wordcloud, create_rating_distribution_plot, app_data_from_url
import requests
#from TTS.api import TTS
st.title("Apple Store Review Summariser :iphone:")
# animated stars
def load_lottieurl(url: str):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
lottie_stars = load_lottieurl(
"https://lottie.host/05701045-44c3-4741-a484-dbcc7f923cf7/C8quegn0oG.json"
)
st_lottie(lottie_stars, height=300, key="stars_animation")
# include text to explain the app
st.markdown(
"""
This Streamlit app provides insightful analysis of App Store reviews for any given app.
By inputting the App Store URL, users can summaries of positive and negative reviews within a specified date range.
"""
)
st.markdown(
"""
1. **Enter the URL of your app** in the input field above. To find the URL, open the app in the App Store and copy the URL from the address bar. For example, to analyze the reviews for the app 'Slack', you would copy the URL [https://apps.apple.com/de/app/slack/id618783545](https://apps.apple.com/de/app/slack/id618783545).
2. **Select the date range** for the reviews you want to analyze.
3. Click the **'Analyze reviews'** button to start the analysis.
""",
unsafe_allow_html=True
)
app_store_url = st.text_input("**Your App Store URL** 📱", placeholder="https://apps.apple.com/...")
today = datetime.datetime.now()
default_start_date = datetime.date(today.year - 1, 1, 1)
date_range = st.date_input(
"Date range",
value=(default_start_date, today),
max_value=today,
format="DD.MM.YYYY",
)
start_date = date_range[0].strftime("%Y-%m-%d")
end_date = today.strftime("%Y-%m-%d") # Default to today if only one date is picked
if len(date_range) > 1:
end_date = date_range[1].strftime("%Y-%m-%d")
def get_reviews():
reviews = app_store_reviews(url=app_store_url, start_date=start_date, end_date=end_date)
return reviews
def click_button():
st.session_state.clicked = True
# def text_to_speech(text):
# # Initialize the TTS object with default settings
# tts = TTS()
# # Assuming the method to generate speech is called 'synthesize' or similar
# try:
# speech = tts.synthesize(text)
# return speech
# except AttributeError:
# # Handle the case where the 'synthesize' method does not exist
# st.error("Failed to generate speech. Please check the TTS library's documentation for the correct method.")
st.button("Analyze reviews", on_click=click_button)
@st.cache_resource
def summarize_reviews(reviews_text, model_name="sshleifer/distilbart-cnn-12-6"):
summarizer = pipeline("summarization", model=model_name)
reviews_text = reviews_text[:1024] # Ensure the text does not exceed 1024 characters
try:
summary = summarizer(reviews_text, max_length=130, min_length=30, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"Error summarizing reviews: {str(e)}"
if st.session_state.clicked:
with st.spinner("Loading reviews..."):
if start_date and end_date and start_date < end_date:
st.session_state.reviews = get_reviews() # Store reviews in session state
if st.session_state.reviews is not None:
# Show success message and analysis only if reviews are loaded
n_reviews_found = len(st.session_state.reviews)
st.toast(f"🎉 Reviews successfully loaded! Found {n_reviews_found} reviews.")
# Continue analysis only if reviews are loaded
country, app_name, app_id = app_data_from_url(app_store_url)
p_positive_reviews = len(
st.session_state.reviews[st.session_state.reviews["rating"] > 3]
) / len(st.session_state.reviews) if len(st.session_state.reviews) > 0 else 0
# Generate an introductory text for the analysis
intro_text = f'The App **"{app_name.capitalize()}"** received **{n_reviews_found} App Store reviews** in the \
selected time frame. About **{round(p_positive_reviews * 100)}% of these \
reviews were positive**, with a rating of 4 or 5 stars.'
st.write(intro_text)
# Summarize positive reviews
st.subheader("🤩 Highlights")
if n_reviews_found > 0:
positive_reviews = " ".join(
st.session_state.reviews[st.session_state.reviews["rating"] > 3]["review"].tolist()
)
positive_summary = summarize_reviews(positive_reviews)
st.write("The following points were highlighted by satisfied users:")
st.write(positive_summary)
# Convert the summary to speech and display the audio player
# positive_speech = text_to_speech(positive_summary)
# st.audio(positive_speech, format='audio/mp3', start_time=0)
# else:
st.write("No positive reviews to summarize.")
# Summarize negative reviews
st.subheader("🤔 Room for improvement")
if n_reviews_found > 0:
negative_reviews = " ".join(
st.session_state.reviews[st.session_state.reviews["rating"] < 4]["review"].tolist()
)
negative_summary = summarize_reviews(negative_reviews)
st.write("The following issues were raised by dissatisfied users:")
st.write(negative_summary)
# # Convert the summary to speech and display the audio player
# negative_speech = text_to_speech(negative_summary)
# st.audio(negative_speech, format='audio/mp3', start_time=0)
else:
st.write("No negative reviews to summarize.")
# Show rating distribution
st.subheader("Rating distribution")
fig = create_rating_distribution_plot(st.session_state.reviews)
st.plotly_chart(fig)
image = generate_wordcloud(st.session_state.reviews)
st.image(image, caption="Word Cloud", use_column_width=True)
with st.expander("Inspect raw data"):
st.dataframe(st.session_state.reviews)