Update app.py
Browse files
app.py
CHANGED
@@ -274,64 +274,94 @@ def main():
|
|
274 |
# Detailed insights
|
275 |
for foundation, score in moral_scores.items():
|
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 |
-
#
|
283 |
-
scaled_trajectory = np.array(emotional_trajectory)
|
284 |
-
scaled_trajectory = np.clip(scaled_trajectory, -1, 1)
|
285 |
|
286 |
-
# Create segment labels
|
287 |
num_segments = len(scaled_trajectory)
|
288 |
-
segment_labels = [f"
|
289 |
|
290 |
trajectory_fig = go.Figure(data=go.Scatter(
|
291 |
x=segment_labels,
|
292 |
y=scaled_trajectory,
|
293 |
mode='lines+markers',
|
294 |
-
name='Emotional Intensity',
|
295 |
line=dict(
|
296 |
color='#1f77b4',
|
297 |
width=3
|
298 |
),
|
299 |
marker=dict(
|
300 |
size=8,
|
301 |
-
color='#1f77b4'
|
|
|
302 |
)
|
303 |
))
|
304 |
|
305 |
trajectory_fig.update_layout(
|
306 |
-
title='
|
307 |
-
xaxis_title='Speech
|
308 |
-
yaxis_title='
|
309 |
yaxis=dict(
|
310 |
ticktext=['Very Negative', 'Neutral', 'Very Positive'],
|
311 |
tickvals=[-1, 0, 1],
|
312 |
-
range=[-1, 1]
|
|
|
313 |
),
|
314 |
hovermode='x unified',
|
315 |
-
showlegend=False
|
|
|
316 |
)
|
317 |
|
318 |
st.plotly_chart(trajectory_fig)
|
319 |
-
|
320 |
|
321 |
with tab3:
|
322 |
-
st.subheader("Linguistic
|
323 |
readability = analyzer.calculate_readability(text)
|
324 |
|
|
|
325 |
col1, col2 = st.columns(2)
|
326 |
with col1:
|
327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
with col2:
|
329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
|
331 |
-
#
|
332 |
-
st.subheader("Key
|
333 |
key_phrases = analyzer.extract_key_phrases(text)
|
334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
|
336 |
with tab4:
|
337 |
st.subheader("Semantic Network")
|
@@ -403,71 +433,43 @@ def main():
|
|
403 |
st.plotly_chart(network_fig, use_container_width=True)
|
404 |
|
405 |
with tab5:
|
406 |
-
st.subheader("
|
407 |
-
|
408 |
-
# Named Entities with clear explanations
|
409 |
-
st.write("### Key People, Organizations, and Places")
|
410 |
named_entities = analyzer.detect_named_entities(text)
|
411 |
|
412 |
-
#
|
413 |
-
entity_type_mapping = {
|
414 |
-
'PER': 'Person',
|
415 |
-
'ORG': 'Organization',
|
416 |
-
'LOC': 'Location',
|
417 |
-
'GPE': 'Country/City',
|
418 |
-
'MISC': 'Miscellaneous'
|
419 |
-
}
|
420 |
-
|
421 |
-
# Transform the entities dataframe
|
422 |
entities_df = pd.DataFrame(named_entities)
|
423 |
-
entities_df['entity_type'] = entities_df['entity_group'].map(entity_type_mapping)
|
424 |
-
entities_df['confidence'] = entities_df['score'].apply(lambda x: f"{x*100:.1f}%")
|
425 |
-
|
426 |
-
# Display enhanced table
|
427 |
-
display_df = entities_df[['word', 'entity_type', 'confidence']].rename(columns={
|
428 |
-
'word': 'Name/Term',
|
429 |
-
'entity_type': 'Type',
|
430 |
-
'confidence': 'Confidence Level'
|
431 |
-
})
|
432 |
-
|
433 |
-
st.dataframe(
|
434 |
-
display_df,
|
435 |
-
column_config={
|
436 |
-
"Name/Term": st.column_config.TextColumn(
|
437 |
-
help="The identified name or term from the text"
|
438 |
-
),
|
439 |
-
"Type": st.column_config.TextColumn(
|
440 |
-
help="Category of the identified term"
|
441 |
-
),
|
442 |
-
"Confidence Level": st.column_config.TextColumn(
|
443 |
-
help="How certain the AI is about this identification"
|
444 |
-
)
|
445 |
-
},
|
446 |
-
hide_index=True
|
447 |
-
)
|
448 |
|
449 |
-
#
|
450 |
-
|
451 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
452 |
|
453 |
-
#
|
454 |
-
|
|
|
|
|
|
|
|
|
455 |
|
456 |
-
#
|
457 |
-
|
458 |
-
'
|
459 |
-
'
|
460 |
-
|
461 |
-
'hyperbole': 'Dramatic exaggerations',
|
462 |
-
'rhetorical_question': 'Questions asked for effect'
|
463 |
-
}
|
464 |
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
|
472 |
|
473 |
if __name__ == "__main__":
|
|
|
274 |
# Detailed insights
|
275 |
for foundation, score in moral_scores.items():
|
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")
|
323 |
readability = analyzer.calculate_readability(text)
|
324 |
|
325 |
+
# Readability metrics with context
|
326 |
col1, col2 = st.columns(2)
|
327 |
with col1:
|
328 |
+
score = readability['Flesch Reading Ease']
|
329 |
+
interpretation = "Complex" if score < 50 else "Standard" if score < 70 else "Easy"
|
330 |
+
st.metric(
|
331 |
+
label="Reading Ease",
|
332 |
+
value=f"{score:.1f}/100",
|
333 |
+
delta=interpretation,
|
334 |
+
delta_color="normal"
|
335 |
+
)
|
336 |
+
|
337 |
with col2:
|
338 |
+
grade = readability['Flesch-Kincaid Grade Level']
|
339 |
+
st.metric(
|
340 |
+
label="Education Level",
|
341 |
+
value=f"Grade {grade:.1f}",
|
342 |
+
delta="Years of Education",
|
343 |
+
delta_color="normal"
|
344 |
+
)
|
345 |
|
346 |
+
# Enhanced key phrases display
|
347 |
+
st.subheader("Key Topics and Themes")
|
348 |
key_phrases = analyzer.extract_key_phrases(text)
|
349 |
+
|
350 |
+
# Create columns for better phrase organization
|
351 |
+
cols = st.columns(3)
|
352 |
+
for idx, phrase in enumerate(key_phrases):
|
353 |
+
col_idx = idx % 3
|
354 |
+
cols[col_idx].markdown(
|
355 |
+
f"""<div style='
|
356 |
+
background-color: rgba(31, 119, 180, {0.9 - idx*0.05});
|
357 |
+
color: white;
|
358 |
+
padding: 8px 15px;
|
359 |
+
margin: 5px 0;
|
360 |
+
border-radius: 15px;
|
361 |
+
text-align: center;
|
362 |
+
'>{phrase}</div>""",
|
363 |
+
unsafe_allow_html=True
|
364 |
+
)
|
365 |
|
366 |
with tab4:
|
367 |
st.subheader("Semantic Network")
|
|
|
433 |
st.plotly_chart(network_fig, use_container_width=True)
|
434 |
|
435 |
with tab5:
|
436 |
+
st.subheader("Named Entity Recognition")
|
|
|
|
|
|
|
437 |
named_entities = analyzer.detect_named_entities(text)
|
438 |
|
439 |
+
# Process entities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
entities_df = pd.DataFrame(named_entities)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
441 |
|
442 |
+
# Map entity types to friendly names
|
443 |
+
type_mapping = {
|
444 |
+
'B-PER': 'Person',
|
445 |
+
'I-PER': 'Person',
|
446 |
+
'B-ORG': 'Organization',
|
447 |
+
'I-ORG': 'Organization',
|
448 |
+
'B-LOC': 'Location',
|
449 |
+
'I-LOC': 'Location',
|
450 |
+
'B-MISC': 'Other',
|
451 |
+
'I-MISC': 'Other'
|
452 |
+
}
|
453 |
|
454 |
+
# Clean and transform the data
|
455 |
+
display_df = pd.DataFrame({
|
456 |
+
'Term': entities_df['word'],
|
457 |
+
'Category': entities_df['entity'].map(type_mapping),
|
458 |
+
'Confidence': entities_df['score'].apply(lambda x: f"{x*100:.1f}%")
|
459 |
+
})
|
460 |
|
461 |
+
# Group similar entities
|
462 |
+
grouped_df = display_df.groupby('Category').agg({
|
463 |
+
'Term': lambda x: ', '.join(set(x)),
|
464 |
+
'Confidence': 'count'
|
465 |
+
}).reset_index()
|
|
|
|
|
|
|
466 |
|
467 |
+
# Display results in an organized way
|
468 |
+
for category in grouped_df['Category'].unique():
|
469 |
+
category_data = grouped_df[grouped_df['Category'] == category]
|
470 |
+
st.write(f"### {category}")
|
471 |
+
st.markdown(f"**Found**: {category_data['Term'].iloc[0]}")
|
472 |
+
|
473 |
|
474 |
|
475 |
if __name__ == "__main__":
|