Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,50 +29,44 @@ def frame_capture(video_path, num_frames=5):
|
|
29 |
vidObj.release()
|
30 |
return frames
|
31 |
|
32 |
-
# Function to generate text descriptions for frames
|
33 |
-
def
|
34 |
frames = frame_capture(video_path)
|
35 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
39 |
|
40 |
-
responses = model.generate_content(images_with_prompt)
|
41 |
-
descriptions = [response.text for response in responses]
|
42 |
-
|
43 |
-
formatted_description = format_descriptions(descriptions)
|
44 |
-
return formatted_description
|
45 |
-
|
46 |
-
# Function to handle chat interaction
|
47 |
-
def chat_interaction(video_path, chatbot, user_message):
|
48 |
-
frames = frame_capture(video_path)
|
49 |
-
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
50 |
-
|
51 |
-
prompt = f"Based on these video frames, {user_message}"
|
52 |
images_with_prompt = [prompt] + images
|
53 |
|
54 |
responses = model.generate_content(images_with_prompt)
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
return "", chatbot
|
59 |
|
60 |
# Helper function to format descriptions
|
61 |
def format_descriptions(descriptions):
|
62 |
return ' '.join(descriptions).strip()
|
63 |
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
video_input_2 = gr.Video(label="Upload Video", autoplay=True)
|
70 |
-
chatbot = gr.Chatbot(label="Video Analysis Chatbot")
|
71 |
-
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
72 |
-
user_input.submit(fn=chat_interaction, inputs=[video_input_2, chatbot, user_input], outputs=[user_input, chatbot])
|
73 |
|
74 |
-
#
|
75 |
with gr.Blocks() as demo:
|
76 |
-
gr.
|
|
|
|
|
|
|
|
|
77 |
|
78 |
demo.launch()
|
|
|
29 |
vidObj.release()
|
30 |
return frames
|
31 |
|
32 |
+
# Function to generate text descriptions for frames or answer a specific question
|
33 |
+
def analyze_video(video_path, user_question):
|
34 |
frames = frame_capture(video_path)
|
35 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
36 |
|
37 |
+
if user_question.strip():
|
38 |
+
prompt = f"Based on these video frames, {user_question}"
|
39 |
+
else:
|
40 |
+
prompt = "Describe what is happening in each of these frames in this video."
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
images_with_prompt = [prompt] + images
|
43 |
|
44 |
responses = model.generate_content(images_with_prompt)
|
45 |
+
descriptions = [response.text for response in responses]
|
46 |
|
47 |
+
return descriptions[-1] if user_question.strip() else format_descriptions(descriptions)
|
|
|
48 |
|
49 |
# Helper function to format descriptions
|
50 |
def format_descriptions(descriptions):
|
51 |
return ' '.join(descriptions).strip()
|
52 |
|
53 |
+
# Function to handle chat interaction
|
54 |
+
def chat_interaction(video_path, chatbot, user_message):
|
55 |
+
response = analyze_video(video_path, user_message)
|
56 |
+
chatbot.append((user_message, response))
|
57 |
+
return "", chatbot
|
58 |
|
59 |
+
# Define Gradio interface
|
60 |
+
video_input = gr.Video(label="Upload Video", autoplay=True)
|
61 |
+
chatbot = gr.Chatbot(label="Video Analysis Chatbot")
|
62 |
+
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Create Gradio app
|
65 |
with gr.Blocks() as demo:
|
66 |
+
with gr.Column():
|
67 |
+
video_input.render()
|
68 |
+
chatbot.render()
|
69 |
+
user_input.render()
|
70 |
+
user_input.submit(fn=chat_interaction, inputs=[video_input, chatbot, user_input], outputs=[user_input, chatbot])
|
71 |
|
72 |
demo.launch()
|