kambris commited on
Commit
4e1219c
·
verified ·
1 Parent(s): 14c93da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -39
app.py CHANGED
@@ -124,21 +124,20 @@ class SpeechAnalyzer:
124
 
125
  return aggregated_scores
126
 
127
- def analyze_emotional_trajectory(self, text, window_size=5, ngram_size=3):
128
- """Enhanced emotional trajectory analysis using efficient batch processing"""
129
- segments = self.split_text(text, max_length=512)
130
  sentiment_scores = []
131
 
132
- # Process n-grams in larger batches while keeping full text
133
- batch_size = 32
134
  for segment in segments:
135
- words = segment.split()
136
- ngrams = [' '.join(words[i:i+ngram_size]) for i in range(0, len(words)-ngram_size+1)]
137
 
138
- # Process full ngrams in batches
 
139
  segment_scores = []
140
- for i in range(0, len(ngrams), batch_size):
141
- batch = ngrams[i:i+batch_size]
142
  results = self.sentiment_pipeline(batch)
143
  batch_scores = [result['score'] for result in results]
144
  segment_scores.extend(batch_scores)
@@ -146,7 +145,7 @@ class SpeechAnalyzer:
146
  avg_score = np.mean(segment_scores) if segment_scores else 0
147
  sentiment_scores.append(avg_score)
148
 
149
- # Normalize scores to preserve full range of sentiment
150
  if sentiment_scores:
151
  min_score = min(sentiment_scores)
152
  max_score = max(sentiment_scores)
@@ -156,6 +155,7 @@ class SpeechAnalyzer:
156
 
157
  return sentiment_scores
158
 
 
159
  def detect_named_entities(self, text):
160
  """Detect named entities in the text"""
161
  entities = self.ner_pipeline(text)
@@ -305,44 +305,21 @@ def main():
305
  # Detailed insights
306
  for foundation, score in moral_scores.items():
307
  st.write(f"**{MORAL_FOUNDATIONS[foundation]}**: {score:.2%}")
308
-
309
  with tab2:
310
  status_text.text('Processing Emotional Trajectory...')
311
  progress_bar.progress(40)
312
  st.subheader("Speech Trajectory Analysis")
313
  col1, col2 = st.columns(2)
314
 
315
- # First, create consistent segments for both analyses
316
- segments = analyzer.split_text(text, max_length=512)
317
  num_segments = len(segments)
318
  segment_labels = [f"{i+1}" for i in range(num_segments)]
319
 
320
  with col1:
321
  st.write("### Emotional Flow")
322
- sentiment_scores = []
323
-
324
- for segment in segments:
325
- # Get words for n-gram analysis within each segment
326
- words = segment.split()
327
- ngram_size = 3
328
- ngrams = [' '.join(words[i:i+ngram_size]) for i in range(0, len(words)-ngram_size+1)]
329
-
330
- # Calculate segment score from n-grams
331
- segment_scores = []
332
- for ngram in ngrams:
333
- result = analyzer.sentiment_pipeline(ngram)[0]
334
- segment_scores.append(result['score'])
335
-
336
- # Use average score for the segment
337
- avg_score = np.mean(segment_scores) if segment_scores else 0
338
- sentiment_scores.append(avg_score)
339
-
340
- # Normalize scores to show relative variations
341
- min_score = min(sentiment_scores)
342
- max_score = max(sentiment_scores)
343
- score_range = max_score - min_score
344
- if score_range > 0:
345
- sentiment_scores = [(s - min_score) / score_range * 2 - 1 for s in sentiment_scores]
346
 
347
  trajectory_fig = go.Figure(data=go.Scatter(
348
  x=segment_labels,
@@ -402,7 +379,7 @@ def main():
402
  )
403
  )
404
  st.plotly_chart(moral_fig)
405
-
406
  with tab3:
407
  status_text.text('Analyzing Linguistic Features...')
408
  progress_bar.progress(60)
 
124
 
125
  return aggregated_scores
126
 
127
+ def analyze_emotional_trajectory(self, text, window_size=5):
128
+ """Enhanced emotional trajectory analysis using sentence-level processing"""
129
+ segments = self.split_text(text, max_length=1024)
130
  sentiment_scores = []
131
 
 
 
132
  for segment in segments:
133
+ # Split into sentences using spaCy or NLTK
134
+ sentences = nltk.sent_tokenize(segment)
135
 
136
+ # Process sentences in batches
137
+ batch_size = 64
138
  segment_scores = []
139
+ for i in range(0, len(sentences), batch_size):
140
+ batch = sentences[i:i+batch_size]
141
  results = self.sentiment_pipeline(batch)
142
  batch_scores = [result['score'] for result in results]
143
  segment_scores.extend(batch_scores)
 
145
  avg_score = np.mean(segment_scores) if segment_scores else 0
146
  sentiment_scores.append(avg_score)
147
 
148
+ # Normalize scores
149
  if sentiment_scores:
150
  min_score = min(sentiment_scores)
151
  max_score = max(sentiment_scores)
 
155
 
156
  return sentiment_scores
157
 
158
+
159
  def detect_named_entities(self, text):
160
  """Detect named entities in the text"""
161
  entities = self.ner_pipeline(text)
 
305
  # Detailed insights
306
  for foundation, score in moral_scores.items():
307
  st.write(f"**{MORAL_FOUNDATIONS[foundation]}**: {score:.2%}")
308
+
309
  with tab2:
310
  status_text.text('Processing Emotional Trajectory...')
311
  progress_bar.progress(40)
312
  st.subheader("Speech Trajectory Analysis")
313
  col1, col2 = st.columns(2)
314
 
315
+ # Create consistent segments for both analyses
316
+ segments = analyzer.split_text(text, max_length=1024)
317
  num_segments = len(segments)
318
  segment_labels = [f"{i+1}" for i in range(num_segments)]
319
 
320
  with col1:
321
  st.write("### Emotional Flow")
322
+ sentiment_scores = analyzer.analyze_emotional_trajectory(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
  trajectory_fig = go.Figure(data=go.Scatter(
325
  x=segment_labels,
 
379
  )
380
  )
381
  st.plotly_chart(moral_fig)
382
+
383
  with tab3:
384
  status_text.text('Analyzing Linguistic Features...')
385
  progress_bar.progress(60)