Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ openai_client = AzureOpenAI(
|
|
14 |
azure_endpoint = "https://hkust.azure-api.net" # per HKUST instructions
|
15 |
)
|
16 |
|
17 |
-
# ─── (your existing cache decorators) ────────────────────────────────────────
|
18 |
@st.cache_resource
|
19 |
def load_sentiment_pipeline():
|
20 |
model_name = "mayf/amazon_reviews_bert_ft"
|
@@ -31,11 +30,9 @@ def load_sentiment_pipeline():
|
|
31 |
def load_keybert_model():
|
32 |
return KeyBERT(model="all-MiniLM-L6-v2")
|
33 |
|
34 |
-
|
35 |
def main():
|
36 |
st.title("📊 Review Sentiment & Keyword Analyzer + GPT Insights")
|
37 |
|
38 |
-
# ─── Inputs & Models ──────────────────────────────────────────────────────
|
39 |
review = st.text_area("Enter your review:")
|
40 |
if not st.button("Analyze Review"):
|
41 |
return
|
@@ -44,15 +41,27 @@ def main():
|
|
44 |
st.warning("Please enter a review to analyze.")
|
45 |
return
|
46 |
|
47 |
-
#
|
48 |
sentiment_pipeline = load_sentiment_pipeline()
|
49 |
kw_model = load_keybert_model()
|
50 |
|
51 |
scores = sentiment_pipeline(review)[0]
|
52 |
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
|
|
53 |
st.subheader("Sentiment Scores")
|
54 |
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
keywords = kw_model.extract_keywords(
|
57 |
review,
|
58 |
keyphrase_ngram_range=(1, 2),
|
@@ -61,50 +70,31 @@ def main():
|
|
61 |
)
|
62 |
st.subheader("Top 3 Keywords")
|
63 |
for kw, score in keywords:
|
64 |
-
st.write(f"
|
65 |
-
|
66 |
-
# ─── Determine Highest Sentiment ───────────────────────────────────────────
|
67 |
-
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
68 |
-
st.subheader("Highest Sentiment")
|
69 |
-
st.write(f"{max_label} (Score: {max_score:.4f})")
|
70 |
|
71 |
-
#
|
72 |
st.subheader("GPT Analysis & Seller Suggestions")
|
73 |
-
|
74 |
-
# build a single text prompt for GPT
|
75 |
prompt = f"""
|
76 |
-
You are a
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
{sentiment_results}
|
84 |
-
|
85 |
-
Top keywords:
|
86 |
-
{[kw for kw, _ in keywords]}
|
87 |
-
|
88 |
-
First, provide a one-paragraph professional analysis of what the customer feels and why (combine sentiment + keywords).
|
89 |
-
Then, give 3 detailed, actionable suggestions the seller can implement to improve future reviews or address the feedback.
|
90 |
"""
|
91 |
|
92 |
-
# call Azure OpenAI
|
93 |
response = openai_client.chat.completions.create(
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
)
|
102 |
-
|
103 |
-
# display GPT’s reply
|
104 |
-
gpt_reply = response.choices[0].message.content
|
105 |
st.markdown(gpt_reply)
|
106 |
|
107 |
-
|
108 |
if __name__ == "__main__":
|
109 |
-
# make sure your env vars are set: AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT
|
110 |
main()
|
|
|
14 |
azure_endpoint = "https://hkust.azure-api.net" # per HKUST instructions
|
15 |
)
|
16 |
|
|
|
17 |
@st.cache_resource
|
18 |
def load_sentiment_pipeline():
|
19 |
model_name = "mayf/amazon_reviews_bert_ft"
|
|
|
30 |
def load_keybert_model():
|
31 |
return KeyBERT(model="all-MiniLM-L6-v2")
|
32 |
|
|
|
33 |
def main():
|
34 |
st.title("📊 Review Sentiment & Keyword Analyzer + GPT Insights")
|
35 |
|
|
|
36 |
review = st.text_area("Enter your review:")
|
37 |
if not st.button("Analyze Review"):
|
38 |
return
|
|
|
41 |
st.warning("Please enter a review to analyze.")
|
42 |
return
|
43 |
|
44 |
+
# Sentiment & Keywords
|
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 |
st.subheader("Sentiment Scores")
|
52 |
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
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 |
+
# Highest sentiment
|
60 |
+
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
61 |
+
st.subheader("Highest Sentiment")
|
62 |
+
st.write(f"**{max_label}** ({max_score:.4f})")
|
63 |
+
|
64 |
+
# Top 3 keywords
|
65 |
keywords = kw_model.extract_keywords(
|
66 |
review,
|
67 |
keyphrase_ngram_range=(1, 2),
|
|
|
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 (concise)
|
76 |
st.subheader("GPT Analysis & Seller Suggestions")
|
|
|
|
|
77 |
prompt = f"""
|
78 |
+
You are a concise e-commerce feedback analyst.
|
79 |
+
Review: """{review}"""
|
80 |
+
Scores: {sentiment_results}
|
81 |
+
Keywords: {[kw for kw, _ in keywords]}
|
82 |
+
Provide:
|
83 |
+
1. One-sentence summary of customer sentiment.
|
84 |
+
2. Three bullet-point suggestions, each no more than 8 words.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
"""
|
86 |
|
|
|
87 |
response = openai_client.chat.completions.create(
|
88 |
+
model="gpt-35-turbo",
|
89 |
+
messages=[
|
90 |
+
{"role": "system", "content": "You are a product-feedback analyst."},
|
91 |
+
{"role": "user", "content": prompt}
|
92 |
+
],
|
93 |
+
temperature=0.5,
|
94 |
+
max_tokens=120
|
95 |
+
)
|
96 |
+
gpt_reply = response.choices[0].message.content.strip()
|
|
|
|
|
97 |
st.markdown(gpt_reply)
|
98 |
|
|
|
99 |
if __name__ == "__main__":
|
|
|
100 |
main()
|