-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_video_client.py
More file actions
246 lines (212 loc) · 7.73 KB
/
generate_video_client.py
File metadata and controls
246 lines (212 loc) · 7.73 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
import base64
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, Iterable, Optional, Tuple
import requests
from PIL import Image
RUNPOD_API_BASE = os.environ.get("RUNPOD_API_BASE", "https://api.runpod.ai/v2")
def _strip_data_url_prefix(data_url: str) -> str:
if "," in data_url and data_url.strip().lower().startswith("data:"):
return data_url.split(",", 1)[1]
return data_url
def _encode_image_to_base64(path: Path) -> str:
img = Image.open(path).convert("RGB")
from io import BytesIO
buf = BytesIO()
img.save(buf, format="PNG")
b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
return f"data:image/png;base64,{b64}"
@dataclass
class GenerateVideoClient:
"""
Simple HTTP client for the LTX-2 RunPod Serverless endpoint.
It uses the RunPod v2 REST API directly instead of the Python SDK, to keep
the template dependency-light.
"""
runpod_endpoint_id: str
runpod_api_key: str
api_base: str = RUNPOD_API_BASE
def _runsync(self, input_payload: Dict[str, Any]) -> Dict[str, Any]:
"""
Call the endpoint using the synchronous runsync API.
"""
url = f"{self.api_base.rstrip('/')}/{self.runpod_endpoint_id}/runsync"
headers = {
"Authorization": f"Bearer {self.runpod_api_key}",
"Content-Type": "application/json",
}
body = {"input": input_payload}
resp = requests.post(url, json=body, headers=headers, timeout=600)
resp.raise_for_status()
data = resp.json()
# RunPod wraps the handler's return value under "output".
return data.get("output") or data
# --------------------------------------------------------------------- #
# Public methods
# --------------------------------------------------------------------- #
def create_video_t2v(
self,
prompt: str,
negative_prompt: Optional[str] = None,
width: Optional[int] = None,
height: Optional[int] = None,
num_frames: Optional[int] = None,
fps: Optional[float] = None,
steps: Optional[int] = None,
seed: Optional[int] = None,
cfg: Optional[float] = None,
ckpt_name: Optional[str] = None,
extra: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Text-to-video generation (mode='t2v').
"""
if not prompt:
raise ValueError("prompt is required")
payload: Dict[str, Any] = {
"mode": "t2v",
"prompt": prompt,
}
if negative_prompt:
payload["negative_prompt"] = negative_prompt
if width is not None:
payload["width"] = width
if height is not None:
payload["height"] = height
if num_frames is not None:
payload["num_frames"] = num_frames
if fps is not None:
payload["fps"] = fps
if steps is not None:
payload["steps"] = steps
if seed is not None:
payload["seed"] = seed
if cfg is not None:
payload["cfg"] = cfg
if ckpt_name is not None:
payload["ckpt_name"] = ckpt_name
if extra:
payload.update(extra)
return self._runsync(payload)
def create_video_i2v(
self,
image_path: Optional[str] = None,
image_url: Optional[str] = None,
image_base64: Optional[str] = None,
prompt: str = "",
negative_prompt: Optional[str] = None,
width: Optional[int] = None,
height: Optional[int] = None,
num_frames: Optional[int] = None,
fps: Optional[float] = None,
steps: Optional[int] = None,
seed: Optional[int] = None,
cfg: Optional[float] = None,
ckpt_name: Optional[str] = None,
extra: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""
Image-to-video generation (mode='i2v').
Provide exactly one of:
- image_path
- image_url
- image_base64
"""
if not prompt:
raise ValueError("prompt is required")
provided = [v for v in (image_path, image_url, image_base64) if v]
if len(provided) != 1:
raise ValueError("Provide exactly one of image_path, image_url, image_base64")
payload: Dict[str, Any] = {
"mode": "i2v",
"prompt": prompt,
}
if image_path:
payload["image_base64"] = _encode_image_to_base64(Path(image_path))
elif image_url:
payload["image_url"] = image_url
elif image_base64:
payload["image_base64"] = image_base64
if negative_prompt:
payload["negative_prompt"] = negative_prompt
if width is not None:
payload["width"] = width
if height is not None:
payload["height"] = height
if num_frames is not None:
payload["num_frames"] = num_frames
if fps is not None:
payload["fps"] = fps
if steps is not None:
payload["steps"] = steps
if seed is not None:
payload["seed"] = seed
if cfg is not None:
payload["cfg"] = cfg
if ckpt_name is not None:
payload["ckpt_name"] = ckpt_name
if extra:
payload.update(extra)
return self._runsync(payload)
def batch_process_images(
self,
image_folder_path: str,
output_folder_path: str,
prompt: str,
negative_prompt: Optional[str] = None,
valid_extensions: Tuple[str, ...] = (".jpg", ".jpeg", ".png", ".bmp", ".tiff"),
**kwargs: Any,
) -> Dict[str, Any]:
"""
Convenience helper to process a folder of images with mode='i2v'.
"""
src_dir = Path(image_folder_path)
out_dir = Path(output_folder_path)
out_dir.mkdir(parents=True, exist_ok=True)
files: Iterable[Path] = sorted(
p for p in src_dir.iterdir() if p.is_file() and p.suffix.lower() in valid_extensions
)
total = 0
successful = 0
errors: Dict[str, str] = {}
for img_path in files:
total += 1
try:
result = self.create_video_i2v(
image_path=str(img_path),
prompt=prompt,
negative_prompt=negative_prompt,
**kwargs,
)
if "video" not in result:
errors[img_path.name] = f"no video field in result: {result}"
continue
out_file = out_dir / f"{img_path.stem}.mp4"
self.save_video_result(result, str(out_file))
successful += 1
except Exception as exc: # noqa: BLE001
errors[img_path.name] = str(exc)
return {
"total_files": total,
"successful": successful,
"errors": errors,
}
@staticmethod
def save_video_result(result: Dict[str, Any], output_path: str) -> None:
"""
Save the video Base64 from a handler result to disk.
Accepts either:
- {"video": "data:video/mp4;base64,..."}
- {"output": {"video": "data:video/mp4;base64,..."}}
"""
video_str = result.get("video")
if video_str is None and isinstance(result.get("output"), dict):
video_str = result["output"].get("video")
if not video_str:
raise ValueError("Result does not contain a 'video' field.")
raw_b64 = _strip_data_url_prefix(video_str)
data = base64.b64decode(raw_b64)
out_path = Path(output_path)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_bytes(data)