mayf commited on
Commit
1341687
·
verified ·
1 Parent(s): 8ee7329

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -33,15 +33,13 @@ def load_keybert_model():
33
  # ─── FLAN-T5 Generation Pipeline ────────────────────────────────────────────
34
  @st.cache_resource
35
  def load_flant5_pipeline():
36
- # Explicitly load the Seq2Seq model & tokenizer to avoid truncation/classification fallback
37
- seq_tok = AutoTokenizer.from_pretrained("google/flan-t5-large")
38
- seq_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
39
  return pipeline(
40
  "text2text-generation",
41
  model=seq_model,
42
  tokenizer=seq_tok,
43
- # ensure we generate up to 400 new tokens
44
- max_new_tokens=400,
45
  do_sample=True,
46
  temperature=0.7
47
  )
@@ -77,10 +75,7 @@ def main():
77
  # Sentiment Analysis
78
  progress.text("Analyzing sentiment...")
79
  raw_scores = sentiment_pipeline(review)[0]
80
- sentiment_results = {
81
- LABEL_MAP[item['label']]: float(item['score'])
82
- for item in raw_scores
83
- }
84
  progress.progress(40)
85
 
86
  # Keyword Extraction
@@ -115,28 +110,47 @@ def main():
115
  progress.progress(80)
116
 
117
  # Highlight Highest Sentiment
118
- max_label, max_score = max(
119
- sentiment_results.items(), key=lambda x: x[1]
120
- )
121
- st.markdown(
122
- f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})"
123
- )
124
-
125
- # FLAN-T5 Analysis & Suggestions
126
- progress.text("Generating insights...")
127
- prompt = f"""
128
- You are an analytical Amazon feedback expert.
129
- Review: \"{review}\"
130
- Sentiment Scores: {sentiment_results}
131
- Top Keywords: {[kw for kw, _ in keywords]}
132
-
133
- Please complete the following:
134
- - ANALYSIS: A concise paragraph (3 sentences) interpreting customer sentiment.
135
- - RECOMMENDATIONS: Three separate paragraphs with actionable suggestions (max 30 words each).
136
- """
137
- response = generation_pipeline(prompt)
138
- output = response[0]["generated_text"]
139
- st.markdown(output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  # Done
142
  progress.progress(100)
@@ -145,3 +159,4 @@ Please complete the following:
145
 
146
  if __name__ == "__main__":
147
  main()
 
 
33
  # ─── FLAN-T5 Generation Pipeline ────────────────────────────────────────────
34
  @st.cache_resource
35
  def load_flant5_pipeline():
36
+ seq_tok = AutoTokenizer.from_pretrained("google/flan-t5-base")
37
+ seq_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
 
38
  return pipeline(
39
  "text2text-generation",
40
  model=seq_model,
41
  tokenizer=seq_tok,
42
+ max_new_tokens=300,
 
43
  do_sample=True,
44
  temperature=0.7
45
  )
 
75
  # Sentiment Analysis
76
  progress.text("Analyzing sentiment...")
77
  raw_scores = sentiment_pipeline(review)[0]
78
+ sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores}
 
 
 
79
  progress.progress(40)
80
 
81
  # Keyword Extraction
 
110
  progress.progress(80)
111
 
112
  # Highlight Highest Sentiment
113
+ max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
114
+ st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})")
115
+
116
+ # Generate Detailed Recommendations for select sentiments
117
+ progress.text("Generating detailed recommendations...")
118
+ if max_label in ["Very Negative", "Negative", "Neutral"]:
119
+ prompt = (
120
+ "You are a senior product quality and customer experience specialist at an e-commerce food retailer.
121
+
122
+ "
123
+ f"Customer Review:
124
+ \"{review}\"
125
+
126
+ "
127
+ "Please analyze this feedback and provide **three** distinct, actionable improvement recommendations designed to reduce customer pain points.
128
+ "
129
+ "For each recommendation, include:
130
+ "
131
+ " 1. **Recommendation Title**: a concise summary of the action.
132
+ "
133
+ " 2. the specific issue or frustration extracted from the review.
134
+ "
135
+ " 3. why this action addresses the pain point and how it will improve the customer experience.
136
+ "
137
+ " 4. a bullet-point list of 3–5 clear steps for operations or product teams to execute.
138
+ "
139
+ " 5. how to measure the impact.
140
+
141
+ "
142
+ "Write each recommendation in at least 5–7 sentences, grounding every detail in the customer's own words. "
143
+ "Avoid generic advice—focus on specifics from the review.
144
+
145
+ "
146
+ "Recommendations:
147
+ "
148
+ )
149
+ response = generation_pipeline(prompt)
150
+ detailed = response[0]["generated_text"]
151
+ st.markdown(detailed)
152
+ else:
153
+ st.info("Detailed recommendations are provided only for Neutral, Negative, or Very Negative reviews.")
154
 
155
  # Done
156
  progress.progress(100)
 
159
 
160
  if __name__ == "__main__":
161
  main()
162
+