spacesedan commited on
Commit
4bec4bc
·
1 Parent(s): b4d678b

updating sentiment classification

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -28,12 +28,12 @@ class SentimentResponse(BaseModel):
28
  sentiment_label: str
29
  confidence: float
30
 
31
- # Mapping BERT star ratings to sentiment scores
32
  LABEL_MAP = {
33
- "1 star": -1.0, # Very Negative
34
- "2 stars": -0.5, # Negative
35
  "3 stars": 0.0, # Neutral
36
- "4 stars": 0.5, # Positive
37
  "5 stars": 1.0 # Very Positive
38
  }
39
 
@@ -49,10 +49,22 @@ def analyze_sentiment(request: SentimentRequest):
49
  sentiment_score = LABEL_MAP.get(label, 0.0) # Default to neutral (0.0) if label is unexpected
50
  confidence = round(score, 3)
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  return SentimentResponse(
53
  content_id=request.content_id,
54
  sentiment_score=sentiment_score,
55
- sentiment_label="positive" if sentiment_score > 0 else "neutral" if sentiment_score == 0 else "negative",
56
  confidence=confidence
57
  )
58
  except Exception as e:
@@ -71,10 +83,22 @@ def analyze_sentiment_batch(request: BatchSentimentRequest):
71
  sentiment_score = LABEL_MAP.get(label, 0.0)
72
  confidence = round(score, 3)
73
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  responses.append(SentimentResponse(
75
  content_id=post.content_id,
76
  sentiment_score=sentiment_score,
77
- sentiment_label="positive" if sentiment_score > 0 else "neutral" if sentiment_score == 0 else "negative",
78
  confidence=confidence
79
  ))
80
  return responses
 
28
  sentiment_label: str
29
  confidence: float
30
 
31
+ # Updated mapping of BERT star ratings to sentiment scores
32
  LABEL_MAP = {
33
+ "1 star": -1.0, # Very Negative
34
+ "2 stars": -0.7, # Negative
35
  "3 stars": 0.0, # Neutral
36
+ "4 stars": 0.7, # Positive
37
  "5 stars": 1.0 # Very Positive
38
  }
39
 
 
49
  sentiment_score = LABEL_MAP.get(label, 0.0) # Default to neutral (0.0) if label is unexpected
50
  confidence = round(score, 3)
51
 
52
+ # Assign a sentiment label based on the score
53
+ if sentiment_score == -1.0:
54
+ sentiment_label = "very negative"
55
+ elif sentiment_score == -0.7:
56
+ sentiment_label = "negative"
57
+ elif sentiment_score == 0.0:
58
+ sentiment_label = "neutral"
59
+ elif sentiment_score == 0.7:
60
+ sentiment_label = "positive"
61
+ else:
62
+ sentiment_label = "very positive"
63
+
64
  return SentimentResponse(
65
  content_id=request.content_id,
66
  sentiment_score=sentiment_score,
67
+ sentiment_label=sentiment_label,
68
  confidence=confidence
69
  )
70
  except Exception as e:
 
83
  sentiment_score = LABEL_MAP.get(label, 0.0)
84
  confidence = round(score, 3)
85
 
86
+ # Assign a sentiment label based on the score
87
+ if sentiment_score == -1.0:
88
+ sentiment_label = "very negative"
89
+ elif sentiment_score == -0.7:
90
+ sentiment_label = "negative"
91
+ elif sentiment_score == 0.0:
92
+ sentiment_label = "neutral"
93
+ elif sentiment_score == 0.7:
94
+ sentiment_label = "positive"
95
+ else:
96
+ sentiment_label = "very positive"
97
+
98
  responses.append(SentimentResponse(
99
  content_id=post.content_id,
100
  sentiment_score=sentiment_score,
101
+ sentiment_label=sentiment_label,
102
  confidence=confidence
103
  ))
104
  return responses