Spaces:
Runtime error
Runtime error
Update visualization.py
Browse files- visualization.py +16 -42
visualization.py
CHANGED
@@ -1,35 +1,25 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import re
|
4 |
-
from collections import Counter
|
5 |
-
|
6 |
-
def extract_speaker_data(text):
|
7 |
-
speakers = {}
|
8 |
-
current_speaker = None
|
9 |
explanation = ""
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
elif current_speaker and ":" in line:
|
15 |
-
key, value = line.split(":", 1)
|
16 |
try:
|
17 |
-
|
18 |
except ValueError:
|
19 |
-
|
20 |
-
if match:
|
21 |
-
speakers[current_speaker][key.strip()] = float(match.group())
|
22 |
elif line.lower().startswith("explanation:"):
|
23 |
explanation = line.split("Explanation:", 1)[1].strip()
|
24 |
-
return
|
25 |
|
26 |
-
def create_bar_chart(data, title
|
27 |
fig = go.Figure(data=[go.Bar(
|
28 |
x=list(data.keys()),
|
29 |
y=list(data.values()),
|
30 |
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)]
|
31 |
)])
|
32 |
-
fig.update_layout(title=
|
33 |
return fig
|
34 |
|
35 |
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities):
|
@@ -40,27 +30,11 @@ def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive,
|
|
40 |
]
|
41 |
|
42 |
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]:
|
43 |
-
|
44 |
-
if
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
speaker_counts = Counter(speakers_data.keys())
|
49 |
-
main_speakers = [speaker for speaker, count in speaker_counts.most_common(2)]
|
50 |
-
|
51 |
-
for speaker in main_speakers:
|
52 |
-
data = speakers_data.get(speaker, {})
|
53 |
-
if analysis_type == "Attachments":
|
54 |
-
chart_data = {k: v for k, v in data.items() if k in ["Secured", "Anxious-Preoccupied", "Dismissive-Avoidant", "Fearful-Avoidant"]}
|
55 |
-
elif analysis_type == "Big Five":
|
56 |
-
chart_data = {k: v for k, v in data.items() if k in ["Extraversion", "Agreeableness", "Conscientiousness", "Neuroticism", "Openness"]}
|
57 |
-
elif analysis_type == "Personalities":
|
58 |
-
chart_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"]}
|
59 |
-
|
60 |
-
if chart_data:
|
61 |
-
fig = create_bar_chart(chart_data, f"{analysis_type} Styles", speaker)
|
62 |
-
outputs.append(gr.Plot(value=fig))
|
63 |
-
outputs.append(gr.Textbox(value=explanation, label=f"{analysis_type} Explanation - Speaker {speaker}"))
|
64 |
|
65 |
print("Outputs generated:", outputs)
|
66 |
return outputs
|
|
|
1 |
+
def extract_data_and_explanation(text):
|
2 |
+
data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
explanation = ""
|
4 |
+
lines = text.split('\n')
|
5 |
+
for line in lines:
|
6 |
+
if ':' in line:
|
7 |
+
key, value = line.split(':', 1)
|
|
|
|
|
8 |
try:
|
9 |
+
data[key.strip()] = float(value.strip())
|
10 |
except ValueError:
|
11 |
+
pass
|
|
|
|
|
12 |
elif line.lower().startswith("explanation:"):
|
13 |
explanation = line.split("Explanation:", 1)[1].strip()
|
14 |
+
return data, explanation
|
15 |
|
16 |
+
def create_bar_chart(data, title):
|
17 |
fig = go.Figure(data=[go.Bar(
|
18 |
x=list(data.keys()),
|
19 |
y=list(data.values()),
|
20 |
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)]
|
21 |
)])
|
22 |
+
fig.update_layout(title=title, xaxis_title="Traits", yaxis_title="Score")
|
23 |
return fig
|
24 |
|
25 |
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities):
|
|
|
30 |
]
|
31 |
|
32 |
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]:
|
33 |
+
data, explanation = extract_data_and_explanation(analysis_text)
|
34 |
+
if data:
|
35 |
+
fig = create_bar_chart(data, f"{analysis_type} Analysis")
|
36 |
+
outputs.append(gr.Plot(value=fig))
|
37 |
+
outputs.append(gr.Textbox(value=explanation, label=f"{analysis_type} Explanation"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
print("Outputs generated:", outputs)
|
40 |
return outputs
|