reab5555 commited on
Commit
1e9e06f
·
verified ·
1 Parent(s): e9bc219

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -100
app.py CHANGED
@@ -4,7 +4,9 @@ from processing import process_input
4
  from transcription_diarization import diarize_audio
5
  from visualization import create_charts
6
  import time
 
7
  import cv2
 
8
  from config import openai_api_key
9
 
10
  # Load the model
@@ -13,7 +15,7 @@ llm = load_model(openai_api_key)
13
  def analyze_video(video_path, progress=gr.Progress()):
14
  start_time = time.time()
15
  if not video_path:
16
- return [None] * 32 # Return None for all outputs
17
 
18
  progress(0, desc="Starting analysis...")
19
  progress(0.2, desc="Starting transcription and diarization")
@@ -32,41 +34,91 @@ def analyze_video(video_path, progress=gr.Progress()):
32
  end_time = time.time()
33
  execution_time = end_time - start_time
34
 
35
- output_components = []
36
 
37
  output_components.append(f"Completed in {int(execution_time)} seconds.")
38
  output_components.append(gr.Textbox(value=transcription, label="Transcript", lines=10, visible=True))
39
 
40
- for i in range(3): # Always process 3 speakers, even if some are empty
41
- speaker_id = f"Speaker {i+1}"
42
- speaker_charts = charts.get(speaker_id, {})
43
- speaker_explanations = explanations.get(speaker_id, {})
44
- speaker_general_impression = general_impressions.get(speaker_id, "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- output_components.extend([
47
- gr.Markdown(f"### {speaker_id}", visible=True),
48
- gr.Textbox(value=speaker_general_impression, label="General Impression", visible=True),
49
- gr.Plot(value=speaker_charts.get("attachment", None), visible=True),
50
- gr.Plot(value=speaker_charts.get("dimensions", None), visible=True),
51
- gr.Textbox(value=speaker_explanations.get("attachment", ""), label="Attachment Styles Explanation", visible=True),
52
- gr.Plot(value=speaker_charts.get("bigfive", None), visible=True),
53
- gr.Textbox(value=speaker_explanations.get("bigfive", ""), label="Big Five Traits Explanation", visible=True),
54
- gr.Plot(value=speaker_charts.get("personality", None), visible=bool(speaker_charts)),
55
- gr.Textbox(value=speaker_explanations.get("personality", ""), label="Personality Disorders Explanation", visible=True)
56
- ])
57
-
58
- # Ensure we always return 32 components
59
- while len(output_components) < 32:
60
- output_components.append(None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  return output_components
63
 
 
64
  def use_example_1():
65
  return "examples/Scenes.From.A.Marriage.US.mp4"
66
-
67
  def use_example_2():
68
  return "examples/Billie Eilish.mp4"
69
-
70
  def use_example_3():
71
  return "examples/Elliot Rodger.mp4"
72
 
@@ -83,9 +135,6 @@ def get_middle_frame(video_path):
83
  return preview_path
84
  return None
85
 
86
- def clear_outputs():
87
- return [None] * 32
88
-
89
  with gr.Blocks() as iface:
90
  gr.Markdown("# Multiple-Speakers-Personality-Analyzer")
91
  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.")
@@ -97,48 +146,65 @@ with gr.Blocks() as iface:
97
 
98
  # Create output components
99
  output_components = []
 
100
  execution_box = gr.Textbox(label="Execution Info", value="N/A", lines=1)
101
  output_components.append(execution_box)
102
 
103
  transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
104
  output_components.append(transcript)
105
 
106
- for n in range(3): # Always create 3 speaker tabs
107
- with gr.Tab(label=f'Speaker {n + 1}', visible=True) as tab:
108
- gr.Markdown(visible=False)
109
- gr.Textbox(label="General Impression", visible=False)
110
- gr.Plot(visible=False)
111
- gr.Plot(visible=False)
112
- gr.Textbox(label="Attachment Styles Explanation", visible=False)
113
- gr.Plot(visible=False)
114
- gr.Textbox(label="Big Five Traits Explanation", visible=False)
115
- gr.Plot(visible=False)
116
- gr.Textbox(label="Personality Disorders Explanation", visible=False)
117
- output_components.extend([tab] + [component for component in tab.children])
118
-
119
-
120
- gr.Markdown("### Example Videos")
121
- with gr.Row():
122
- with gr.Column(scale=1):
123
- example_video_1_path = "examples/Scenes.From.A.Marriage.US.mp4"
124
- preview_1 = get_middle_frame(example_video_1_path)
125
- gr.Image(preview_1, label="Scenes From A Marriage")
126
- example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
127
- use_example_button_1 = gr.Button("Load Example 1")
128
-
129
- with gr.Column(scale=1):
130
- example_video_2_path = "examples/Billie Eilish.mp4"
131
- preview_2 = get_middle_frame(example_video_2_path)
132
- gr.Image(preview_2, label="Billie Eilish")
133
- example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
134
- use_example_button_2 = gr.Button("Load Example 2")
135
-
136
- with gr.Column(scale=1):
137
- example_video_3_path = "examples/Elliot Rodger.mp4"
138
- preview_3 = get_middle_frame(example_video_3_path)
139
- gr.Image(preview_3, label="Elliot Rodger")
140
- example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
141
- use_example_button_3 = gr.Button("Load Example 3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  with open('description.txt', 'r') as file:
144
  description_txt = file.read()
@@ -147,13 +213,8 @@ with gr.Blocks() as iface:
147
  gr.HTML("<div style='height: 20px;'></div>")
148
  gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=1000)
149
  gr.Image(value="appendix/AI Personality Detection flow - 2.png", label='Flowchart 2', width=1000)
150
-
151
 
152
  analyze_button.click(
153
- fn=clear_outputs,
154
- inputs=[],
155
- outputs=output_components
156
- ).then(
157
  fn=analyze_video,
158
  inputs=[video_input],
159
  outputs=output_components,
@@ -164,46 +225,29 @@ with gr.Blocks() as iface:
164
  fn=use_example_1,
165
  inputs=[],
166
  outputs=[video_input],
167
- ).then(
168
- fn=clear_outputs,
169
- inputs=[],
170
- outputs=output_components
171
- ).then(
172
- fn=analyze_video,
173
- inputs=[video_input],
174
- outputs=output_components,
175
- show_progress=True
176
- )
177
-
178
  use_example_button_2.click(
179
  fn=use_example_2,
180
  inputs=[],
181
  outputs=[video_input],
182
- ).then(
183
- fn=clear_outputs,
184
- inputs=[],
185
- outputs=output_components
186
- ).then(
187
- fn=analyze_video,
188
- inputs=[video_input],
189
- outputs=output_components,
190
- show_progress=True
191
- )
192
-
193
  use_example_button_3.click(
194
  fn=use_example_3,
195
  inputs=[],
196
  outputs=[video_input],
197
- ).then(
198
- fn=clear_outputs,
199
- inputs=[],
200
- outputs=output_components
201
- ).then(
202
- fn=analyze_video,
203
- inputs=[video_input],
204
- outputs=output_components,
205
- show_progress=True
206
- )
207
 
208
  if __name__ == "__main__":
209
  iface.launch()
 
4
  from transcription_diarization import diarize_audio
5
  from visualization import create_charts
6
  import time
7
+ import re
8
  import cv2
9
+ import os
10
  from config import openai_api_key
11
 
12
  # Load the model
 
15
  def analyze_video(video_path, progress=gr.Progress()):
16
  start_time = time.time()
17
  if not video_path:
18
+ return [None] * 29 # Return None for all outputs
19
 
20
  progress(0, desc="Starting analysis...")
21
  progress(0.2, desc="Starting transcription and diarization")
 
34
  end_time = time.time()
35
  execution_time = end_time - start_time
36
 
37
+ output_components = [] # transcript
38
 
39
  output_components.append(f"Completed in {int(execution_time)} seconds.")
40
  output_components.append(gr.Textbox(value=transcription, label="Transcript", lines=10, visible=True))
41
 
42
+ with gr.Tab(label=f'Examples', visible=False):
43
+ gr.Markdown("### Example Videos")
44
+ with gr.Row():
45
+ with gr.Column(scale=1):
46
+ example_video_1_path = "examples/Scenes.From.A.Marriage.US.mp4"
47
+ preview_1 = get_middle_frame(example_video_1_path)
48
+ gr.Image(preview_1, label="Scenes From A Marriage")
49
+ example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
50
+ use_example_button_1 = gr.Button("Load Example 1")
51
+
52
+ with gr.Column(scale=1):
53
+ example_video_2_path = "examples/Billie Eilish.mp4"
54
+ preview_2 = get_middle_frame(example_video_2_path)
55
+ gr.Image(preview_2, label="Billie Eilish")
56
+ example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
57
+ use_example_button_2 = gr.Button("Load Example 2")
58
+
59
+ with gr.Column(scale=1):
60
+ example_video_3_path = "examples/Elliot Rodger.mp4"
61
+ preview_3 = get_middle_frame(example_video_3_path)
62
+ gr.Image(preview_3, label="Elliot Rodger")
63
+ example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
64
+ use_example_button_3 = gr.Button("Load Example 3")
65
+
66
+ with gr.Tab(label=f'Description', visible=False):
67
+ gr.Markdown(description_txt)
68
+ gr.HTML("<div style='height: 20px;'></div>")
69
+ gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=1000)
70
+ gr.Image(value="appendix/AI Personality Detection flow - 2.png", label='Flowchart 2', width=1000)
71
+
72
+ for i, (speaker_id, speaker_charts) in enumerate(charts.items(), start=1):
73
+ print(speaker_id)
74
+ speaker_explanations = explanations[speaker_id]
75
+ speaker_general_impression = general_impressions[speaker_id]
76
 
