Update app.py
Browse files
app.py
CHANGED
@@ -41,47 +41,49 @@ def main():
|
|
41 |
st.warning("Please enter a review to analyze.")
|
42 |
return
|
43 |
|
44 |
-
#
|
45 |
sentiment_pipeline = load_sentiment_pipeline()
|
46 |
kw_model = load_keybert_model()
|
47 |
|
|
|
48 |
scores = sentiment_pipeline(review)[0]
|
49 |
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Bar chart of sentiment scores
|
55 |
df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
|
56 |
df_scores.index.name = 'label'
|
57 |
st.bar_chart(df_scores)
|
58 |
|
59 |
-
#
|
60 |
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
61 |
-
st.subheader("Highest Sentiment")
|
62 |
st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
|
63 |
|
64 |
-
# Top 3 keywords
|
65 |
-
keywords = kw_model.extract_keywords(
|
66 |
-
review,
|
67 |
-
keyphrase_ngram_range=(1, 2),
|
68 |
-
stop_words="english",
|
69 |
-
top_n=3
|
70 |
-
)
|
71 |
-
st.subheader("Top 3 Keywords")
|
72 |
-
for kw, score in keywords:
|
73 |
-
st.write(f"• {kw} ({score:.4f})")
|
74 |
-
|
75 |
# GPT-Driven Analysis & Suggestions (detailed)
|
76 |
st.subheader("GPT Analysis & Seller Suggestions")
|
77 |
prompt = f"""
|
78 |
You are an analytical e-commerce feedback expert.
|
79 |
-
Review: "{review}"
|
80 |
Sentiment Scores: {sentiment_results}
|
81 |
Top Keywords: {[kw for kw, _ in keywords]}
|
82 |
Tasks:
|
83 |
1. Write a concise paragraph (2 sentences) interpreting customer sentiment by combining the scores and keywords.
|
84 |
-
2. Provide 3 actionable suggestions with brief explanations (up to
|
85 |
"""
|
86 |
|
87 |
response = openai_client.chat.completions.create(
|
|
|
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)
|
54 |
+
with col1:
|
55 |
+
st.subheader("Sentiment Scores")
|
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 of sentiment scores
|
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 without subheader
|
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 (detailed)
|
78 |
st.subheader("GPT Analysis & Seller Suggestions")
|
79 |
prompt = f"""
|
80 |
You are an analytical e-commerce feedback expert.
|
81 |
+
Review: \"{review}\"
|
82 |
Sentiment Scores: {sentiment_results}
|
83 |
Top Keywords: {[kw for kw, _ in keywords]}
|
84 |
Tasks:
|
85 |
1. Write a concise paragraph (2 sentences) interpreting customer sentiment by combining the scores and keywords.
|
86 |
+
2. Provide 3 actionable suggestions with brief explanations (up to 12 words each).
|
87 |
"""
|
88 |
|
89 |
response = openai_client.chat.completions.create(
|