File size: 8,745 Bytes
aa52433
b8a7acb
 
 
 
a26ac7e
8505717
1e9e06f
cd46f8b
1e9e06f
b8a7acb
 
 
 
 
aa52433
3670697
c114fac
 
1e9e06f
c114fac
 
 
3670697
c114fac
 
 
 
4855721
c114fac
 
75ea1cc
445cb9a
c114fac
aa52433
c114fac
 
 
aa52433
 
ac6aba3
1452771
aa52433
1e9e06f
aa52433
1e9e06f
 
41754b5
01e963a
1e9e06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa52433
1e9e06f
 
 
 
 
 
aa52433
 
6d1f344
875db1b
e77657a
aa52433
67c78b1
1e7c569
aa52433
 
67c78b1
 
aa52433
 
67c78b1
 
1e7c569
aa52433
cd46f8b
 
 
f300f75
cd46f8b
 
 
 
 
 
 
 
6d1f344
aa52433
2078afe
28c881a
cb96df1
 
 
 
6d1f344
336fe2c
5daec0b
aa52433
5daec0b
aa52433
6062e5c
 
1e9e06f
ac6aba3
 
0e007bd
a14dda6
1b7ed19
aa52433
b99be8d
 
aa52433
edf218a
a32b89e
edf218a
 
 
24f04a1
edf218a
 
 
 
 
24f04a1
edf218a
 
 
 
24f04a1
edf218a
 
 
 
24f04a1
edf218a
 
 
 
 
 
a32b89e
 
 
 
 
 
 
 
aa52433
a32b89e
 
 
 
 
 
aa52433
a32b89e
 
 
 
 
 
aa52433
 
a32b89e
 
47f05e5
 
a32b89e
336fe2c
2f5d69d
6bbaa62
f6b2216
336fe2c
 
b02a7f6
3ad43ca
 
 
 
1e9e06f
 
 
 
 
8053e2c
3ad43ca
 
 
1e9e06f
 
 
 
 
8053e2c
3ad43ca
e541f17
 
1e9e06f
 
 
 
 
e541f17
466cd11
aa52433
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

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
import re
import cv2
import os
from config import openai_api_key

# Load the model
llm = load_model(openai_api_key)


def analyze_video(video_path, progress=gr.Progress()):
    start_time = time.time()
    if not video_path:
        return [None] * 29  # Return None for all outputs

    progress(0, desc="Starting analysis...")
    progress(0.2, desc="Starting transcription and diarization")
    transcription = diarize_audio(video_path)
    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, general_impressions = create_charts(results)

    progress(1.0, desc="Charts generation complete.")

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

    output_components = []  # transcript

    output_components.append(f"Completed in {int(execution_time)} seconds.")
    output_components.append(gr.Textbox(value=transcription, label="Transcript", lines=10, visible=True))

    for i, (speaker_id, speaker_charts) in enumerate(charts.items(), start=1):
        print(speaker_id)
        speaker_explanations = explanations[speaker_id]
        speaker_general_impression = general_impressions[speaker_id]

        with gr.Tab(visible=True):
            with gr.TabItem(label=f'General Impression'):
                speaker_section1 = [
                    gr.Markdown(f"### {speaker_id}", visible=True),
                    gr.Textbox(value=speaker_general_impression, label="General Impression", visible=True, lines=10)
                ]

            with gr.TabItem(label=f'Attachment Styles'):
                speaker_section2 = [
                    gr.Plot(value=speaker_charts.get("attachment", None), visible=True),
                    gr.Plot(value=speaker_charts.get("dimensions", None), visible=True),
                    gr.Textbox(value=speaker_explanations.get("attachment", ""), label="Attachment Styles Explanation",
                               visible=True, lines=2)
                ]

            with gr.TabItem(label=f'Big Five Traits'):
                speaker_section3 = [
                    gr.Plot(value=speaker_charts.get("bigfive", None), visible=True),
                    gr.Textbox(value=speaker_explanations.get("bigfive", ""), label="Big Five Traits Explanation",
                               visible=True, lines=2)
                ]

            with gr.TabItem(label=f'Personalities'):
                speaker_section4 = [
                    gr.Plot(value=speaker_charts.get("personality", None), visible=True),
                    gr.Textbox(value=speaker_explanations.get("personality", ""),
                               label="Personality Disorders Explanation", visible=True, lines=2)
                ]

        output_components.extend(speaker_section1)
        output_components.extend(speaker_section2)
        output_components.extend(speaker_section3)
        output_components.extend(speaker_section4)

    # Pad with None for any missing speakers
    while len(output_components) < 28:
        output_components.extend([gr.update(visible=False)] * 9)

    return output_components


