reab5555 commited on
Commit
aa52433
·
verified ·
1 Parent(s): 92b5afd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -84
app.py CHANGED
@@ -1,54 +1,19 @@
 
1
  import gradio as gr
2
  from llm_loader import load_model
3
  from processing import process_input
4
  from transcription_diarization import diarize_audio
5
  from visualization import create_charts
6
- from interview import get_interview_instance
7
  import time
8
  import re
9
  import cv2
10
  import os
11
- import traceback
12
  from config import openai_api_key
13
 
14
  # Load the model
15
  llm = load_model(openai_api_key)
16
 
17
- def process_message(message, history, speaker_id):
18
- try:
19
- print(f"Entering process_message with message: {message}, history: {history}, speaker_id: {speaker_id}")
20
- interview = get_interview_instance()
21
- response = interview.process_message(message)
22
- print(f"Response from interview.process_message: {response}")
23
-
24
- history = history or []
25
- history.append((message, response))
26
-
27
- print(f"Returning history: {history}")
28
- print(f"Type of history: {type(history)}")
29
- print(f"Content of history: {history}")
30
- return history
31
- except Exception as e:
32
- print(f"Error in process_message: {str(e)}")
33
- print(traceback.format_exc())
34
- return [] # Return an empty list in case of error
35
-
36
- def start_new_interview(general_impression, speaker_id):
37
- try:
38
- print(f"Entering start_new_interview with general_impression: {general_impression}, speaker_id: {speaker_id}")
39
- interview = get_interview_instance()
40
- interview.set_general_impression(general_impression)
41
- opening_question = interview.start_interview()
42
- print(f"Opening question: {opening_question}")
43
-
44
- result = [(None, opening_question)]
45
- print(f"Returning result: {result}")
46
- return result
47
- except Exception as e:
48
- print(f"Error in start_new_interview: {str(e)}")
49
- print(traceback.format_exc())
50
- return [] # Return an empty list in case of error
51
-
52
  def analyze_video(video_path, progress=gr.Progress()):
53
  start_time = time.time()
54
  if not video_path:
@@ -67,32 +32,21 @@ def analyze_video(video_path, progress=gr.Progress()):
67
  charts, explanations, general_impressions = create_charts(results)
68
 
69
  progress(1.0, desc="Charts generation complete.")
70
-
71
  end_time = time.time()
72
  execution_time = end_time - start_time
73
 
74
- output_components = []
75
-
76
  output_components.append(f"Completed in {int(execution_time)} seconds.")
77
  output_components.append(gr.Textbox(value=transcription, label="Transcript", lines=10, visible=True))
78
-
79
  for i, (speaker_id, speaker_charts) in enumerate(charts.items(), start=1):
 
80
  speaker_explanations = explanations[speaker_id]
81
  speaker_general_impression = general_impressions[speaker_id]
82
 
83
- # Update the hidden textbox with the general impression
84
- output_components.append(gr.Textbox(value=speaker_general_impression, visible=False, label=f"General Impression Speaker {i}"))
85
-
86
  with gr.Tab(visible=True):
87
- with gr.TabItem(label=f'Interactive Interview'):
88
- chatbot = gr.Chatbot(visible=True)
89
- msg = gr.Textbox(visible=True)
90
- clear = gr.Button("Clear", visible=True)
91
- start_interview = gr.Button("Start Interview", visible=True)
92
-
93
- print(f"Setting up chatbot for speaker {i}")
94
- print(f"Type of chatbot: {type(chatbot)}")
95
-
96
  with gr.TabItem(label=f'General Impression'):
97
  speaker_section1 = [
98
  gr.Markdown(f"### {speaker_id}", visible=True),
@@ -120,26 +74,31 @@ def analyze_video(video_path, progress=gr.Progress()):
120
  gr.Textbox(value=speaker_explanations.get("personality", ""),
121
  label="Personality Disorders Explanation", visible=True, lines=2)
122
  ]
123
-
124
- output_components.extend([chatbot, msg, clear, start_interview])
125
  output_components.extend(speaker_section1)
126
  output_components.extend(speaker_section2)
127
  output_components.extend(speaker_section3)
128
  output_components.extend(speaker_section4)
129
 
130
  # Pad with None for any missing speakers
131
- while len(output_components) < 40:
132
- output_components.extend([gr.update(visible=False)] * 13)
133
 
134
  return output_components
135
 
 
136
  def use_example_1():
137
  return "examples/Scenes.From.A.Marriage.US.mp4"
 
 
138
  def use_example_2():
