Refactor: Modularized RunwayML logic and extracted to src/runwayml/
Browse files- src/api_clients.py +0 -165
- src/automation.py +3 -2
- src/runwayml/__init__.py +0 -0
- src/runwayml/generate_video.py +168 -0
- src/video_generation_process.py +84 -0
src/api_clients.py
CHANGED
|
@@ -100,171 +100,6 @@ class APIClients:
|
|
| 100 |
except: pass
|
| 101 |
|
| 102 |
|
| 103 |
-
async def generate_video(self, prompt: str, duration: int, image_input: str = None) -> Dict:
|
| 104 |
-
"""
|
| 105 |
-
Generate video using RunwayML gen4_turbo ($0.25 per video / 25 credits)
|
| 106 |
-
|
| 107 |
-
Args:
|
| 108 |
-
prompt: Text prompt for video generation
|
| 109 |
-
duration: Video duration in seconds
|
| 110 |
-
image_input: A HTTPS URL or a LOCAL FILE PATH to an image.
|
| 111 |
-
"""
|
| 112 |
-
try:
|
| 113 |
-
if os.getenv("TEST_AUTOMATION", "").lower() == "true":
|
| 114 |
-
if image_input:
|
| 115 |
-
return {
|
| 116 |
-
"task_id": "644319db-5226-42cf-b45f-5388e40d38a6",
|
| 117 |
-
"video_url": f"{os.getenv('TEST_DATA_DIRECTORY')}/image-to-video.mp4",
|
| 118 |
-
"local_path": f"{os.getenv('TEST_DATA_DIRECTORY')}/image-to-video.mp4",
|
| 119 |
-
"duration": 3,
|
| 120 |
-
"prompt": prompt,
|
| 121 |
-
"status": "SUCCEEDED",
|
| 122 |
-
"created_at": "2025-10-15T12:39:24.279Z",
|
| 123 |
-
"model": "veo3.1_fast",
|
| 124 |
-
}
|
| 125 |
-
else:
|
| 126 |
-
return {
|
| 127 |
-
"task_id": "644319db-5226-42cf-b45f-5388e40d38a6",
|
| 128 |
-
"video_url": f"{os.getenv('TEST_DATA_DIRECTORY')}/veo_text_to_video.mp4",
|
| 129 |
-
"local_path": f"{os.getenv('TEST_DATA_DIRECTORY')}/veo_text_to_video.mp4",
|
| 130 |
-
"duration": 3,
|
| 131 |
-
"prompt": prompt,
|
| 132 |
-
"status": "SUCCEEDED",
|
| 133 |
-
"created_at": "2025-10-15T12:39:24.279Z",
|
| 134 |
-
"model": "gen4_turbo",
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
logger.info(f"🎬 Generating video with: {prompt[:1000]}...")
|
| 138 |
-
|
| 139 |
-
prompt_image_value = ""
|
| 140 |
-
|
| 141 |
-
if image_input:
|
| 142 |
-
if image_input.startswith("http"):
|
| 143 |
-
# It's a URL, use it directly
|
| 144 |
-
logger.info("Using provided image URL for RunwayML.")
|
| 145 |
-
prompt_image_value = image_input
|
| 146 |
-
else:
|
| 147 |
-
# It's a local file path, convert it to a Base64 Data URI
|
| 148 |
-
logger.info(f"Encoding local image {image_input} to Base64 Data URI.")
|
| 149 |
-
try:
|
| 150 |
-
# Determine the image type from the file extension
|
| 151 |
-
image_type = os.path.splitext(image_input)[1].replace('.', '') # e.g., 'png'
|
| 152 |
-
|
| 153 |
-
with open(image_input, "rb") as image_file:
|
| 154 |
-
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 155 |
-
|
| 156 |
-
# Construct the full Data URI
|
| 157 |
-
prompt_image_value = f"data:image/{image_type};base64,{encoded_string}"
|
| 158 |
-
logger.info("✓ Successfully encoded image to Data URI.")
|
| 159 |
-
except Exception as e:
|
| 160 |
-
logger.error(f"❌ Failed to encode local image to Base64: {e}")
|
| 161 |
-
raise
|
| 162 |
-
|
| 163 |
-
headers = {
|
| 164 |
-
"Authorization": f"Bearer {self.runway_api_key}",
|
| 165 |
-
"Content-Type": "application/json",
|
| 166 |
-
"X-Runway-Version": "2024-11-06",
|
| 167 |
-
}
|
| 168 |
-
|
| 169 |
-
model_name = "gen4_turbo"
|
| 170 |
-
ratio = "720:1280"
|
| 171 |
-
url = "https://api.dev.runwayml.com/v1/image_to_video"
|
| 172 |
-
duration = 3
|
| 173 |
-
if not image_input:
|
| 174 |
-
# ratio = "1080:1920" # not working wih veo3.1_fast
|
| 175 |
-
model_name = "veo3.1_fast"
|
| 176 |
-
duration = 4
|
| 177 |
-
# ratio = "1080:1920" # not working with veo in this ratio
|
| 178 |
-
url = "https://api.dev.runwayml.com/v1/text_to_video"
|
| 179 |
-
|
| 180 |
-
if os.getenv("USE_GEMIMI_VIDEO", "false").lower() == "true":
|
| 181 |
-
logger.info("Using Gemini SDK for video generation...")
|
| 182 |
-
|
| 183 |
-
output_path = await self.get_cache_url(f"ai_studio_sdk.generate_video_{model_name}", ".mp4")
|
| 184 |
-
if not output_path:
|
| 185 |
-
output_path = f'/tmp/video_{duration}_{model_name}_{uuid.uuid4().hex[:8]}.mp4'
|
| 186 |
-
ai_studio_sdk.generate_video(prompt, output_path, image_input)
|
| 187 |
-
await self.store_in_cache(output_path, f"ai_studio_sdk.generate_video_{model_name}", ".mp4")
|
| 188 |
-
|
| 189 |
-
video_result = {
|
| 190 |
-
"local_path": output_path,
|
| 191 |
-
"task_id": None,
|
| 192 |
-
"duration": duration,
|
| 193 |
-
"prompt": prompt,
|
| 194 |
-
"status": "success",
|
| 195 |
-
"created_at": None,
|
| 196 |
-
"model": model_name,
|
| 197 |
-
}
|
| 198 |
-
return video_result
|
| 199 |
-
|
| 200 |
-
payload = {
|
| 201 |
-
"promptImage": prompt_image_value,
|
| 202 |
-
"promptText": prompt[:1000],
|
| 203 |
-
"model": model_name,
|
| 204 |
-
"duration": duration,
|
| 205 |
-
"ratio": ratio,
|
| 206 |
-
}
|
| 207 |
-
|
| 208 |
-
method_type = "gen4_video_google_video" if image_input else "veo_google_video"
|
| 209 |
-
content = await self.get_from_cache(method_type, duration)
|
| 210 |
-
if content:
|
| 211 |
-
return json.loads(content)
|
| 212 |
-
|
| 213 |
-
if not image_input:
|
| 214 |
-
payload.pop("promptImage", None)
|
| 215 |
-
|
| 216 |
-
async with aiohttp.ClientSession() as session:
|
| 217 |
-
# Create task
|
| 218 |
-
async with session.post(
|
| 219 |
-
url, headers=headers, json=payload
|
| 220 |
-
) as response:
|
| 221 |
-
if response.status != 200:
|
| 222 |
-
error_text = await response.text()
|
| 223 |
-
# Log the full error for easier debugging
|
| 224 |
-
logger.error(f"RunwayML API Error Response: {error_text}")
|
| 225 |
-
raise Exception(f"RunwayML error: {error_text}")
|
| 226 |
-
|
| 227 |
-
task_data = await response.json()
|
| 228 |
-
task_id = task_data["id"]
|
| 229 |
-
logger.info(f"✓ Task created with {model_name}: {task_id}")
|
| 230 |
-
|
| 231 |
-
# Poll for completion
|
| 232 |
-
# task_id = "3b6d5a82-923f-4fa6-a7bc-4844de6e31e1"
|
| 233 |
-
max_attempts = 120
|
| 234 |
-
for _ in range(max_attempts):
|
| 235 |
-
await asyncio.sleep(10)
|
| 236 |
-
|
| 237 |
-
async with session.get(
|
| 238 |
-
f"https://api.dev.runwayml.com/v1/tasks/{task_id}", headers=headers
|
| 239 |
-
) as status_response:
|
| 240 |
-
status_data = await status_response.json()
|
| 241 |
-
status = status_data["status"]
|
| 242 |
-
|
| 243 |
-
if status == "SUCCEEDED":
|
| 244 |
-
video_url = status_data["output"][0]
|
| 245 |
-
logger.info(f"✅ Video generated with {model_name}: {video_url}")
|
| 246 |
-
video_result = {
|
| 247 |
-
"video_url": video_url,
|
| 248 |
-
"task_id": task_id,
|
| 249 |
-
"duration": duration,
|
| 250 |
-
"prompt": prompt,
|
| 251 |
-
"status": status,
|
| 252 |
-
"created_at": status_data.get("createdAt"),
|
| 253 |
-
"model": model_name,
|
| 254 |
-
}
|
| 255 |
-
await self.store_in_cache_file(method_type, json.dumps(video_result), duration)
|
| 256 |
-
return video_result
|
| 257 |
-
elif status == "FAILED":
|
| 258 |
-
raise Exception(f"Generation failed: {status_data.get('failure')}")
|
| 259 |
-
elif status == "RUNNING":
|
| 260 |
-
progress = status_data.get("progress", 0)
|
| 261 |
-
logger.info(f"⏳ Progress: {progress*100:.0f}%")
|
| 262 |
-
|
| 263 |
-
raise Exception("Timeout waiting for video generation")
|
| 264 |
-
|
| 265 |
-
except Exception as e:
|
| 266 |
-
logger.error(f"❌ Video generation error: {e}")
|
| 267 |
-
raise
|
| 268 |
|
| 269 |
|
| 270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|
|
|
| 103 |
|
| 104 |
|
| 105 |
|
src/automation.py
CHANGED
|
@@ -7,6 +7,7 @@ import os
|
|
| 7 |
import time
|
| 8 |
import json
|
| 9 |
from google_src import ai_studio_sdk
|
|
|
|
| 10 |
from typing import Dict, List, Optional, Any
|
| 11 |
from pathlib import Path
|
| 12 |
from api_clients import APIClients
|
|
@@ -477,7 +478,7 @@ class ContentAutomation:
|
|
| 477 |
upload_file_to_gcs(image_path)
|
| 478 |
|
| 479 |
# Step 3: Generate video using gen4_turbo
|
| 480 |
-
video_data = await
|
| 481 |
prompt=strategy["runway_prompt"], image_input=image_path, duration=strategy.get("duration", 3)
|
| 482 |
)
|
| 483 |
|
|
@@ -485,7 +486,7 @@ class ContentAutomation:
|
|
| 485 |
video_data["script"] = self.data_holder.tts_script
|
| 486 |
|
| 487 |
if os.getenv("USE_VEO", "false").lower() == "true":
|
| 488 |
-
veo_video_data = await
|
| 489 |
prompt=strategy["runway_veo_prompt"], duration=strategy.get("duration", 4)
|
| 490 |
)
|
| 491 |
video_data["veo_video_data"] = veo_video_data
|
|
|
|
| 7 |
import time
|
| 8 |
import json
|
| 9 |
from google_src import ai_studio_sdk
|
| 10 |
+
from video_generation_process import generate_video_process
|
| 11 |
from typing import Dict, List, Optional, Any
|
| 12 |
from pathlib import Path
|
| 13 |
from api_clients import APIClients
|
|
|
|
| 478 |
upload_file_to_gcs(image_path)
|
| 479 |
|
| 480 |
# Step 3: Generate video using gen4_turbo
|
| 481 |
+
video_data = await generate_video_process(
|
| 482 |
prompt=strategy["runway_prompt"], image_input=image_path, duration=strategy.get("duration", 3)
|
| 483 |
)
|
| 484 |
|
|
|
|
| 486 |
video_data["script"] = self.data_holder.tts_script
|
| 487 |
|
| 488 |
if os.getenv("USE_VEO", "false").lower() == "true":
|
| 489 |
+
veo_video_data = await generate_video_process(
|
| 490 |
prompt=strategy["runway_veo_prompt"], duration=strategy.get("duration", 4)
|
| 491 |
)
|
| 492 |
video_data["veo_video_data"] = veo_video_data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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import aiohttp
|
| 3 |
+
import asyncio
|
| 4 |
+
import base64
|
| 5 |
+
import logging
|
| 6 |
+
from typing import Dict, Tuple, Optional
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
def _get_api_key() -> str:
|
| 11 |
+
"""Retrieve RunwayML API key from environment variables."""
|
| 12 |
+
runway_api_key = os.getenv("RUNWAYML_API_KEY") or os.getenv("RUNWAY_2ND_API_KEY") or os.getenv("SPARK_KEY")
|
| 13 |
+
if not runway_api_key:
|
| 14 |
+
logger.error("RunwayML API key not found in environment variables.")
|
| 15 |
+
raise ValueError("RunwayML API key not found.")
|
| 16 |
+
return runway_api_key
|
| 17 |
+
|
| 18 |
+
def _get_headers(api_key: str) -> Dict[str, str]:
|
| 19 |
+
"""Construct headers for RunwayML API."""
|
| 20 |
+
return {
|
| 21 |
+
"Authorization": f"Bearer {api_key}",
|
| 22 |
+
"Content-Type": "application/json",
|
| 23 |
+
"X-Runway-Version": "2024-11-06",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
def _encode_image(image_input: str) -> str:
|
| 27 |
+
"""Encode local image to Base64 Data URI or return URL as is."""
|
| 28 |
+
if image_input.startswith("http"):
|
| 29 |
+
logger.info("Using provided image URL for RunwayML.")
|
| 30 |
+
return image_input
|
| 31 |
+
|
| 32 |
+
logger.info(f"Encoding local image {image_input} to Base64 Data URI.")
|
| 33 |
+
try:
|
| 34 |
+
image_type = os.path.splitext(image_input)[1].replace('.', '') # e.g., 'png'
|
| 35 |
+
with open(image_input, "rb") as image_file:
|
| 36 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 37 |
+
|
| 38 |
+
data_uri = f"data:image/{image_type};base64,{encoded_string}"
|
| 39 |
+
logger.info("✓ Successfully encoded image to Data URI.")
|
| 40 |
+
return data_uri
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.error(f"❌ Failed to encode local image to Base64: {e}")
|
| 43 |
+
raise
|
| 44 |
+
|
| 45 |
+
async def _submit_task(session: aiohttp.ClientSession, url: str, headers: Dict, payload: Dict, model_name: str) -> str:
|
| 46 |
+
"""Submit generation task to RunwayML API and return task ID."""
|
| 47 |
+
async with session.post(url, headers=headers, json=payload) as response:
|
| 48 |
+
if response.status != 200:
|
| 49 |
+
error_text = await response.text()
|
| 50 |
+
logger.error(f"RunwayML API Error Response: {error_text}")
|
| 51 |
+
raise Exception(f"RunwayML error: {error_text}")
|
| 52 |
+
|
| 53 |
+
task_data = await response.json()
|
| 54 |
+
task_id = task_data["id"]
|
| 55 |
+
logger.info(f"✓ Task created with {model_name}: {task_id}")
|
| 56 |
+
return task_id
|
| 57 |
+
|
| 58 |
+
async def _poll_task(session: aiohttp.ClientSession, task_id: str, headers: Dict, model_name: str) -> Dict:
|
| 59 |
+
"""Poll RunwayML task until completion."""
|
| 60 |
+
max_attempts = 120
|
| 61 |
+
for _ in range(max_attempts):
|
| 62 |
+
await asyncio.sleep(10)
|
| 63 |
+
|
| 64 |
+
async with session.get(
|
| 65 |
+
f"https://api.dev.runwayml.com/v1/tasks/{task_id}", headers=headers
|
| 66 |
+
) as status_response:
|
| 67 |
+
status_data = await status_response.json()
|
| 68 |
+
status = status_data["status"]
|
| 69 |
+
|
| 70 |
+
if status == "SUCCEEDED":
|
| 71 |
+
video_url = status_data["output"][0]
|
| 72 |
+
logger.info(f"✅ Video generated with {model_name}: {video_url}")
|
| 73 |
+
return {
|
| 74 |
+
"video_url": video_url,
|
| 75 |
+
"status": status,
|
| 76 |
+
"created_at": status_data.get("createdAt"),
|
| 77 |
+
}
|
| 78 |
+
elif status == "FAILED":
|
| 79 |
+
raise Exception(f"Generation failed: {status_data.get('failure')}")
|
| 80 |
+
elif status == "RUNNING":
|
| 81 |
+
progress = status_data.get("progress", 0)
|
| 82 |
+
logger.info(f"⏳ Progress: {progress*100:.0f}%")
|
| 83 |
+
|
| 84 |
+
raise Exception("Timeout waiting for video generation")
|
| 85 |
+
|
| 86 |
+
async def _handle_image_to_video(
|
| 87 |
+
session: aiohttp.ClientSession,
|
| 88 |
+
headers: Dict,
|
| 89 |
+
prompt: str,
|
| 90 |
+
duration: int,
|
| 91 |
+
image_input: str
|
| 92 |
+
) -> Tuple[str, str, int]:
|
| 93 |
+
"""Handle Image-to-Video generation workflow."""
|
| 94 |
+
model_name = "gen4_turbo"
|
| 95 |
+
ratio = "720:1280"
|
| 96 |
+
url = "https://api.dev.runwayml.com/v1/image_to_video"
|
| 97 |
+
|
| 98 |
+
prompt_image_value = _encode_image(image_input)
|
| 99 |
+
|
| 100 |
+
payload = {
|
| 101 |
+
"promptImage": prompt_image_value,
|
| 102 |
+
"promptText": prompt[:1000],
|
| 103 |
+
"model": model_name,
|
| 104 |
+
"duration": duration,
|
| 105 |
+
"ratio": ratio,
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
task_id = await _submit_task(session, url, headers, payload, model_name)
|
| 109 |
+
return task_id, model_name, duration
|
| 110 |
+
|
| 111 |
+
async def _handle_text_to_video(
|
| 112 |
+
session: aiohttp.ClientSession,
|
| 113 |
+
headers: Dict,
|
| 114 |
+
prompt: str
|
| 115 |
+
) -> Tuple[str, str, int]:
|
| 116 |
+
"""Handle Text-to-Video generation workflow."""
|
| 117 |
+
model_name = "veo3.1_fast"
|
| 118 |
+
duration = 4 # Fixed duration for Veo text-to-video for now
|
| 119 |
+
url = "https://api.dev.runwayml.com/v1/text_to_video"
|
| 120 |
+
|
| 121 |
+
payload = {
|
| 122 |
+
"promptText": prompt[:1000],
|
| 123 |
+
"model": model_name,
|
| 124 |
+
"duration": duration,
|
| 125 |
+
# "ratio": "1080:1920" # Note: Ratio not supported for Veo currently
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
task_id = await _submit_task(session, url, headers, payload, model_name)
|
| 129 |
+
return task_id, model_name, duration
|
| 130 |
+
|
| 131 |
+
async def generate_video_runway(prompt: str, duration: int, image_input: str = None) -> Dict:
|
| 132 |
+
"""
|
| 133 |
+
Generate video using RunwayML (Text-to-Video or Image-to-Video).
|
| 134 |
+
|
| 135 |
+
Args:
|
| 136 |
+
prompt: Text prompt for video generation
|
| 137 |
+
duration: Video duration in seconds (may be overridden by specific models)
|
| 138 |
+
image_input: Optional HTTPS URL or local file path to an image.
|
| 139 |
+
"""
|
| 140 |
+
try:
|
| 141 |
+
api_key = _get_api_key()
|
| 142 |
+
headers = _get_headers(api_key)
|
| 143 |
+
|
| 144 |
+
async with aiohttp.ClientSession() as session:
|
| 145 |
+
if image_input:
|
| 146 |
+
task_id, model_name, final_duration = await _handle_image_to_video(
|
| 147 |
+
session, headers, prompt, duration, image_input
|
| 148 |
+
)
|
| 149 |
+
else:
|
| 150 |
+
task_id, model_name, final_duration = await _handle_text_to_video(
|
| 151 |
+
session, headers, prompt
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
result_data = await _poll_task(session, task_id, headers, model_name)
|
| 155 |
+
|
| 156 |
+
return {
|
| 157 |
+
"video_url": result_data["video_url"],
|
| 158 |
+
"task_id": task_id,
|
| 159 |
+
"duration": final_duration,
|
| 160 |
+
"prompt": prompt,
|
| 161 |
+
"status": result_data["status"],
|
| 162 |
+
"created_at": result_data["created_at"],
|
| 163 |
+
"model": model_name,
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
logger.error(f"❌ Video generation error: {e}")
|
| 168 |
+
raise
|
src/video_generation_process.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import json
|
| 5 |
+
import logging
|
| 6 |
+
from typing import Dict, Optional
|
| 7 |
+
|
| 8 |
+
from google_src import ai_studio_sdk
|
| 9 |
+
from runwayml.generate_video import generate_video_runway
|
| 10 |
+
from utils import logger
|
| 11 |
+
from google_src.gcs_utils import upload_file_to_gcs
|
| 12 |
+
|
| 13 |
+
async def generate_video_process(prompt: str, duration: int, image_input: str = None) -> Dict:
|
| 14 |
+
"""
|
| 15 |
+
Orchestrate video generation:
|
| 16 |
+
1. Check if TEST_AUTOMATION is on -> return mock data.
|
| 17 |
+
2. Check USE_GEMIMI_VIDEO -> use ai_studio_sdk.
|
| 18 |
+
3. Else -> use RunwayML.
|
| 19 |
+
|
| 20 |
+
Handles caching implicitly via caller or here if needed (previously cached in api_clients was complex,
|
| 21 |
+
but automation usually re-checks cache. We can reimplement simple caching here or rely on the fact
|
| 22 |
+
that the logic is now streamlined).
|
| 23 |
+
|
| 24 |
+
The original api_clients code had extensive caching using APIClients.store_in_cache which uploaded to GCS.
|
| 25 |
+
We should probably return the result and let the caller handle it or replicate the upload if needed for
|
| 26 |
+
consistency with 'video_url' in result.
|
| 27 |
+
|
| 28 |
+
RunwayML returns a public URL. Gemini SDK returns a local path, so we upload it to GCS to get a URL,
|
| 29 |
+
making the result format consistent.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
# 1. Test Mode
|
| 33 |
+
if os.getenv("TEST_AUTOMATION", "").lower() == "true":
|
| 34 |
+
if image_input:
|
| 35 |
+
return {
|
| 36 |
+
"task_id": "644319db-5226-42cf-b45f-5388e40d38a6",
|
| 37 |
+
"video_url": f"{os.getenv('TEST_DATA_DIRECTORY')}/image-to-video.mp4",
|
| 38 |
+
"local_path": f"{os.getenv('TEST_DATA_DIRECTORY')}/image-to-video.mp4",
|
| 39 |
+
"duration": 3,
|
| 40 |
+
"prompt": prompt,
|
| 41 |
+
"status": "SUCCEEDED",
|
| 42 |
+
"created_at": "2025-10-15T12:39:24.279Z",
|
| 43 |
+
"model": "veo3.1_fast",
|
| 44 |
+
}
|
| 45 |
+
else:
|
| 46 |
+
return {
|
| 47 |
+
"task_id": "644319db-5226-42cf-b45f-5388e40d38a6",
|
| 48 |
+
"video_url": f"{os.getenv('TEST_DATA_DIRECTORY')}/veo_text_to_video.mp4",
|
| 49 |
+
"local_path": f"{os.getenv('TEST_DATA_DIRECTORY')}/veo_text_to_video.mp4",
|
| 50 |
+
"duration": 3,
|
| 51 |
+
"prompt": prompt,
|
| 52 |
+
"status": "SUCCEEDED",
|
| 53 |
+
"created_at": "2025-10-15T12:39:24.279Z",
|
| 54 |
+
"model": "gen4_turbo",
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# 2. Gemini / Veo
|
| 58 |
+
if os.getenv("USE_GEMIMI_VIDEO", "false").lower() == "true":
|
| 59 |
+
logger.info("Using Gemini SDK for video generation...")
|
| 60 |
+
model_name = "veo3.1_fast" # implied default from context
|
| 61 |
+
|
| 62 |
+
# Original code checked cache here. We'll simplify: generate -> upload -> return.
|
| 63 |
+
output_path = f'/tmp/video_{duration}_{model_name}_{uuid.uuid4().hex[:8]}.mp4'
|
| 64 |
+
ai_studio_sdk.generate_video(prompt, output_path, image_input)
|
| 65 |
+
|
| 66 |
+
# Upload to GCS to get a URL to match expectations
|
| 67 |
+
upload_result = upload_file_to_gcs(output_path)
|
| 68 |
+
video_url = upload_result.get('url')
|
| 69 |
+
|
| 70 |
+
video_result = {
|
| 71 |
+
"local_path": output_path,
|
| 72 |
+
"video_url": video_url,
|
| 73 |
+
"task_id": None,
|
| 74 |
+
"duration": duration,
|
| 75 |
+
"prompt": prompt,
|
| 76 |
+
"status": "success",
|
| 77 |
+
"created_at": None,
|
| 78 |
+
"model": model_name,
|
| 79 |
+
}
|
| 80 |
+
return video_result
|
| 81 |
+
|
| 82 |
+
# 3. RunwayML
|
| 83 |
+
logger.info("Using RunwayML for video generation...")
|
| 84 |
+
return await generate_video_runway(prompt, duration, image_input)
|