Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,70 +29,59 @@ def frame_capture(video_path, num_frames=5):
|
|
| 29 |
vidObj.release()
|
| 30 |
return frames
|
| 31 |
|
| 32 |
-
# Function
|
| 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 |
-
prompt = "Describe
|
| 38 |
images_with_prompt = [prompt] + images
|
| 39 |
|
| 40 |
responses = model.generate_content(images_with_prompt)
|
| 41 |
descriptions = [response.text for response in responses]
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
# Function to
|
| 46 |
-
def
|
| 47 |
frames = frame_capture(video_path)
|
| 48 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
| 49 |
|
| 50 |
-
|
| 51 |
-
prompt = f"Based on these video frames, {user_question}"
|
| 52 |
-
else:
|
| 53 |
-
prompt = "Describe what is happening in each of these frames in this video sequentially."
|
| 54 |
-
|
| 55 |
images_with_prompt = [prompt] + images
|
| 56 |
|
| 57 |
responses = model.generate_content(images_with_prompt)
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
# Helper function to format descriptions
|
| 63 |
def format_descriptions(descriptions):
|
| 64 |
return ' '.join(descriptions).strip()
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
chatbot.append((user_message, response))
|
| 70 |
-
return "", chatbot
|
| 71 |
-
|
| 72 |
-
# Define Gradio interface
|
| 73 |
-
# Separate video inputs for each tab to avoid the DuplicateBlockError
|
| 74 |
-
railway_video_input = gr.Video(label="Upload Video for Railway Analysis", autoplay=True)
|
| 75 |
-
chat_video_input = gr.Video(label="Upload Video for Chat Analysis", autoplay=True)
|
| 76 |
-
|
| 77 |
-
# Railway defect analysis tab
|
| 78 |
-
with gr.Blocks() as railway_tab:
|
| 79 |
with gr.Column():
|
| 80 |
-
gr.Markdown("###
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
analyze_button.
|
|
|
|
| 84 |
|
| 85 |
-
# Interactive
|
| 86 |
-
with gr.Blocks() as
|
| 87 |
with gr.Column():
|
| 88 |
gr.Markdown("### Interactive Chat Mode")
|
|
|
|
| 89 |
chatbot = gr.Chatbot(label="Video Analysis Chatbot")
|
| 90 |
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
| 91 |
-
user_input.submit(fn=chat_interaction, inputs=[
|
| 92 |
|
| 93 |
# Combine the two tabs into a single interface
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
-
|
| 96 |
-
pass
|
| 97 |
|
| 98 |
demo.launch()
|
|
|
|
| 29 |
vidObj.release()
|
| 30 |
return frames
|
| 31 |
|
| 32 |
+
# Function to generate text descriptions for frames
|
| 33 |
+
def generate_descriptions_for_frames(video_path):
|
| 34 |
frames = frame_capture(video_path)
|
| 35 |
images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
|
| 36 |
|
| 37 |
+
prompt = "Describe what is happening in each of these frames in this video sequentially."
|
| 38 |
images_with_prompt = [prompt] + images
|
| 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 |
+
response_text = responses[-1].text if user_message.strip() else format_descriptions([r.text for r in responses])
|
| 56 |
|
| 57 |
+
chatbot.append((user_message, response_text))
|
| 58 |
+
return "", chatbot
|
| 59 |
|
| 60 |
# Helper function to format descriptions
|
| 61 |
def format_descriptions(descriptions):
|
| 62 |
return ' '.join(descriptions).strip()
|
| 63 |
|
| 64 |
+
# Define the Gradio interfaces for each tab
|
| 65 |
+
# Tab 1: Video Analysis System with Set Prompt
|
| 66 |
+
with gr.Blocks() as tab1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Column():
|
| 68 |
+
gr.Markdown("### Video Analysis System")
|
| 69 |
+
video_input_1 = gr.Video(label="Upload Video", autoplay=True)
|
| 70 |
+
output_text = gr.Textbox(label="What's in this video")
|
| 71 |
+
analyze_button = gr.Button("Analyze Video")
|
| 72 |
+
analyze_button.click(fn=generate_descriptions_for_frames, inputs=video_input_1, outputs=output_text)
|
| 73 |
|
| 74 |
+
# Tab 2: Interactive Chat Mode
|
| 75 |
+
with gr.Blocks() as tab2:
|
| 76 |
with gr.Column():
|
| 77 |
gr.Markdown("### Interactive Chat Mode")
|
| 78 |
+
video_input_2 = gr.Video(label="Upload Video", autoplay=True)
|
| 79 |
chatbot = gr.Chatbot(label="Video Analysis Chatbot")
|
| 80 |
user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")
|
| 81 |
+
user_input.submit(fn=chat_interaction, inputs=[video_input_2, chatbot, user_input], outputs=[user_input, chatbot])
|
| 82 |
|
| 83 |
# Combine the two tabs into a single interface
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
+
gr.TabbedInterface([tab1, tab2], ["Video Analysis", "Interactive Chat"])
|
|
|
|
| 86 |
|
| 87 |
demo.launch()
|