Update app.py
Browse files
app.py
CHANGED
@@ -126,39 +126,60 @@ class SpeechAnalyzer:
|
|
126 |
}
|
127 |
|
128 |
return aggregated_scores
|
129 |
-
|
130 |
def analyze_emotional_trajectory(self, text, window_size=5):
|
131 |
-
"""Enhanced emotional trajectory analysis using
|
132 |
segments = self.split_text(text, max_length=512)
|
133 |
sentiment_scores = []
|
134 |
|
135 |
for segment in segments:
|
136 |
-
# Split into sentences using
|
137 |
sentences = nltk.sent_tokenize(segment)
|
138 |
|
139 |
# Process sentences in batches
|
140 |
batch_size = 64
|
141 |
segment_scores = []
|
|
|
142 |
for i in range(0, len(sentences), batch_size):
|
143 |
batch = sentences[i:i+batch_size]
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
|
|
148 |
avg_score = np.mean(segment_scores) if segment_scores else 0
|
149 |
sentiment_scores.append(avg_score)
|
150 |
|
151 |
-
# Normalize scores
|
152 |
-
if sentiment_scores:
|
153 |
-
min_score = min(sentiment_scores)
|
154 |
-
max_score = max(sentiment_scores)
|
155 |
-
score_range = max_score - min_score
|
156 |
-
if score_range > 0:
|
157 |
-
sentiment_scores = [(s - min_score) / score_range * 2 - 1 for s in sentiment_scores]
|
158 |
-
|
159 |
return sentiment_scores
|
160 |
|
161 |
-
|
162 |
def detect_named_entities(self, text):
|
163 |
"""Detect named entities in the text"""
|
164 |
entities = self.ner_pipeline(text)
|
|
|
126 |
}
|
127 |
|
128 |
return aggregated_scores
|
129 |
+
|
130 |
def analyze_emotional_trajectory(self, text, window_size=5):
|
131 |
+
"""Enhanced emotional trajectory analysis using batch processing"""
|
132 |
segments = self.split_text(text, max_length=512)
|
133 |
sentiment_scores = []
|
134 |
|
135 |
for segment in segments:
|
136 |
+
# Split into sentences using NLTK
|
137 |
sentences = nltk.sent_tokenize(segment)
|
138 |
|
139 |
# Process sentences in batches
|
140 |
batch_size = 64
|
141 |
segment_scores = []
|
142 |
+
|
143 |
for i in range(0, len(sentences), batch_size):
|
144 |
batch = sentences[i:i+batch_size]
|
145 |
+
|
146 |
+
try:
|
147 |
+
# Pad or truncate sentences to ensure consistent length
|
148 |
+
batch = [sent[:512] for sent in batch] # Truncate to max model input
|
149 |
+
|
150 |
+
# Ensure all sentences are strings and non-empty
|
151 |
+
batch = [sent if sent.strip() else "." for sent in batch]
|
152 |
+
|
153 |
+
results = self.sentiment_pipeline(batch)
|
154 |
+
|
155 |
+
# Process batch results
|
156 |
+
batch_scores = []
|
157 |
+
for result in results:
|
158 |
+
# Convert to signed score (-1 to 1 range)
|
159 |
+
score = result['score']
|
160 |
+
score = score * 2 - 1 if result['label'] == 'POSITIVE' else -score
|
161 |
+
batch_scores.append(score)
|
162 |
+
|
163 |
+
segment_scores.extend(batch_scores)
|
164 |
+
|
165 |
+
except Exception as e:
|
166 |
+
print(f"Batch processing error: {e}")
|
167 |
+
# Fallback to individual processing if batch fails
|
168 |
+
for sent in batch:
|
169 |
+
try:
|
170 |
+
result = self.sentiment_pipeline(sent)[0]
|
171 |
+
score = result['score']
|
172 |
+
score = score * 2 - 1 if result['label'] == 'POSITIVE' else -score
|
173 |
+
segment_scores.append(score)
|
174 |
+
except:
|
175 |
+
segment_scores.append(0)
|
176 |
|
177 |
+
# Calculate average score for the segment
|
178 |
avg_score = np.mean(segment_scores) if segment_scores else 0
|
179 |
sentiment_scores.append(avg_score)
|
180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
return sentiment_scores
|
182 |
|
|
|
183 |
def detect_named_entities(self, text):
|
184 |
"""Detect named entities in the text"""
|
185 |
entities = self.ner_pipeline(text)
|