77
+ with gr.Tab():
78
+ with gr.TabItem(label=f'General Impression'):
79
+ speaker_section1 = [
80
+ gr.Markdown(f"### {speaker_id}", visible=True),
81
+ gr.Textbox(value=speaker_general_impression, label="General Impression", visible=True, lines=10)
82
+ ]
83
+
84
+ with gr.TabItem(label=f'Attachment Styles'):
85
+ speaker_section2 = [
86
+ gr.Plot(value=speaker_charts.get("attachment", None), visible=True),
87
+ gr.Plot(value=speaker_charts.get("dimensions", None), visible=True),
88
+ gr.Textbox(value=speaker_explanations.get("attachment", ""), label="Attachment Styles Explanation",
89
+ visible=True, lines=2)
90
+ ]
91
+
92
+ with gr.TabItem(label=f'Big Five Traits'):
93
+ speaker_section3 = [
94
+ gr.Plot(value=speaker_charts.get("bigfive", None), visible=True),
95
+ gr.Textbox(value=speaker_explanations.get("bigfive", ""), label="Big Five Traits Explanation",
96
+ visible=True, lines=2)
97
+ ]
98
+
99
+ with gr.TabItem(label=f'Personalities'):
100
+ speaker_section4 = [
101
+ gr.Plot(value=speaker_charts.get("personality", None), visible=True),
102
+ gr.Textbox(value=speaker_explanations.get("personality", ""),
103
+ label="Personality Disorders Explanation", visible=True, lines=2)
104
+ ]
105
+
106
+ output_components.extend(speaker_section1)
107
+ output_components.extend(speaker_section2)
108
+ output_components.extend(speaker_section3)
109
+ output_components.extend(speaker_section4)
110
+
111
+ # Pad with None for any missing speakers
112
+ while len(output_components) < 28:
113
+ output_components.extend([gr.update(visible=False)] * 9)
114
 
