Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import time, os | |
| from google import genai | |
| from google.genai import types | |
| app = FastAPI(title="Gemini Veo3 Video Generator") | |
| API_KEY = os.getenv("GEMINI_API_KEY") | |
| if not API_KEY: | |
| raise ValueError("β GEMINI_API_KEY not found. Add it in Hugging Face β Settings β Repository secrets.") | |
| client = genai.Client(api_key=API_KEY) | |
| class VideoPrompt(BaseModel): | |
| prompt: str | |
| async def generate_video(data: VideoPrompt): | |
| try: | |
| print(f"π¬ Generating video for prompt: {data.prompt}") | |
| operation = client.models.generate_videos( | |
| model="veo-3.0-generate-001", | |
| prompt=data.prompt, | |
| config=types.GenerateVideosConfig( | |
| aspect_ratio="16:9", | |
| resolution="1080p", | |
| negative_prompt="cartoon, unrealistic, text distortion" | |
| ), | |
| ) | |
| # Poll until generation completes | |
| while not operation.done: | |
| print("β³ Still generating...") | |
| time.sleep(10) | |
| operation = client.operations.get(name=operation.name) # β correct call | |
| if not operation.response.generated_videos: | |
| raise HTTPException(status_code=500, detail="No video generated.") | |
| generated_video = operation.response.generated_videos[0] | |
| video_file_id = generated_video.video.name # β file name or ID | |
| # β Correct file retrieval syntax | |
| file_info = client.files.get(name=video_file_id) | |
| video_url = file_info.uri # This is the downloadable URL | |
| print(f"β Video ready: {video_url}") | |
| return { | |
| "success": True, | |
| "video_url": video_url, | |
| "message": "Video generation complete." | |
| } | |
| except Exception as e: | |
| print("β Error:", e) | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def root(): | |
| return {"message": "β Gemini Veo3 API running. Use /docs to test."} | |