139
  return "examples/Billie Eilish.mp4"
 
 
140
  def use_example_3():
141
  return "examples/Elliot Rodger.mp4"
142
 
 
143
  def get_middle_frame(video_path):
144
  cap = cv2.VideoCapture(video_path)
145
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
@@ -153,45 +112,31 @@ def get_middle_frame(video_path):
153
  return preview_path
154
  return None
155
 
 
156
  with gr.Blocks() as iface:
157
  gr.Markdown("# Multiple Speakers Personality Analyzer")
158
- 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.")
 
159
 
160
  with gr.Row():
161
  video_input = gr.Video(label="Upload Video")
162
-
163
  analyze_button = gr.Button("Analyze")
164
-
165
  # Create output components
166
  output_components = []
167
-
168
  # Add transcript output near the top
169
  execution_box = gr.Textbox(label="Execution Info", value="N/A", lines=1)
170
  output_components.append(execution_box)
171
 
172
  transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
173
  output_components.append(transcript)
174
-
175
  with open('description.txt', 'r') as file:
176
  description_txt = file.read()
177
-
178
  for n in range(3): # Assuming maximum of 3 speakers
179
  with gr.Tab(label=f'Speaker {n + 1}', visible=True):
180
- with gr.TabItem(label=f'Interactive Interview'):
181
- chatbot = gr.Chatbot(visible=False)
182
- msg = gr.Textbox(visible=False)
183
- clear = gr.Button("Clear", visible=False)
184
- start_interview = gr.Button("Start Interview", visible=False)
185
-
186
- # Update event listeners
187
- msg.submit(process_message,
188
- inputs=[msg, chatbot, gr.State(f'Speaker {n + 1}')],
189
- outputs=[chatbot])
190
- clear.click(lambda: [], None, chatbot, queue=False)
191
- start_interview.click(start_new_interview,
192
- inputs=[gr.Textbox(visible=False, label=f"General Impression Speaker {n+1}"), gr.State(f'Speaker {n + 1}')],
193
- outputs=[chatbot])
194
-
195
  with gr.TabItem(label=f'General Impression'):
196
  column_components1 = [
197
  gr.Markdown(visible=False),
@@ -213,7 +158,6 @@ with gr.Blocks() as iface:
213
  gr.Plot(visible=False),
214
  gr.Textbox(label="Personality Disorders Explanation")]
215
 
216
- output_components.extend([chatbot, msg, clear, start_interview])
217
  output_components.extend(column_components1)
218
  output_components.extend(column_components2)
219
  output_components.extend(column_components3)
@@ -227,22 +171,22 @@ with gr.Blocks() as iface:
227
  gr.Image(preview_1, label="Scenes From A Marriage")
228
  example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
229
  use_example_button_1 = gr.Button("Load Example 1")
230
-
231
  with gr.Column(scale=1):
232
  example_video_2_path = "examples/Billie Eilish.mp4"
233
  preview_2 = get_middle_frame(example_video_2_path)
234
  gr.Image(preview_2, label="Billie Eilish")
235
  example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
236
  use_example_button_2 = gr.Button("Load Example 2")
237
-
238
  with gr.Column(scale=1):
239
  example_video_3_path = "examples/Elliot Rodger.mp4"
240
  preview_3 = get_middle_frame(example_video_3_path)
241
  gr.Image(preview_3, label="Elliot Rodger")
242
  example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
243
  use_example_button_3 = gr.Button("Load Example 3")
244
-
245
- gr.HTML("<div style='height: 20px;'></div>")
246
  gr.Markdown(description_txt)
247
  gr.HTML("<div style='height: 20px;'></div>")
248
  gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=900)
@@ -284,4 +228,4 @@ with gr.Blocks() as iface:
284
  )
285
 
286
  if __name__ == "__main__":
287
- iface.launch()
 
1
+
2
  import gradio as gr
3
  from llm_loader import load_model
4
  from processing import process_input
5
  from transcription_diarization import diarize_audio
6
  from visualization import create_charts
 
7
  import time
8
  import re
9
  import cv2
10
  import os
 
11
  from config import openai_api_key
12
 
13
  # Load the model
14
  llm = load_model(openai_api_key)
15
 
16
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def analyze_video(video_path, progress=gr.Progress()):
18
  start_time = time.time()
19
  if not video_path:
 
32
  charts, explanations, general_impressions = create_charts(results)
33
 
34
  progress(1.0, desc="Charts generation complete.")
35
+
36
  end_time = time.time()
37
  execution_time = end_time - start_time
38
 
