Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,50 +3,64 @@ from transformers import pipeline
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
# Initialize sentiment analysis pipeline
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def textclassification():
|
| 19 |
-
st.title("Amazon
|
| 20 |
-
st.write("Enter a
|
| 21 |
-
|
| 22 |
-
user_input = st.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
st.error(f"Analysis error: {str(e)}")
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
-
|
|
|
|
| 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 |
+
Optimized rating calculation that:
|
| 14 |
+
1. Provides more granular rating distribution (1-5 stars)
|
| 15 |
+
2. Uses confidence score to determine rating strength
|
| 16 |
+
3. Better reflects actual review distributions
|
| 17 |
+
"""
|
| 18 |
+
if sentiment == "POSITIVE":
|
| 19 |
+
# Positive reviews get 3-5 stars based on confidence
|
| 20 |
+
if confidence > 0.9:
|
| 21 |
+
return 5 # Very confident positive = 5 stars
|
| 22 |
+
elif confidence > 0.7:
|
| 23 |
+
return 4 # Strong positive = 4 stars
|
| 24 |
+
else:
|
| 25 |
+
return 3 # Mild positive = 3 stars
|
| 26 |
+
else:
|
| 27 |
+
# Negative reviews get 1-2 stars based on confidence
|
| 28 |
+
if confidence > 0.7:
|
| 29 |
+
return 1 # Very confident negative = 1 star
|
| 30 |
+
else:
|
| 31 |
+
return 2 # Less confident negative = 2 stars
|
| 32 |
|
| 33 |
def textclassification():
|
| 34 |
+
st.title("Amazon Customer Sentiment Analysis, Ratings, and Reasons")
|
| 35 |
+
st.write("Enter a sentence to analyze its sentiment and reason:")
|
| 36 |
+
|
| 37 |
+
user_input = st.text_input("Input your text:")
|
| 38 |
+
if user_input:
|
| 39 |
+
|
| 40 |
+
# Sentiment Analysis
|
| 41 |
+
sentiment_result = sentiment_pipeline(user_input)
|
| 42 |
+
sentiment = sentiment_result[0]["label"]
|
| 43 |
+
confidence = sentiment_result[0]["score"]
|
| 44 |
+
|
| 45 |
+
st.write(f"Sentiment: {sentiment}")
|
| 46 |
+
st.write(f"Confidence: {confidence:.2f}")
|
| 47 |
+
|
| 48 |
+
# Determine the rating using optimized calculation
|
| 49 |
+
rating = calculate_rating(sentiment, confidence)
|
| 50 |
+
|
| 51 |
+
# Display the rating
|
| 52 |
+
st.write(f"The rating is {rating} stars")
|
| 53 |
+
|
| 54 |
+
# Question Answering
|
| 55 |
+
qa_input = {
|
| 56 |
+
'question': f'Why is the rating {rating} star?',
|
| 57 |
+
'context': user_input
|
| 58 |
+
}
|
| 59 |
+
qa_result = qa_pipeline(qa_input)
|
| 60 |
+
st.write(f"Reasons: {qa_result['answer']}")
|
| 61 |
+
|
| 62 |
+
def main():
|
| 63 |
+
textclassification()
|
|
|
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
main()
|