File size: 5,049 Bytes
50eb966
 
 
9cec98e
2114678
 
 
 
 
 
 
 
 
 
 
42a5dad
2114678
9cec98e
37fe11b
 
 
 
 
 
 
 
 
 
 
 
 
9cec98e
37fe11b
 
 
 
 
 
 
 
 
 
9cec98e
 
37fe11b
 
9cec98e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
import plotly.graph_objs as go

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 = extract_data_and_explanation(analysis_text)
        for speaker, data in speakers_data.items():
            if data:
                if analysis_type == "Attachments":
                    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))}
                    if chart_data:
                        fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
                        outputs.append(gr.update(value=fig, visible=True))
                    else:
                        outputs.append(gr.update(visible=False))
                    
                    radar_data = {k: v for k, v in data.items() if k in ["Anxiety", "Avoidance", "Self", "Others"] and isinstance(v, (int, float))}
                    if radar_data:
                        radar_fig = create_radar_chart(radar_data, f"Anxiety-Avoidance-Self-Others - {speaker}")
                        outputs.append(gr.update(value=radar_fig, visible=True))
                    else:
                        outputs.append(gr.update(visible=False))
                else:
                    chart_data = {k: v for k, v in data.items() if k not in ["explanation"] and isinstance(v, (int, float))}
                    if chart_data:
                        fig = create_bar_chart(chart_data, f"{analysis_type} Analysis - {speaker}")
                        outputs.append(gr.update(value=fig, visible=True))
                    else:
                        outputs.append(gr.update(visible=False))
                    outputs.append(gr.update(visible=False))  # Placeholder for consistency
                
                explanation = data.get("explanation", "No explanation provided.")
                outputs.append(gr.update(value=explanation, visible=True, label=f"{analysis_type} Explanation - {speaker}"))
            else:
                outputs.extend([gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)])
    
    # Ensure we always return exactly 21 outputs
    while len(outputs) < 21:
        outputs.append(gr.update(visible=False))
    
    return outputs[:21]  # Trim to exactly 21 outputs

def create_bar_chart(data, title):
    if not data:
        return None
    fig = go.Figure(data=[go.Bar(
        x=list(data.keys()), 
        y=list(data.values()),
        text=list(data.values()),
        textposition='auto',
        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")
    fig.update_xaxes(tickangle=45)
    return fig

def create_radar_chart(data, title):
    if not data:
        return None
    values = [data.get('Avoidance', 0), data.get('Self', 0), data.get('Anxiety', 0), data.get('Others', 0)]
    fig = go.Figure(data=go.Scatterpolar(
        r=values,
        theta=['Avoidance', 'Self', 'Anxiety', 'Others'],
        fill='toself'
    ))
    fig.update_layout(
        polar=dict(
            radialaxis=dict(visible=True, range=[0, max(values + [10])])
        ),
        showlegend=False,
        title=title
    )
    return fig

# Add this function to extract data from the text
def extract_data_and_explanation(text):
    speakers_data = {}
    current_speaker = None
    explanation = ""
    for line in text.split('\n'):
        line = line.strip()
        if line.startswith("-----------------------"):
            if current_speaker and explanation:
                speakers_data[current_speaker]["explanation"] = explanation.strip()
            explanation = ""
            current_speaker = None
            continue
        if line.startswith("Speaker"):
            current_speaker = line.strip()
            speakers_data[current_speaker] = {}
        elif ':' in line and current_speaker:
            key, value = line.split(':', 1)
            key = key.strip()
            value = value.strip()
            if key.lower() == "explanation":
                explanation += value + " "
            else:
                try:
                    speakers_data[current_speaker][key] = float(value)
                except ValueError:
                    speakers_data[current_speaker][key] = value
        elif line and current_speaker and not line.startswith("Explanation"):
            explanation += line + " "
    
    if current_speaker and explanation:
        speakers_data[current_speaker]["explanation"] = explanation.strip()

    return speakers_data