import time
from pathlib import Path
from agno.agent import Agent
from agno.media import Video
from agno.models.google import Gemini
from agno.utils.log import logger
model = Gemini(id="gemini-2.0-flash-exp")
agent = Agent(
model=model,
markdown=True,
)
# 请下载一个示例视频文件来测试这个 Agent
# 运行:`wget https://storage.googleapis.com/generativeai-downloads/images/GreatRedSpot.mp4` 来下载示例视频
video_path = Path(__file__).parent.joinpath("samplevideo.mp4")
video_file = None
remote_file_name = f"files/{video_path.stem.lower().replace('_', '')}"
try:
video_file = model.get_client().files.get(name=remote_file_name)
except Exception as e:
logger.info(f"获取文件 {video_path.stem} 时出错:{e}")
pass
# 如果视频文件不存在,则上传该视频文件
if not video_file:
try:
logger.info(f"正在上传视频:{video_path}")
video_file = model.get_client().files.upload(
file=video_path,
config=dict(name=video_path.stem, display_name=video_path.stem),
)
# 检查文件是否已准备好使用。
while video_file.state.name == "PROCESSING":
time.sleep(2)
video_file = model.get_client().files.get(name=video_file.name)
logger.info(f"已上传视频:{video_file}")
except Exception as e:
logger.error(f"上传视频时出错:{e}")
if __name__ == "__main__":
agent.print_response(
"告诉我关于这个视频的信息",
videos=[Video(content=video_file)],
stream=True,
)
创建虚拟环境
打开 Terminal
并创建一个 python 虚拟环境。
python3 -m venv .venv
source .venv/bin/activate
设置你的 API 密钥
export GOOGLE_API_KEY=xxx
安装库
pip install -U google-genai agno
运行 Agent
python cookbook/models/google/gemini/video_input_file_upload.py