kambris commited on
Commit
5be03e1
·
verified ·
1 Parent(s): cae4daa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -50
app.py CHANGED
@@ -138,54 +138,20 @@ class SpeechAnalyzer:
138
  sentiment_scores = []
139
  basic_emotions = []
140
 
141
- # Add emotion classifier pipeline
142
- emotion_classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
143
-
144
  for segment in segments:
145
- sentences = nltk.sent_tokenize(segment)
146
- batch_size = 64
147
- segment_scores = []
148
- segment_emotions = []
149
-
150
- for i in range(0, len(sentences), batch_size):
151
- batch = sentences[i:i+batch_size]
152
- batch = [sent[:512] for sent in batch]
153
- batch = [sent if sent.strip() else "." for sent in batch]
154
-
155
- # Get sentiment scores
156
- try:
157
- results = self.sentiment_pipeline(batch)
158
- batch_scores = []
159
- for result in results:
160
- score = result['score']
161
- # Enhanced score scaling for better visualization
162
- if result['label'] == 'POSITIVE':
163
- score = 0.5 + (score * 0.5) # Scale from 0.5 to 1.0
164
- else:
165
- score = 0.5 - (score * 0.5) # Scale from 0.0 to 0.5
166
- batch_scores.append(score)
167
- segment_scores.extend(batch_scores)
168
-
169
- # Get emotion classifications
170
- emotion_results = emotion_classifier(batch)
171
- batch_emotions = []
172
- for result in emotion_results:
173
- # Get the dominant emotion
174
- dominant_emotion = max(result[0], key=lambda x: x['score'])
175
- batch_emotions.append(dominant_emotion['label'])
176
- segment_emotions.extend(batch_emotions)
177
-
178
- except Exception as e:
179
- print(f"Batch processing error: {e}")
180
- segment_scores.extend([0.5] * len(batch))
181
- segment_emotions.extend(['neutral'] * len(batch))
182
-
183
- sentiment_scores.append(np.mean(segment_scores))
184
- # Get most frequent emotion in segment
185
- if segment_emotions:
186
- basic_emotions.append(max(set(segment_emotions), key=segment_emotions.count))
187
  else:
188
- basic_emotions.append('neutral')
 
 
 
 
 
 
189
 
190
  return sentiment_scores, basic_emotions
191
 
@@ -417,13 +383,15 @@ def main():
417
  unified_fig.add_trace(go.Bar(
418
  x=segment_labels,
419
  y=[1] * len(basic_emotions), # Full height bars
420
- name='Basic Emotions',
421
  marker=dict(
422
- color=[emotion_colors.get(e.lower(), '#808080') for e in basic_emotions]
 
423
  ),
424
  opacity=0.8,
425
- hovertemplate="Segment: %{x}<br>Emotion: %{text}<extra></extra>",
426
- text=basic_emotions
 
427
  ))
428
 
429
  st.plotly_chart(unified_fig, use_container_width=True)
 
138
  sentiment_scores = []
139
  basic_emotions = []
140
 
 
 
 
141
  for segment in segments:
142
+ # Get sentiment scores
143
+ sentiment_result = self.sentiment_pipeline(segment)
144
+ score = sentiment_result[0]['score']
145
+ if sentiment_result[0]['label'] == 'POSITIVE':
146
+ score = 0.5 + (score * 0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  else:
148
+ score = 0.5 - (score * 0.5)
149
+ sentiment_scores.append(score)
150
+
151
+ # Get emotion classification
152
+ emotion_result = self.emotion_classifier(segment)
153
+ emotion = emotion_result[0]['label']
154
+ basic_emotions.append(emotion)
155
 
156
  return sentiment_scores, basic_emotions
157
 
 
383
  unified_fig.add_trace(go.Bar(
384
  x=segment_labels,
385
  y=[1] * len(basic_emotions), # Full height bars
386
+ name=f'Emotions Found: {", ".join(sorted(set(basic_emotions)))}', # Shows all unique emotions
387
  marker=dict(
388
+ color=[emotion_colors.get(e.lower(), '#808080') for e in basic_emotions],
389
+ line=dict(width=1, color='#000000') # Adds border for better visibility
390
  ),
391
  opacity=0.8,
392
+ hovertemplate="Segment %{x}<br>Emotion: %{text}<extra></extra>",
393
+ text=basic_emotions,
394
+ textposition='auto'
395
  ))
396
 
397
  st.plotly_chart(unified_fig, use_container_width=True)