Spaces:
Runtime error
Runtime error
Update visualization.py
Browse files- visualization.py +72 -2
visualization.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import plotly.graph_objs as go
|
3 |
-
import re
|
4 |
|
5 |
def extract_data_and_explanation(text):
|
6 |
speakers_data = {}
|
@@ -49,4 +48,75 @@ def extract_data_and_explanation(text):
|
|
49 |
if current_speaker and explanation:
|
50 |
speakers_data[current_speaker]["explanation"] = explanation
|
51 |
|
52 |
-
return speakers_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import plotly.graph_objs as go
|
|
|
3 |
|
4 |
def extract_data_and_explanation(text):
|
5 |
speakers_data = {}
|
|
|
48 |
if current_speaker and explanation:
|
49 |
speakers_data[current_speaker]["explanation"] = explanation
|
50 |
|
51 |
+
return speakers_data
|
52 |
+
|
53 |
+
def create_bar_chart(data, title):
|
54 |
+
fig = go.Figure(data=[go.Bar(
|
55 |
+
x=list(data.keys()),
|
56 |
+
y=list(data.values()),
|
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 |
+
return fig
|
61 |
+
|
62 |
+
def create_radar_chart(data, title):
|
63 |
+
values = [data.get('Self', 0), data.get('Avoidance', 0), data.get('Others', 0), data.get('Anxiety', 0)]
|
64 |
+
fig = go.Figure(data=go.Scatterpolar(
|
65 |
+
r=values,
|
66 |
+
theta=['Self', 'Avoidance', 'Others', 'Anxiety'],
|
67 |
+
fill='toself'
|
68 |
+
))
|
69 |
+
fig.update_layout(
|
70 |
+
polar=dict(
|
71 |
+
radialaxis=dict(visible=True, range=[0, max(values + [10])])
|
72 |
+
),
|
73 |
+
showlegend=False,
|
74 |
+
title=title
|
75 |
+
)
|
76 |
+
return fig
|
77 |
+
|
78 |
+
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities):
|
79 |
+
outputs = [
|
80 |
+
gr.update(value=status, visible=True),
|
81 |
+
gr.update(value=exec_time, visible=True),
|
82 |
+
gr.update(value=lang, visible=True),
|
83 |
+
]
|
84 |
+
|
85 |
+
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]:
|
86 |
+
speakers_data = extract_data_and_explanation(analysis_text)
|
87 |
+
for speaker, data in speakers_data.items():
|
88 |
+
if data:
|
89 |
+
if analysis_type == "Attachments":
|
90 |
+
chart_data = {k: v for k, v in data.items() if k in ["Secured", "Anxious-Preoccupied", "Dismissive-Avoidant", "Fearful-Avoidant"]}
|
91 |
+
if chart_data:
|
92 |
+
fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
|
93 |
+
outputs.append(gr.update(value=fig, visible=True))
|
94 |
+
else:
|
95 |
+
outputs.append(gr.update(visible=False))
|
96 |
+
|
97 |
+
radar_data = {k: v for k, v in data.items() if k in ["Anxiety", "Avoidance", "Self", "Others"]}
|
98 |
+
if any(radar_data.values()): # Only create radar chart if there's data
|
99 |
+
radar_fig = create_radar_chart(radar_data, f"Anxiety-Avoidance-Self-Others - {speaker}")
|
100 |
+
outputs.append(gr.update(value=radar_fig, visible=True))
|
101 |
+
else:
|
102 |
+
outputs.append(gr.update(visible=False))
|
103 |
+
else:
|
104 |
+
chart_data = {k: v for k, v in data.items() if k not in ["explanation"] and isinstance(v, (int, float))}
|
105 |
+
if chart_data:
|
106 |
+
fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
|
107 |
+
outputs.append(gr.update(value=fig, visible=True))
|
108 |
+
else:
|
109 |
+
outputs.append(gr.update(visible=False))
|
110 |
+
outputs.append(gr.update(visible=False)) # Placeholder for consistency
|
111 |
+
|
112 |
+
explanation = data.get("explanation", "No explanation provided.")
|
113 |
+
outputs.append(gr.update(value=explanation, visible=True, label=f"{analysis_type} Explanation - {speaker}"))
|
114 |
+
else:
|
115 |
+
outputs.extend([gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)])
|
116 |
+
|
117 |
+
# Pad the outputs to ensure we always return the same number of outputs
|
118 |
+
while len(outputs) < 21: # 3 initial outputs + 6 plots + 6 radar charts + 6 explanations
|
119 |
+
outputs.append(gr.update(visible=False))
|
120 |
+
|
121 |
+
print("Outputs generated:", outputs)
|
122 |
+
return outputs
|