Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,20 @@ from huggingface_hub import login
|
|
4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
5 |
from keybert import KeyBERT
|
6 |
|
7 |
-
# ─── Streamlit Page Config
|
8 |
st.set_page_config(page_title="Review Analyzer", layout="wide")
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
# ─── Cached resource: Sentiment Analysis Pipeline ────────────────────────────
|
11 |
@st.cache_resource
|
12 |
def load_sentiment_pipeline():
|
13 |
model_name = "mayf/amazon_reviews_bert_ft"
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
15 |
-
|
16 |
-
use_auth_token=True
|
17 |
-
)
|
18 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
19 |
-
model_name,
|
20 |
-
use_auth_token=True
|
21 |
-
)
|
22 |
return pipeline(
|
23 |
"sentiment-analysis",
|
24 |
model=model,
|
@@ -37,7 +36,6 @@ def main():
|
|
37 |
kw_model = load_keybert_model()
|
38 |
|
39 |
st.title("📊 Review Sentiment & Keyword Analyzer")
|
40 |
-
|
41 |
review = st.text_area("Enter your review:")
|
42 |
|
43 |
if st.button("Analyze Review"):
|
@@ -47,9 +45,9 @@ def main():
|
|
47 |
|
48 |
# Sentiment Analysis
|
49 |
scores = sentiment_pipeline(review)[0]
|
50 |
-
sentiment_results = {item['label']:
|
51 |
st.subheader("Sentiment Scores")
|
52 |
-
st.json(sentiment_results)
|
53 |
|
54 |
# Keyword Extraction (top 3)
|
55 |
keywords = kw_model.extract_keywords(
|
@@ -62,11 +60,11 @@ def main():
|
|
62 |
for kw, score in keywords:
|
63 |
st.write(f"- **{kw}** (Score: {score:.4f})")
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
st.subheader("
|
68 |
-
st.write(f"
|
69 |
-
st.write("Keywords used for this
|
70 |
st.write(', '.join([kw for kw, _ in keywords]))
|
71 |
|
72 |
if __name__ == "__main__":
|
|
|
4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
5 |
from keybert import KeyBERT
|
6 |
|
7 |
+
# ─── Streamlit Page Config ─────────────────────────────────────────────────
|
8 |
st.set_page_config(page_title="Review Analyzer", layout="wide")
|
9 |
|
10 |
+
# ─── Authenticate to Hugging Face Hub ───────────────────────────────────────
|
11 |
+
# ensure HF_TOKEN is set in your environment
|
12 |
+
token = os.environ.get("HF_TOKEN", "")
|
13 |
+
login(token=token, add_to_git_credential=False)
|
14 |
+
|
15 |
# ─── Cached resource: Sentiment Analysis Pipeline ────────────────────────────
|
16 |
@st.cache_resource
|
17 |
def load_sentiment_pipeline():
|
18 |
model_name = "mayf/amazon_reviews_bert_ft"
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
|
20 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, use_auth_token=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return pipeline(
|
22 |
"sentiment-analysis",
|
23 |
model=model,
|
|
|
36 |
kw_model = load_keybert_model()
|
37 |
|
38 |
st.title("📊 Review Sentiment & Keyword Analyzer")
|
|
|
39 |
review = st.text_area("Enter your review:")
|
40 |
|
41 |
if st.button("Analyze Review"):
|
|
|
45 |
|
46 |
# Sentiment Analysis
|
47 |
scores = sentiment_pipeline(review)[0]
|
48 |
+
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
49 |
st.subheader("Sentiment Scores")
|
50 |
+
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
51 |
|
52 |
# Keyword Extraction (top 3)
|
53 |
keywords = kw_model.extract_keywords(
|
|
|
60 |
for kw, score in keywords:
|
61 |
st.write(f"- **{kw}** (Score: {score:.4f})")
|
62 |
|
63 |
+
# Highest Sentiment Label
|
64 |
+
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
65 |
+
st.subheader("Highest Sentiment")
|
66 |
+
st.write(f"{max_label} (Score: {max_score:.4f})")
|
67 |
+
st.write("Keywords used for this analysis:")
|
68 |
st.write(', '.join([kw for kw, _ in keywords]))
|
69 |
|
70 |
if __name__ == "__main__":
|