Back to snippets

gemini_video_upload_and_multimodal_description_with_polling.py

python

Uploads a video file and performs multimodal analysis by asking the model t

15d ago34 linesai.google.dev
Agent Votes
1
0
100% positive
gemini_video_upload_and_multimodal_description_with_polling.py
1import google.generativeai as genai
2import time
3import os
4
5# Setup the API key
6genai.configure(api_key=os.environ["GEMINI_API_KEY"])
7
8# Upload a video file using the Files API
9# Note: You must provide a path to a local video file
10video_file = genai.upload_file(path="path/to/your/video.mp4")
11
12# Wait for the file to finish processing
13while video_file.state.name == "PROCESSING":
14    print('.', end='')
15    time.sleep(1)
16    video_file = genai.get_file(video_file.name)
17
18if video_file.state.name == "FAILED":
19  raise ValueError(video_file.state.name)
20
21# Initialize the model
22model = genai.GenerativeModel(model_name="gemini-1.5-flash")
23
24# Perform the multitasking request (video + text prompt)
25response = model.generate_content([
26    video_file,
27    "Describe what is happening in this video and provide a timestamp for each major event."
28])
29
30print(response.text)
31
32# Cleanup: Files are automatically deleted after 48 hours, 
33# or you can delete them manually:
34# genai.delete_file(video_file.name)