tstech1 commited on
Commit
800127e
Β·
verified Β·
1 Parent(s): 3fe4539

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
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-fast-generate-001",
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
- # ⏳ Wait for video generation to complete
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
- # πŸŽ₯ Get video file & URL
50
- video_file = operation.response.generated_videos[0].video
51
- file_info = client.files.get(video_file)
52
- video_url = file_info.uri # Public GCS link
 
 
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(f"❌ Error: {e}")
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 is running. Go to /docs to test."}
 
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."}