-
-
Notifications
You must be signed in to change notification settings - Fork 869
feat(multimodal): add Video support for Gemini with structured outputs #1851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
1
commit into
main
Choose a base branch
from
devin/1760618723-video-support-gemini
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Video Analysis with Gemini 2.5 Pro | ||
|
||
This example demonstrates how to use Google's Gemini 2.5 Pro model with Instructor to analyze videos and extract structured information about tourist destinations. | ||
|
||
## Features | ||
|
||
- Upload videos to Gemini API using `VideoWithGenaiFile` | ||
- Extract structured recommendations using Pydantic models | ||
- Support for analyzing travel content and tourist destinations | ||
- Type-safe structured outputs | ||
|
||
## Requirements | ||
|
||
```bash | ||
pip install instructor google-genai pydantic | ||
``` | ||
|
||
## Setup | ||
|
||
1. Get your Google API key from [Google AI Studio](https://makersuite.google.com/app/apikey) | ||
2. Set the environment variable: | ||
|
||
```bash | ||
export GOOGLE_API_KEY=your_api_key_here | ||
``` | ||
|
||
## Usage | ||
|
||
Run the script with a path to your video file: | ||
|
||
```bash | ||
python run.py path/to/your/video.mp4 | ||
``` | ||
|
||
Example with a travel video: | ||
|
||
```bash | ||
python run.py takayama_travel.mp4 | ||
``` | ||
|
||
## How It Works | ||
|
||
The example: | ||
|
||
1. Uploads your video file to the Gemini API using `VideoWithGenaiFile.from_new_genai_file()` | ||
2. Sends a prompt asking for tourist destination recommendations | ||
3. Uses Instructor to parse the response into structured Pydantic models | ||
4. Returns a list of destinations with names, descriptions, and locations | ||
|
||
## Output Structure | ||
|
||
The analysis returns: | ||
|
||
- **chain_of_thought**: Detailed reasoning about the video content | ||
- **description**: Overall summary of the video | ||
- **destinations**: List of tourist destinations, each with: | ||
- name: Name of the destination | ||
- description: What makes it interesting | ||
- location: Where it's located | ||
|
||
## Supported Video Formats | ||
|
||
Gemini supports the following video formats: | ||
- MP4 | ||
- MPEG | ||
- MOV | ||
- AVI | ||
- FLV | ||
- MPG | ||
- WebM | ||
- WMV | ||
- 3GPP | ||
- QuickTime | ||
|
||
## Notes | ||
|
||
- Video files are uploaded to Google's servers for processing | ||
- Large videos may take longer to upload and process | ||
- The API automatically waits for the upload to complete before processing | ||
|
||
## Related Examples | ||
|
||
- [Multimodal Gemini Guide](../../docs/blog/posts/multimodal-gemini.md) | ||
- [Image Analysis with Gemini](../vision/) | ||
- [PDF Processing with Gemini](../../docs/blog/posts/chat-with-your-pdf-with-gemini.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Video Analysis with Gemini 2.5 Pro | ||
|
||
This example demonstrates how to use Gemini 2.5 Pro with Instructor to analyze videos | ||
and extract structured information. We'll process a video and extract tourist destinations | ||
mentioned in it. | ||
|
||
Requirements: | ||
pip install instructor google-genai pydantic | ||
|
||
Usage: | ||
export GOOGLE_API_KEY=your_api_key_here | ||
|
||
python run.py path/to/your/video.mp4 | ||
""" | ||
|
||
import instructor | ||
from pydantic import BaseModel | ||
import sys | ||
|
||
|
||
class TouristDestination(BaseModel): | ||
"""Represents a tourist destination mentioned in the video.""" | ||
|
||
name: str | ||
description: str | ||
location: str | ||
|
||
|
||
class VideoRecommendations(BaseModel): | ||
"""Structured output containing recommendations from the video.""" | ||
|
||
chain_of_thought: str | ||
description: str | ||
destinations: list[TouristDestination] | ||
|
||
|
||
def analyze_video(video_path: str): | ||
""" | ||
Analyze a video and extract tourist destination recommendations. | ||
|
||
Args: | ||
video_path: Path to the video file to analyze | ||
|
||
Returns: | ||
VideoRecommendations object containing structured data | ||
""" | ||
client = instructor.from_provider( | ||
"google/gemini-2.0-flash-exp", | ||
async_client=False, | ||
) | ||
|
||
print(f"Uploading video: {video_path}") | ||
video = instructor.VideoWithGenaiFile.from_new_genai_file(video_path) | ||
print(f"Video uploaded successfully: {video.source}") | ||
|
||
print("Analyzing video content...") | ||
recommendations = client.messages.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
"What tourist destinations and places do they recommend in this video? " | ||
"Provide a detailed analysis including the name, description, and location of each place.", | ||
video, | ||
], | ||
} | ||
], | ||
response_model=VideoRecommendations, | ||
) | ||
|
||
return recommendations | ||
|
||
|
||
def main(): | ||
"""Main function to run the video analysis.""" | ||
if len(sys.argv) < 2: | ||
print("Usage: python run.py <path_to_video>") | ||
print("Example: python run.py travel_video.mp4") | ||
sys.exit(1) | ||
|
||
video_path = sys.argv[1] | ||
|
||
try: | ||
results = analyze_video(video_path) | ||
|
||
print("\n" + "=" * 80) | ||
print("VIDEO ANALYSIS RESULTS") | ||
print("=" * 80) | ||
|
||
print(f"\nOverview: {results.description}") | ||
print(f"\nAnalysis: {results.chain_of_thought}") | ||
|
||
print(f"\nDestinations Found: {len(results.destinations)}") | ||
print("-" * 80) | ||
|
||
for i, dest in enumerate(results.destinations, 1): | ||
print(f"\n{i}. {dest.name}") | ||
print(f" Location: {dest.location}") | ||
print(f" Description: {dest.description}") | ||
|
||
print("\n" + "=" * 80) | ||
|
||
except Exception as e: | ||
print(f"Error analyzing video: {e}") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model version mismatch: the title mentions Gemini 2.5 Pro but the provider string is 'google/gemini-2.0-flash-exp'. Ensure consistency.