Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,82 @@
|
|
| 1 |
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
from scipy.io.wavfile import write as write_wav
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
max_new_tokens=200,
|
| 22 |
-
temperature=0.9,
|
| 23 |
-
top_k=50
|
| 24 |
-
)[0]['generated_text']
|
| 25 |
-
return story_text.split("Happy ending")[-1].strip()
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
audio_bytes = io.BytesIO()
|
| 34 |
-
audio_np = (audio_output["audio"] * 32767).astype(np.int16)
|
| 35 |
-
write_wav(audio_bytes, audio_output["sampling_rate"], audio_np)
|
| 36 |
-
audio_bytes.seek(0)
|
| 37 |
-
|
| 38 |
-
return audio_bytes
|
| 39 |
|
| 40 |
def main():
|
| 41 |
-
st.title("
|
| 42 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
| 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 |
if __name__ == "__main__":
|
| 71 |
-
main()
|
|
|
|
| 1 |
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
+
# Page config
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Amazon Review Sentiment Analysis",
|
| 10 |
+
page_icon="π",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_model():
|
| 16 |
+
"""Load the model and tokenizer."""
|
| 17 |
+
model_name = "LiYuan/amazon-review-sentiment-analysis"
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 20 |
+
return tokenizer, model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def predict_sentiment(text, tokenizer, model):
|
| 23 |
+
"""Predict sentiment for given text."""
|
| 24 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 27 |
+
return predictions.detach().numpy()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def main():
|
| 30 |
+
st.title("π Amazon Review Sentiment Analysis")
|
| 31 |
+
st.write("""
|
| 32 |
+
This application analyzes the sentiment of product reviews and predicts ratings (1-5 stars).
|
| 33 |
+
Enter your review text below to get started!
|
| 34 |
+
""")
|
| 35 |
|
| 36 |
+
# Load model
|
| 37 |
+
with st.spinner("Loading model..."):
|
| 38 |
+
tokenizer, model = load_model()
|
| 39 |
|
| 40 |
+
# Text input
|
| 41 |
+
text_input = st.text_area("Enter your review text:", height=150)
|
| 42 |
+
|
| 43 |
+
if st.button("Analyze Sentiment"):
|
| 44 |
+
if text_input.strip():
|
| 45 |
+
with st.spinner("Analyzing..."):
|
| 46 |
+
# Get prediction
|
| 47 |
+
predictions = predict_sentiment(text_input, tokenizer, model)
|
| 48 |
+
predicted_rating = np.argmax(predictions) + 1 # Add 1 since ratings are 1-5
|
| 49 |
+
|
| 50 |
+
# Display results
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
st.subheader("Predicted Rating")
|
| 55 |
+
st.markdown(f"<h1 style='text-align: center; color: #1f77b4;'>{'β' * predicted_rating}</h1>", unsafe_allow_html=True)
|
| 56 |
+
|
| 57 |
+
with col2:
|
| 58 |
+
st.subheader("Confidence Scores")
|
| 59 |
+
for i, score in enumerate(predictions, 1):
|
| 60 |
+
st.progress(float(score))
|
| 61 |
+
st.write(f"{i} Stars: {score:.2%}")
|
| 62 |
+
else:
|
| 63 |
+
st.warning("Please enter some text to analyze.")
|
| 64 |
+
|
| 65 |
+
# Additional information
|
| 66 |
+
with st.expander("About this Model"):
|
| 67 |
+
st.write("""
|
| 68 |
+
This application uses the LiYuan/amazon-review-sentiment-analysis model from HuggingFace.
|
| 69 |
+
The model is based on DistilBERT and was trained on a large dataset of Amazon product reviews.
|
| 70 |
+
It can predict ratings from 1 to 5 stars based on the review text.
|
| 71 |
|
| 72 |
+
Supported languages:
|
| 73 |
+
- English
|
| 74 |
+
- Dutch
|
| 75 |
+
- German
|
| 76 |
+
- French
|
| 77 |
+
- Spanish
|
| 78 |
+
- Italian
|
| 79 |
+
""")
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
main()
|