Spaces:
Running
Running
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import time | |
| from google import genai | |
| from google.genai import types | |
| app = FastAPI(title="Gemini Veo3 Video Generator") | |
| API_KEY = "YOUR_GEMINI_API_KEY" # π Replace with your API key | |
| client = genai.Client(api_key=API_KEY) | |
| class VideoPrompt(BaseModel): | |
| prompt: str | |
| async def generate_video(data: VideoPrompt): | |
| try: | |
| operation = client.models.generate_videos( | |
| model="veo-3.0-fast-generate-001", | |
| prompt=data.prompt, | |
| config=types.GenerateVideosConfig( | |
| aspect_ratio="16:9", | |
| resolution="1080p", | |
| negative_prompt="cartoon, unrealistic, text distortion" | |
| ), | |
| ) | |
| print("π¬ Video generation started...") | |
| # Poll until generation completes | |
| while not operation.done: | |
| time.sleep(10) | |
| operation = client.operations.get(operation) | |
| if not operation.response.generated_videos: | |
| raise HTTPException(status_code=500, detail="No video generated.") | |
| video_file = operation.response.generated_videos[0].video | |
| file_info = client.files.get(video_file) | |
| video_url = file_info.uri # β Returns downloadable URL | |
| return { | |
| "success": True, | |
| "video_url": video_url, | |
| "message": "Video generation complete." | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |