Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,12 @@ import os
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import streamlit as st
|
5 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
6 |
from keybert import KeyBERT
|
7 |
|
8 |
# ─── Sentiment & Keyword Models ─────────────────────────────────────────────
|
@@ -10,7 +15,10 @@ from keybert import KeyBERT
|
|
10 |
def load_sentiment_pipeline():
|
11 |
model_name = "mayf/amazon_reviews_bert_ft"
|
12 |
tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
|
13 |
-
mdl = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
|
|
|
14 |
return pipeline(
|
15 |
"sentiment-analysis",
|
16 |
model=mdl,
|
@@ -60,13 +68,16 @@ def main():
|
|
60 |
generation_pipeline = load_flant5_pipeline()
|
61 |
progress.progress(20)
|
62 |
|
63 |
-
# Sentiment
|
64 |
progress.text("Analyzing sentiment...")
|
65 |
raw_scores = sentiment_pipeline(review)[0]
|
66 |
-
sentiment_results = {
|
|
|
|
|
|
|
67 |
progress.progress(40)
|
68 |
|
69 |
-
#
|
70 |
progress.text("Extracting keywords...")
|
71 |
keywords = kw_model.extract_keywords(
|
72 |
review,
|
@@ -76,7 +87,7 @@ def main():
|
|
76 |
)
|
77 |
progress.progress(60)
|
78 |
|
79 |
-
# Display
|
80 |
col1, col2 = st.columns(2)
|
81 |
with col1:
|
82 |
st.subheader("Sentiment Scores")
|
@@ -86,16 +97,24 @@ def main():
|
|
86 |
for kw, score in keywords:
|
87 |
st.write(f"• {kw} ({score:.4f})")
|
88 |
|
89 |
-
# Chart
|
90 |
progress.text("Rendering chart...")
|
91 |
-
df_scores = pd.DataFrame.from_dict(
|
|
|
|
|
|
|
|
|
92 |
df_scores.index.name = 'Sentiment'
|
93 |
st.bar_chart(df_scores)
|
94 |
progress.progress(80)
|
95 |
|
96 |
-
# Highlight
|
97 |
-
max_label, max_score = max(
|
98 |
-
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# FLAN-T5 Analysis & Suggestions
|
101 |
progress.text("Generating insights...")
|
@@ -109,13 +128,17 @@ Tasks:
|
|
109 |
1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
|
110 |
2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
|
111 |
"""
|
112 |
-
output = generation_pipeline(
|
|
|
|
|
|
|
|
|
113 |
st.markdown(output)
|
114 |
|
|
|
115 |
progress.progress(100)
|
116 |
progress.text("Done!")
|
117 |
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
main()
|
121 |
-
|
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import streamlit as st
|
5 |
+
from transformers import (
|
6 |
+
pipeline,
|
7 |
+
AutoTokenizer,
|
8 |
+
AutoModelForSequenceClassification,
|
9 |
+
AutoModelForSeq2SeqLM
|
10 |
+
)
|
11 |
from keybert import KeyBERT
|
12 |
|
13 |
# ─── Sentiment & Keyword Models ─────────────────────────────────────────────
|
|
|
15 |
def load_sentiment_pipeline():
|
16 |
model_name = "mayf/amazon_reviews_bert_ft"
|
17 |
tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
|
18 |
+
mdl = AutoModelForSequenceClassification.from_pretrained(
|
19 |
+
model_name,
|
20 |
+
use_auth_token=True
|
21 |
+
)
|
22 |
return pipeline(
|
23 |
"sentiment-analysis",
|
24 |
model=mdl,
|
|
|
68 |
generation_pipeline = load_flant5_pipeline()
|
69 |
progress.progress(20)
|
70 |
|
71 |
+
# Sentiment Analysis
|
72 |
progress.text("Analyzing sentiment...")
|
73 |
raw_scores = sentiment_pipeline(review)[0]
|
74 |
+
sentiment_results = {
|
75 |
+
LABEL_MAP[item['label']]: float(item['score'])
|
76 |
+
for item in raw_scores
|
77 |
+
}
|
78 |
progress.progress(40)
|
79 |
|
80 |
+
# Keyword Extraction
|
81 |
progress.text("Extracting keywords...")
|
82 |
keywords = kw_model.extract_keywords(
|
83 |
review,
|
|
|
87 |
)
|
88 |
progress.progress(60)
|
89 |
|
90 |
+
# Display Results
|
91 |
col1, col2 = st.columns(2)
|
92 |
with col1:
|
93 |
st.subheader("Sentiment Scores")
|
|
|
97 |
for kw, score in keywords:
|
98 |
st.write(f"• {kw} ({score:.4f})")
|
99 |
|
100 |
+
# Bar Chart
|
101 |
progress.text("Rendering chart...")
|
102 |
+
df_scores = pd.DataFrame.from_dict(
|
103 |
+
sentiment_results,
|
104 |
+
orient='index',
|
105 |
+
columns=['score']
|
106 |
+
)
|
107 |
df_scores.index.name = 'Sentiment'
|
108 |
st.bar_chart(df_scores)
|
109 |
progress.progress(80)
|
110 |
|
111 |
+
# Highlight Highest Sentiment
|
112 |
+
max_label, max_score = max(
|
113 |
+
sentiment_results.items(), key=lambda x: x[1]
|
114 |
+
)
|
115 |
+
st.markdown(
|
116 |
+
f"**Highest Sentiment:** **{max_label}** ({max_score:.4f})"
|
117 |
+
)
|
118 |
|
119 |
# FLAN-T5 Analysis & Suggestions
|
120 |
progress.text("Generating insights...")
|
|
|
128 |
1. Analysis: Write a concise paragraph (3 sentences) interpreting customer sentiment by combining the scores and keywords.
|
129 |
2. Recommendations: Three separate paragraphs with actionable suggestions (max 30 words each).
|
130 |
"""
|
131 |
+
output = generation_pipeline(
|
132 |
+
prompt,
|
133 |
+
max_length=200,
|
134 |
+
do_sample=False
|
135 |
+
)[0]['generated_text']
|
136 |
st.markdown(output)
|
137 |
|
138 |
+
# Done
|
139 |
progress.progress(100)
|
140 |
progress.text("Done!")
|
141 |
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
main()
|
|