Spaces:
Runtime error
Runtime error
import gradio as gr | |
import plotly.graph_objs as go | |
import re | |
import json | |
def extract_data_and_explanation(text): | |
speakers_data = {} | |
current_speaker = None | |
explanation = "" | |
for line in text.split('\n'): | |
if line.startswith("Speaker"): | |
current_speaker = line.split(':')[0].strip() | |
speakers_data[current_speaker] = {} | |
elif ':' in line and current_speaker: | |
key, value = line.split(':', 1) | |
try: | |
speakers_data[current_speaker][key.strip()] = float(value.strip()) | |
except ValueError: | |
pass | |
elif line.lower().startswith("explanation:"): | |
explanation = line.split("Explanation:", 1)[1].strip() | |
return speakers_data, explanation | |
def create_bar_chart(data, title): | |
fig = go.Figure(data=[go.Bar( | |
x=list(data.keys()), | |
y=list(data.values()), | |
marker_color=['red', 'green', 'blue', 'yellow', 'purple', 'orange', 'pink', 'cyan', 'magenta', 'brown'][:len(data)] | |
)]) | |
fig.update_layout(title=title, xaxis_title="Traits", yaxis_title="Score") | |
return fig | |
def update_visibility_and_charts(status, exec_time, lang, attachments, bigfive, personalities): | |
outputs = [ | |
gr.update(value=status, visible=True), | |
gr.update(value=exec_time, visible=True), | |
gr.update(value=lang, visible=True), | |
] | |
for analysis_text, analysis_type in [(attachments, "Attachments"), (bigfive, "Big Five"), (personalities, "Personalities")]: | |
speakers_data, explanation = extract_data_and_explanation(analysis_text) | |
for speaker, data in speakers_data.items(): | |
if data: | |
fig = create_bar_chart(data, f"{analysis_type} Analysis - {speaker}") | |
outputs.append(gr.update(value=fig, visible=True)) | |
outputs.append(gr.update(value=explanation, visible=True, label=f"{analysis_type} Explanation - {speaker}")) | |
else: | |
outputs.append(gr.update(visible=False)) | |
outputs.append(gr.update(visible=False)) | |
# Pad the outputs to ensure we always return the same number of outputs | |
while len(outputs) < 15: # 3 initial outputs + 6 plots + 6 explanations | |
outputs.append(gr.update(visible=False)) | |
print("Outputs generated:", outputs) | |
return outputs |