39
+ output_components = [] # transcript
40
+
41
  output_components.append(f"Completed in {int(execution_time)} seconds.")
42
  output_components.append(gr.Textbox(value=transcription, label="Transcript", lines=10, visible=True))
43
+
44
  for i, (speaker_id, speaker_charts) in enumerate(charts.items(), start=1):
45
+ print(speaker_id)
46
  speaker_explanations = explanations[speaker_id]
47
  speaker_general_impression = general_impressions[speaker_id]
48
 
 
 
 
49
  with gr.Tab(visible=True):
 
 
 
 
 
 
 
 
 
50
  with gr.TabItem(label=f'General Impression'):
51
  speaker_section1 = [
52
  gr.Markdown(f"### {speaker_id}", visible=True),
 
74
  gr.Textbox(value=speaker_explanations.get("personality", ""),
75
  label="Personality Disorders Explanation", visible=True, lines=2)
76
  ]
77
+
 
78
  output_components.extend(speaker_section1)
79
  output_components.extend(speaker_section2)
80
  output_components.extend(speaker_section3)
81
  output_components.extend(speaker_section4)
82
 
83
  # Pad with None for any missing speakers
84
+ while len(output_components) < 28:
85
+ output_components.extend([gr.update(visible=False)] * 9)
86
 
87
  return output_components
88
 
89
+
90
  def use_example_1():
91
  return "examples/Scenes.From.A.Marriage.US.mp4"
92
+
93
+
94
  def use_example_2():
95
  return "examples/Billie Eilish.mp4"
96
+
97
+
98
  def use_example_3():
99
  return "examples/Elliot Rodger.mp4"
100
 
101
+
102
  def get_middle_frame(video_path):
103
  cap = cv2.VideoCapture(video_path)
104
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
 
112
  return preview_path
113
  return None
114
 
115
+
116
  with gr.Blocks() as iface:
117
  gr.Markdown("# Multiple Speakers Personality Analyzer")
118
+ gr.Markdown(
119
+ "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.")
120
 
121
  with gr.Row():
122
  video_input = gr.Video(label="Upload Video")
123
+
124
  analyze_button = gr.Button("Analyze")
125
+
126
  # Create output components
127
  output_components = []
 
128
  # Add transcript output near the top
129
  execution_box = gr.Textbox(label="Execution Info", value="N/A", lines=1)
130
  output_components.append(execution_box)
131
 
132
  transcript = gr.Textbox(label="Transcript", lines=10, visible=False)
133
  output_components.append(transcript)
134
+
135
  with open('description.txt', 'r') as file:
136
  description_txt = file.read()
137
+
138
  for n in range(3): # Assuming maximum of 3 speakers
139
  with gr.Tab(label=f'Speaker {n + 1}', visible=True):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  with gr.TabItem(label=f'General Impression'):
141
  column_components1 = [
142
  gr.Markdown(visible=False),
 
158
  gr.Plot(visible=False),
159
  gr.Textbox(label="Personality Disorders Explanation")]
160
 
 
161
  output_components.extend(column_components1)
162
  output_components.extend(column_components2)
163
  output_components.extend(column_components3)
 
171
  gr.Image(preview_1, label="Scenes From A Marriage")
172
  example_video_1 = gr.Video(example_video_1_path, label="Example 1", visible=False)
173
  use_example_button_1 = gr.Button("Load Example 1")
174
+
175
  with gr.Column(scale=1):
176
  example_video_2_path = "examples/Billie Eilish.mp4"
177
  preview_2 = get_middle_frame(example_video_2_path)
178
  gr.Image(preview_2, label="Billie Eilish")
179
  example_video_2 = gr.Video(example_video_2_path, label="Example 2", visible=False)
180
  use_example_button_2 = gr.Button("Load Example 2")
181
+
182
  with gr.Column(scale=1):
183
  example_video_3_path = "examples/Elliot Rodger.mp4"
184
  preview_3 = get_middle_frame(example_video_3_path)
185
  gr.Image(preview_3, label="Elliot Rodger")
186
  example_video_3 = gr.Video(example_video_3_path, label="Example 3", visible=False)
187
  use_example_button_3 = gr.Button("Load Example 3")
188
+
189
+ gr.HTML("<div style='height: 20px;'></div>")
190
  gr.Markdown(description_txt)
191
  gr.HTML("<div style='height: 20px;'></div>")
192
  gr.Image(value="appendix/AI Personality Detection flow - 1.png", label='Flowchart 1', width=900)
 
228
  )
229
 
230
  if __name__ == "__main__":
231
+ iface.launch()