-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathvideo_generation.py
More file actions
65 lines (48 loc) · 1.84 KB
/
Copy pathvideo_generation.py
File metadata and controls
65 lines (48 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Text-to-video task creation and polling example for Agnes AI."""
import os
import time
from typing import Any
import requests
API_KEY = os.environ["AGNES_API_KEY"]
API_ROOT = "https://apihub.agnes-ai.com"
def request_json(method: str, url: str, **kwargs: Any) -> dict[str, Any]:
headers = kwargs.pop("headers", {})
headers["Authorization"] = f"Bearer {API_KEY}"
headers["Content-Type"] = "application/json"
response = requests.request(method, url, headers=headers, timeout=60, **kwargs)
response.raise_for_status()
return response.json()
def create_video() -> str:
payload = {
"model": "agnes-video-v2.0",
"prompt": (
"A cinematic product shot of a glowing AI model catalog interface, "
"smooth camera movement, realistic lighting"
),
"height": 768,
"width": 1152,
"num_frames": 121,
"frame_rate": 24,
}
data = request_json("POST", f"{API_ROOT}/v1/videos", json=payload)
video_id = data.get("video_id") or data.get("id")
if not video_id:
raise RuntimeError(f"Video response did not include video_id: {data}")
return video_id
def poll_video(video_id: str) -> dict[str, Any]:
for _ in range(60):
data = request_json("GET", f"{API_ROOT}/agnesapi", params={"video_id": video_id})
status = str(data.get("status", "")).lower()
if status in {"succeeded", "success", "completed", "done"}:
return data
if status in {"failed", "error", "cancelled"}:
raise RuntimeError(f"Video generation failed: {data}")
time.sleep(5)
raise TimeoutError(f"Timed out waiting for video_id={video_id}")
def main() -> None:
video_id = create_video()
print(f"Created video task: {video_id}")
result = poll_video(video_id)
print(result)
if __name__ == "__main__":
main()