Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines29303132333435363738394041424344454647484950515253545556575859606128 unmodified linesvidObj.release()return frames# Function to generate text descriptions for framesdef generate_descriptions_for_frames(video_path, user_prompt):frames = frame_capture(video_path)images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]prompt = f"Describe what is happening in each of these frames in this video sequentially. {user_prompt}"images_with_prompt = [prompt] + imagesresponses = model.generate_content(images_with_prompt)descriptions = [response.text for response in responses]formatted_description = format_descriptions(descriptions)return formatted_description# Helper function to format descriptionsdef format_descriptions(descriptions):return ' '.join(descriptions).strip()# Define Gradio interfacevideo_input = gr.Video(label="Upload Video", autoplay=True)user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")output_text = gr.Textbox(label="What's in this video")# Create Gradio appgr.Interface(fn=generate_descriptions_for_frames,inputs=[video_input, user_input],outputs=output_text,title="Interactive Video Analysis System").launch()28 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364656667686970717228 unmodified linesvidObj.release()return frames# Function to generate text descriptions for frames or answer a specific questiondef analyze_video(video_path, user_question):frames = frame_capture(video_path)images = [PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in frames]if user_question.strip():prompt = f"Based on these video frames, {user_question}"else:prompt = "Describe what is happening in each of these frames in this video sequentially."images_with_prompt = [prompt] + imagesresponses = model.generate_content(images_with_prompt)descriptions = [response.text for response in responses]return descriptions[-1] if user_question.strip() else format_descriptions(descriptions)# Helper function to format descriptionsdef format_descriptions(descriptions):return ' '.join(descriptions).strip()# Function to handle chat interactiondef chat_interaction(video_path, chatbot, user_message):response = analyze_video(video_path, user_message)chatbot.append((user_message, response))return "", chatbot# Define Gradio interfacevideo_input = gr.Video(label="Upload Video", autoplay=True)chatbot = gr.Chatbot(label="Video Analysis Chatbot")user_input = gr.Textbox(label="Ask something specific about the video", placeholder="E.g., Are there any cars in this video?")# Create Gradio appwith gr.Blocks() as demo:with gr.Column():video_input.render()chatbot.render()user_input.render()user_input.submit(fn=chat_interaction, inputs=[video_input, chatbot, user_input], outputs=[user_input, chatbot])demo.launch()
app.py+23-12