-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsglang_client.py
More file actions
351 lines (286 loc) · 12.4 KB
/
sglang_client.py
File metadata and controls
351 lines (286 loc) · 12.4 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
"""
SGLang API Client for Video Generation
Provides a Python client for interacting with SGLang video generation API.
Uses requests library for direct API communication.
"""
import os
import requests
import time
from pathlib import Path
from typing import Dict, Any, Optional, List
from dataclasses import dataclass
from config import SGLANG_SERVERS
@dataclass
class VideoTask:
"""Represents a video generation task."""
id: str
status: str
progress: Optional[int] = None
created_at: Optional[int] = None
completed_at: Optional[int] = None
download_url: Optional[str] = None
video_url: Optional[str] = None # Alias for download_url, some APIs use this field name
file_path: Optional[str] = None # Local file path on server
inference_time_s: Optional[float] = None
peak_memory_mb: Optional[float] = None
error: Optional[str] = None
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'VideoTask':
"""Create VideoTask from API response dictionary."""
# Handle both video_url and download_url fields
download_url = data.get('download_url') or data.get('video_url')
return cls(
id=data.get('id', ''),
status=data.get('status', 'unknown'),
progress=data.get('progress'),
created_at=data.get('created_at'),
completed_at=data.get('completed_at'),
download_url=download_url,
video_url=download_url, # Set both for compatibility
file_path=data.get('file_path'), # Local file path on server
inference_time_s=data.get('inference_time_s'),
peak_memory_mb=data.get('peak_memory_mb'),
error=data.get('error')
)
class SGLangClient:
"""Client for SGLang video generation API."""
def __init__(self, server_key: str):
"""
Initialize SGLang client.
Args:
server_key: Key for server configuration (e.g., "mova-360p")
"""
if server_key not in SGLANG_SERVERS:
raise ValueError(f"Unknown server key: {server_key}. Available: {list(SGLANG_SERVERS.keys())}")
self.server_config = SGLANG_SERVERS[server_key]
self.base_url = self.server_config['base_url'].rstrip('/')
def _parse_task_response(self, result) -> VideoTask:
"""Parse API response (list or dict) into VideoTask."""
if isinstance(result, list) and len(result) > 0:
task_data = result[0]
elif isinstance(result, dict):
task_data = result
else:
raise ValueError(f"Unexpected API response format: {result}")
return VideoTask.from_dict(task_data)
def _normalize_download_url(self, url: str) -> str:
"""Remove double slashes in path (e.g. //v1 -> /v1)."""
if '://' not in url:
return url
protocol, path = url.split('://', 1)
path = '/' + path.lstrip('/')
return f"{protocol}://{path}"
def _get_headers(self) -> Dict[str, str]:
"""Get HTTP headers for API requests."""
return {}
def submit_video_task(
self,
prompt: str,
image_path: str,
size: str = "640x360",
num_frames: int = 193,
fps: int = 24,
seed: Optional[int] = None,
guidance_scale: float = 5.0,
num_inference_steps: int = 50
) -> VideoTask:
"""
Submit a video generation task.
Args:
prompt: Text prompt describing the video
image_path: Path to the first frame image
size: Video size (e.g., "640x360" for 360p landscape)
num_frames: Number of frames to generate
fps: Frames per second
seed: Random seed for reproducibility (None or 0 means random)
guidance_scale: Classifier-free guidance scale
num_inference_steps: Number of denoising steps
Returns:
VideoTask object with task ID and initial status
Raises:
FileNotFoundError: If image_path doesn't exist
requests.HTTPError: If API request fails
"""
# Verify image exists
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image file not found: {image_path}")
# Prepare multipart form data
url = f"{self.base_url}/v1/videos"
# Read image file
with open(image_path, 'rb') as f:
files = {
'input_reference': (os.path.basename(image_path), f, 'image/png')
}
# Only pass num_frames, not seconds, to let server use num_frames directly
# According to SGLang API: if num_frames is provided, it takes priority
data = {
'prompt': prompt,
'size': size,
'num_frames': str(num_frames), # Primary parameter - should take priority
'fps': str(fps),
'guidance_scale': str(guidance_scale),
'num_inference_steps': str(num_inference_steps)
}
# Generate random seed if seed is None or 0
if seed is None or seed == 0:
import random
seed = random.randint(1, 2**31 - 1) # Generate random seed
data['seed'] = str(seed)
response = requests.post(
url,
headers=self._get_headers(),
data=data,
files=files,
timeout=60
)
response.raise_for_status()
return self._parse_task_response(response.json())
def get_task_status(self, task_id: str) -> VideoTask:
"""
Get the status of a video generation task.
Args:
task_id: Task ID returned from submit_video_task
Returns:
VideoTask object with current status
Raises:
requests.HTTPError: If API request fails
"""
url = f"{self.base_url}/v1/videos/{task_id}"
response = requests.get(url, headers=self._get_headers(), timeout=30)
response.raise_for_status()
return self._parse_task_response(response.json())
def get_download_url(self, task_id: str) -> str:
"""
Get the download URL for a completed video.
Args:
task_id: Task ID
Returns:
Download URL string
Raises:
requests.HTTPError: If API request fails
"""
task = self.get_task_status(task_id)
if task.download_url:
return self._normalize_download_url(task.download_url)
return f"{self.base_url}/v1/videos/{task_id}/download"
def download_video(self, task_id: str, save_path: str) -> str:
"""
Download a completed video to local path.
First tries to copy from local file_path if available, otherwise downloads via HTTP.
Args:
task_id: Task ID
save_path: Local path to save the video file
Returns:
Path to the downloaded video file
Raises:
requests.HTTPError: If download fails
FileNotFoundError: If save directory doesn't exist or source file doesn't exist
"""
# Ensure save directory exists
save_dir = Path(save_path).parent
save_dir.mkdir(parents=True, exist_ok=True)
# Get task status to check for file_path
task = self.get_task_status(task_id)
# If file_path is available and file exists, copy from local filesystem
if task.file_path and Path(task.file_path).exists():
try:
import shutil
print(f"Copying video from local file: {task.file_path}")
shutil.copy2(task.file_path, save_path)
# Verify file was copied
if not Path(save_path).exists() or Path(save_path).stat().st_size == 0:
raise IOError(f"Copied file is empty or doesn't exist: {save_path}")
file_size = Path(save_path).stat().st_size
print(f"Successfully copied video to {save_path} ({file_size} bytes)")
return save_path
except Exception as copy_error:
print(f"Failed to copy from local file_path: {copy_error}, trying HTTP download...")
# Fall through to HTTP download
# If no file_path or copy failed, try HTTP download
download_url = self.get_download_url(task_id)
try:
response = requests.get(
download_url,
headers=self._get_headers(),
stream=True,
timeout=300 # 5 minutes for large videos
)
response.raise_for_status()
# Check content type
content_type = response.headers.get('content-type', '')
if 'video' not in content_type and 'octet-stream' not in content_type:
print(f"Warning: Unexpected content type: {content_type}")
# Save to file
total_size = 0
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
total_size += len(chunk)
# Verify file was written
if not Path(save_path).exists() or Path(save_path).stat().st_size == 0:
raise IOError(f"Downloaded file is empty or doesn't exist: {save_path}")
print(f"Successfully downloaded video to {save_path} ({total_size} bytes)")
return save_path
except requests.exceptions.RequestException as e:
error_msg = f"HTTP error downloading video: {e}"
if hasattr(e, 'response') and e.response is not None:
error_msg += f" (Status: {e.response.status_code}, Response: {e.response.text[:200]})"
raise RuntimeError(error_msg) from e
except Exception as e:
raise RuntimeError(f"Error downloading video: {str(e)}") from e
def list_videos(self, limit: Optional[int] = None) -> List[VideoTask]:
"""
List all video generation tasks.
Args:
limit: Maximum number of tasks to return
Returns:
List of VideoTask objects
Raises:
requests.HTTPError: If API request fails
"""
url = f"{self.base_url}/v1/videos"
params = {}
if limit is not None:
params['limit'] = limit
response = requests.get(url, headers=self._get_headers(), params=params, timeout=30)
response.raise_for_status()
result = response.json()
# Handle list response
if isinstance(result, dict) and 'data' in result:
tasks_data = result['data']
elif isinstance(result, list):
tasks_data = result
else:
raise ValueError(f"Unexpected API response format: {result}")
return [VideoTask.from_dict(task_data) for task_data in tasks_data]
def wait_for_completion(
self,
task_id: str,
poll_interval: int = 5,
timeout: int = 1800,
callback: Optional[callable] = None
) -> VideoTask:
"""
Wait for a video generation task to complete.
Args:
task_id: Task ID
poll_interval: Seconds between status checks
timeout: Maximum seconds to wait
callback: Optional callback function(task: VideoTask) called on each poll
Returns:
Completed VideoTask
Raises:
TimeoutError: If task doesn't complete within timeout
"""
start_time = time.time()
while True:
task = self.get_task_status(task_id)
if callback:
callback(task)
if task.status in ('completed', 'failed', 'error'):
return task
if time.time() - start_time > timeout:
raise TimeoutError(f"Task {task_id} did not complete within {timeout} seconds")
time.sleep(poll_interval)