Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,30 +4,24 @@ import time, os
|
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
|
| 7 |
-
# π Initialize FastAPI
|
| 8 |
app = FastAPI(title="Gemini Veo3 Video Generator")
|
| 9 |
|
| 10 |
-
# π Load Gemini API key from Hugging Face secret
|
| 11 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 12 |
-
|
| 13 |
if not API_KEY:
|
| 14 |
raise ValueError("β GEMINI_API_KEY not found. Add it in Hugging Face β Settings β Repository secrets.")
|
| 15 |
|
| 16 |
-
# π Initialize client
|
| 17 |
client = genai.Client(api_key=API_KEY)
|
| 18 |
|
| 19 |
-
# π§ Request model
|
| 20 |
class VideoPrompt(BaseModel):
|
| 21 |
prompt: str
|
| 22 |
|
| 23 |
-
# π¬ Endpoint to generate video
|
| 24 |
@app.post("/generate_video")
|
| 25 |
async def generate_video(data: VideoPrompt):
|
| 26 |
try:
|
| 27 |
print(f"π¬ Generating video for prompt: {data.prompt}")
|
| 28 |
|
| 29 |
operation = client.models.generate_videos(
|
| 30 |
-
model="veo-3.0-
|
| 31 |
prompt=data.prompt,
|
| 32 |
config=types.GenerateVideosConfig(
|
| 33 |
aspect_ratio="16:9",
|
|
@@ -36,20 +30,21 @@ async def generate_video(data: VideoPrompt):
|
|
| 36 |
),
|
| 37 |
)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
while not operation.done:
|
| 41 |
print("β³ Still generating...")
|
| 42 |
time.sleep(10)
|
| 43 |
-
operation = client.operations.get(operation)
|
| 44 |
|
| 45 |
-
# β
Check if video is generated
|
| 46 |
if not operation.response.generated_videos:
|
| 47 |
raise HTTPException(status_code=500, detail="No video generated.")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
print(f"β
Video ready: {video_url}")
|
| 55 |
|
|
@@ -60,11 +55,10 @@ async def generate_video(data: VideoPrompt):
|
|
| 60 |
}
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
-
print(
|
| 64 |
raise HTTPException(status_code=500, detail=str(e))
|
| 65 |
|
| 66 |
|
| 67 |
-
# π Optional root endpoint for basic health check
|
| 68 |
@app.get("/")
|
| 69 |
def root():
|
| 70 |
-
return {"message": "β
Gemini Veo3 API
|
|
|
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
|
|
|
|
| 7 |
app = FastAPI(title="Gemini Veo3 Video Generator")
|
| 8 |
|
|
|
|
| 9 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
|
|
|
| 10 |
if not API_KEY:
|
| 11 |
raise ValueError("β GEMINI_API_KEY not found. Add it in Hugging Face β Settings β Repository secrets.")
|
| 12 |
|
|
|
|
| 13 |
client = genai.Client(api_key=API_KEY)
|
| 14 |
|
|
|
|
| 15 |
class VideoPrompt(BaseModel):
|
| 16 |
prompt: str
|
| 17 |
|
|
|
|
| 18 |
@app.post("/generate_video")
|
| 19 |
async def generate_video(data: VideoPrompt):
|
| 20 |
try:
|
| 21 |
print(f"π¬ Generating video for prompt: {data.prompt}")
|
| 22 |
|
| 23 |
operation = client.models.generate_videos(
|
| 24 |
+
model="veo-3.0-generate-001",
|
| 25 |
prompt=data.prompt,
|
| 26 |
config=types.GenerateVideosConfig(
|
| 27 |
aspect_ratio="16:9",
|
|
|
|
| 30 |
),
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Poll until generation completes
|
| 34 |
while not operation.done:
|
| 35 |
print("β³ Still generating...")
|
| 36 |
time.sleep(10)
|
| 37 |
+
operation = client.operations.get(name=operation.name) # β
correct call
|
| 38 |
|
|
|
|
| 39 |
if not operation.response.generated_videos:
|
| 40 |
raise HTTPException(status_code=500, detail="No video generated.")
|
| 41 |
|
| 42 |
+
generated_video = operation.response.generated_videos[0]
|
| 43 |
+
video_file_id = generated_video.video.name # β
file name or ID
|
| 44 |
+
|
| 45 |
+
# β
Correct file retrieval syntax
|
| 46 |
+
file_info = client.files.get(name=video_file_id)
|
| 47 |
+
video_url = file_info.uri # This is the downloadable URL
|
| 48 |
|
| 49 |
print(f"β
Video ready: {video_url}")
|
| 50 |
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
print("β Error:", e)
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
|
| 61 |
|
|
|
|
| 62 |
@app.get("/")
|
| 63 |
def root():
|
| 64 |
+
return {"message": "β
Gemini Veo3 API running. Use /docs to test."}
|