Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def load_keybert_model():
|
|
32 |
|
33 |
|
34 |
def main():
|
35 |
-
st.title("📊 Review
|
36 |
|
37 |
review = st.text_area("Enter your review:")
|
38 |
if not st.button("Analyze Review"):
|
@@ -41,13 +41,30 @@ def main():
|
|
41 |
st.warning("Please enter a review to analyze.")
|
42 |
return
|
43 |
|
|
|
|
|
|
|
44 |
# Load models
|
|
|
45 |
sentiment_pipeline = load_sentiment_pipeline()
|
46 |
kw_model = load_keybert_model()
|
|
|
47 |
|
48 |
# Run sentiment analysis
|
|
|
49 |
scores = sentiment_pipeline(review)[0]
|
50 |
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# Display scores and keywords side by side
|
53 |
col1, col2 = st.columns(2)
|
@@ -56,26 +73,22 @@ def main():
|
|
56 |
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
57 |
with col2:
|
58 |
st.subheader("Top 3 Keywords")
|
59 |
-
keywords = kw_model.extract_keywords(
|
60 |
-
review,
|
61 |
-
keyphrase_ngram_range=(1, 2),
|
62 |
-
stop_words="english",
|
63 |
-
top_n=3
|
64 |
-
)
|
65 |
for kw, score in keywords:
|
66 |
st.write(f"• {kw} ({score:.4f})")
|
67 |
|
68 |
-
# Bar chart
|
|
|
69 |
df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
|
70 |
df_scores.index.name = 'label'
|
71 |
st.bar_chart(df_scores)
|
|
|
72 |
|
73 |
-
# Highlight highest sentiment
|
74 |
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
75 |
st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
|
76 |
|
77 |
-
# GPT-Driven Analysis & Suggestions
|
78 |
-
|
79 |
prompt = f"""
|
80 |
You are an analytical e-commerce feedback expert.
|
81 |
Review: \"{review}\"
|
@@ -98,5 +111,9 @@ Tasks:
|
|
98 |
gpt_reply = response.choices[0].message.content.strip()
|
99 |
st.markdown(gpt_reply)
|
100 |
|
|
|
|
|
|
|
|
|
101 |
if __name__ == "__main__":
|
102 |
main()
|
|
|
32 |
|
33 |
|
34 |
def main():
|
35 |
+
st.title("📊 Review Analyzer")
|
36 |
|
37 |
review = st.text_area("Enter your review:")
|
38 |
if not st.button("Analyze Review"):
|
|
|
41 |
st.warning("Please enter a review to analyze.")
|
42 |
return
|
43 |
|
44 |
+
# Initialize progress bar
|
45 |
+
progress = st.progress(0)
|
46 |
+
|
47 |
# Load models
|
48 |
+
progress.text("Loading models...")
|
49 |
sentiment_pipeline = load_sentiment_pipeline()
|
50 |
kw_model = load_keybert_model()
|
51 |
+
progress.progress(20)
|
52 |
|
53 |
# Run sentiment analysis
|
54 |
+
progress.text("Analyzing sentiment...")
|
55 |
scores = sentiment_pipeline(review)[0]
|
56 |
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
57 |
+
progress.progress(40)
|
58 |
+
|
59 |
+
# Extract keywords
|
60 |
+
progress.text("Extracting keywords...")
|
61 |
+
keywords = kw_model.extract_keywords(
|
62 |
+
review,
|
63 |
+
keyphrase_ngram_range=(1, 2),
|
64 |
+
stop_words="english",
|
65 |
+
top_n=3
|
66 |
+
)
|
67 |
+
progress.progress(60)
|
68 |
|
69 |
# Display scores and keywords side by side
|
70 |
col1, col2 = st.columns(2)
|
|
|
73 |
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
74 |
with col2:
|
75 |
st.subheader("Top 3 Keywords")
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
for kw, score in keywords:
|
77 |
st.write(f"• {kw} ({score:.4f})")
|
78 |
|
79 |
+
# Bar chart
|
80 |
+
progress.text("Rendering chart...")
|
81 |
df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
|
82 |
df_scores.index.name = 'label'
|
83 |
st.bar_chart(df_scores)
|
84 |
+
progress.progress(80)
|
85 |
|
86 |
+
# Highlight highest sentiment
|
87 |
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
88 |
st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
|
89 |
|
90 |
+
# GPT-Driven Analysis & Suggestions
|
91 |
+
progress.text("Generating insights...")
|
92 |
prompt = f"""
|
93 |
You are an analytical e-commerce feedback expert.
|
94 |
Review: \"{review}\"
|
|
|
111 |
gpt_reply = response.choices[0].message.content.strip()
|
112 |
st.markdown(gpt_reply)
|
113 |
|
114 |
+
# Complete
|
115 |
+
progress.progress(100)
|
116 |
+
progress.text("Done!")
|
117 |
+
|
118 |
if __name__ == "__main__":
|
119 |
main()
|