Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,110 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from huggingface_hub import login
|
4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
5 |
from keybert import KeyBERT
|
|
|
6 |
|
7 |
-
# ───
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# ───
|
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 |
-
|
20 |
-
|
21 |
return pipeline(
|
22 |
"sentiment-analysis",
|
23 |
-
model=
|
24 |
-
tokenizer=
|
25 |
return_all_scores=True
|
26 |
)
|
27 |
|
28 |
-
# ─── Cached resource: KeyBERT Model ─────────────────────────────────────────
|
29 |
@st.cache_resource
|
30 |
def load_keybert_model():
|
31 |
return KeyBERT(model="all-MiniLM-L6-v2")
|
32 |
|
33 |
-
|
34 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
sentiment_pipeline = load_sentiment_pipeline()
|
36 |
kw_model = load_keybert_model()
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
if st.button("Analyze Review"):
|
42 |
-
if not review:
|
43 |
-
st.warning("Please enter a review to analyze.")
|
44 |
-
return
|
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(
|
54 |
-
review,
|
55 |
-
keyphrase_ngram_range=(1, 2),
|
56 |
-
stop_words="english",
|
57 |
-
top_n=3
|
58 |
-
)
|
59 |
-
st.subheader("Top 3 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__":
|
71 |
-
|
|
|
|
1 |
import os
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
import streamlit as st
|
5 |
from huggingface_hub import login
|
6 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
7 |
from keybert import KeyBERT
|
8 |
+
from openai import AzureOpenAI # new
|
9 |
|
10 |
+
# ─── Azure OpenAI Client ─────────────────────────────────────────────────────
|
11 |
+
openai_client = AzureOpenAI(
|
12 |
+
api_key=os.environ["AZURE_OPENAI_KEY"],
|
13 |
+
api_version="2023-05-15",
|
14 |
+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"]
|
15 |
+
)
|
16 |
|
17 |
+
# ─── (your existing cache decorators) ────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
18 |
@st.cache_resource
|
19 |
def load_sentiment_pipeline():
|
20 |
model_name = "mayf/amazon_reviews_bert_ft"
|
21 |
+
tok = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
|
22 |
+
mdl = AutoModelForSequenceClassification.from_pretrained(model_name, use_auth_token=True)
|
23 |
return pipeline(
|
24 |
"sentiment-analysis",
|
25 |
+
model=mdl,
|
26 |
+
tokenizer=tok,
|
27 |
return_all_scores=True
|
28 |
)
|
29 |
|
|
|
30 |
@st.cache_resource
|
31 |
def load_keybert_model():
|
32 |
return KeyBERT(model="all-MiniLM-L6-v2")
|
33 |
|
34 |
+
|
35 |
def main():
|
36 |
+
st.title("📊 Review Sentiment & Keyword Analyzer + GPT Insights")
|
37 |
+
|
38 |
+
# ─── Inputs & Models ──────────────────────────────────────────────────────
|
39 |
+
review = st.text_area("Enter your review:")
|
40 |
+
if not st.button("Analyze Review"):
|
41 |
+
return
|
42 |
+
|
43 |
+
if not review:
|
44 |
+
st.warning("Please enter a review to analyze.")
|
45 |
+
return
|
46 |
+
|
47 |
+
# ─── Sentiment & Keywords ─────────────────────────────────────────────────
|
48 |
sentiment_pipeline = load_sentiment_pipeline()
|
49 |
kw_model = load_keybert_model()
|
50 |
|
51 |
+
scores = sentiment_pipeline(review)[0]
|
52 |
+
sentiment_results = {item['label']: float(item['score']) for item in scores}
|
53 |
+
st.subheader("Sentiment Scores")
|
54 |
+
st.json({k: round(v, 4) for k, v in sentiment_results.items()})
|
55 |
+
|
56 |
+
keywords = kw_model.extract_keywords(
|
57 |
+
review,
|
58 |
+
keyphrase_ngram_range=(1, 2),
|
59 |
+
stop_words="english",
|
60 |
+
top_n=3
|
61 |
+
)
|
62 |
+
st.subheader("Top 3 Keywords")
|
63 |
+
for kw, score in keywords:
|
64 |
+
st.write(f"- **{kw}** (Score: {score:.4f})")
|
65 |
+
|
66 |
+
# ─── Determine Highest Sentiment ───────────────────────────────────────────
|
67 |
+
max_label, max_score = max(sentiment_results.items(), key=lambda x: x[1])
|
68 |
+
st.subheader("Highest Sentiment")
|
69 |
+
st.write(f"{max_label} (Score: {max_score:.4f})")
|
70 |
+
|
71 |
+
# ─── GPT-Driven Analysis & Suggestions ────────────────────────────────────
|
72 |
+
st.subheader("GPT Analysis & Seller Suggestions")
|
73 |
+
|
74 |
+
# build a single text prompt for GPT
|
75 |
+
prompt = f"""
|
76 |
+
You are a helpful assistant for e-commerce sellers.
|
77 |
+
Here is a product review, its sentiment breakdown, and the top keywords:
|
78 |
+
|
79 |
+
Review:
|
80 |
+
\"\"\"{review}\"\"\"
|
81 |
+
|
82 |
+
Sentiment scores:
|
83 |
+
{sentiment_results}
|
84 |
+
|
85 |
+
Top keywords:
|
86 |
+
{[kw for kw, _ in keywords]}
|
87 |
+
|
88 |
+
First, provide a one-paragraph professional analysis of what the customer feels and why (combine sentiment + keywords).
|
89 |
+
Then, give 3 detailed, actionable suggestions the seller can implement to improve future reviews or address the feedback.
|
90 |
+
"""
|
91 |
+
|
92 |
+
# call Azure OpenAI
|
93 |
+
response = openai_client.chat.completions.create(
|
94 |
+
engine="gpt-35-turbo", # or your deployed model name
|
95 |
+
messages=[
|
96 |
+
{"role": "system", "content": "You are a product-feedback analyst."},
|
97 |
+
{"role": "user", "content": prompt}
|
98 |
+
],
|
99 |
+
temperature=0.7,
|
100 |
+
max_tokens=400
|
101 |
+
)
|
102 |
+
|
103 |
+
# display GPT’s reply
|
104 |
+
gpt_reply = response.choices[0].message.content
|
105 |
+
st.markdown(gpt_reply)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
+
# make sure your env vars are set: AZURE_OPENAI_KEY, AZURE_OPENAI_ENDPOINT
|
110 |
+
main()
|