Spaces:
Runtime error
Runtime error
Create visualization.py
Browse files- visualization.py +76 -0
visualization.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import plotly.graph_objs as go
|
3 |
+
import re
|
4 |
+
from collections import Counter
|
5 |
+
|
6 |
+
def extract_speaker_data(text):
|
7 |
+
speakers = {}
|
8 |
+
current_speaker = None
|
9 |
+
for line in text.split('\n'):
|
10 |
+
if line.lower().startswith("speaker"):
|
11 |
+
current_speaker = line.split(":")[1].strip() if ":" in line else line.split()[1]
|
12 |
+
speakers[current_speaker] = {}
|
13 |
+
elif current_speaker and ":" in line:
|
14 |
+
key, value = line.split(":", 1)
|
15 |
+
try:
|
16 |
+
speakers[current_speaker][key.strip()] = float(value.strip())
|
17 |
+
except ValueError:
|
18 |
+
match = re.search(r"[-+]?\d*\.\d+|\d+", value)
|
19 |
+
if match:
|
20 |
+
speakers[current_speaker][key.strip()] = float(match.group())
|
21 |
+
return speakers
|
22 |
+
|
23 |
+
def create_bar_chart(data, title, speaker):
|
24 |
+
fig = go.Figure(data=[go.Bar(
|
25 |
+
x=list(data.keys()),
|
26 |
+
y=list(data.values()),
|
27 |
+
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)]
|
28 |
+
)])
|
29 |
+
fig.update_layout(title=f"{title} - Speaker {speaker}", xaxis_title="Traits", yaxis_title="Score")
|
30 |
+
return fig
|
31 |
+
|
32 |
+
def create_heatmap(data, title, speaker):
|
33 |
+
fig = go.Figure(data=go.Heatmap(
|
34 |
+
z=[[data[k] for k in data]],
|
35 |
+
x=list(data.keys()),
|
36 |
+
y=[speaker],
|
37 |
+
colorscale='Viridis'
|
38 |
+
))
|
39 |
+
fig.update_layout(title=f"{title} - Speaker {speaker}")
|
40 |
+
return fig
|
41 |
+
|
42 |
+
def update_visibility_and_charts(status, exec_time, lang, info, attachments, bigfive, personalities):
|
43 |
+
charts = []
|
44 |
+
|
45 |
+
speakers_data = extract_speaker_data(attachments + "\n" + bigfive + "\n" + personalities)
|
46 |
+
main_speakers = list(speakers_data.keys())[:2] # Limit to 2 main speakers
|
47 |
+
|
48 |
+
for speaker in main_speakers:
|
49 |
+
data = speakers_data[speaker]
|
50 |
+
|
51 |
+
attachment_data = {k: v for k, v in data.items() if k in ["Secured", "Anxious-Preoccupied", "Dismissive-Avoidant", "Fearful-Avoidant"]}
|
52 |
+
if attachment_data:
|
53 |
+
charts.append(create_bar_chart(attachment_data, "Attachment Styles", speaker))
|
54 |
+
|
55 |
+
bigfive_data = {k: v for k, v in data.items() if k in ["Extraversion", "Agreeableness", "Conscientiousness", "Neuroticism", "Openness"]}
|
56 |
+
if bigfive_data:
|
57 |
+
charts.append(create_bar_chart(bigfive_data, "Big Five Traits", speaker))
|
58 |
+
|
59 |
+
personality_data = {k: v for k, v in data.items() if k in ["Depressed", "Paranoid", "Schizoid-Schizotypal", "Antisocial-Psychopathic", "Borderline-Dysregulated", "Hysteric-Histrionic", "Narcissistic", "Anxious-Avoidant", "Dependent-Victimized", "Obsessional"]}
|
60 |
+
if personality_data:
|
61 |
+
charts.append(create_bar_chart(personality_data, "Personality Traits", speaker))
|
62 |
+
|
63 |
+
anxiety_avoidance_data = {k: v for k, v in data.items() if k in ["Anxiety", "Avoidance"]}
|
64 |
+
if anxiety_avoidance_data:
|
65 |
+
charts.append(create_heatmap(anxiety_avoidance_data, "Anxiety-Avoidance", speaker))
|
66 |
+
|
67 |
+
self_others_data = {k: v for k, v in data.items() if k in ["Self", "Others"]}
|
68 |
+
if self_others_data:
|
69 |
+
charts.append(create_heatmap(self_others_data, "Self-Others", speaker))
|
70 |
+
|
71 |
+
return [
|
72 |
+
gr.update(value=status, visible=True),
|
73 |
+
gr.update(value=exec_time, visible=True),
|
74 |
+
gr.update(value=lang, visible=True),
|
75 |
+
gr.update(value=info, visible=True),
|
76 |
+
] + charts
|