topcoderkz
commited on
Commit
Β·
e598b7d
1
Parent(s):
4efa307
Refactor code, remove deepseek integration
Browse files- src/api_clients.py +85 -85
src/api_clients.py
CHANGED
|
@@ -82,29 +82,29 @@ class APIClients:
|
|
| 82 |
Local path to generated image or None
|
| 83 |
"""
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
output_path = '/Users/topcoderkz/Downloads/gen4-ultra.png'
|
| 108 |
logger.info(f"β Image generated with Imagen 4 Ultra: {output_path}")
|
| 109 |
return output_path
|
| 110 |
|
|
@@ -190,76 +190,76 @@ class APIClients:
|
|
| 190 |
"""
|
| 191 |
try:
|
| 192 |
logger.info(f"π¬ Generating video with gen4_turbo: {prompt[:100]}...")
|
| 193 |
-
return {
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
"duration": duration,
|
| 197 |
-
"prompt": prompt,
|
| 198 |
-
"status": 'SUCCEEDED',
|
| 199 |
-
"created_at": '2025-10-12T18:57:27.240Z',
|
| 200 |
-
"model": "gen4_turbo"
|
| 201 |
-
}
|
| 202 |
-
|
| 203 |
-
# headers = {
|
| 204 |
-
# "Authorization": f"Bearer {self.runway_api_key}",
|
| 205 |
-
# "Content-Type": "application/json",
|
| 206 |
-
# "X-Runway-Version": "2024-11-06",
|
| 207 |
-
# }
|
| 208 |
-
|
| 209 |
-
# payload = {
|
| 210 |
-
# "promptImage": image_url,
|
| 211 |
-
# "promptText": prompt[:1000],
|
| 212 |
-
# "model": "gen4_turbo", # Updated to gen4_turbo ($0.25/video)
|
| 213 |
# "duration": duration,
|
| 214 |
-
# "
|
|
|
|
|
|
|
|
|
|
| 215 |
# }
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
except Exception as e:
|
| 265 |
logger.error(f"β Video generation error: {e}")
|
|
|
|
| 82 |
Local path to generated image or None
|
| 83 |
"""
|
| 84 |
try:
|
| 85 |
+
import vertexai
|
| 86 |
+
from vertexai.preview.vision_models import ImageGenerationModel
|
| 87 |
|
| 88 |
+
logger.info(f"π¨ Generating image with Imagen 4 Ultra: {prompt[:200]}...")
|
| 89 |
|
| 90 |
+
vertexai.init(project=self.config.get("gcp_project_id"), location="us-central1")
|
| 91 |
|
| 92 |
+
# Use correct Imagen 4 Ultra model name
|
| 93 |
+
model = ImageGenerationModel.from_pretrained("imagen-4.0-ultra-generate-001")
|
| 94 |
|
| 95 |
+
images = model.generate_images(
|
| 96 |
+
prompt=prompt,
|
| 97 |
+
number_of_images=1,
|
| 98 |
+
aspect_ratio="9:16", # Vertical for TikTok/Instagram
|
| 99 |
+
safety_filter_level="block_some",
|
| 100 |
+
person_generation="allow_adult",
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
# Save to temp file
|
| 104 |
+
import tempfile
|
| 105 |
+
output_path = f"/tmp/hook_image_{hash(prompt)}.png"
|
| 106 |
+
images[0].save(location=output_path, include_generation_parameters=False)
|
| 107 |
+
# output_path = '/Users/topcoderkz/Downloads/gen4-ultra.png'
|
| 108 |
logger.info(f"β Image generated with Imagen 4 Ultra: {output_path}")
|
| 109 |
return output_path
|
| 110 |
|
|
|
|
| 190 |
"""
|
| 191 |
try:
|
| 192 |
logger.info(f"π¬ Generating video with gen4_turbo: {prompt[:100]}...")
|
| 193 |
+
# return {
|
| 194 |
+
# "video_url": 'https://dnznrvs05pmza.cloudfront.net/764d8b31-4e1f-4ba2-bf4f-360cf029e0b7.mp4?_jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXlIYXNoIjoiMGIyZjMyMzc5NDA4ZTU0NCIsImJ1Y2tldCI6InJ1bndheS10YXNrLWFydGlmYWN0cyIsInN0YWdlIjoicHJvZCIsImV4cCI6MTc2MDQwMDAwMH0.5ltZPfO-gWilm_lt6sK-tPvgwJUgPluOjeeUOOIbyEE',
|
| 195 |
+
# "task_id": '08fbc334-7d36-45c2-8b71-7f20fa075f10',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
# "duration": duration,
|
| 197 |
+
# "prompt": prompt,
|
| 198 |
+
# "status": 'SUCCEEDED',
|
| 199 |
+
# "created_at": '2025-10-12T18:57:27.240Z',
|
| 200 |
+
# "model": "gen4_turbo"
|
| 201 |
# }
|
| 202 |
|
| 203 |
+
headers = {
|
| 204 |
+
"Authorization": f"Bearer {self.runway_api_key}",
|
| 205 |
+
"Content-Type": "application/json",
|
| 206 |
+
"X-Runway-Version": "2024-11-06",
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
payload = {
|
| 210 |
+
"promptImage": image_url,
|
| 211 |
+
"promptText": prompt[:1000],
|
| 212 |
+
"model": "gen4_turbo", # Updated to gen4_turbo ($0.25/video)
|
| 213 |
+
"duration": duration,
|
| 214 |
+
"ratio": "1280:720"
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
async with aiohttp.ClientSession() as session:
|
| 218 |
+
# Create task
|
| 219 |
+
async with session.post(
|
| 220 |
+
"https://api.dev.runwayml.com/v1/image_to_video",
|
| 221 |
+
headers=headers,
|
| 222 |
+
json=payload
|
| 223 |
+
) as response:
|
| 224 |
+
if response.status != 200:
|
| 225 |
+
error_text = await response.text()
|
| 226 |
+
raise Exception(f"RunwayML error: {error_text}")
|
| 227 |
+
|
| 228 |
+
task_data = await response.json()
|
| 229 |
+
task_id = task_data["id"]
|
| 230 |
+
logger.info(f"β Task created with gen4_turbo: {task_id}")
|
| 231 |
+
|
| 232 |
+
# Poll for completion
|
| 233 |
+
max_attempts = 120
|
| 234 |
+
for attempt 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}",
|
| 239 |
+
headers=headers
|
| 240 |
+
) as status_response:
|
| 241 |
+
status_data = await status_response.json()
|
| 242 |
+
status = status_data["status"]
|
| 243 |
+
|
| 244 |
+
if status == "SUCCEEDED":
|
| 245 |
+
video_url = status_data["output"][0]
|
| 246 |
+
logger.info(f"β
Video generated with gen4_turbo: {video_url}")
|
| 247 |
+
return {
|
| 248 |
+
"video_url": video_url,
|
| 249 |
+
"task_id": task_id,
|
| 250 |
+
"duration": duration,
|
| 251 |
+
"prompt": prompt,
|
| 252 |
+
"status": status,
|
| 253 |
+
"created_at": status_data.get("createdAt"),
|
| 254 |
+
"model": "gen4_turbo"
|
| 255 |
+
}
|
| 256 |
+
elif status == "FAILED":
|
| 257 |
+
raise Exception(f"Generation failed: {status_data.get('failure')}")
|
| 258 |
+
elif status == "RUNNING":
|
| 259 |
+
progress = status_data.get("progress", 0)
|
| 260 |
+
logger.info(f"β³ Progress: {progress*100:.0f}%")
|
| 261 |
+
|
| 262 |
+
raise Exception("Timeout waiting for video generation")
|
| 263 |
|
| 264 |
except Exception as e:
|
| 265 |
logger.error(f"β Video generation error: {e}")
|