Spaces:
Runtime error
Runtime error
Update visualization.py
Browse files- visualization.py +73 -77
visualization.py
CHANGED
@@ -1,82 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import plotly.graph_objs as go
|
3 |
|
4 |
-
def
|
5 |
-
speakers_data = {}
|
6 |
-
current_speaker = None
|
7 |
-
explanation = ""
|
8 |
-
for line in text.split('\n'):
|
9 |
-
line = line.strip()
|
10 |
-
if line.startswith("-------"):
|
11 |
-
if current_speaker and explanation:
|
12 |
-
speakers_data[current_speaker]["explanation"] = explanation.strip()
|
13 |
-
explanation = ""
|
14 |
-
current_speaker = None
|
15 |
-
continue
|
16 |
-
if line.startswith("Speaker"):
|
17 |
-
current_speaker = line.strip() # Remove the colon
|
18 |
-
speakers_data[current_speaker] = {}
|
19 |
-
elif ':' in line and current_speaker:
|
20 |
-
key, value = line.split(':', 1)
|
21 |
-
key = key.strip()
|
22 |
-
value = value.strip()
|
23 |
-
if key.lower() == "explanation":
|
24 |
-
explanation += value + " "
|
25 |
-
elif key in ["Self", "Others", "Anxiety", "Avoidance"]:
|
26 |
-
try:
|
27 |
-
speakers_data[current_speaker][key] = float(value)
|
28 |
-
except ValueError:
|
29 |
-
speakers_data[current_speaker][key] = value
|
30 |
-
else:
|
31 |
-
try:
|
32 |
-
speakers_data[current_speaker][key] = float(value)
|
33 |
-
except ValueError:
|
34 |
-
speakers_data[current_speaker][key] = value
|
35 |
-
elif line and current_speaker and not line.startswith("Explanation"):
|
36 |
-
explanation += line + " "
|
37 |
-
|
38 |
-
if current_speaker and explanation:
|
39 |
-
speakers_data[current_speaker]["explanation"] = explanation.strip()
|
40 |
-
|
41 |
-
return speakers_data
|
42 |
-
|
43 |
-
def create_bar_chart(data, title):
|
44 |
-
fig = go.Figure(data=[go.Bar(
|
45 |
-
x=list(data.keys()),
|
46 |
-
y=list(data.values()),
|
47 |
-
text=list(data.values()),
|
48 |
-
textposition='auto',
|
49 |
-
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)]
|
50 |
-
)])
|
51 |
-
fig.update_layout(title=title, xaxis_title="Traits", yaxis_title="Score")
|
52 |
-
fig.update_xaxes(tickangle=45)
|
53 |
-
return fig
|
54 |
-
|
55 |
-
def create_radar_chart(data, title):
|
56 |
-
values = [data.get('Avoidance', 0), data.get('Self', 0), data.get('Anxiety', 0), data.get('Others', 0)]
|
57 |
-
fig = go.Figure(data=go.Scatterpolar(
|
58 |
-
r=values,
|
59 |
-
theta=['Avoidance', 'Self', 'Anxiety', 'Others'],
|
60 |
-
fill='toself'
|
61 |
-
))
|
62 |
-
fig.update_layout(
|
63 |
-
polar=dict(
|
64 |
-
radialaxis=dict(visible=True, range=[0, max(values + [10])])
|
65 |
-
),
|
66 |
-
showlegend=False,
|
67 |
-
title=title
|
68 |
-
)
|
69 |
-
return fig
|
70 |
-
|
71 |
-
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities, original_tokens, attachments_tokens, bigfive_tokens, personalities_tokens):
|
72 |
outputs = [
|
73 |
gr.update(value=status, visible=True),
|
74 |
gr.update(value=exec_time, visible=True),
|
75 |
gr.update(value=lang, visible=True),
|
76 |
-
gr.update(value=f"Original tokens: {original_tokens}", visible=True),
|
77 |
-
gr.update(value=f"Attachments tokens: {attachments_tokens}", visible=True),
|
78 |
-
gr.update(value=f"Big Five tokens: {bigfive_tokens}", visible=True),
|
79 |
-
gr.update(value=f"Personalities tokens: {personalities_tokens}", visible=True),
|
80 |
]
|
81 |
|
82 |
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]:
|
@@ -86,7 +15,7 @@ def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive,
|
|
86 |
if analysis_type == "Attachments":
|
87 |
chart_data = {k: v for k, v in data.items() if k in ["Secured", "Anxious-Preoccupied", "Dismissive-Avoidant", "Fearful-Avoidant"] and isinstance(v, (int, float))}
|
88 |
if chart_data:
|
89 |
-
fig = create_bar_chart(chart_data, f"{analysis_type} - {speaker}")
|
90 |
outputs.append(gr.update(value=fig, visible=True))
|
91 |
else:
|
92 |
outputs.append(gr.update(visible=False))
|
@@ -100,7 +29,7 @@ def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive,
|
|
100 |
else:
|
101 |
chart_data = {k: v for k, v in data.items() if k not in ["explanation"] and isinstance(v, (int, float))}
|
102 |
if chart_data:
|
103 |
-
fig = create_bar_chart(chart_data, f"{analysis_type} - {speaker}")
|
104 |
outputs.append(gr.update(value=fig, visible=True))
|
105 |
else:
|
106 |
outputs.append(gr.update(visible=False))
|
@@ -111,8 +40,75 @@ def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive,
|
|
111 |
else:
|
112 |
outputs.extend([gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)])
|
113 |
|
114 |
-
# Ensure we always return exactly
|
115 |
-
while len(outputs) <
|
116 |
outputs.append(gr.update(visible=False))
|
117 |
|
118 |
-
return outputs[:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import plotly.graph_objs as go
|
3 |
|
4 |
+
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
outputs = [
|
6 |
gr.update(value=status, visible=True),
|
7 |
gr.update(value=exec_time, visible=True),
|
8 |
gr.update(value=lang, visible=True),
|
|
|
|
|
|
|
|
|
9 |
]
|
10 |
|
11 |
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]:
|
|
|
15 |
if analysis_type == "Attachments":
|
16 |
chart_data = {k: v for k, v in data.items() if k in ["Secured", "Anxious-Preoccupied", "Dismissive-Avoidant", "Fearful-Avoidant"] and isinstance(v, (int, float))}
|
17 |
if chart_data:
|
18 |
+
fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
|
19 |
outputs.append(gr.update(value=fig, visible=True))
|
20 |
else:
|
21 |
outputs.append(gr.update(visible=False))
|
|
|
29 |
else:
|
30 |
chart_data = {k: v for k, v in data.items() if k not in ["explanation"] and isinstance(v, (int, float))}
|
31 |
if chart_data:
|
32 |
+
fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
|
33 |
outputs.append(gr.update(value=fig, visible=True))
|
34 |
else:
|
35 |
outputs.append(gr.update(visible=False))
|
|
|
40 |
else:
|
41 |
outputs.extend([gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)])
|
42 |
|
43 |
+
# Ensure we always return exactly 21 outputs
|
44 |
+
while len(outputs) < 21:
|
45 |
outputs.append(gr.update(visible=False))
|
46 |
|
47 |
+
return outputs[:21] # Trim to exactly 21 outputs
|
48 |
+
|
49 |
+
def create_bar_chart(data, title):
|
50 |
+
if not data:
|
51 |
+
return None
|
52 |
+
fig = go.Figure(data=[go.Bar(
|
53 |
+
x=list(data.keys()),
|
54 |
+
y=list(data.values()),
|
55 |
+
text=list(data.values()),
|
56 |
+
textposition='auto',
|
57 |
+
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)]
|
58 |
+
)])
|
59 |
+
fig.update_layout(title=title, xaxis_title="Traits", yaxis_title="Score")
|
60 |
+
fig.update_xaxes(tickangle=45)
|
61 |
+
return fig
|
62 |
+
|
63 |
+
def create_radar_chart(data, title):
|
64 |
+
if not data:
|
65 |
+
return None
|
66 |
+
values = [data.get('Avoidance', 0), data.get('Self', 0), data.get('Anxiety', 0), data.get('Others', 0)]
|
67 |
+
fig = go.Figure(data=go.Scatterpolar(
|
68 |
+
r=values,
|
69 |
+
theta=['Avoidance', 'Self', 'Anxiety', 'Others'],
|
70 |
+
fill='toself'
|
71 |
+
))
|
72 |
+
fig.update_layout(
|
73 |
+
polar=dict(
|
74 |
+
radialaxis=dict(visible=True, range=[0, max(values + [10])])
|
75 |
+
),
|
76 |
+
showlegend=False,
|
77 |
+
title=title
|
78 |
+
)
|
79 |
+
return fig
|
80 |
+
|
81 |
+
# Add this function to extract data from the text
|
82 |
+
def extract_data_and_explanation(text):
|
83 |
+
speakers_data = {}
|
84 |
+
current_speaker = None
|
85 |
+
explanation = ""
|
86 |
+
for line in text.split('\n'):
|
87 |
+
line = line.strip()
|
88 |
+
if line.startswith("-----------------------"):
|
89 |
+
if current_speaker and explanation:
|
90 |
+
speakers_data[current_speaker]["explanation"] = explanation.strip()
|
91 |
+
explanation = ""
|
92 |
+
current_speaker = None
|
93 |
+
continue
|
94 |
+
if line.startswith("Speaker"):
|
95 |
+
current_speaker = line.strip()
|
96 |
+
speakers_data[current_speaker] = {}
|
97 |
+
elif ':' in line and current_speaker:
|
98 |
+
key, value = line.split(':', 1)
|
99 |
+
key = key.strip()
|
100 |
+
value = value.strip()
|
101 |
+
if key.lower() == "explanation":
|
102 |
+
explanation += value + " "
|
103 |
+
else:
|
104 |
+
try:
|
105 |
+
speakers_data[current_speaker][key] = float(value)
|
106 |
+
except ValueError:
|
107 |
+
speakers_data[current_speaker][key] = value
|
108 |
+
elif line and current_speaker and not line.startswith("Explanation"):
|
109 |
+
explanation += line + " "
|
110 |
+
|
111 |
+
if current_speaker and explanation:
|
112 |
+
speakers_data[current_speaker]["explanation"] = explanation.strip()
|
113 |
+
|
114 |
+
return speakers_data
|