import os import numpy as np import pandas as pd import streamlit as st from transformers import ( pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM ) from keybert import KeyBERT # ─── Sentiment & Keyword Models ───────────────────────────────────────────── @st.cache_resource def load_sentiment_pipeline(): model_name = "mayf/amazon_reviews_bert_ft" tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True) mdl = AutoModelForSequenceClassification.from_pretrained( model_name, use_auth_token=True ) return pipeline( "sentiment-analysis", model=mdl, tokenizer=tok, return_all_scores=True ) @st.cache_resource def load_keybert_model(): return KeyBERT(model="all-MiniLM-L6-v2") # ─── FLAN-T5 Generation Pipeline ──────────────────────────────────────────── @st.cache_resource def load_response_pipeline(): seq_tok = AutoTokenizer.from_pretrained("google/flan-t5-base") seq_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base") return pipeline( "text2text-generation", model=seq_model, tokenizer=seq_tok, max_new_tokens=150, do_sample=False ) LABEL_MAP = { "LABEL_0": "Very Negative", "LABEL_1": "Negative", "LABEL_2": "Neutral", "LABEL_3": "Positive", "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 # ─── KEEP THIS BLOCK UNCHANGED ───────────────────────────────────────── # Sentiment Analysis sentiment_pipeline = load_sentiment_pipeline() raw_scores = sentiment_pipeline(review)[0] sentiment_results = {LABEL_MAP[item['label']]: float(item['score']) for item in raw_scores} # Keyword Extraction kw_model = load_keybert_model() keywords = kw_model.extract_keywords( review, keyphrase_ngram_range=(1, 2), stop_words="english", top_n=3 ) # Display Results 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})") # Bar Chart df_scores = pd.DataFrame.from_dict( sentiment_results, orient='index', columns=['score'] ) df_scores.index.name = 'Sentiment' st.bar_chart(df_scores) # Highlight Highest Sentiment max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1]) st.markdown(f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})") # ──────────────────────────────────────────────────────────────────── # Generate appropriate reply response_pipeline = load_response_pipeline() if max_label in ["Positive", "Very Positive"]: prompt = ( f"You are a friendly customer success representative. The customer said: \"{review}\". " "Write a warm, appreciative reply celebrating their positive experience." ) else: prompt = ( f"You are a helpful customer support specialist. The customer said: \"{review}\". " f"Identified issues: {', '.join([kw for kw, _ in keywords])}. " "First, ask 1-2 clarifying questions to better understand their situation. " "Then, provide two concrete suggestions or next steps to address these issues, grounded in their feedback." ) result = response_pipeline(prompt) reply = result[0]['generated_text'].strip() st.subheader("Generated Reply") st.write(reply) if __name__ == '__main__': main()