Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from keybert import KeyBERT
|
4 |
+
|
5 |
+
# Initialize sentiment analysis pipeline
|
6 |
+
@st.cache_resource
|
7 |
+
def load_sentiment_pipeline():
|
8 |
+
model_name = "mayf/amazon_reviews_bert_ft"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
11 |
+
return pipeline(
|
12 |
+
"sentiment-analysis",
|
13 |
+
model=model,
|
14 |
+
tokenizer=tokenizer,
|
15 |
+
return_all_scores=True
|
16 |
+
)
|
17 |
+
|
18 |
+
# Initialize KeyBERT model
|
19 |
+
@st.cache_resource
|
20 |
+
def load_keybert_model():
|
21 |
+
return KeyBERT(model="all-MiniLM-L6-v2")
|
22 |
+
|
23 |
+
sentiment_pipeline = load_sentiment_pipeline()
|
24 |
+
kw_model = load_keybert_model()
|
25 |
+
|
26 |
+
# Streamlit App
|
27 |
+
st.set_page_config(page_title="Review Analyzer", layout="wide")
|
28 |
+
st.title("📊 Review Sentiment & Keyword Analyzer")
|
29 |
+
|
30 |
+
review = st.text_area("Enter your review:")
|
31 |
+
|
32 |
+
if st.button("Analyze Review"):
|
33 |
+
if not review:
|
34 |
+
st.warning("Please enter a review to analyze.")
|
35 |
+
else:
|
36 |
+
# Sentiment Analysis
|
37 |
+
scores = sentiment_pipeline(review)[0]
|
38 |
+
# Convert scores to displayable format
|
39 |
+
sentiment_results = {item['label']: round(item['score'], 4) for item in scores}
|
40 |
+
st.subheader("Sentiment Scores")
|
41 |
+
st.json(sentiment_results)
|
42 |
+
|
43 |
+
# Keyword Extraction
|
44 |
+
keywords = kw_model.extract_keywords(
|
45 |
+
review,
|
46 |
+
keyphrase_ngram_range=(1, 2),
|
47 |
+
stop_words="english",
|
48 |
+
top_n=5
|
49 |
+
)
|
50 |
+
st.subheader("Top Keywords")
|
51 |
+
for kw, score in keywords:
|
52 |
+
st.write(f"- **{kw}** (Score: {score:.4f})")
|
53 |
+
|
54 |
+
# Final Score: example combining sentiment and keywords
|
55 |
+
avg_sentiment = sum(sentiment_results.values()) / len(sentiment_results)
|
56 |
+
st.subheader("Composite Score with Keywords")
|
57 |
+
st.write(f"Average Sentiment Score: {avg_sentiment:.4f}")
|
58 |
+
st.write("Keywords used for this score:")
|
59 |
+
st.write(', '.join([kw for kw, _ in keywords]))
|