tstech1 commited on
Commit
4a3338b
Β·
verified Β·
1 Parent(s): 8c93ede

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import time
4
+ from google import genai
5
+ from google.genai import types
6
+
7
+ app = FastAPI(title="Gemini Veo3 Video Generator")
8
+
9
+ API_KEY = "YOUR_GEMINI_API_KEY" # πŸ”‘ Replace with your API key
10
+ client = genai.Client(api_key=API_KEY)
11
+
12
+ class VideoPrompt(BaseModel):
13
+ prompt: str
14
+
15
+ @app.post("/generate_video")
16
+ async def generate_video(data: VideoPrompt):
17
+ try:
18
+ operation = client.models.generate_videos(
19
+ model="veo-3.0-fast-generate-001",
20
+ prompt=data.prompt,
21
+ config=types.GenerateVideosConfig(
22
+ aspect_ratio="16:9",
23
+ resolution="1080p",
24
+ negative_prompt="cartoon, unrealistic, text distortion"
25
+ ),
26
+ )
27
+
28
+ print("🎬 Video generation started...")
29
+
30
+ # Poll until generation completes
31
+ while not operation.done:
32
+ time.sleep(10)
33
+ operation = client.operations.get(operation)
34
+
35
+ if not operation.response.generated_videos:
36
+ raise HTTPException(status_code=500, detail="No video generated.")
37
+
38
+ video_file = operation.response.generated_videos[0].video
39
+ file_info = client.files.get(video_file)
40
+ video_url = file_info.uri # βœ… Returns downloadable URL
41
+
42
+ return {
43
+ "success": True,
44
+ "video_url": video_url,
45
+ "message": "Video generation complete."
46
+ }
47
+
48
+ except Exception as e:
49
+ raise HTTPException(status_code=500, detail=str(e))