Rahatara commited on
Commit
0e1249d
·
verified ·
1 Parent(s): a328d46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -35
app.py CHANGED
@@ -29,70 +29,59 @@ def frame_capture(video_path, num_frames=5):
29
  vidObj.release()
30
  return frames
31
 
32
- # Function for railway defect analysis with a set prompt
33
- def railway_defect_analysis(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 any potential railway defects or issues visible in these video frames."
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
- return format_descriptions(descriptions)
 
44
 
45
- # Function to generate text descriptions for frames or answer a specific question
46
- def analyze_video(video_path, user_question):
47
  frames = frame_capture(video_path)
48
  images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]
49
 
50
- if user_question.strip():
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
- descriptions = [response.text for response in responses]
59
 
60
- return descriptions[-1] if user_question.strip() else format_descriptions(descriptions)
 
61
 
62
  # Helper function to format descriptions
63
  def format_descriptions(descriptions):
64
  return ' '.join(descriptions).strip()
65
 
66
- # Function to handle chat interaction
67
- def chat_interaction(video_path, chatbot, user_message):
68
- response = analyze_video(video_path, user_message)
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("### Railway Defect Analysis")
81
- defect_analysis_output = gr.Textbox(label="Defect Analysis")
82
- analyze_button = gr.Button("Analyze for Railway Defects")
83
- analyze_button.click(fn=railway_defect_analysis, inputs=railway_video_input, outputs=defect_analysis_output)
 
84
 
85
- # Interactive chat tab
86
- with gr.Blocks() as chat_tab:
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=[chat_video_input, chatbot, user_input], outputs=[user_input, chatbot])
92
 
93
  # Combine the two tabs into a single interface
94
  with gr.Blocks() as demo:
95
- with gr.TabbedInterface([railway_tab, chat_tab], ["Railway Defect Analysis", "Interactive Chat"]):
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()