1
File size: 2,811 Bytes
fae49e7
 
 
533636b
05ec195
7204d99
9c2aa41
 
 
 
 
 
 
 
3832b1b
 
6268cef
3832b1b
05ec195
 
2ecaff0
3832b1b
05ec195
fe934dd
3832b1b
fae49e7
1341687
3832b1b
 
fe934dd
3832b1b
 
 
 
 
 
 
 
f24967f
fe934dd
6268cef
 
 
 
 
 
 
 
9c2aa41
fe934dd
3832b1b
fe934dd
 
 
 
 
fae49e7
f24967f
3832b1b
f24967f
fe934dd
1341687
 
 
b2e17db
1341687
 
b2e17db
 
 
 
 
 
5a29e86
b2e17db
5a29e86
1341687
 
 
 
 
9c2aa41
fe934dd
3832b1b
 
 
2ecaff0
05ec195
fae49e7
1341687
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    "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)

    # Load models
    progress.text("Loading models...")
    sentiment_pipeline = load_sentiment_pipeline()
    kw_model = load_keybert_model()
    generation_pipeline = load_flant5_pipeline()
    progress.progress(20)

    # Sentiment Analysis
    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)

    # Keyword Extraction
    progress.text("Extracting keywords...")
    keywords = kw_model.extract_keywords(
        review,
        keyphrase_ngram_range=(1, 2),
        stop_words="english",
        top_n=3
    )
    progress.progress(60)

    # 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
    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)

    # 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 Detailed Recommendations for select sentiments
    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.")

    # Done
    progress.progress(100)
    progress.text("Done!")


if __name__ == "__main__":
    main()