File size: 5,366 Bytes
50eb966
 
 
ec471fc
69443bf
 
2017ddc
69443bf
e1775fa
 
f50e42e
 
250543a
 
 
69443bf
 
 
ec471fc
f50e42e
 
250543a
 
 
aebcc3f
2e64057
 
 
 
250543a
e1775fa
 
 
250543a
e1775fa
 
 
 
f50e42e
 
 
 
2e64057
e1775fa
 
f50e42e
 
 
250543a
2114678
 
 
 
 
 
 
 
 
 
 
 
250543a
2114678
 
250543a
2114678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250543a
2114678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250543a
2114678
 
 
 
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
115
116
117
118
119
120
import gradio as gr
import plotly.graph_objs as go

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
            explanation = ""
            continue
        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)
            key = key.strip()
            value = value.strip()
            if key.lower() == "explanation":
                explanation = value
            elif '|' in value:
                for part in value.split('|'):
                    part = part.strip()
                    if ':' in part:
                        sub_key, sub_value = part.split(':', 1)
                    else:
                        parts = part.split(None, 1)
                        if len(parts) == 2:
                            sub_key, sub_value = parts
                        else:
                            continue
                    try:
                        speakers_data[current_speaker][sub_key.strip()] = float(sub_value.strip())
                    except ValueError:
                        speakers_data[current_speaker][sub_key.strip()] = sub_value.strip()
            else:
                try:
                    speakers_data[current_speaker][key] = float(value)
                except ValueError:
                    speakers_data[current_speaker][key] = value
        elif line and current_speaker:
            speakers_data[current_speaker][line] = 0
    
    if current_speaker and explanation:
        speakers_data[current_speaker]["explanation"] = explanation

    return speakers_data

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 create_radar_chart(data, title):
    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

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"]}
                    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"]}
                    if any(radar_data.values()):
                        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)])
    
    while len(outputs) < 21:
        outputs.append(gr.update(visible=False))
    
    print("Outputs generated:", outputs)
    return outputs