Spaces:
Running
Running
Kevin King
commited on
Commit
·
b18efa0
1
Parent(s):
fa097da
REFAC: Enhance emotion vector creation and add ECI timeline calculation in Streamlit app
Browse files- src/streamlit_app.py +15 -13
src/streamlit_app.py
CHANGED
@@ -55,6 +55,7 @@ whisper_model, text_classifier, ser_model, ser_feature_extractor = load_models()
|
|
55 |
def create_unified_vector(scores_dict, mapping_dict):
|
56 |
vector = np.zeros(len(UNIFIED_EMOTIONS))
|
57 |
total_score = 0
|
|
|
58 |
for label, score in scores_dict.items():
|
59 |
unified_label = mapping_dict.get(label)
|
60 |
if unified_label in UNIFIED_EMOTIONS:
|
@@ -169,7 +170,8 @@ if uploaded_file is not None:
|
|
169 |
fer_avg_scores = get_avg_unified_scores(fer_df)
|
170 |
ser_avg_scores = get_avg_unified_scores(ser_df)
|
171 |
ter_avg_scores = get_avg_unified_scores(ter_df)
|
172 |
-
|
|
|
173 |
fer_vector = create_unified_vector(fer_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
174 |
ser_vector = create_unified_vector(ser_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
175 |
text_vector = create_unified_vector(ter_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
@@ -197,19 +199,21 @@ if uploaded_file is not None:
|
|
197 |
|
198 |
full_index = np.arange(0, duration, 0.5)
|
199 |
combined_df = pd.DataFrame(index=full_index)
|
200 |
-
|
201 |
# --- NEW: ECI Timeline Calculation ---
|
202 |
eci_timeline = {}
|
203 |
for t_stamp in full_index:
|
204 |
vectors = []
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
210 |
vectors.append(create_unified_vector(ser_df.loc[int(t_stamp)].to_dict(), {e:e for e in UNIFIED_EMOTIONS}))
|
211 |
-
|
212 |
-
if
|
213 |
vectors.append(create_unified_vector(ter_df.loc[int(t_stamp)].to_dict(), {e:e for e in UNIFIED_EMOTIONS}))
|
214 |
|
215 |
if len(vectors) >= 2:
|
@@ -228,10 +232,9 @@ if uploaded_file is not None:
|
|
228 |
ter_df_resampled = ter_df.reindex(ter_df.index.union(full_index)).interpolate(method='linear').reindex(full_index)
|
229 |
for e in UNIFIED_EMOTIONS: combined_df[f'Text_{e}'] = ter_df_resampled.get(e, 0.0)
|
230 |
|
231 |
-
# Add ECI timeline to the DataFrame
|
232 |
if eci_timeline:
|
233 |
-
|
234 |
-
combined_df['ECI'] =
|
235 |
|
236 |
combined_df.fillna(0, inplace=True)
|
237 |
|
@@ -246,7 +249,6 @@ if uploaded_file is not None:
|
|
246 |
if emotion in colors:
|
247 |
ax.plot(combined_df.index, combined_df[col], label=f'{modality} {emotion.capitalize()}', color=colors[emotion], linestyle=styles[modality], alpha=0.7)
|
248 |
|
249 |
-
# Plot ECI on the same axis
|
250 |
if 'ECI' in combined_df.columns:
|
251 |
ax.plot(combined_df.index, combined_df['ECI'], label='Emotion Consistency', color='black', linewidth=2.5, alpha=0.9)
|
252 |
|
|
|
55 |
def create_unified_vector(scores_dict, mapping_dict):
|
56 |
vector = np.zeros(len(UNIFIED_EMOTIONS))
|
57 |
total_score = 0
|
58 |
+
# Use .items() to iterate over keys and values
|
59 |
for label, score in scores_dict.items():
|
60 |
unified_label = mapping_dict.get(label)
|
61 |
if unified_label in UNIFIED_EMOTIONS:
|
|
|
170 |
fer_avg_scores = get_avg_unified_scores(fer_df)
|
171 |
ser_avg_scores = get_avg_unified_scores(ser_df)
|
172 |
ter_avg_scores = get_avg_unified_scores(ter_df)
|
173 |
+
|
174 |
+
# Use the raw dictionaries for vector creation, not the mapped ones
|
175 |
fer_vector = create_unified_vector(fer_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
176 |
ser_vector = create_unified_vector(ser_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
177 |
text_vector = create_unified_vector(ter_avg_scores, {e:e for e in UNIFIED_EMOTIONS})
|
|
|
199 |
|
200 |
full_index = np.arange(0, duration, 0.5)
|
201 |
combined_df = pd.DataFrame(index=full_index)
|
202 |
+
|
203 |
# --- NEW: ECI Timeline Calculation ---
|
204 |
eci_timeline = {}
|
205 |
for t_stamp in full_index:
|
206 |
vectors = []
|
207 |
+
|
208 |
+
# Interpolate to get a value for any timestamp
|
209 |
+
fer_scores = fer_df.reindex(fer_df.index.union([t_stamp])).interpolate(method='linear').loc[t_stamp]
|
210 |
+
if not fer_scores.isnull().all():
|
211 |
+
vectors.append(create_unified_vector(fer_scores.to_dict(), {e:e for e in UNIFIED_EMOTIONS}))
|
212 |
+
|
213 |
+
if int(t_stamp) in ser_df.index:
|
214 |
vectors.append(create_unified_vector(ser_df.loc[int(t_stamp)].to_dict(), {e:e for e in UNIFIED_EMOTIONS}))
|
215 |
+
|
216 |
+
if int(t_stamp) in ter_df.index:
|
217 |
vectors.append(create_unified_vector(ter_df.loc[int(t_stamp)].to_dict(), {e:e for e in UNIFIED_EMOTIONS}))
|
218 |
|
219 |
if len(vectors) >= 2:
|
|
|
232 |
ter_df_resampled = ter_df.reindex(ter_df.index.union(full_index)).interpolate(method='linear').reindex(full_index)
|
233 |
for e in UNIFIED_EMOTIONS: combined_df[f'Text_{e}'] = ter_df_resampled.get(e, 0.0)
|
234 |
|
|
|
235 |
if eci_timeline:
|
236 |
+
eci_series = pd.Series(eci_timeline).reindex(full_index).interpolate(method='linear')
|
237 |
+
combined_df['ECI'] = eci_series
|
238 |
|
239 |
combined_df.fillna(0, inplace=True)
|
240 |
|
|
|
249 |
if emotion in colors:
|
250 |
ax.plot(combined_df.index, combined_df[col], label=f'{modality} {emotion.capitalize()}', color=colors[emotion], linestyle=styles[modality], alpha=0.7)
|
251 |
|
|
|
252 |
if 'ECI' in combined_df.columns:
|
253 |
ax.plot(combined_df.index, combined_df['ECI'], label='Emotion Consistency', color='black', linewidth=2.5, alpha=0.9)
|
254 |
|