Update app.py
Browse files
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 |
-
|
37 |
-
|
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 |
-
|
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 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
"
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|