115
  return output_components
116
 
117
+
118
  def use_example_1():
119
  return "examples/Scenes.From.A.Marriage.US.mp4"
 
120
  def use_example_2():
121
  return "examples/Billie Eilish.mp4"
 
122
  def use_example_3():
123
  return "examples/Elliot Rodger.mp4"
124
 
 
135
  return preview_path
136
  return None
137
 
 
 
 
138
  with gr.Blocks() as iface:
139
  gr.Markdown("# Multiple-Speakers-Personality-Analyzer")
140
  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.")
 
146
 
147
  # Create output components
148
  output_components = []
149
+ # Add transcript output near the top
150
  execution_box = gr.Textbox(label="Execution Info", value="N/A", lines=1)
151
  output_components.append(execution_box)
152
 
153
  transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
154
  output_components.append(transcript)
155
 
156
+ for n in range(3): # Assuming maximum of 3 speakers
157
+ with gr.Tab(label=f'Speaker {n + 1}', visible=True):
158
+ with gr.TabItem(label=f'General Impression'):
159
+ column_components1 = [
160
+ gr.Markdown(visible=False),
161
+ gr.Textbox(label="General Impression", visible=False)]
162
+
163
+ with gr.TabItem(label=f'Attachment Styles'):
164
+ column_components2 = [
165
+ gr.Plot(visible=False),
166
+ gr.Plot(visible=False),
167
+ gr.Textbox(label="Attachment Styles Explanation", visible=False)]
168
+
169
+ with gr.TabItem(label=f'Big Five Traits'):
170
+ column_components3 = [
171
+ gr.Plot(visible=False),
172
+ gr.Textbox(label="Big Five Traits Explanation", visible=False)]
173
+
174
+ with gr.TabItem(label=f'Personalities'):
175
+ column_components4 = [
176
+ gr.Plot(visible=False),
177
+ gr.Textbox(label="Personality Disorders Explanation", visible=False)]
178
+
179
+ output_components.extend(column_components1)
180
+ output_components.extend(column_components2)
181
+ output_components.extend(column_components3)
182
+ output_components.extend(column_components4)
183
+
184
+
185
+ with gr.Tab(label=f'Examples', visible=True):
186
+ gr.Markdown("### Example Videos")
187
+ with gr.Row():
188
+ with gr.Column(scale=1):
189
+ example_video_1_path = "examples/Scenes.From.A.Marriage.US.mp4"
190
+ preview_1 = get_middle_frame(example_video_1_path)
191
+ gr.Image(preview_1, label="Scenes From A Marriage")
192
+ example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
193
+ use_example_button_1 = gr.Button("Load Example 1")
194
+
195
+ with gr.Column(scale=1):
196
+ example_video_2_path = "examples/Billie Eilish.mp4"
197
+ preview_2 = get_middle_frame(example_video_2_path)
198
+ gr.Image(preview_2, label="Billie Eilish")
199
+ example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
200
+ use_example_button_2 = gr.Button("Load Example 2")
201
+
202
+ with gr.Column(scale=1):
203
+ example_video_3_path = "examples/Elliot Rodger.mp4"
204
+ preview_3 = get_middle_frame(example_video_3_path)
205
+ gr.Image(preview_3, label="Elliot Rodger")
206
+ example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
207
+ use_example_button_3 = gr.Button("Load Example 3")
208
 
