|
"LABEL_4": "Very Positive" |
|
} |
|
|
|
|
|
def main(): |
|
st.title("📊 Amazon Review Analyzer") |
|
|
|
review = st.text_area("Enter your review:") |
|
if not st.button("Analyze Review"): |
|
return |
|
if not review: |
|
st.warning("Please enter a review to analyze.") |
|
return |
|
|
|
progress = st.progress(0) |
|
|
|
|
|
progress.text("Loading models...") |
|
sentiment_pipeline = load_sentiment_pipeline() |
|
kw_model = load_keybert_model() |
|
generation_pipeline = load_flant5_pipeline() |
|
progress.progress(20) |
|
|
|
|
|
progress.text("Analyzing sentiment...") |
|
raw_scores = sentiment_pipeline(review)[0] |
|
sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores} |
|
progress.progress(40) |
|
|
|
|
|
progress.text("Extracting keywords...") |
|
keywords = kw_model.extract_keywords( |
|
review, |
|
keyphrase_ngram_range=(1, 2), |
|
stop_words="english", |
|
top_n=3 |
|
) |
|
progress.progress(60) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
st.subheader("Sentiment Scores") |
|
st.json({k: round(v, 4) for k, v in sentiment_results.items()}) |
|
with col2: |
|
st.subheader("Top 3 Keywords") |
|
for kw, score in keywords: |
|
st.write(f"• {kw} ({score:.4f})") |
|
|
|
|
|
progress.text("Rendering chart...") |
|
df_scores = pd.DataFrame.from_dict( |
|
sentiment_results, |
|
orient='index', |
|
columns=['score'] |
|
) |
|
df_scores.index.name = 'Sentiment' |
|
st.bar_chart(df_scores) |
|
progress.progress(80) |
|
|
|
|
|
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1]) |
|
st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})") |
|
|
|
|
|
progress.text("Generating detailed recommendations...") |
|
if max_label in ["Very Negative", "Negative", "Neutral"]: |
|
prompt = f""" |
|
You are a senior product quality and customer experience specialist at an e-commerce food retailer. |
|
|
|
Customer Review: |
|
"{review}" |
|
|
|
Instructions: Analyze the feedback and provide three distinct, actionable improvement recommendations. For each, include a concise title and a detailed explanation in 5–7 sentences, plus a bullet list of 3–5 execution steps and a measure of impact. |
|
|
|
**Output only the recommendations as numbered items (1–3).*""" |
|
response = generation_pipeline(prompt) |
|
detailed = response[0]["generated_text"] |
|
st.markdown(detailed) |
|
else: |
|
st.info("Detailed recommendations are provided only for Neutral, Negative, or Very Negative reviews.") |
|
|
|
|
|
progress.progress(100) |
|
progress.text("Done!") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
|
|