File size: 5,286 Bytes
b8a7acb
 
 
 
8505717
 
b8a7acb
 
 
 
 
 
a3950aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b3086f
08e85df
 
 
31ecbda
 
08e85df
31ecbda
 
 
 
 
 
 
 
 
 
 
 
08e85df
844115d
2078afe
ed43bfe
6419ee7
7b3086f
710aa69
 
7925424
7b3086f
08e85df
7b3086f
ae931c0
 
08e85df
 
a655d29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646f3d2
ae931c0
646f3d2
a655d29
 
 
 
 
646f3d2
 
bd18cbf
466cd11
6132824
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
from llm_loader import load_model
from processing import process_input
from transcription_diarization import diarize_audio
from visualization import create_charts
import time
from config import openai_api_key

# Load the model
llm = load_model(openai_api_key)

def analyze_video(video_path, max_speakers, progress=gr.Progress()):
    start_time = time.time()
    if not video_path:
        return {"error": "Please upload a video file."}

    progress(0, desc="Starting analysis...")
    progress(0.2, desc="Starting transcription and diarization")
    transcription = diarize_audio(video_path, max_speakers)
    print("Transcription:", transcription)  # Debug print
    progress(0.5, desc="Transcription and diarization complete.")

    progress(0.6, desc="Processing transcription")
    results = process_input(transcription, llm)
    progress(0.7, desc="Transcription processing complete.")

    progress(0.9, desc="Generating charts")
    charts, explanations = create_charts(results)
    progress(1.0, desc="Charts generation complete.")

    end_time = time.time()
    execution_time = end_time - start_time

    return {
        "transcript": transcription,
        "charts": charts,
        "explanations": explanations,
        "execution_time": int(execution_time)
    }

def create_output_components():
    with gr.Row() as row:
        with gr.Column():
            transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
            
            tabs = gr.Tabs()
            for i in range(3):  # Pre-create 3 tabs (max number of speakers)
                with gr.Tab(f"Speaker {i+1}", visible=False) as tab:
                    gr.Markdown(f"## Speaker {i+1}", visible=False)
                    gr.Plot(label="Attachment", visible=False)
                    gr.Textbox(label="Attachment Styles Explanation", visible=False)
                    gr.Plot(label="Dimensions", visible=False)
                    gr.Plot(label="Big Five", visible=False)
                    gr.Textbox(label="Big Five Traits Explanation", visible=False)
                    gr.Plot(label="Personality", visible=False)
                    gr.Textbox(label="Personality Disorders Explanation", visible=False)
            
            execution_info = gr.Textbox(label="Execution Information", visible=False)
    return row, transcript, tabs, execution_info

with gr.Blocks() as iface:
    gr.Markdown("# AI Personality Detection")
    gr.Markdown("Upload a video")

    video_input = gr.Video(label="Upload Video")
    max_speakers = gr.Slider(minimum=1, maximum=3, step=1, value=2, label="Maximum Number of Speakers")
    analyze_button = gr.Button("Analyze")

    output_row, transcript_output, tabs_output, execution_info_output = create_output_components()

    def run_analysis(video_path, max_speakers):
        results = analyze_video(video_path, max_speakers)
        
        if "error" in results:
            return [
                "",  # transcript
                gr.Tabs(),  # tabs
                results["error"],  # execution_info
            ] + [gr.update(visible=False)] * 24  # 8 components per tab * 3 tabs
    
        transcript = results["transcript"]
        execution_info = f"Completed in {results['execution_time']} seconds."
    
        tab_updates = []
        for i in range(3):  # For each potential speaker
            if i < len(results["charts"]):
                speaker_id = list(results["charts"].keys())[i]
                speaker_charts = results["charts"][speaker_id]
                speaker_explanations = results["explanations"][speaker_id]
                
                tab_updates.extend([
                    gr.update(visible=True, label=speaker_id),  # Tab visibility and label
                    gr.update(value=f"## {speaker_id}", visible=True),  # Markdown
                    gr.update(value=speaker_charts.get("attachment", None), visible=True),  # Attachment plot
                    gr.update(value=speaker_explanations.get("attachment", ""), visible=True),  # Attachment explanation
                    gr.update(value=speaker_charts.get("dimensions", None), visible=True),  # Dimensions plot
                    gr.update(value=speaker_charts.get("bigfive", None), visible=True),  # Big Five plot
                    gr.update(value=speaker_explanations.get("bigfive", ""), visible=True),  # Big Five explanation
                    gr.update(value=speaker_charts.get("personality", None), visible=True),  # Personality plot
                    gr.update(value=speaker_explanations.get("personality", ""), visible=True),  # Personality explanation
                ])
            else:
                tab_updates.extend([gr.update(visible=False)] * 9)  # Hide unused tab and its components
    
        return [transcript, gr.Tabs.update(selected=0), execution_info] + tab_updates
    
    # Modify the analyze_button.click setup
    analyze_button.click(
        fn=run_analysis,
        inputs=[video_input, max_speakers],
        outputs=[
            transcript_output,
            tabs_output,
            execution_info_output
        ] + [component for tab in tabs_output.children for component in tab.children],
        show_progress=True
    )

if __name__ == "__main__":
    iface.launch()