209
  with open('description.txt', 'r') as file:
210
  description_txt = file.read()
 
213
  gr.HTML("<div style='height: 20px;'></div>")
214
  gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=1000)
215
  gr.Image(value="appendix/AI Personality Detection flow - 2.png", label='Flowchart 2', width=1000)
 
216
 
217
  analyze_button.click(
 
 
 
 
218
  fn=analyze_video,
219
  inputs=[video_input],
220
  outputs=output_components,
 
225
  fn=use_example_1,
226
  inputs=[],
227
  outputs=[video_input],
228
+ ).then(fn=analyze_video,
229
+ inputs=[video_input],
230
+ outputs=output_components,
231
+ show_progress=True
232
+ )
 
 
 
 
 
 
233
  use_example_button_2.click(
234
  fn=use_example_2,
235
  inputs=[],
236
  outputs=[video_input],
237
+ ).then(fn=analyze_video,
238
+ inputs=[video_input],
239
+ outputs=output_components,
240
+ show_progress=True
241
+ )
 
 
 
 
 
 
242
  use_example_button_3.click(
243
  fn=use_example_3,
244
  inputs=[],
245
  outputs=[video_input],
246
+ ).then(fn=analyze_video,
247
+ inputs=[video_input],
248
+ outputs=output_components,
249
+ show_progress=True
250
+ )
 
 
 
 
 
251
 
252
  if __name__ == "__main__":
253
  iface.launch()