kambris commited on
Commit
2ed6513
·
verified ·
1 Parent(s): fc27bac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -37
app.py CHANGED
@@ -276,47 +276,89 @@ def main():
276
  st.write(f"**{MORAL_FOUNDATIONS[foundation]}**: {score:.2%}")
277
 
278
  with tab2:
279
- st.subheader("Emotional Trajectory")
280
- emotional_trajectory = analyzer.analyze_emotional_trajectory(text)
281
 
282
- # Normalize and scale the sentiment values
283
- scaled_trajectory = np.array(emotional_trajectory) * 2 - 1 # Scale to [-1, 1]
284
-
285
- # Create segment labels
286
- num_segments = len(scaled_trajectory)
287
- segment_labels = [f"{i+1}" for i in range(num_segments)]
288
 
289
- trajectory_fig = go.Figure(data=go.Scatter(
290
- x=segment_labels,
291
- y=scaled_trajectory,
292
- mode='lines+markers',
293
- line=dict(
294
- color='#1f77b4',
295
- width=3
296
- ),
297
- marker=dict(
298
- size=8,
299
- color='#1f77b4',
300
- symbol='circle'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  )
302
- ))
303
-
304
- trajectory_fig.update_layout(
305
- title='Emotional Flow Throughout the Speech',
306
- xaxis_title='Speech Segments',
307
- yaxis_title='Emotional Tone',
308
- yaxis=dict(
309
- ticktext=['Very Negative', 'Neutral', 'Very Positive'],
310
- tickvals=[-1, 0, 1],
311
- range=[-1.1, 1.1],
312
- gridcolor='lightgray'
313
- ),
314
- hovermode='x unified',
315
- showlegend=False,
316
- plot_bgcolor='white'
317
- )
318
 
319
- st.plotly_chart(trajectory_fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
  with tab3:
322
  st.subheader("Linguistic Analysis")
 
276
  st.write(f"**{MORAL_FOUNDATIONS[foundation]}**: {score:.2%}")
277
 
278
  with tab2:
279
+ st.subheader("Speech Trajectory Analysis")
 
280
 
281
+ col1, col2 = st.columns(2)
 
 
 
 
 
282
 
283
+ with col1:
284
+ st.write("### Emotional Flow")
285
+ # Get raw sentiment scores
286
+ sentiment_results = [analyzer.sentiment_pipeline(segment)[0] for segment in analyzer.split_text(text)]
287
+
288
+ # Convert sentiment labels to numerical values
289
+ sentiment_values = [1 if result['label'] == 'POSITIVE' else -1 for result in sentiment_results]
290
+
291
+ # Create segment labels
292
+ num_segments = len(sentiment_values)
293
+ segment_labels = [f"{i+1}" for i in range(num_segments)]
294
+
295
+ trajectory_fig = go.Figure(data=go.Scatter(
296
+ x=segment_labels,
297
+ y=sentiment_values,
298
+ mode='lines+markers',
299
+ line=dict(
300
+ color='#1f77b4',
301
+ width=3
302
+ ),
303
+ marker=dict(
304
+ size=8,
305
+ color='#1f77b4',
306
+ symbol='circle'
307
+ )
308
+ ))
309
+
310
+ trajectory_fig.update_layout(
311
+ title='Emotional Flow Throughout the Speech',
312
+ xaxis_title='Speech Segments',
313
+ yaxis_title='Emotional Tone',
314
+ yaxis=dict(
315
+ ticktext=['Very Negative', 'Neutral', 'Very Positive'],
316
+ tickvals=[-1, 0, 1],
317
+ range=[-1.1, 1.1],
318
+ gridcolor='lightgray'
319
+ ),
320
+ hovermode='x unified',
321
+ showlegend=False,
322
+ plot_bgcolor='white'
323
  )
324
+
325
+ st.plotly_chart(trajectory_fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
 
327
+ with col2:
328
+ st.write("### Moral Foundations Flow")
329
+ # Get moral scores per segment
330
+ segments = analyzer.split_text(text)
331
+ moral_trajectories = {foundation: [] for foundation in MORAL_FOUNDATIONS}
332
+
333
+ for segment in segments:
334
+ segment_scores = analyzer.analyze_moral_foundations(segment)
335
+ for foundation, score in segment_scores.items():
336
+ moral_trajectories[foundation].append(score)
337
+
338
+ moral_fig = go.Figure()
339
+ for foundation, scores in moral_trajectories.items():
340
+ moral_fig.add_trace(go.Scatter(
341
+ x=list(range(1, len(scores) + 1)),
342
+ y=scores,
343
+ name=MORAL_FOUNDATIONS[foundation],
344
+ mode='lines+markers'
345
+ ))
346
+
347
+ moral_fig.update_layout(
348
+ title='Moral Foundations Flow',
349
+ xaxis_title='Speech Segments',
350
+ yaxis_title='Foundation Strength',
351
+ hovermode='x unified',
352
+ plot_bgcolor='white',
353
+ showlegend=True,
354
+ legend=dict(
355
+ yanchor="top",
356
+ y=0.99,
357
+ xanchor="left",
358
+ x=1.05
359
+ )
360
+ )
361
+ st.plotly_chart(moral_fig)
362
 
363
  with tab3:
364
  st.subheader("Linguistic Analysis")