Spaces:
Sleeping
Sleeping
File size: 1,537 Bytes
4a3338b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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
@app.post("/generate_video")
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))
|