kambris commited on
Commit
377878f
·
verified ·
1 Parent(s): 0aac7b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -282,6 +282,21 @@ class SpeechAnalyzer:
282
  G.nodes[node]['pos'] = pos[node]
283
 
284
  return G
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  def main():
286
  st.title("🗣️ Political Text Analysis Toolkit")
287
 
@@ -344,19 +359,8 @@ def main():
344
  progress_bar.progress(40)
345
  st.subheader("Speech Trajectory Analysis")
346
 
347
- # Process ALL data upfront
348
- segments = analyzer.split_text(text, max_length=512)
349
- num_segments = len(segments)
350
- segment_labels = [f"{i+1}" for i in range(num_segments)]
351
-
352
- # Get all analysis data at once
353
- with st.spinner('Processing all analyses...'):
354
- sentiment_scores, basic_emotions = analyzer.analyze_emotional_trajectory(text)
355
- moral_trajectories = {foundation: [] for foundation in ['care', 'fairness', 'loyalty', 'authority', 'sanctity']}
356
- for segment in segments:
357
- moral_scores = analyzer.analyze_moral_foundations(segment)
358
- for foundation in moral_trajectories.keys():
359
- moral_trajectories[foundation].append(moral_scores[foundation])
360
 
361
  # Create unified figure
362
  unified_fig = go.Figure()
@@ -401,9 +405,12 @@ def main():
401
  unified_fig.add_trace(go.Bar(
402
  x=segment_labels,
403
  y=[1 if e == 'joy' else 0.5 if e == 'neutral' else -0.5 for e in basic_emotions],
404
- name='Emotions',
405
  marker_color='#2ecc71',
406
- opacity=0.6
 
 
 
407
  ))
408
 
409
  unified_fig.update_layout(
 
282
  G.nodes[node]['pos'] = pos[node]
283
 
284
  return G
285
+
286
+ @st.cache_data
287
+ def process_all_analyses(text, analyzer):
288
+ segments = analyzer.split_text(text, max_length=512)
289
+ num_segments = len(segments)
290
+ segment_labels = [f"{i+1}" for i in range(num_segments)]
291
+
292
+ sentiment_scores, basic_emotions = analyzer.analyze_emotional_trajectory(text)
293
+ moral_trajectories = {foundation: [] for foundation in ['care', 'fairness', 'loyalty', 'authority', 'sanctity']}
294
+ for segment in segments:
295
+ moral_scores = analyzer.analyze_moral_foundations(segment)
296
+ for foundation in moral_trajectories.keys():
297
+ moral_trajectories[foundation].append(moral_scores[foundation])
298
+
299
+ return segments, segment_labels, sentiment_scores, basic_emotions, moral_trajectories
300
  def main():
301
  st.title("🗣️ Political Text Analysis Toolkit")
302
 
 
359
  progress_bar.progress(40)
360
  st.subheader("Speech Trajectory Analysis")
361
 
362
+ # Get cached data
363
+ segments, segment_labels, sentiment_scores, basic_emotions, moral_trajectories = process_all_analyses(text, analyzer)
 
 
 
 
 
 
 
 
 
 
 
364
 
365
  # Create unified figure
366
  unified_fig = go.Figure()
 
405
  unified_fig.add_trace(go.Bar(
406
  x=segment_labels,
407
  y=[1 if e == 'joy' else 0.5 if e == 'neutral' else -0.5 for e in basic_emotions],
408
+ name=f'Emotions ({", ".join(set(basic_emotions))})',
409
  marker_color='#2ecc71',
410
+ opacity=0.6,
411
+ hovertemplate="Segment: %{x}<br>Emotion: " +
412
+ "<br>".join([f"{e}" for e in basic_emotions]) +
413
+ "<extra></extra>"
414
  ))
415
 
416
  unified_fig.update_layout(