Spaces:
Sleeping
Sleeping
File size: 2,037 Bytes
4a3338b 3fe4539 4a3338b 3fe4539 4a3338b 3fe4539 4a3338b 800127e 4a3338b 800127e 4a3338b 3fe4539 4a3338b 800127e 4a3338b 800127e 3fe4539 4a3338b 800127e 4a3338b 3fe4539 800127e |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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
@app.post("/generate_video")
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))
@app.get("/")
def root():
return {"message": "β
Gemini Veo3 API running. Use /docs to test."}
|