Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,61 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Initialize
|
| 7 |
-
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained("EmmaL1/CustomModel_amazon")
|
| 9 |
|
| 10 |
# Initialize question-answering pipeline
|
| 11 |
qa_pipeline = pipeline("question-answering", model="distilbert/distilbert-base-cased-distilled-squad")
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def textclassification():
|
| 15 |
st.title("Amazon Customer Sentiment Analysis: Ratings & Reasons")
|
| 16 |
st.write("Enter a sentence to analyze its rating and reason:")
|
| 17 |
|
| 18 |
user_input = st.text_input("Input your text:")
|
| 19 |
if user_input:
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Calculate rating
|
| 29 |
-
rating =
|
| 30 |
-
|
| 31 |
-
# Display the
|
| 32 |
-
st.write(f"The rating is {rating} stars")
|
| 33 |
-
st.write(f"Prediction probabilities: {predictions}") # For debugging
|
| 34 |
-
|
| 35 |
-
# Question Answering
|
| 36 |
-
qa_input = {
|
| 37 |
-
'question': f'Why is the rating {rating} stars?',
|
| 38 |
-
'context': user_input
|
| 39 |
-
}
|
| 40 |
-
qa_result = qa_pipeline(qa_input)
|
| 41 |
-
st.write(f"Reasons: {qa_result['answer']}")
|
| 42 |
-
|
| 43 |
-
def main():
|
| 44 |
-
textclassification()
|
| 45 |
-
|
| 46 |
-
if __name__ == "__main__":
|
| 47 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Initialize sentiment analysis pipeline
|
| 6 |
+
sentiment_pipeline = pipeline(model="EmmaL1/CustomModel_amazon")
|
|
|
|
| 7 |
|
| 8 |
# Initialize question-answering pipeline
|
| 9 |
qa_pipeline = pipeline("question-answering", model="distilbert/distilbert-base-cased-distilled-squad")
|
| 10 |
|
| 11 |
+
def calculate_rating(sentiment, confidence):
|
| 12 |
+
"""
|
| 13 |
+
Improved rating calculation that:
|
| 14 |
+
1. Uses non-linear mapping for more realistic star distributions
|
| 15 |
+
2. Handles edge cases better
|
| 16 |
+
3. Provides more 5-star ratings for highly positive sentiment
|
| 17 |
+
"""
|
| 18 |
+
if sentiment == "POSITIVE":
|
| 19 |
+
# Non-linear mapping that gives more weight to higher confidence
|
| 20 |
+
# This creates a more natural distribution of star ratings
|
| 21 |
+
if confidence > 0.9:
|
| 22 |
+
return 5
|
| 23 |
+
elif confidence > 0.7:
|
| 24 |
+
return 4
|
| 25 |
+
elif confidence > 0.5:
|
| 26 |
+
return 3
|
| 27 |
+
elif confidence > 0.3:
|
| 28 |
+
return 2
|
| 29 |
+
else:
|
| 30 |
+
return 1
|
| 31 |
+
else: # NEGATIVE sentiment
|
| 32 |
+
# Inverted scale for negative sentiment
|
| 33 |
+
if confidence > 0.9:
|
| 34 |
+
return 1
|
| 35 |
+
elif confidence > 0.7:
|
| 36 |
+
return 2
|
| 37 |
+
elif confidence > 0.5:
|
| 38 |
+
return 3
|
| 39 |
+
elif confidence > 0.3:
|
| 40 |
+
return 4
|
| 41 |
+
else:
|
| 42 |
+
return 5
|
| 43 |
+
|
| 44 |
def textclassification():
|
| 45 |
st.title("Amazon Customer Sentiment Analysis: Ratings & Reasons")
|
| 46 |
st.write("Enter a sentence to analyze its rating and reason:")
|
| 47 |
|
| 48 |
user_input = st.text_input("Input your text:")
|
| 49 |
if user_input:
|
| 50 |
+
# Sentiment Analysis
|
| 51 |
+
sentiment_result = sentiment_pipeline(user_input)
|
| 52 |
+
sentiment = sentiment_result[0]["label"]
|
| 53 |
+
confidence = sentiment_result[0]["score"]
|
| 54 |
+
|
| 55 |
+
st.write(f"Sentiment: {sentiment}")
|
| 56 |
+
st.write(f"Confidence Score: {confidence:.2f}")
|
| 57 |
+
|
| 58 |
+
# Calculate rating using improved function
|
| 59 |
+
rating = calculate_rating(sentiment, confidence)
|
| 60 |
+
|
| 61 |
+
# Display the ra
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|