EmmaL1 commited on
Commit
1a10e36
·
verified ·
1 Parent(s): 6c821ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -34
app.py CHANGED
@@ -1,47 +1,61 @@
1
  import streamlit as st
2
- import torch
3
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
4
  import numpy as np
5
 
6
- # Initialize the raw model and tokenizer (assuming a custom 5-class model)
7
- model = AutoModelForSequenceClassification.from_pretrained("EmmaL1/CustomModel_amazon")
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
- # Text classification function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Tokenize input and get model predictions
21
- inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
22
- with torch.no_grad(): # Line 23
23
- outputs = model(**inputs) # Line 24
24
- predictions = outputs.logits # Line 25
25
- predictions = torch.softmax(predictions, dim=-1) # Line 26
26
- predictions = predictions.cpu().detach().numpy()[0] # Line 27
27
-
28
- # Calculate rating (1-5 stars)
29
- rating = np.argmax(predictions) + 1 # Index 0-4 -> 1-5 stars
30
-
31
- # Display the rating
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