You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importosimportgoogle.generativeaiasgenaifromgoogle.generativeaiimportcachingimportdatetimeimporttime# Get your API key from https://aistudio.google.com/app/apikey# and access your API key as an environment variable.# To authenticate from a Colab, see# https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynbgenai.configure(api_key=os.environ['API_KEY'])
# Download video file# curl -O https://storage.googleapis.com/generativeai-downloads/data/Sherlock_Jr_FullMovie.mp4path_to_video_file='Sherlock_Jr_FullMovie.mp4'# Upload the video using the Files APIvideo_file=genai.upload_file(path=path_to_video_file)
# Wait for the file to finish processingwhilevideo_file.state.name=='PROCESSING':
print('Waiting for video to be processed.')
time.sleep(2)
video_file=genai.get_file(video_file.name)
print(f'Video processing complete: {video_file.uri}')
# Create a cache with a 5 minute TTLcache=caching.CachedContent.create(
model='models/gemini-1.5-flash-001',
display_name='sherlock jr movie', # used to identify the cachesystem_instruction=(
'You are an expert video analyzer, and your job is to answer ''the user\'s query based on the video file you have access to.'
),
contents=[video_file],
ttl=datetime.timedelta(minutes=5),
)
# Construct a GenerativeModel which uses the created cache.model=genai.GenerativeModel.from_cached_content(cached_content=cache)
# Query the modelresponse=model.generate_content([(
'Introduce different characters in the movie by describing ''their personality, looks, and names. Also list the timestamps ''they were introduced for the first time.')])
print(response.usage_metadata)
# The output should look something like this:## prompt_token_count: 696219# cached_content_token_count: 696190# candidates_token_count: 214# total_token_count: 696433print(response.text)
``
The text was updated successfully, but these errors were encountered:
https://ai.google.dev/gemini-api/docs/caching?lang=python
The text was updated successfully, but these errors were encountered: