deapi-mcp 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deapi_mcp/__init__.py +24 -0
- deapi_mcp/auth.py +33 -0
- deapi_mcp/auth_code_store.py +226 -0
- deapi_mcp/config.py +297 -0
- deapi_mcp/consent.py +549 -0
- deapi_mcp/deapi_client.py +466 -0
- deapi_mcp/fastmcp_auth.py +227 -0
- deapi_mcp/key_verifier.py +225 -0
- deapi_mcp/landing.py +431 -0
- deapi_mcp/middleware.py +756 -0
- deapi_mcp/oauth_clients.py +443 -0
- deapi_mcp/oauth_endpoints.py +973 -0
- deapi_mcp/observability.py +199 -0
- deapi_mcp/polling_manager.py +319 -0
- deapi_mcp/py.typed +0 -0
- deapi_mcp/schemas.py +397 -0
- deapi_mcp/server_remote.py +416 -0
- deapi_mcp/tools/__init__.py +0 -0
- deapi_mcp/tools/_price_helpers.py +167 -0
- deapi_mcp/tools/_responses.py +157 -0
- deapi_mcp/tools/audio.py +1195 -0
- deapi_mcp/tools/embedding.py +102 -0
- deapi_mcp/tools/image.py +884 -0
- deapi_mcp/tools/prompt.py +155 -0
- deapi_mcp/tools/utility.py +230 -0
- deapi_mcp/tools/video.py +1117 -0
- deapi_mcp/utils.py +666 -0
- deapi_mcp/webhooks.py +349 -0
- deapi_mcp-0.7.0.dist-info/METADATA +642 -0
- deapi_mcp-0.7.0.dist-info/RECORD +33 -0
- deapi_mcp-0.7.0.dist-info/WHEEL +4 -0
- deapi_mcp-0.7.0.dist-info/entry_points.txt +2 -0
- deapi_mcp-0.7.0.dist-info/licenses/LICENSE +21 -0
deapi_mcp/tools/video.py
ADDED
|
@@ -0,0 +1,1117 @@
|
|
|
1
|
+
"""Video generation tools for deAPI MCP server."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated, Any, List, Literal, Optional
|
|
4
|
+
|
|
5
|
+
from pydantic import Field
|
|
6
|
+
|
|
7
|
+
from ..deapi_client import DeapiAPIError, get_client
|
|
8
|
+
from ..middleware import ensure_model_cache
|
|
9
|
+
from ..polling_manager import PollingManager
|
|
10
|
+
from ..utils import (
|
|
11
|
+
form_bool,
|
|
12
|
+
prepare_audio_upload_async,
|
|
13
|
+
prepare_image_upload_async,
|
|
14
|
+
prepare_video_upload_async,
|
|
15
|
+
)
|
|
16
|
+
from ._price_helpers import (
|
|
17
|
+
feature_enabled,
|
|
18
|
+
resolve_from_card,
|
|
19
|
+
resolve_generation_params,
|
|
20
|
+
resolve_upscale_factor,
|
|
21
|
+
)
|
|
22
|
+
from ._responses import build_job_response, build_price_response, input_error_response
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def image_to_video(
|
|
26
|
+
prompt: Annotated[str, Field(description="Text prompt for video generation")],
|
|
27
|
+
model: Annotated[str, Field(description="Video generation model name")],
|
|
28
|
+
first_frame_image: Annotated[
|
|
29
|
+
Optional[str],
|
|
30
|
+
Field(
|
|
31
|
+
description="First frame image as URL (e.g., from text_to_image result), data URI (data:image/png;base64,...), or base64 string. URLs are recommended when chaining from text_to_image to avoid base64 context bloat. Required unless ref_images or ref_videos are given; the two modes are mutually exclusive"
|
|
32
|
+
),
|
|
33
|
+
] = None,
|
|
34
|
+
negative_prompt: Annotated[
|
|
35
|
+
Optional[str], Field(description="Things to exclude (optional)")
|
|
36
|
+
] = None,
|
|
37
|
+
last_frame_image: Annotated[
|
|
38
|
+
Optional[str],
|
|
39
|
+
Field(
|
|
40
|
+
description="Last frame image (optional, URL, data URI, or base64). Not allowed with reference inputs"
|
|
41
|
+
),
|
|
42
|
+
] = None,
|
|
43
|
+
ref_images: Annotated[
|
|
44
|
+
Optional[List[str]],
|
|
45
|
+
Field(
|
|
46
|
+
description="Reference images (URL, data URI or base64), in order: ref_images[0] is <Picture 1> in the prompt, ref_images[1] is <Picture 2>, and so on. Each within a 1:4-4:1 aspect ratio. Only for models listing image refs in the model list; replaces first_frame_image"
|
|
47
|
+
),
|
|
48
|
+
] = None,
|
|
49
|
+
ref_videos: Annotated[
|
|
50
|
+
Optional[List[str]],
|
|
51
|
+
Field(
|
|
52
|
+
description="Reference videos (URL, data URI or base64), in order: ref_videos[0] is <Video 1>. Usually at most one, a few seconds long (see the model list). A video with a soundtrack takes <Audio 1> and shifts every ref_audios label by one"
|
|
53
|
+
),
|
|
54
|
+
] = None,
|
|
55
|
+
ref_audios: Annotated[
|
|
56
|
+
Optional[List[str]],
|
|
57
|
+
Field(
|
|
58
|
+
description="Reference audio clips (URL, data URI or base64), in order: ref_audios[0] is <Audio 1> (or <Audio 2> after a sounded reference video). Needs at least one ref_images or ref_videos entry"
|
|
59
|
+
),
|
|
60
|
+
] = None,
|
|
61
|
+
width: Annotated[
|
|
62
|
+
Optional[int],
|
|
63
|
+
Field(
|
|
64
|
+
ge=1,
|
|
65
|
+
description="Video width in pixels. Defaults to the model's own value (see the model list)",
|
|
66
|
+
),
|
|
67
|
+
] = None,
|
|
68
|
+
height: Annotated[
|
|
69
|
+
Optional[int],
|
|
70
|
+
Field(
|
|
71
|
+
ge=1,
|
|
72
|
+
description="Video height in pixels. Defaults to the model's own value (see the model list)",
|
|
73
|
+
),
|
|
74
|
+
] = None,
|
|
75
|
+
frames: Annotated[
|
|
76
|
+
Optional[int],
|
|
77
|
+
Field(
|
|
78
|
+
ge=1,
|
|
79
|
+
description="Number of video frames. Defaults to the model's own value (see the model list); each model has its own range",
|
|
80
|
+
),
|
|
81
|
+
] = None,
|
|
82
|
+
fps: Annotated[
|
|
83
|
+
Optional[int],
|
|
84
|
+
Field(
|
|
85
|
+
ge=1,
|
|
86
|
+
description="Frames per second. Defaults to the model's own value (see the model list); most models fix it",
|
|
87
|
+
),
|
|
88
|
+
] = None,
|
|
89
|
+
steps: Annotated[
|
|
90
|
+
Optional[int],
|
|
91
|
+
Field(
|
|
92
|
+
ge=1,
|
|
93
|
+
description="Number of inference steps. Defaults to the model's own value (see the model list); ignored by models that do not support steps",
|
|
94
|
+
),
|
|
95
|
+
] = None,
|
|
96
|
+
guidance_scale: Annotated[
|
|
97
|
+
Optional[float],
|
|
98
|
+
Field(
|
|
99
|
+
ge=0.0,
|
|
100
|
+
description="Guidance scale. Leave unset to use the model's own default; no model in the catalogue currently supports guidance",
|
|
101
|
+
),
|
|
102
|
+
] = None,
|
|
103
|
+
seed: Annotated[int, Field(description="Random seed for reproducibility")] = -1,
|
|
104
|
+
enhance_prompt: Annotated[
|
|
105
|
+
Optional[bool],
|
|
106
|
+
Field(
|
|
107
|
+
description="Let the API rewrite the prompt before generating. Only on models with a prompt-booster guide (422 otherwise); its fee is charged on the job and is not in the /price quote"
|
|
108
|
+
),
|
|
109
|
+
] = None,
|
|
110
|
+
webhook_url: Annotated[
|
|
111
|
+
Optional[str],
|
|
112
|
+
Field(
|
|
113
|
+
description="HTTPS URL for deAPI to call when this job finishes. Notifies your own systems; the tool still returns the result here"
|
|
114
|
+
),
|
|
115
|
+
] = None,
|
|
116
|
+
webhook_secret: Annotated[
|
|
117
|
+
Optional[str],
|
|
118
|
+
Field(
|
|
119
|
+
description="32-255 char key deAPI signs the callback with (HMAC-SHA256). Needs webhook_url. A secret given in chat stays in the transcript"
|
|
120
|
+
),
|
|
121
|
+
] = None,
|
|
122
|
+
) -> dict[str, Any]:
|
|
123
|
+
"""Generate video from static image(s), or from reference images, videos and audio.
|
|
124
|
+
|
|
125
|
+
Two modes, mutually exclusive:
|
|
126
|
+
- Keyframes: first_frame_image (and optionally last_frame_image) is animated.
|
|
127
|
+
- References: ref_images / ref_videos / ref_audios, on models that support them
|
|
128
|
+
(the model list shows "refs: ..." for those). The prompt addresses each reference
|
|
129
|
+
by its tag: <Picture 1>, <Picture 2> for images, <Video 1> for a video, <Audio 1>
|
|
130
|
+
for audio, numbered from 1 within each type in the order given. Order matters:
|
|
131
|
+
swapping two images swaps what the tags refer to. Example prompt:
|
|
132
|
+
"<Picture 1> walks into the room from <Picture 2>, speaking with the voice of <Audio 1>".
|
|
133
|
+
|
|
134
|
+
Accepts files as:
|
|
135
|
+
- Public URLs (recommended: chains with text_to_image, and the ONLY way in for a
|
|
136
|
+
file of your own - chat clients do not pass files through to tools)
|
|
137
|
+
- Data URIs or raw base64 (for callers that already hold the bytes)
|
|
138
|
+
|
|
139
|
+
IMPORTANT: Check model specifications using get_available_models() before calling.
|
|
140
|
+
Pay attention to limits for width/height/frames/fps and, in reference mode, the
|
|
141
|
+
max_ref_* counts and durations.
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
dict: Contains 'success', 'result_url' with video URL, 'job_id'
|
|
145
|
+
"""
|
|
146
|
+
has_refs = bool(ref_images or ref_videos or ref_audios)
|
|
147
|
+
if has_refs and (first_frame_image or last_frame_image):
|
|
148
|
+
return input_error_response(
|
|
149
|
+
"Pass either first_frame_image/last_frame_image or ref_images/ref_videos/ref_audios, not both: the API rejects requests that mix the two modes."
|
|
150
|
+
)
|
|
151
|
+
if ref_audios and not (ref_images or ref_videos):
|
|
152
|
+
return input_error_response("ref_audios needs at least one ref_images or ref_videos entry.")
|
|
153
|
+
if not first_frame_image and not (ref_images or ref_videos):
|
|
154
|
+
return input_error_response(
|
|
155
|
+
"A source is required: pass first_frame_image, or ref_images/ref_videos on a model that supports references."
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
client = get_client()
|
|
160
|
+
async with client:
|
|
161
|
+
# A list of (field, file) pairs: repeated `ref_images[]` parts are what
|
|
162
|
+
# Laravel parses back into an array, in the order sent — and that order
|
|
163
|
+
# is what binds each file to its <Picture N> / <Video N> / <Audio N> tag.
|
|
164
|
+
files = []
|
|
165
|
+
if first_frame_image:
|
|
166
|
+
files.append(
|
|
167
|
+
await prepare_image_upload_async(first_frame_image, "first_frame_image")
|
|
168
|
+
)
|
|
169
|
+
if last_frame_image:
|
|
170
|
+
files.append(await prepare_image_upload_async(last_frame_image, "last_frame_image"))
|
|
171
|
+
for item in ref_images or []:
|
|
172
|
+
files.append(("ref_images[]", (await prepare_image_upload_async(item))[1]))
|
|
173
|
+
for item in ref_videos or []:
|
|
174
|
+
files.append(("ref_videos[]", (await prepare_video_upload_async(item))[1]))
|
|
175
|
+
for item in ref_audios or []:
|
|
176
|
+
files.append(("ref_audios[]", (await prepare_audio_upload_async(item))[1]))
|
|
177
|
+
|
|
178
|
+
# Anything unset comes from the model card: the API requires these
|
|
179
|
+
# fields and validates them against the card, and no fixed number
|
|
180
|
+
# satisfies every model (fps alone is 30 on Ltxv and 24 on Ltx2).
|
|
181
|
+
await ensure_model_cache(model=model)
|
|
182
|
+
card = resolve_from_card(
|
|
183
|
+
model,
|
|
184
|
+
{
|
|
185
|
+
"width": width,
|
|
186
|
+
"height": height,
|
|
187
|
+
"frames": frames,
|
|
188
|
+
"fps": fps,
|
|
189
|
+
"steps": steps,
|
|
190
|
+
"guidance": guidance_scale,
|
|
191
|
+
},
|
|
192
|
+
)
|
|
193
|
+
form_data = {
|
|
194
|
+
"prompt": prompt,
|
|
195
|
+
"model": model,
|
|
196
|
+
**{key: str(value) for key, value in card.items()},
|
|
197
|
+
"seed": str(seed),
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if negative_prompt:
|
|
201
|
+
form_data["negative_prompt"] = negative_prompt
|
|
202
|
+
if enhance_prompt is not None:
|
|
203
|
+
form_data["enhance_prompt"] = form_bool(enhance_prompt)
|
|
204
|
+
|
|
205
|
+
job_response = await client.submit_job(
|
|
206
|
+
endpoint="videos/animations",
|
|
207
|
+
data=form_data,
|
|
208
|
+
files=files,
|
|
209
|
+
webhook_url=webhook_url,
|
|
210
|
+
webhook_secret=webhook_secret,
|
|
211
|
+
)
|
|
212
|
+
job_id = job_response.data.request_id
|
|
213
|
+
|
|
214
|
+
polling_manager = PollingManager(client, job_type="video")
|
|
215
|
+
result = await polling_manager.poll_until_complete(job_id)
|
|
216
|
+
|
|
217
|
+
return build_job_response(result, job_id)
|
|
218
|
+
|
|
219
|
+
except ValueError as e:
|
|
220
|
+
return {"success": False, "error": f"Invalid file format: {str(e)}"}
|
|
221
|
+
except DeapiAPIError as e:
|
|
222
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
223
|
+
except Exception as e:
|
|
224
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
async def text_to_video(
|
|
228
|
+
prompt: Annotated[str, Field(description="Text prompt for video generation")],
|
|
229
|
+
model: Annotated[str, Field(description="Video generation model name")],
|
|
230
|
+
negative_prompt: Annotated[
|
|
231
|
+
Optional[str], Field(description="Things to exclude (optional)")
|
|
232
|
+
] = None,
|
|
233
|
+
width: Annotated[
|
|
234
|
+
Optional[int],
|
|
235
|
+
Field(
|
|
236
|
+
ge=1,
|
|
237
|
+
description="Video width in pixels. Defaults to the model's own value (see the model list)",
|
|
238
|
+
),
|
|
239
|
+
] = None,
|
|
240
|
+
height: Annotated[
|
|
241
|
+
Optional[int],
|
|
242
|
+
Field(
|
|
243
|
+
ge=1,
|
|
244
|
+
description="Video height in pixels. Defaults to the model's own value (see the model list)",
|
|
245
|
+
),
|
|
246
|
+
] = None,
|
|
247
|
+
frames: Annotated[
|
|
248
|
+
Optional[int],
|
|
249
|
+
Field(
|
|
250
|
+
ge=1,
|
|
251
|
+
description="Number of video frames. Defaults to the model's own value (see the model list); each model has its own range",
|
|
252
|
+
),
|
|
253
|
+
] = None,
|
|
254
|
+
fps: Annotated[
|
|
255
|
+
Optional[int],
|
|
256
|
+
Field(
|
|
257
|
+
ge=1,
|
|
258
|
+
description="Frames per second. Defaults to the model's own value (see the model list); most models fix it",
|
|
259
|
+
),
|
|
260
|
+
] = None,
|
|
261
|
+
steps: Annotated[
|
|
262
|
+
Optional[int],
|
|
263
|
+
Field(
|
|
264
|
+
ge=1,
|
|
265
|
+
description="Number of inference steps. Defaults to the model's own value (see the model list); ignored by models that do not support steps",
|
|
266
|
+
),
|
|
267
|
+
] = None,
|
|
268
|
+
guidance_scale: Annotated[
|
|
269
|
+
Optional[float],
|
|
270
|
+
Field(
|
|
271
|
+
ge=0.0,
|
|
272
|
+
description="Guidance scale. Leave unset to use the model's own default; no model in the catalogue currently supports guidance",
|
|
273
|
+
),
|
|
274
|
+
] = None,
|
|
275
|
+
seed: Annotated[int, Field(description="Random seed for reproducibility")] = -1,
|
|
276
|
+
enhance_prompt: Annotated[
|
|
277
|
+
Optional[bool],
|
|
278
|
+
Field(
|
|
279
|
+
description="Let the API rewrite the prompt before generating. Only on models with a prompt-booster guide (422 otherwise); its fee is charged on the job and is not in the /price quote"
|
|
280
|
+
),
|
|
281
|
+
] = None,
|
|
282
|
+
webhook_url: Annotated[
|
|
283
|
+
Optional[str],
|
|
284
|
+
Field(
|
|
285
|
+
description="HTTPS URL for deAPI to call when this job finishes. Notifies your own systems; the tool still returns the result here"
|
|
286
|
+
),
|
|
287
|
+
] = None,
|
|
288
|
+
webhook_secret: Annotated[
|
|
289
|
+
Optional[str],
|
|
290
|
+
Field(
|
|
291
|
+
description="32-255 char key deAPI signs the callback with (HMAC-SHA256). Needs webhook_url. A secret given in chat stays in the transcript"
|
|
292
|
+
),
|
|
293
|
+
] = None,
|
|
294
|
+
) -> dict[str, Any]:
|
|
295
|
+
"""Generate video from text prompt.
|
|
296
|
+
|
|
297
|
+
Creates videos from text descriptions. Returns the generated video URL.
|
|
298
|
+
|
|
299
|
+
IMPORTANT: Before calling this tool, use get_available_models() to check the model's
|
|
300
|
+
specifications. Pay special attention to:
|
|
301
|
+
- limits.max_steps/min_steps: Valid range for steps parameter
|
|
302
|
+
- limits.max_fps/min_fps: Valid range for fps parameter
|
|
303
|
+
- defaults: Recommended values for the model
|
|
304
|
+
|
|
305
|
+
Returns:
|
|
306
|
+
dict: Contains 'success', 'result_url' with video URL, 'job_id'
|
|
307
|
+
"""
|
|
308
|
+
try:
|
|
309
|
+
client = get_client()
|
|
310
|
+
async with client:
|
|
311
|
+
# Anything unset comes from the model card (see image_to_video).
|
|
312
|
+
await ensure_model_cache(model=model)
|
|
313
|
+
card = resolve_from_card(
|
|
314
|
+
model,
|
|
315
|
+
{
|
|
316
|
+
"width": width,
|
|
317
|
+
"height": height,
|
|
318
|
+
"frames": frames,
|
|
319
|
+
"fps": fps,
|
|
320
|
+
"steps": steps,
|
|
321
|
+
"guidance": guidance_scale,
|
|
322
|
+
},
|
|
323
|
+
)
|
|
324
|
+
# txt2video uses multipart/form-data
|
|
325
|
+
request_data = {
|
|
326
|
+
"prompt": prompt,
|
|
327
|
+
"model": model,
|
|
328
|
+
**{key: str(value) for key, value in card.items()},
|
|
329
|
+
"seed": str(seed),
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if negative_prompt:
|
|
333
|
+
request_data["negative_prompt"] = negative_prompt
|
|
334
|
+
if enhance_prompt is not None:
|
|
335
|
+
request_data["enhance_prompt"] = form_bool(enhance_prompt)
|
|
336
|
+
|
|
337
|
+
job_response = await client.submit_job(
|
|
338
|
+
endpoint="videos/generations",
|
|
339
|
+
data=request_data, # Use data for form-data, not json_data
|
|
340
|
+
webhook_url=webhook_url,
|
|
341
|
+
webhook_secret=webhook_secret,
|
|
342
|
+
)
|
|
343
|
+
job_id = job_response.data.request_id
|
|
344
|
+
|
|
345
|
+
polling_manager = PollingManager(client, job_type="video")
|
|
346
|
+
result = await polling_manager.poll_until_complete(job_id)
|
|
347
|
+
|
|
348
|
+
return build_job_response(result, job_id)
|
|
349
|
+
|
|
350
|
+
except DeapiAPIError as e:
|
|
351
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
352
|
+
except Exception as e:
|
|
353
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
async def image_to_video_price(
|
|
357
|
+
model: Annotated[str, Field(description="Video generation model name")],
|
|
358
|
+
width: Annotated[
|
|
359
|
+
Optional[int],
|
|
360
|
+
Field(ge=1, description="Video width in pixels. Limits per model (see the model list)"),
|
|
361
|
+
] = None,
|
|
362
|
+
height: Annotated[
|
|
363
|
+
Optional[int],
|
|
364
|
+
Field(ge=1, description="Video height in pixels. Limits per model (see the model list)"),
|
|
365
|
+
] = None,
|
|
366
|
+
frames: Annotated[
|
|
367
|
+
Optional[int],
|
|
368
|
+
Field(ge=1, description="Number of video frames. Limits per model (see the model list)"),
|
|
369
|
+
] = None,
|
|
370
|
+
steps: Annotated[
|
|
371
|
+
Optional[int],
|
|
372
|
+
Field(ge=1, description="Number of inference steps. Limits per model (see the model list)"),
|
|
373
|
+
] = None,
|
|
374
|
+
fps: Annotated[
|
|
375
|
+
Optional[int],
|
|
376
|
+
Field(
|
|
377
|
+
ge=1, description="Frames per second (optional; limits per model, see the model list)"
|
|
378
|
+
),
|
|
379
|
+
] = None,
|
|
380
|
+
ref_images_count: Annotated[
|
|
381
|
+
Optional[int],
|
|
382
|
+
Field(
|
|
383
|
+
ge=0,
|
|
384
|
+
description="How many ref_images the job will send. More than 2 doubles the price",
|
|
385
|
+
),
|
|
386
|
+
] = None,
|
|
387
|
+
ref_videos_count: Annotated[
|
|
388
|
+
Optional[int],
|
|
389
|
+
Field(
|
|
390
|
+
ge=0, description="How many ref_videos the job will send. Any video triples the price"
|
|
391
|
+
),
|
|
392
|
+
] = None,
|
|
393
|
+
ref_audios_count: Annotated[
|
|
394
|
+
Optional[int],
|
|
395
|
+
Field(ge=0, description="How many ref_audios the job will send. No surcharge"),
|
|
396
|
+
] = None,
|
|
397
|
+
) -> dict[str, Any]:
|
|
398
|
+
"""Calculate price for image-to-video generation.
|
|
399
|
+
|
|
400
|
+
For reference mode, pass the number of reference files the job will carry
|
|
401
|
+
(not the files themselves). Surcharges do not stack: the highest applies, so
|
|
402
|
+
4 images plus a video is x3, not x6.
|
|
403
|
+
|
|
404
|
+
Returns:
|
|
405
|
+
dict: Contains 'success' and 'price' information
|
|
406
|
+
"""
|
|
407
|
+
try:
|
|
408
|
+
client = get_client()
|
|
409
|
+
async with client:
|
|
410
|
+
await ensure_model_cache(model=model)
|
|
411
|
+
params = resolve_generation_params(
|
|
412
|
+
model,
|
|
413
|
+
{
|
|
414
|
+
"width": width,
|
|
415
|
+
"height": height,
|
|
416
|
+
"frames": frames,
|
|
417
|
+
"steps": steps,
|
|
418
|
+
"fps": fps,
|
|
419
|
+
},
|
|
420
|
+
)
|
|
421
|
+
request_data = {
|
|
422
|
+
"model": model,
|
|
423
|
+
**params,
|
|
424
|
+
}
|
|
425
|
+
# seed not required for video price calc
|
|
426
|
+
request_data.pop("seed", None)
|
|
427
|
+
# Guidance is only required (and priced) on a model that turns it on;
|
|
428
|
+
# every current card has it off, where the API drops it anyway.
|
|
429
|
+
if not feature_enabled(model, "supports_guidance"):
|
|
430
|
+
request_data.pop("guidance", None)
|
|
431
|
+
for key, count in (
|
|
432
|
+
("ref_images_count", ref_images_count),
|
|
433
|
+
("ref_videos_count", ref_videos_count),
|
|
434
|
+
("ref_audios_count", ref_audios_count),
|
|
435
|
+
):
|
|
436
|
+
if count:
|
|
437
|
+
request_data[key] = count
|
|
438
|
+
|
|
439
|
+
price_response = await client.calculate_price(
|
|
440
|
+
endpoint="videos/animations/price",
|
|
441
|
+
json_data=request_data,
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
return build_price_response(price_response)
|
|
445
|
+
|
|
446
|
+
except DeapiAPIError as e:
|
|
447
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
448
|
+
except Exception as e:
|
|
449
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
async def text_to_video_price(
|
|
453
|
+
model: Annotated[str, Field(description="Video generation model name")],
|
|
454
|
+
width: Annotated[
|
|
455
|
+
Optional[int],
|
|
456
|
+
Field(ge=1, description="Video width in pixels. Limits per model (see the model list)"),
|
|
457
|
+
] = None,
|
|
458
|
+
height: Annotated[
|
|
459
|
+
Optional[int],
|
|
460
|
+
Field(ge=1, description="Video height in pixels. Limits per model (see the model list)"),
|
|
461
|
+
] = None,
|
|
462
|
+
frames: Annotated[
|
|
463
|
+
Optional[int],
|
|
464
|
+
Field(ge=1, description="Number of video frames. Limits per model (see the model list)"),
|
|
465
|
+
] = None,
|
|
466
|
+
steps: Annotated[
|
|
467
|
+
Optional[int],
|
|
468
|
+
Field(ge=1, description="Number of inference steps. Limits per model (see the model list)"),
|
|
469
|
+
] = None,
|
|
470
|
+
fps: Annotated[
|
|
471
|
+
Optional[int],
|
|
472
|
+
Field(
|
|
473
|
+
ge=1, description="Frames per second (optional; limits per model, see the model list)"
|
|
474
|
+
),
|
|
475
|
+
] = None,
|
|
476
|
+
) -> dict[str, Any]:
|
|
477
|
+
"""Calculate price for text-to-video generation.
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
dict: Contains 'success' and 'price' information
|
|
481
|
+
"""
|
|
482
|
+
try:
|
|
483
|
+
client = get_client()
|
|
484
|
+
async with client:
|
|
485
|
+
await ensure_model_cache(model=model)
|
|
486
|
+
params = resolve_generation_params(
|
|
487
|
+
model,
|
|
488
|
+
{
|
|
489
|
+
"width": width,
|
|
490
|
+
"height": height,
|
|
491
|
+
"frames": frames,
|
|
492
|
+
"steps": steps,
|
|
493
|
+
"fps": fps,
|
|
494
|
+
},
|
|
495
|
+
)
|
|
496
|
+
request_data = {
|
|
497
|
+
"model": model,
|
|
498
|
+
**params,
|
|
499
|
+
}
|
|
500
|
+
# seed not required for video price calc
|
|
501
|
+
request_data.pop("seed", None)
|
|
502
|
+
# Guidance is only required (and priced) on a model that turns it on;
|
|
503
|
+
# every current card has it off, where the API drops it anyway.
|
|
504
|
+
if not feature_enabled(model, "supports_guidance"):
|
|
505
|
+
request_data.pop("guidance", None)
|
|
506
|
+
|
|
507
|
+
price_response = await client.calculate_price(
|
|
508
|
+
endpoint="videos/generations/price",
|
|
509
|
+
json_data=request_data,
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
return build_price_response(price_response)
|
|
513
|
+
|
|
514
|
+
except DeapiAPIError as e:
|
|
515
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
516
|
+
except Exception as e:
|
|
517
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
async def audio_to_video(
|
|
521
|
+
prompt: Annotated[str, Field(description="Text prompt for video generation")],
|
|
522
|
+
model: Annotated[
|
|
523
|
+
str,
|
|
524
|
+
Field(description="Video generation model name (must support audio2video inference type)"),
|
|
525
|
+
],
|
|
526
|
+
audio: Annotated[
|
|
527
|
+
str,
|
|
528
|
+
Field(
|
|
529
|
+
description="Audio file as URL, data URI (data:audio/mpeg;base64,...), or base64 string. Conditions the video generation on the audio content"
|
|
530
|
+
),
|
|
531
|
+
],
|
|
532
|
+
width: Annotated[
|
|
533
|
+
Optional[int],
|
|
534
|
+
Field(
|
|
535
|
+
ge=1,
|
|
536
|
+
description="Video width in pixels. Defaults to the model's own value (see the model list)",
|
|
537
|
+
),
|
|
538
|
+
] = None,
|
|
539
|
+
height: Annotated[
|
|
540
|
+
Optional[int],
|
|
541
|
+
Field(
|
|
542
|
+
ge=1,
|
|
543
|
+
description="Video height in pixels. Defaults to the model's own value (see the model list)",
|
|
544
|
+
),
|
|
545
|
+
] = None,
|
|
546
|
+
frames: Annotated[
|
|
547
|
+
Optional[int],
|
|
548
|
+
Field(
|
|
549
|
+
ge=1,
|
|
550
|
+
description="Number of video frames. Defaults to the model's own value (see the model list); each model has its own range",
|
|
551
|
+
),
|
|
552
|
+
] = None,
|
|
553
|
+
fps: Annotated[
|
|
554
|
+
Optional[int],
|
|
555
|
+
Field(
|
|
556
|
+
ge=1,
|
|
557
|
+
description="Frames per second. Defaults to the model's own value (see the model list); most models fix it",
|
|
558
|
+
),
|
|
559
|
+
] = None,
|
|
560
|
+
seed: Annotated[int, Field(description="Random seed (-1 for random)")] = -1,
|
|
561
|
+
negative_prompt: Annotated[
|
|
562
|
+
Optional[str], Field(description="Things to exclude (optional)")
|
|
563
|
+
] = None,
|
|
564
|
+
guidance: Annotated[
|
|
565
|
+
Optional[float],
|
|
566
|
+
Field(
|
|
567
|
+
ge=0.0,
|
|
568
|
+
description="Guidance scale. Only used if model's features.supports_guidance is true. Limits per model (see the model list)",
|
|
569
|
+
),
|
|
570
|
+
] = None,
|
|
571
|
+
steps: Annotated[
|
|
572
|
+
Optional[int],
|
|
573
|
+
Field(
|
|
574
|
+
ge=1,
|
|
575
|
+
description="Inference steps. Only used if model's features.supports_steps is true. Limits per model (see the model list)",
|
|
576
|
+
),
|
|
577
|
+
] = None,
|
|
578
|
+
first_frame_image: Annotated[
|
|
579
|
+
Optional[str],
|
|
580
|
+
Field(description="First frame anchor image (URL, data URI, or base64, optional)"),
|
|
581
|
+
] = None,
|
|
582
|
+
last_frame_image: Annotated[
|
|
583
|
+
Optional[str],
|
|
584
|
+
Field(description="Last frame anchor image (URL, data URI, or base64, optional)"),
|
|
585
|
+
] = None,
|
|
586
|
+
webhook_url: Annotated[
|
|
587
|
+
Optional[str],
|
|
588
|
+
Field(
|
|
589
|
+
description="HTTPS URL for deAPI to call when this job finishes. Notifies your own systems; the tool still returns the result here"
|
|
590
|
+
),
|
|
591
|
+
] = None,
|
|
592
|
+
webhook_secret: Annotated[
|
|
593
|
+
Optional[str],
|
|
594
|
+
Field(
|
|
595
|
+
description="32-255 char key deAPI signs the callback with (HMAC-SHA256). Needs webhook_url. A secret given in chat stays in the transcript"
|
|
596
|
+
),
|
|
597
|
+
] = None,
|
|
598
|
+
) -> dict[str, Any]:
|
|
599
|
+
"""Generate video from audio and text prompt.
|
|
600
|
+
|
|
601
|
+
Creates videos conditioned on audio content. The audio drives the video generation,
|
|
602
|
+
allowing synchronized audio-visual output. Optionally accepts anchor frame images.
|
|
603
|
+
|
|
604
|
+
IMPORTANT: Check model specifications using get_available_models() before calling.
|
|
605
|
+
Pay attention to features.supports_guidance, features.supports_steps, and model limits.
|
|
606
|
+
|
|
607
|
+
Returns:
|
|
608
|
+
dict: Contains 'success', 'result_url' with video URL, 'job_id'
|
|
609
|
+
"""
|
|
610
|
+
try:
|
|
611
|
+
client = get_client()
|
|
612
|
+
async with client:
|
|
613
|
+
# Prepare audio file upload (required)
|
|
614
|
+
audio_field, audio_tuple = await prepare_audio_upload_async(audio, "audio")
|
|
615
|
+
files = {audio_field: audio_tuple}
|
|
616
|
+
|
|
617
|
+
# Prepare optional frame images
|
|
618
|
+
if first_frame_image:
|
|
619
|
+
ff_field, ff_tuple = await prepare_image_upload_async(
|
|
620
|
+
first_frame_image, "first_frame_image"
|
|
621
|
+
)
|
|
622
|
+
files[ff_field] = ff_tuple
|
|
623
|
+
if last_frame_image:
|
|
624
|
+
lf_field, lf_tuple = await prepare_image_upload_async(
|
|
625
|
+
last_frame_image, "last_frame_image"
|
|
626
|
+
)
|
|
627
|
+
files[lf_field] = lf_tuple
|
|
628
|
+
|
|
629
|
+
# Anything unset comes from the model card (see image_to_video).
|
|
630
|
+
await ensure_model_cache(model=model)
|
|
631
|
+
card = resolve_from_card(
|
|
632
|
+
model,
|
|
633
|
+
{
|
|
634
|
+
"width": width,
|
|
635
|
+
"height": height,
|
|
636
|
+
"frames": frames,
|
|
637
|
+
"fps": fps,
|
|
638
|
+
"steps": steps,
|
|
639
|
+
"guidance": guidance,
|
|
640
|
+
},
|
|
641
|
+
)
|
|
642
|
+
form_data = {
|
|
643
|
+
"prompt": prompt,
|
|
644
|
+
"model": model,
|
|
645
|
+
**{key: str(value) for key, value in card.items()},
|
|
646
|
+
"seed": str(seed),
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
if negative_prompt:
|
|
650
|
+
form_data["negative_prompt"] = negative_prompt
|
|
651
|
+
|
|
652
|
+
job_response = await client.submit_job(
|
|
653
|
+
endpoint="videos/audio-syncs",
|
|
654
|
+
data=form_data,
|
|
655
|
+
files=files,
|
|
656
|
+
webhook_url=webhook_url,
|
|
657
|
+
webhook_secret=webhook_secret,
|
|
658
|
+
)
|
|
659
|
+
job_id = job_response.data.request_id
|
|
660
|
+
|
|
661
|
+
polling_manager = PollingManager(client, job_type="video")
|
|
662
|
+
result = await polling_manager.poll_until_complete(job_id)
|
|
663
|
+
|
|
664
|
+
return build_job_response(result, job_id)
|
|
665
|
+
|
|
666
|
+
except ValueError as e:
|
|
667
|
+
return {"success": False, "error": f"Invalid file format: {str(e)}"}
|
|
668
|
+
except DeapiAPIError as e:
|
|
669
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
670
|
+
except Exception as e:
|
|
671
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
async def audio_to_video_price(
|
|
675
|
+
model: Annotated[str, Field(description="Video generation model name")],
|
|
676
|
+
width: Annotated[
|
|
677
|
+
Optional[int],
|
|
678
|
+
Field(ge=1, description="Video width in pixels. Limits per model (see the model list)"),
|
|
679
|
+
] = None,
|
|
680
|
+
height: Annotated[
|
|
681
|
+
Optional[int],
|
|
682
|
+
Field(ge=1, description="Video height in pixels. Limits per model (see the model list)"),
|
|
683
|
+
] = None,
|
|
684
|
+
frames: Annotated[
|
|
685
|
+
Optional[int],
|
|
686
|
+
Field(ge=1, description="Number of video frames. Limits per model (see the model list)"),
|
|
687
|
+
] = None,
|
|
688
|
+
steps: Annotated[
|
|
689
|
+
Optional[int],
|
|
690
|
+
Field(
|
|
691
|
+
ge=1,
|
|
692
|
+
description="Number of inference steps (optional; limits per model, see the model list)",
|
|
693
|
+
),
|
|
694
|
+
] = None,
|
|
695
|
+
fps: Annotated[
|
|
696
|
+
Optional[int],
|
|
697
|
+
Field(
|
|
698
|
+
ge=1, description="Frames per second (optional; limits per model, see the model list)"
|
|
699
|
+
),
|
|
700
|
+
] = None,
|
|
701
|
+
) -> dict[str, Any]:
|
|
702
|
+
"""Calculate price for audio-to-video generation.
|
|
703
|
+
|
|
704
|
+
Returns:
|
|
705
|
+
dict: Contains 'success' and 'price' information
|
|
706
|
+
"""
|
|
707
|
+
try:
|
|
708
|
+
client = get_client()
|
|
709
|
+
async with client:
|
|
710
|
+
await ensure_model_cache(model=model)
|
|
711
|
+
params = resolve_generation_params(
|
|
712
|
+
model,
|
|
713
|
+
{
|
|
714
|
+
"width": width,
|
|
715
|
+
"height": height,
|
|
716
|
+
"frames": frames,
|
|
717
|
+
"steps": steps,
|
|
718
|
+
"fps": fps,
|
|
719
|
+
},
|
|
720
|
+
)
|
|
721
|
+
request_data = {
|
|
722
|
+
"model": model,
|
|
723
|
+
**params,
|
|
724
|
+
}
|
|
725
|
+
# seed is not part of the quote; guidance only on a model that turns it on.
|
|
726
|
+
request_data.pop("seed", None)
|
|
727
|
+
if not feature_enabled(model, "supports_guidance"):
|
|
728
|
+
request_data.pop("guidance", None)
|
|
729
|
+
|
|
730
|
+
price_response = await client.calculate_price(
|
|
731
|
+
endpoint="videos/audio-syncs/price",
|
|
732
|
+
json_data=request_data,
|
|
733
|
+
)
|
|
734
|
+
|
|
735
|
+
return build_price_response(price_response)
|
|
736
|
+
|
|
737
|
+
except DeapiAPIError as e:
|
|
738
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
739
|
+
except Exception as e:
|
|
740
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
VideoDescriptionPreset = Literal["fast", "balanced", "detail"]
|
|
744
|
+
DescriptionPlatform = Literal["youtube", "twitter", "twitch", "kick", "tiktok"]
|
|
745
|
+
|
|
746
|
+
_VIDEO_URL_HELP = (
|
|
747
|
+
"Link to a video on YouTube, Twitter/X, Twitch, Kick or TikTok; the API fetches it "
|
|
748
|
+
"itself. Not for a direct link to a video file: pass that as video_file. "
|
|
749
|
+
"Live streams and audio-only sources (Twitter Spaces) are rejected"
|
|
750
|
+
)
|
|
751
|
+
_VIDEO_FILE_HELP = (
|
|
752
|
+
"Video file as a direct URL to the file (preferred), data URI (data:video/mp4;base64,...) "
|
|
753
|
+
"or base64 string; this server uploads it. Must contain a video stream"
|
|
754
|
+
)
|
|
755
|
+
_PRESET_HELP = (
|
|
756
|
+
"Detail level: 'fast', 'balanced' or 'detail'. Higher presets read small on-screen text "
|
|
757
|
+
"and logos but cost more (roughly x1 / x1.7 / x3.75 machine time). Unset: 'balanced', "
|
|
758
|
+
"or the cheapest preset the model offers. Ignored by models without presets"
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
def _description_source_error(video_url: Optional[str], video_file: Optional[str]) -> Optional[str]:
|
|
763
|
+
"""The API takes exactly one source; say which rule was broken before uploading anything."""
|
|
764
|
+
if video_url and video_file:
|
|
765
|
+
return "Pass either 'video_url' or 'video_file', not both: the API rejects requests carrying both."
|
|
766
|
+
if not video_url and not video_file:
|
|
767
|
+
return (
|
|
768
|
+
"A source is required: pass 'video_url' for a platform link or 'video_file' for a file."
|
|
769
|
+
)
|
|
770
|
+
return None
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
async def video_description(
|
|
774
|
+
model: Annotated[str, Field(description="Video description model (e.g., 'Marlin_2B')")],
|
|
775
|
+
video_url: Annotated[Optional[str], Field(description=_VIDEO_URL_HELP)] = None,
|
|
776
|
+
video_file: Annotated[Optional[str], Field(description=_VIDEO_FILE_HELP)] = None,
|
|
777
|
+
preset: Annotated[Optional[VideoDescriptionPreset], Field(description=_PRESET_HELP)] = None,
|
|
778
|
+
include_metadata: Annotated[
|
|
779
|
+
Optional[bool],
|
|
780
|
+
Field(
|
|
781
|
+
description="Return the source's title, channel, upload date and view/like counts. URL sources only; carries a flat surcharge"
|
|
782
|
+
),
|
|
783
|
+
] = None,
|
|
784
|
+
return_result_in_response: Annotated[
|
|
785
|
+
bool,
|
|
786
|
+
Field(
|
|
787
|
+
description="Return the description JSON inline in 'result'. Set to False for long videos to get only the download URL"
|
|
788
|
+
),
|
|
789
|
+
] = True,
|
|
790
|
+
webhook_url: Annotated[
|
|
791
|
+
Optional[str],
|
|
792
|
+
Field(
|
|
793
|
+
description="HTTPS URL for deAPI to call when this job finishes. Notifies your own systems; the tool still returns the result here"
|
|
794
|
+
),
|
|
795
|
+
] = None,
|
|
796
|
+
webhook_secret: Annotated[
|
|
797
|
+
Optional[str],
|
|
798
|
+
Field(
|
|
799
|
+
description="32-255 char key deAPI signs the callback with (HMAC-SHA256). Needs webhook_url. A secret given in chat stays in the transcript"
|
|
800
|
+
),
|
|
801
|
+
] = None,
|
|
802
|
+
) -> dict[str, Any]:
|
|
803
|
+
"""Describe what happens on screen in a video, as a timestamped index of events.
|
|
804
|
+
|
|
805
|
+
The visual counterpart of transcription: reads the picture, not the audio track
|
|
806
|
+
(for speech use the transcription tools). Pass exactly one source: video_url for a
|
|
807
|
+
YouTube, Twitter/X, Twitch, Kick or TikTok link, or video_file for a file.
|
|
808
|
+
|
|
809
|
+
The result is a JSON document:
|
|
810
|
+
{"events": [{"start", "end", "description", ...}], "scenes": [...], "meta": {"duration", "preset", ...}},
|
|
811
|
+
with start/end in seconds. Priced on machine time, so longer videos and higher
|
|
812
|
+
presets cost more; quote first with video_description_price. A long video can
|
|
813
|
+
take several minutes: if the tool times out, follow up with check_job_status.
|
|
814
|
+
|
|
815
|
+
Returns:
|
|
816
|
+
dict: Contains 'success', 'result' (description JSON, when returned inline),
|
|
817
|
+
'result_url', 'job_id'
|
|
818
|
+
"""
|
|
819
|
+
error = _description_source_error(video_url, video_file)
|
|
820
|
+
if error:
|
|
821
|
+
return input_error_response(error)
|
|
822
|
+
|
|
823
|
+
try:
|
|
824
|
+
client = get_client()
|
|
825
|
+
async with client:
|
|
826
|
+
form_data = {
|
|
827
|
+
"model": model,
|
|
828
|
+
"return_result_in_response": form_bool(return_result_in_response),
|
|
829
|
+
}
|
|
830
|
+
if preset:
|
|
831
|
+
form_data["preset"] = preset
|
|
832
|
+
if include_metadata is not None:
|
|
833
|
+
form_data["include_metadata"] = form_bool(include_metadata)
|
|
834
|
+
|
|
835
|
+
files = None
|
|
836
|
+
if video_url:
|
|
837
|
+
form_data["video_url"] = video_url
|
|
838
|
+
else:
|
|
839
|
+
assert video_file # _description_source_error requires exactly one source
|
|
840
|
+
field_name, file_tuple = await prepare_video_upload_async(video_file, "video_file")
|
|
841
|
+
files = {field_name: file_tuple}
|
|
842
|
+
|
|
843
|
+
job_response = await client.submit_job(
|
|
844
|
+
endpoint="videos/descriptions",
|
|
845
|
+
data=form_data,
|
|
846
|
+
files=files,
|
|
847
|
+
webhook_url=webhook_url,
|
|
848
|
+
webhook_secret=webhook_secret,
|
|
849
|
+
)
|
|
850
|
+
job_id = job_response.data.request_id
|
|
851
|
+
|
|
852
|
+
polling_manager = PollingManager(client, job_type="video")
|
|
853
|
+
result = await polling_manager.poll_until_complete(job_id)
|
|
854
|
+
|
|
855
|
+
return build_job_response(result, job_id)
|
|
856
|
+
|
|
857
|
+
except ValueError as e:
|
|
858
|
+
return {"success": False, "error": f"Invalid video format: {str(e)}"}
|
|
859
|
+
except DeapiAPIError as e:
|
|
860
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
861
|
+
except Exception as e:
|
|
862
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
async def video_description_price(
|
|
866
|
+
model: Annotated[str, Field(description="Video description model (e.g., 'Marlin_2B')")],
|
|
867
|
+
video_url: Annotated[
|
|
868
|
+
Optional[str],
|
|
869
|
+
Field(
|
|
870
|
+
description="Platform link to quote (YouTube, Twitter/X, Twitch, Kick). TikTok links are refused: quote those by duration_seconds with platform='tiktok'"
|
|
871
|
+
),
|
|
872
|
+
] = None,
|
|
873
|
+
video_file: Annotated[
|
|
874
|
+
Optional[str],
|
|
875
|
+
Field(description="Video file to quote (URL, data URI or base64); only its length is read"),
|
|
876
|
+
] = None,
|
|
877
|
+
duration_seconds: Annotated[
|
|
878
|
+
Optional[float],
|
|
879
|
+
Field(gt=0, description="Video length in seconds, to quote without a source"),
|
|
880
|
+
] = None,
|
|
881
|
+
platform: Annotated[
|
|
882
|
+
Optional[DescriptionPlatform],
|
|
883
|
+
Field(
|
|
884
|
+
description="Where the video comes from; only with duration_seconds. 'tiktok' uses a fixed price card (max 10 minutes). Omitted, the quote is for an uploaded file and can come in under a URL job's charge"
|
|
885
|
+
),
|
|
886
|
+
] = None,
|
|
887
|
+
preset: Annotated[Optional[VideoDescriptionPreset], Field(description=_PRESET_HELP)] = None,
|
|
888
|
+
include_metadata: Annotated[
|
|
889
|
+
Optional[bool],
|
|
890
|
+
Field(
|
|
891
|
+
description="Whether the job will ask for source metadata (flat surcharge on URL sources)"
|
|
892
|
+
),
|
|
893
|
+
] = None,
|
|
894
|
+
) -> dict[str, Any]:
|
|
895
|
+
"""Calculate the price of a video description.
|
|
896
|
+
|
|
897
|
+
Pass exactly one source: video_url, video_file or duration_seconds (with an optional
|
|
898
|
+
platform). Use the same preset and include_metadata the job will use, since both
|
|
899
|
+
change the price.
|
|
900
|
+
|
|
901
|
+
Returns:
|
|
902
|
+
dict: Contains 'success' and 'price' information
|
|
903
|
+
"""
|
|
904
|
+
sources = [s for s in (video_url, video_file, duration_seconds) if s is not None and s != ""]
|
|
905
|
+
if len(sources) != 1:
|
|
906
|
+
return input_error_response(
|
|
907
|
+
"Provide exactly one of 'video_url', 'video_file' or 'duration_seconds'."
|
|
908
|
+
)
|
|
909
|
+
if platform and duration_seconds is None:
|
|
910
|
+
return input_error_response(
|
|
911
|
+
"'platform' is only accepted with 'duration_seconds'; with a URL or file the API derives it from the source."
|
|
912
|
+
)
|
|
913
|
+
|
|
914
|
+
try:
|
|
915
|
+
client = get_client()
|
|
916
|
+
async with client:
|
|
917
|
+
form_data = {"model": model}
|
|
918
|
+
if preset:
|
|
919
|
+
form_data["preset"] = preset
|
|
920
|
+
if include_metadata is not None:
|
|
921
|
+
form_data["include_metadata"] = form_bool(include_metadata)
|
|
922
|
+
|
|
923
|
+
files = None
|
|
924
|
+
if video_url:
|
|
925
|
+
form_data["video_url"] = video_url
|
|
926
|
+
elif video_file:
|
|
927
|
+
field_name, file_tuple = await prepare_video_upload_async(video_file, "video_file")
|
|
928
|
+
files = {field_name: file_tuple}
|
|
929
|
+
else:
|
|
930
|
+
form_data["duration_seconds"] = str(duration_seconds)
|
|
931
|
+
if platform:
|
|
932
|
+
form_data["platform"] = platform
|
|
933
|
+
|
|
934
|
+
price_response = await client.calculate_price(
|
|
935
|
+
endpoint="videos/descriptions/price",
|
|
936
|
+
data=form_data,
|
|
937
|
+
files=files,
|
|
938
|
+
)
|
|
939
|
+
|
|
940
|
+
return build_price_response(price_response)
|
|
941
|
+
|
|
942
|
+
except ValueError as e:
|
|
943
|
+
return {"success": False, "error": f"Invalid video format: {str(e)}"}
|
|
944
|
+
except DeapiAPIError as e:
|
|
945
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
946
|
+
except Exception as e:
|
|
947
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
async def video_upscale(
|
|
951
|
+
video: Annotated[
|
|
952
|
+
str,
|
|
953
|
+
Field(
|
|
954
|
+
description="Video as URL, data URI (data:video/mp4;base64,...), or base64 string. URLs are recommended to avoid base64 context bloat."
|
|
955
|
+
),
|
|
956
|
+
],
|
|
957
|
+
model: Annotated[str, Field(description="Video upscaling model name")],
|
|
958
|
+
scale: Annotated[
|
|
959
|
+
Optional[int],
|
|
960
|
+
Field(
|
|
961
|
+
ge=1,
|
|
962
|
+
le=16,
|
|
963
|
+
description="Upscale factor, only for models with a 'scale' range in the model list (FlashVSR_Tiny: 2-4). Unset, those models use their smallest factor. Models without a range have a fixed factor (e.g. RealESRGAN_Vid_x4) and refuse this field",
|
|
964
|
+
),
|
|
965
|
+
] = None,
|
|
966
|
+
webhook_url: Annotated[
|
|
967
|
+
Optional[str],
|
|
968
|
+
Field(
|
|
969
|
+
description="HTTPS URL for deAPI to call when this job finishes. Notifies your own systems; the tool still returns the result here"
|
|
970
|
+
),
|
|
971
|
+
] = None,
|
|
972
|
+
webhook_secret: Annotated[
|
|
973
|
+
Optional[str],
|
|
974
|
+
Field(
|
|
975
|
+
description="32-255 char key deAPI signs the callback with (HMAC-SHA256). Needs webhook_url. A secret given in chat stays in the transcript"
|
|
976
|
+
),
|
|
977
|
+
] = None,
|
|
978
|
+
) -> dict[str, Any]:
|
|
979
|
+
"""Upscale a video to higher resolution.
|
|
980
|
+
|
|
981
|
+
Takes a video and upscales it using AI models for enhanced quality.
|
|
982
|
+
Accepts videos as:
|
|
983
|
+
- Public URLs (recommended, and the ONLY way in for a file of your own - chat
|
|
984
|
+
clients do not pass files through to tools)
|
|
985
|
+
- Data URIs or raw base64 (for callers that already hold the bytes)
|
|
986
|
+
|
|
987
|
+
Returns:
|
|
988
|
+
dict: Contains 'success', 'result_url' with upscaled video URL, 'job_id'
|
|
989
|
+
"""
|
|
990
|
+
try:
|
|
991
|
+
client = get_client()
|
|
992
|
+
async with client:
|
|
993
|
+
field_name, file_tuple = await prepare_video_upload_async(video, "video")
|
|
994
|
+
|
|
995
|
+
await ensure_model_cache(model=model)
|
|
996
|
+
scale, error = resolve_upscale_factor(model, scale)
|
|
997
|
+
if error:
|
|
998
|
+
return input_error_response(error)
|
|
999
|
+
|
|
1000
|
+
form_data = {
|
|
1001
|
+
"model": model,
|
|
1002
|
+
}
|
|
1003
|
+
if scale is not None:
|
|
1004
|
+
form_data["scale"] = str(scale)
|
|
1005
|
+
|
|
1006
|
+
job_response = await client.submit_job(
|
|
1007
|
+
endpoint="videos/upscales",
|
|
1008
|
+
data=form_data,
|
|
1009
|
+
files={field_name: file_tuple},
|
|
1010
|
+
webhook_url=webhook_url,
|
|
1011
|
+
webhook_secret=webhook_secret,
|
|
1012
|
+
)
|
|
1013
|
+
job_id = job_response.data.request_id
|
|
1014
|
+
|
|
1015
|
+
polling_manager = PollingManager(client, job_type="video")
|
|
1016
|
+
result = await polling_manager.poll_until_complete(job_id)
|
|
1017
|
+
|
|
1018
|
+
return build_job_response(result, job_id)
|
|
1019
|
+
|
|
1020
|
+
except ValueError as e:
|
|
1021
|
+
return {"success": False, "error": f"Invalid video format: {str(e)}"}
|
|
1022
|
+
except DeapiAPIError as e:
|
|
1023
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
1024
|
+
except Exception as e:
|
|
1025
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
async def video_upscale_price(
|
|
1029
|
+
model: Annotated[str, Field(description="Video upscaling model name")],
|
|
1030
|
+
video: Annotated[
|
|
1031
|
+
Optional[str],
|
|
1032
|
+
Field(
|
|
1033
|
+
description="Video to quote (URL, data URI or base64); its size and length are read from it. Or pass width, height and duration instead"
|
|
1034
|
+
),
|
|
1035
|
+
] = None,
|
|
1036
|
+
width: Annotated[
|
|
1037
|
+
Optional[int],
|
|
1038
|
+
Field(
|
|
1039
|
+
ge=1,
|
|
1040
|
+
le=10240,
|
|
1041
|
+
description="Video width in pixels. Provide width+height to estimate without uploading a file.",
|
|
1042
|
+
),
|
|
1043
|
+
] = None,
|
|
1044
|
+
height: Annotated[
|
|
1045
|
+
Optional[int],
|
|
1046
|
+
Field(
|
|
1047
|
+
ge=1,
|
|
1048
|
+
le=10240,
|
|
1049
|
+
description="Video height in pixels. Provide width+height to estimate without uploading a file.",
|
|
1050
|
+
),
|
|
1051
|
+
] = None,
|
|
1052
|
+
scale: Annotated[
|
|
1053
|
+
Optional[int],
|
|
1054
|
+
Field(
|
|
1055
|
+
ge=1,
|
|
1056
|
+
le=16,
|
|
1057
|
+
description="Upscale factor, only for models with a 'scale' range in the model list (FlashVSR_Tiny: 2-4). Unset, those models use their smallest factor. Models without a range have a fixed factor (e.g. RealESRGAN_Vid_x4) and refuse this field",
|
|
1058
|
+
),
|
|
1059
|
+
] = None,
|
|
1060
|
+
duration: Annotated[
|
|
1061
|
+
Optional[float],
|
|
1062
|
+
Field(
|
|
1063
|
+
gt=0,
|
|
1064
|
+
description="Video length in seconds; the price is linear in frames. Required unless video is given",
|
|
1065
|
+
),
|
|
1066
|
+
] = None,
|
|
1067
|
+
) -> dict[str, Any]:
|
|
1068
|
+
"""Calculate price for video upscaling.
|
|
1069
|
+
|
|
1070
|
+
Pass the video itself, or its width, height and duration.
|
|
1071
|
+
|
|
1072
|
+
Returns:
|
|
1073
|
+
dict: Contains 'success' and 'price' information
|
|
1074
|
+
"""
|
|
1075
|
+
try:
|
|
1076
|
+
client = get_client()
|
|
1077
|
+
async with client:
|
|
1078
|
+
if not video and (width is None or height is None or duration is None):
|
|
1079
|
+
return input_error_response(
|
|
1080
|
+
"Pass the video, or its width, height and duration: the price follows its size and length."
|
|
1081
|
+
)
|
|
1082
|
+
await ensure_model_cache(model=model)
|
|
1083
|
+
scale, error = resolve_upscale_factor(model, scale)
|
|
1084
|
+
if error:
|
|
1085
|
+
return input_error_response(error)
|
|
1086
|
+
|
|
1087
|
+
form_data = {
|
|
1088
|
+
"model": model,
|
|
1089
|
+
}
|
|
1090
|
+
files = None
|
|
1091
|
+
if video:
|
|
1092
|
+
field_name, file_tuple = await prepare_video_upload_async(video, "video")
|
|
1093
|
+
files = {field_name: file_tuple}
|
|
1094
|
+
|
|
1095
|
+
if width is not None:
|
|
1096
|
+
form_data["width"] = str(width)
|
|
1097
|
+
if height is not None:
|
|
1098
|
+
form_data["height"] = str(height)
|
|
1099
|
+
if scale is not None:
|
|
1100
|
+
form_data["scale"] = str(scale)
|
|
1101
|
+
if duration is not None:
|
|
1102
|
+
form_data["duration"] = str(duration)
|
|
1103
|
+
|
|
1104
|
+
price_response = await client.calculate_price(
|
|
1105
|
+
endpoint="videos/upscales/price",
|
|
1106
|
+
data=form_data,
|
|
1107
|
+
files=files,
|
|
1108
|
+
)
|
|
1109
|
+
|
|
1110
|
+
return build_price_response(price_response)
|
|
1111
|
+
|
|
1112
|
+
except ValueError as e:
|
|
1113
|
+
return {"success": False, "error": f"Invalid video format: {str(e)}"}
|
|
1114
|
+
except DeapiAPIError as e:
|
|
1115
|
+
return {"success": False, "error": f"API error: {str(e)}"}
|
|
1116
|
+
except Exception as e:
|
|
1117
|
+
return {"success": False, "error": f"Unexpected error: {str(e)}"}
|