mayf commited on
Commit
443a3e8
·
verified ·
1 Parent(s): ef53265

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -6,12 +6,13 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassifica
6
  from keybert import KeyBERT
7
 
8
  # ─── DeepSeek Model Client ────────────────────────────────────────────────────
9
- # High-level helper pipeline for text-generation (flattened chat messages)
10
  pipe = pipeline(
11
  "text-generation",
12
  model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
13
  trust_remote_code=True
14
  )
 
15
  @st.cache_resource
16
  def load_sentiment_pipeline():
17
  model_name = "mayf/amazon_reviews_bert_ft"
@@ -39,6 +40,7 @@ LABEL_MAP = {
39
  "LABEL_4": "Very Positive"
40
  }
41
 
 
42
  def main():
43
  st.title("📊 Amazon Review Analyzer")
44
 
@@ -49,7 +51,6 @@ def main():
49
  st.warning("Please enter a review to analyze.")
50
  return
51
 
52
- # Initialize progress bar
53
  progress = st.progress(0)
54
 
55
  # Load models
@@ -58,13 +59,15 @@ def main():
58
  kw_model = load_keybert_model()
59
  progress.progress(20)
60
 
61
- # Run sentiment analysis
62
  progress.text("Analyzing sentiment...")
63
  raw_scores = sentiment_pipeline(review)[0]
64
- sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores}
 
 
65
  progress.progress(40)
66
 
67
- # Extract keywords
68
  progress.text("Extracting keywords...")
69
  keywords = kw_model.extract_keywords(
70
  review,
@@ -74,54 +77,56 @@ def main():
74
  )
75
  progress.progress(60)
76
 
77
- # Display scores and keywords side by side
78
  col1, col2 = st.columns(2)
79
  with col1:
80
  st.subheader("Sentiment Scores")
81
  st.json({k: round(v, 4) for k, v in sentiment_results.items()})
82
  with col2:
83
- st.subheader("Top 3 Keywords")
84
  for kw, score in keywords:
85
  st.write(f"• {kw} ({score:.4f})")
86
 
87
  # Bar chart
88
  progress.text("Rendering chart...")
89
- df_scores = pd.DataFrame.from_dict(sentiment_results, orient='index', columns=['score'])
 
 
90
  df_scores.index.name = 'Sentiment'
91
  st.bar_chart(df_scores)
92
  progress.progress(80)
93
 
94
  # Highlight highest sentiment
95
- max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
 
 
96
  st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
97
 
98
  # GPT-Driven Analysis & Suggestions
99
  progress.text("Generating insights...")
 
100
  prompt = f"""
101
  You are an analytical amazon feedback expert.
102
- Review: "{review}"
103
  Sentiment Scores: {sentiment_results}
104
  Top Keywords: {[kw for kw, _ in keywords]}
105
  Tasks:
106
  1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
107
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
108
  """
109
-
110
- # Use the high-level text-generation pipeline with flattened prompt
111
- # Flatten roles into a single text prompt
112
- flat_prompt = "
113
- ".join(f"{msg['role'].upper()}: {msg['content']}" for msg in chat_input)
 
 
 
 
 
114
  gen_output = pipe(flat_prompt, max_new_tokens=200)
115
  gpt_reply = gen_output[0]['generated_text']
116
 
117
- # Alternative: direct model invocation
118
- # inputs = tokenizer_ds(flat_prompt, return_tensors="pt")
119
- # outputs = model_ds.generate(**inputs, max_new_tokens=200)
120
- # gpt_reply = tokenizer_ds.decode(outputs[0], skip_special_tokens=True)
121
- # inputs = tokenizer_ds(prompt, return_tensors="pt")
122
- # outputs = model_ds.generate(**inputs, max_new_tokens=200)
123
- # gpt_reply = tokenizer_ds.decode(outputs[0], skip_special_tokens=True)
124
-
125
  st.markdown(gpt_reply)
126
  progress.progress(100)
127
  progress.text("Done!")
@@ -129,3 +134,4 @@ Tasks:
129
  if __name__ == "__main__":
130
  main()
131
 
 
 
6
  from keybert import KeyBERT
7
 
8
  # ─── DeepSeek Model Client ────────────────────────────────────────────────────
9
+ # High-level helper pipeline for text-generation
10
  pipe = pipeline(
11
  "text-generation",
12
  model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
13
  trust_remote_code=True
14
  )
15
+
16
  @st.cache_resource
17
  def load_sentiment_pipeline():
18
  model_name = "mayf/amazon_reviews_bert_ft"
 
40
  "LABEL_4": "Very Positive"
41
  }
42
 
43
+
44
  def main():
45
  st.title("📊 Amazon Review Analyzer")
46
 
 
51
  st.warning("Please enter a review to analyze.")
52
  return
53
 
 
54
  progress = st.progress(0)
55
 
56
  # Load models
 
59
  kw_model = load_keybert_model()
60
  progress.progress(20)
61
 
62
+ # Sentiment analysis
63
  progress.text("Analyzing sentiment...")
64
  raw_scores = sentiment_pipeline(review)[0]
65
+ sentiment_results = {
66
+ LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores
67
+ }
68
  progress.progress(40)
69
 
70
+ # Keyword extraction
71
  progress.text("Extracting keywords...")
72
  keywords = kw_model.extract_keywords(
73
  review,
 
77
  )
78
  progress.progress(60)
79
 
80
+ # Display results
81
  col1, col2 = st.columns(2)
82
  with col1:
83
  st.subheader("Sentiment Scores")
84
  st.json({k: round(v, 4) for k, v in sentiment_results.items()})
85
  with col2:
86
+ st.subheader("Top Keywords")
87
  for kw, score in keywords:
88
  st.write(f"• {kw} ({score:.4f})")
89
 
90
  # Bar chart
91
  progress.text("Rendering chart...")
92
+ df_scores = pd.DataFrame.from_dict(
93
+ sentiment_results, orient='index', columns=['score']
94
+ )
95
  df_scores.index.name = 'Sentiment'
96
  st.bar_chart(df_scores)
97
  progress.progress(80)
98
 
99
  # Highlight highest sentiment
100
+ max_label, max_score = max(
101
+ sentiment_results.items(), key=lambda x: x[1]
102
+ )
103
  st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
104
 
105
  # GPT-Driven Analysis & Suggestions
106
  progress.text("Generating insights...")
107
+ # Build the prompt
108
  prompt = f"""
109
  You are an analytical amazon feedback expert.
110
+ Review: \"{review}\"
111
  Sentiment Scores: {sentiment_results}
112
  Top Keywords: {[kw for kw, _ in keywords]}
113
  Tasks:
114
  1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
115
  2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
116
  """
117
+ # Prepare chat messages
118
+ chat_input = [
119
+ {"role": "system", "content": "You are a product-feedback analyst."},
120
+ {"role": "user", "content": prompt}
121
+ ]
122
+ # Flatten into a single text prompt
123
+ flat_prompt = "\n".join(
124
+ f"{msg['role'].upper()}: {msg['content']}" for msg in chat_input
125
+ )
126
+ # Generate
127
  gen_output = pipe(flat_prompt, max_new_tokens=200)
128
  gpt_reply = gen_output[0]['generated_text']
129
 
 
 
 
 
 
 
 
 
130
  st.markdown(gpt_reply)
131
  progress.progress(100)
132
  progress.text("Done!")
 
134
  if __name__ == "__main__":
135
  main()
136
 
137
+