Rahatara commited on
Commit
8a33723
1 Parent(s): 31c8ea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
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 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
 
65
- # Tab 2: Interactive Chat Mode
66
- with gr.Blocks() as tab2:
67
- with gr.Column():
68
- gr.Markdown("### Interactive Chat Mode")
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
- # Combine the two tabs into a single interface
75
  with gr.Blocks() as demo:
76
- gr.TabbedInterface([tab2], ["Video Analysis", "Interactive Chat"])
 
 
 
 
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()