def use_example_1():
    return "examples/Scenes.From.A.Marriage.US.mp4"


def use_example_2():
    return "examples/Billie Eilish.mp4"


def use_example_3():
    return "examples/Elliot Rodger.mp4"


def get_middle_frame(video_path):
    cap = cv2.VideoCapture(video_path)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    middle_frame_number = total_frames // 7
    cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_number)
    ret, frame = cap.read()
    cap.release()
    if ret:
        preview_path = video_path.rsplit('.', 1)[0] + '_preview.jpg'
        cv2.imwrite(preview_path, frame)
        return preview_path
    return None


with gr.Blocks() as iface:
    gr.Markdown("# Multiple Speakers Personality Analyzer")
    gr.Markdown("This project provides an advanced AI system designed for diagnosing and profiling personality attributes from video content based on a single speaker or multiple speakers in a conversation.")
    gr.Markdown("""
    1. Select input video 
    2. Click Analyze""")

    with gr.Row():
        video_input = gr.Video(label="Upload Video")

    analyze_button = gr.Button("Analyze")

    # Create output components
    output_components = []
    # Add transcript output near the top
    execution_box = gr.Textbox(label="Execution Info", value="N/A", lines=1)
    output_components.append(execution_box)

    transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
    output_components.append(transcript)

    with open('description.txt', 'r') as file:
        description_txt = file.read()

    for n in range(3):  # Assuming maximum of 3 speakers
        with gr.Tab(label=f'Speaker {n + 1}', visible=True):
            with gr.TabItem(label=f'General Impression'):
                column_components1 = [
                    gr.Markdown(visible=False),
                    gr.Textbox(label="General Impression")]

            with gr.TabItem(label=f'Attachment Styles'):
                column_components2 = [
                    gr.Plot(visible=False),
                    gr.Plot(visible=False),
                    gr.Textbox(label="Attachment Styles Explanation")]

            with gr.TabItem(label=f'Big Five Traits'):
                column_components3 = [
                    gr.Plot(visible=False),
                    gr.Textbox(label="Big Five Traits Explanation")]

            with gr.TabItem(label=f'Personalities'):
                column_components4 = [
                    gr.Plot(visible=False),
                    gr.Textbox(label="Personality Disorders Explanation")]

        output_components.extend(column_components1)
        output_components.extend(column_components2)
        output_components.extend(column_components3)
        output_components.extend(column_components4)

    gr.Markdown("### Example Videos")
    with gr.Row():
        with gr.Column(scale=1):
            example_video_1_path = "examples/Scenes.From.A.Marriage.US.mp4"
            preview_1 = get_middle_frame(example_video_1_path)
            gr.Image(preview_1, label="Scenes From A Marriage")
            example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
            use_example_button_1 = gr.Button("Load Example 1")

        with gr.Column(scale=1):
            example_video_2_path = "examples/Billie Eilish.mp4"
            preview_2 = get_middle_frame(example_video_2_path)
            gr.Image(preview_2, label="Billie Eilish")
            example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
            use_example_button_2 = gr.Button("Load Example 2")

        with gr.Column(scale=1):
            example_video_3_path = "examples/Elliot Rodger.mp4"
            preview_3 = get_middle_frame(example_video_3_path)
            gr.Image(preview_3, label="Elliot Rodger")
            example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
            use_example_button_3 = gr.Button("Load Example 3")

    gr.HTML("<div style='height: 20px;'></div>")
    gr.Markdown(description_txt)
    gr.HTML("<div style='height: 20px;'></div>")
    gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=900)
    gr.Image(value="appendix/AI Personality Detection flow - 2.png", label='Flowchart 2', width=900)

    analyze_button.click(
        fn=analyze_video,
        inputs=[video_input],
        outputs=output_components,
        show_progress=True
    )

    use_example_button_1.click(
        fn=use_example_1,
        inputs=[],
        outputs=[video_input],
    ).then(fn=analyze_video,
           inputs=[video_input],
           outputs=output_components,
           show_progress=True
           )
    use_example_button_2.click(
        fn=use_example_2,
        inputs=[],
        outputs=[video_input],
    ).then(fn=analyze_video,
           inputs=[video_input],
           outputs=output_components,
           show_progress=True
           )
    use_example_button_3.click(
        fn=use_example_3,
        inputs=[],
        outputs=[video_input],
    ).then(fn=analyze_video,
           inputs=[video_input],
           outputs=output_components,
           show_progress=True
           )

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