deapi-sdk 0.6.1__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_sdk/__init__.py +409 -0
- deapi_sdk/_cards.py +313 -0
- deapi_sdk/_client.py +411 -0
- deapi_sdk/_config.py +77 -0
- deapi_sdk/_constants.py +61 -0
- deapi_sdk/_download.py +37 -0
- deapi_sdk/_exceptions.py +184 -0
- deapi_sdk/_files.py +59 -0
- deapi_sdk/_payload.py +50 -0
- deapi_sdk/_polling.py +157 -0
- deapi_sdk/_realtime/__init__.py +7 -0
- deapi_sdk/_realtime/_discovery.py +43 -0
- deapi_sdk/_realtime/_engine.py +419 -0
- deapi_sdk/_realtime/_protocol.py +152 -0
- deapi_sdk/_realtime/_session.py +529 -0
- deapi_sdk/_types.py +31 -0
- deapi_sdk/_version.py +3 -0
- deapi_sdk/py.typed +0 -0
- deapi_sdk/resources/__init__.py +35 -0
- deapi_sdk/resources/account.py +149 -0
- deapi_sdk/resources/audio.py +386 -0
- deapi_sdk/resources/embeddings.py +116 -0
- deapi_sdk/resources/images.py +616 -0
- deapi_sdk/resources/keys.py +277 -0
- deapi_sdk/resources/models.py +161 -0
- deapi_sdk/resources/ocr.py +152 -0
- deapi_sdk/resources/prompts.py +137 -0
- deapi_sdk/resources/transcription.py +308 -0
- deapi_sdk/resources/video.py +985 -0
- deapi_sdk/transcript.py +142 -0
- deapi_sdk/types/__init__.py +68 -0
- deapi_sdk/types/account.py +115 -0
- deapi_sdk/types/audio.py +35 -0
- deapi_sdk/types/common.py +174 -0
- deapi_sdk/types/embeddings.py +11 -0
- deapi_sdk/types/images.py +40 -0
- deapi_sdk/types/keys.py +53 -0
- deapi_sdk/types/ocr.py +14 -0
- deapi_sdk/types/prompts.py +26 -0
- deapi_sdk/types/transcription.py +14 -0
- deapi_sdk/types/video.py +84 -0
- deapi_sdk/types/websocket.py +36 -0
- deapi_sdk/webhook.py +159 -0
- deapi_sdk-0.6.1.dist-info/METADATA +644 -0
- deapi_sdk-0.6.1.dist-info/RECORD +47 -0
- deapi_sdk-0.6.1.dist-info/WHEEL +4 -0
- deapi_sdk-0.6.1.dist-info/licenses/LICENSE +21 -0
deapi_sdk/__init__.py
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
"""deAPI Python SDK — Official client for the deAPI distributed AI inference platform."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import weakref
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from deapi_sdk._client import AsyncHTTPClient, SyncHTTPClient
|
|
11
|
+
from deapi_sdk._config import ClientConfig
|
|
12
|
+
from deapi_sdk._exceptions import (
|
|
13
|
+
AccountSuspendedError,
|
|
14
|
+
ActiveKeyLimitReachedError,
|
|
15
|
+
AuthenticationError,
|
|
16
|
+
DeapiError,
|
|
17
|
+
InsufficientBalanceError,
|
|
18
|
+
InsufficientPermissionsError,
|
|
19
|
+
IpNotAllowedError,
|
|
20
|
+
JobTimeoutError,
|
|
21
|
+
KeyLimitExceededError,
|
|
22
|
+
NetworkError,
|
|
23
|
+
NotFoundError,
|
|
24
|
+
PayloadTooLargeError,
|
|
25
|
+
PaymentRequiredError,
|
|
26
|
+
PermissionDeniedError,
|
|
27
|
+
RateLimitError,
|
|
28
|
+
RealtimeError,
|
|
29
|
+
ServerError,
|
|
30
|
+
ValidationError,
|
|
31
|
+
)
|
|
32
|
+
from deapi_sdk._polling import AsyncJob, AsyncJobs, Job, Jobs
|
|
33
|
+
from deapi_sdk._realtime import AsyncRealtime, Realtime
|
|
34
|
+
from deapi_sdk._realtime._discovery import parse_websocket_config
|
|
35
|
+
from deapi_sdk._types import InferenceType, JobStatus
|
|
36
|
+
from deapi_sdk._version import __version__
|
|
37
|
+
from deapi_sdk.transcript import (
|
|
38
|
+
Transcript,
|
|
39
|
+
TranscriptSegment,
|
|
40
|
+
TranscriptWord,
|
|
41
|
+
parse_transcript,
|
|
42
|
+
transcript_output_format,
|
|
43
|
+
)
|
|
44
|
+
from deapi_sdk.types.common import (
|
|
45
|
+
Balance,
|
|
46
|
+
JobResult,
|
|
47
|
+
JobStatusEvent,
|
|
48
|
+
JobSubmission,
|
|
49
|
+
ModelInfo,
|
|
50
|
+
ModelsResponse,
|
|
51
|
+
PriceResult,
|
|
52
|
+
)
|
|
53
|
+
from deapi_sdk.types.video import VideoDescription, VideoEvent, parse_video_description
|
|
54
|
+
from deapi_sdk.types.websocket import WebSocketConfig, WebSocketConnection
|
|
55
|
+
from deapi_sdk.webhook import (
|
|
56
|
+
InvalidSignatureError,
|
|
57
|
+
JobPrice,
|
|
58
|
+
WebhookEvent,
|
|
59
|
+
WebhookEventData,
|
|
60
|
+
construct_event,
|
|
61
|
+
verify_signature,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _attach_resources_sync(client: DeapiClient) -> None:
|
|
66
|
+
from deapi_sdk.resources import (
|
|
67
|
+
OCR,
|
|
68
|
+
AccountResource,
|
|
69
|
+
Audio,
|
|
70
|
+
Embeddings,
|
|
71
|
+
Images,
|
|
72
|
+
KeysResource,
|
|
73
|
+
Models,
|
|
74
|
+
Prompts,
|
|
75
|
+
Transcription,
|
|
76
|
+
Video,
|
|
77
|
+
)
|
|
78
|
+
client.images = Images(client._http_client)
|
|
79
|
+
client.models = Models(client._http_client)
|
|
80
|
+
client.audio = Audio(client._http_client)
|
|
81
|
+
client.transcription = Transcription(client._http_client)
|
|
82
|
+
client.video = Video(client._http_client)
|
|
83
|
+
client.embeddings = Embeddings(client._http_client)
|
|
84
|
+
client.prompts = Prompts(client._http_client)
|
|
85
|
+
client.ocr = OCR(client._http_client)
|
|
86
|
+
client.jobs = Jobs(client._http_client)
|
|
87
|
+
client.account = AccountResource(client._http_client)
|
|
88
|
+
client.keys = KeysResource(client._http_client)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _attach_resources_async(client: AsyncDeapiClient) -> None:
|
|
92
|
+
from deapi_sdk.resources import (
|
|
93
|
+
AsyncAccountResource,
|
|
94
|
+
AsyncAudio,
|
|
95
|
+
AsyncEmbeddings,
|
|
96
|
+
AsyncImages,
|
|
97
|
+
AsyncKeysResource,
|
|
98
|
+
AsyncModels,
|
|
99
|
+
AsyncOCR,
|
|
100
|
+
AsyncPrompts,
|
|
101
|
+
AsyncTranscription,
|
|
102
|
+
AsyncVideo,
|
|
103
|
+
)
|
|
104
|
+
client.images = AsyncImages(client._http_client)
|
|
105
|
+
client.models = AsyncModels(client._http_client)
|
|
106
|
+
client.audio = AsyncAudio(client._http_client)
|
|
107
|
+
client.transcription = AsyncTranscription(client._http_client)
|
|
108
|
+
client.video = AsyncVideo(client._http_client)
|
|
109
|
+
client.embeddings = AsyncEmbeddings(client._http_client)
|
|
110
|
+
client.prompts = AsyncPrompts(client._http_client)
|
|
111
|
+
client.ocr = AsyncOCR(client._http_client)
|
|
112
|
+
client.jobs = AsyncJobs(client._http_client)
|
|
113
|
+
client.account = AsyncAccountResource(client._http_client)
|
|
114
|
+
client.keys = AsyncKeysResource(client._http_client)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class DeapiClient:
|
|
118
|
+
"""Synchronous deAPI client.
|
|
119
|
+
|
|
120
|
+
``fill_model_defaults`` (default on; env ``DEAPI_FILL_MODEL_DEFAULTS=0`` disables): generation
|
|
121
|
+
parameters you leave unset (width, height, steps, frames, fps, lang, ...) are taken from the
|
|
122
|
+
model card's ``info.defaults`` (one catalogue request per client, cached), and ``seed`` defaults
|
|
123
|
+
to -1 (random) where the API requires it. Pass ``False`` to send exactly what you set.
|
|
124
|
+
|
|
125
|
+
Usage::
|
|
126
|
+
|
|
127
|
+
client = DeapiClient(api_key="sk-...")
|
|
128
|
+
job = client.images.generate(prompt="a cat in space", model="Flux1schnell", width=1024, height=1024, seed=42)
|
|
129
|
+
result = job.wait()
|
|
130
|
+
print(result.result_url)
|
|
131
|
+
|
|
132
|
+
Or as a context manager::
|
|
133
|
+
|
|
134
|
+
with DeapiClient(api_key="sk-...") as client:
|
|
135
|
+
...
|
|
136
|
+
"""
|
|
137
|
+
|
|
138
|
+
images: Any
|
|
139
|
+
models: Any
|
|
140
|
+
audio: Any
|
|
141
|
+
transcription: Any
|
|
142
|
+
video: Any
|
|
143
|
+
embeddings: Any
|
|
144
|
+
prompts: Any
|
|
145
|
+
ocr: Any
|
|
146
|
+
jobs: Any
|
|
147
|
+
account: Any
|
|
148
|
+
keys: Any
|
|
149
|
+
|
|
150
|
+
def __init__(
|
|
151
|
+
self,
|
|
152
|
+
*,
|
|
153
|
+
api_key: str | None = None,
|
|
154
|
+
base_url: str | None = None,
|
|
155
|
+
timeout: float | None = None,
|
|
156
|
+
max_retries: int | None = None,
|
|
157
|
+
api_version: str | None = None,
|
|
158
|
+
fill_model_defaults: bool | None = None,
|
|
159
|
+
max_retry_after: float | None = None,
|
|
160
|
+
http_client: httpx.Client | None = None,
|
|
161
|
+
owns_http_client: bool | None = None,
|
|
162
|
+
) -> None:
|
|
163
|
+
self._config = ClientConfig.from_env(
|
|
164
|
+
api_key=api_key,
|
|
165
|
+
base_url=base_url,
|
|
166
|
+
timeout=timeout,
|
|
167
|
+
max_retries=max_retries,
|
|
168
|
+
api_version=api_version,
|
|
169
|
+
fill_model_defaults=fill_model_defaults,
|
|
170
|
+
max_retry_after=max_retry_after,
|
|
171
|
+
)
|
|
172
|
+
self._http_client = SyncHTTPClient(
|
|
173
|
+
self._config, http_client, owns_http_client=owns_http_client
|
|
174
|
+
)
|
|
175
|
+
self._realtime_sessions: weakref.WeakSet[Realtime] = weakref.WeakSet()
|
|
176
|
+
self._attach_resources()
|
|
177
|
+
|
|
178
|
+
def _attach_resources(self) -> None:
|
|
179
|
+
_attach_resources_sync(self)
|
|
180
|
+
|
|
181
|
+
def balance(self) -> Balance:
|
|
182
|
+
"""Get current account balance."""
|
|
183
|
+
url = self._http_client._resolve_endpoint("balance")
|
|
184
|
+
resp = self._http_client.get(url)
|
|
185
|
+
return Balance.model_validate(resp.get("data", resp))
|
|
186
|
+
|
|
187
|
+
def websocket_config(self) -> WebSocketConfig:
|
|
188
|
+
"""Realtime WebSocket parameters of this account (``GET /websocket``).
|
|
189
|
+
|
|
190
|
+
Contains the Client ID, channel, event name, authorization endpoint and the
|
|
191
|
+
connection details (host, port, TLS, public app key). No secrets.
|
|
192
|
+
:meth:`realtime` uses it automatically when parameters are not supplied.
|
|
193
|
+
"""
|
|
194
|
+
resp = self._http_client.get(self._http_client._resolve_endpoint("websocket"))
|
|
195
|
+
return parse_websocket_config(resp)
|
|
196
|
+
|
|
197
|
+
def realtime(
|
|
198
|
+
self,
|
|
199
|
+
client_id: str | int | None = None,
|
|
200
|
+
*,
|
|
201
|
+
ws_url: str | None = None,
|
|
202
|
+
app_key: str | None = None,
|
|
203
|
+
connect_timeout: float = 30.0,
|
|
204
|
+
) -> Realtime:
|
|
205
|
+
"""Real-time job status updates over WebSocket (needs ``pip install "deapi-sdk[ws]"``).
|
|
206
|
+
|
|
207
|
+
Every parameter is optional. Each one is taken from the argument, else from its
|
|
208
|
+
environment variable (``DEAPI_CLIENT_ID``, ``DEAPI_WS_URL``, ``DEAPI_WS_APP_KEY``),
|
|
209
|
+
else from ``GET /websocket``, which the SDK calls once at connect time when
|
|
210
|
+
something is still missing (see :meth:`websocket_config`). With all three
|
|
211
|
+
set, no extra request is made. ``client_id`` is the Client ID shown in your
|
|
212
|
+
deAPI dashboard; servers acting for many users should pass it explicitly.
|
|
213
|
+
|
|
214
|
+
The connection opens on ``with`` / ``connect()``. It is closed together
|
|
215
|
+
with this client.
|
|
216
|
+
"""
|
|
217
|
+
session = Realtime(
|
|
218
|
+
self._http_client, client_id=client_id, ws_url=ws_url, app_key=app_key, connect_timeout=connect_timeout
|
|
219
|
+
)
|
|
220
|
+
self._realtime_sessions.add(session)
|
|
221
|
+
return session
|
|
222
|
+
|
|
223
|
+
def close(self) -> None:
|
|
224
|
+
"""Close open realtime sessions and the underlying HTTP client."""
|
|
225
|
+
for session in list(self._realtime_sessions):
|
|
226
|
+
session.close()
|
|
227
|
+
self._http_client.close()
|
|
228
|
+
|
|
229
|
+
def __enter__(self) -> DeapiClient:
|
|
230
|
+
return self
|
|
231
|
+
|
|
232
|
+
def __exit__(self, *args: Any) -> None:
|
|
233
|
+
self.close()
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
class AsyncDeapiClient:
|
|
237
|
+
"""Asynchronous deAPI client.
|
|
238
|
+
|
|
239
|
+
Takes the same ``fill_model_defaults`` option as :class:`DeapiClient`.
|
|
240
|
+
|
|
241
|
+
Usage::
|
|
242
|
+
|
|
243
|
+
async with AsyncDeapiClient(api_key="sk-...") as client:
|
|
244
|
+
job = await client.images.generate(prompt="a cat in space", model="Flux1schnell", seed=42)
|
|
245
|
+
result = await job.wait()
|
|
246
|
+
print(result.result_url)
|
|
247
|
+
"""
|
|
248
|
+
|
|
249
|
+
images: Any
|
|
250
|
+
models: Any
|
|
251
|
+
audio: Any
|
|
252
|
+
transcription: Any
|
|
253
|
+
video: Any
|
|
254
|
+
embeddings: Any
|
|
255
|
+
prompts: Any
|
|
256
|
+
ocr: Any
|
|
257
|
+
jobs: Any
|
|
258
|
+
account: Any
|
|
259
|
+
keys: Any
|
|
260
|
+
|
|
261
|
+
def __init__(
|
|
262
|
+
self,
|
|
263
|
+
*,
|
|
264
|
+
api_key: str | None = None,
|
|
265
|
+
base_url: str | None = None,
|
|
266
|
+
timeout: float | None = None,
|
|
267
|
+
max_retries: int | None = None,
|
|
268
|
+
api_version: str | None = None,
|
|
269
|
+
fill_model_defaults: bool | None = None,
|
|
270
|
+
max_retry_after: float | None = None,
|
|
271
|
+
http_client: httpx.AsyncClient | None = None,
|
|
272
|
+
owns_http_client: bool | None = None,
|
|
273
|
+
) -> None:
|
|
274
|
+
self._config = ClientConfig.from_env(
|
|
275
|
+
api_key=api_key,
|
|
276
|
+
base_url=base_url,
|
|
277
|
+
timeout=timeout,
|
|
278
|
+
max_retries=max_retries,
|
|
279
|
+
api_version=api_version,
|
|
280
|
+
fill_model_defaults=fill_model_defaults,
|
|
281
|
+
max_retry_after=max_retry_after,
|
|
282
|
+
)
|
|
283
|
+
self._http_client = AsyncHTTPClient(
|
|
284
|
+
self._config, http_client, owns_http_client=owns_http_client
|
|
285
|
+
)
|
|
286
|
+
self._realtime_sessions: weakref.WeakSet[AsyncRealtime] = weakref.WeakSet()
|
|
287
|
+
self._attach_resources()
|
|
288
|
+
|
|
289
|
+
def _attach_resources(self) -> None:
|
|
290
|
+
_attach_resources_async(self)
|
|
291
|
+
|
|
292
|
+
async def balance(self) -> Balance:
|
|
293
|
+
"""Get current account balance."""
|
|
294
|
+
url = self._http_client._resolve_endpoint("balance")
|
|
295
|
+
resp = await self._http_client.get(url)
|
|
296
|
+
return Balance.model_validate(resp.get("data", resp))
|
|
297
|
+
|
|
298
|
+
async def websocket_config(self) -> WebSocketConfig:
|
|
299
|
+
"""Realtime WebSocket parameters of this account (``GET /websocket``).
|
|
300
|
+
|
|
301
|
+
Contains the Client ID, channel, event name, authorization endpoint and the
|
|
302
|
+
connection details (host, port, TLS, public app key). No secrets.
|
|
303
|
+
:meth:`realtime` uses it automatically when parameters are not supplied.
|
|
304
|
+
"""
|
|
305
|
+
resp = await self._http_client.get(self._http_client._resolve_endpoint("websocket"))
|
|
306
|
+
return parse_websocket_config(resp)
|
|
307
|
+
|
|
308
|
+
def realtime(
|
|
309
|
+
self,
|
|
310
|
+
client_id: str | int | None = None,
|
|
311
|
+
*,
|
|
312
|
+
ws_url: str | None = None,
|
|
313
|
+
app_key: str | None = None,
|
|
314
|
+
connect_timeout: float = 30.0,
|
|
315
|
+
) -> AsyncRealtime:
|
|
316
|
+
"""Real-time job status updates over WebSocket (needs ``pip install "deapi-sdk[ws]"``).
|
|
317
|
+
|
|
318
|
+
Every parameter is optional. Each one is taken from the argument, else from its
|
|
319
|
+
environment variable (``DEAPI_CLIENT_ID``, ``DEAPI_WS_URL``, ``DEAPI_WS_APP_KEY``),
|
|
320
|
+
else from ``GET /websocket``, which the SDK calls once at connect time when
|
|
321
|
+
something is still missing (see :meth:`websocket_config`). With all three
|
|
322
|
+
set, no extra request is made. ``client_id`` is the Client ID shown in your
|
|
323
|
+
deAPI dashboard; servers acting for many users should pass it explicitly.
|
|
324
|
+
|
|
325
|
+
The connection opens on ``async with`` /
|
|
326
|
+
``await connect()``. It is closed together
|
|
327
|
+
with this client.
|
|
328
|
+
"""
|
|
329
|
+
session = AsyncRealtime(
|
|
330
|
+
self._http_client, client_id=client_id, ws_url=ws_url, app_key=app_key, connect_timeout=connect_timeout
|
|
331
|
+
)
|
|
332
|
+
self._realtime_sessions.add(session)
|
|
333
|
+
return session
|
|
334
|
+
|
|
335
|
+
async def close(self) -> None:
|
|
336
|
+
"""Close open realtime sessions and the underlying HTTP client."""
|
|
337
|
+
for session in list(self._realtime_sessions):
|
|
338
|
+
await session.close()
|
|
339
|
+
await self._http_client.close()
|
|
340
|
+
|
|
341
|
+
async def __aenter__(self) -> AsyncDeapiClient:
|
|
342
|
+
return self
|
|
343
|
+
|
|
344
|
+
async def __aexit__(self, *args: Any) -> None:
|
|
345
|
+
await self.close()
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
__all__ = [
|
|
349
|
+
# Clients
|
|
350
|
+
"DeapiClient",
|
|
351
|
+
"AsyncDeapiClient",
|
|
352
|
+
# Jobs
|
|
353
|
+
"Job",
|
|
354
|
+
"AsyncJob",
|
|
355
|
+
"AsyncJobs",
|
|
356
|
+
"Jobs",
|
|
357
|
+
# Realtime (WebSocket)
|
|
358
|
+
"AsyncRealtime",
|
|
359
|
+
"Realtime",
|
|
360
|
+
"JobStatusEvent",
|
|
361
|
+
# Types
|
|
362
|
+
"Balance",
|
|
363
|
+
"InferenceType",
|
|
364
|
+
"JobResult",
|
|
365
|
+
"JobStatus",
|
|
366
|
+
"JobSubmission",
|
|
367
|
+
"ModelInfo",
|
|
368
|
+
"ModelsResponse",
|
|
369
|
+
"PriceResult",
|
|
370
|
+
"WebSocketConfig",
|
|
371
|
+
"WebSocketConnection",
|
|
372
|
+
# Results
|
|
373
|
+
"Transcript",
|
|
374
|
+
"TranscriptSegment",
|
|
375
|
+
"TranscriptWord",
|
|
376
|
+
"VideoDescription",
|
|
377
|
+
"VideoEvent",
|
|
378
|
+
"parse_transcript",
|
|
379
|
+
"parse_video_description",
|
|
380
|
+
"transcript_output_format",
|
|
381
|
+
# Exceptions
|
|
382
|
+
"AccountSuspendedError",
|
|
383
|
+
"ActiveKeyLimitReachedError",
|
|
384
|
+
"AuthenticationError",
|
|
385
|
+
"NetworkError",
|
|
386
|
+
"DeapiError",
|
|
387
|
+
"InsufficientBalanceError",
|
|
388
|
+
"InsufficientPermissionsError",
|
|
389
|
+
"IpNotAllowedError",
|
|
390
|
+
"JobTimeoutError",
|
|
391
|
+
"KeyLimitExceededError",
|
|
392
|
+
"PayloadTooLargeError",
|
|
393
|
+
"PaymentRequiredError",
|
|
394
|
+
"PermissionDeniedError",
|
|
395
|
+
"NotFoundError",
|
|
396
|
+
"RateLimitError",
|
|
397
|
+
"RealtimeError",
|
|
398
|
+
"ServerError",
|
|
399
|
+
"ValidationError",
|
|
400
|
+
# Webhook
|
|
401
|
+
"InvalidSignatureError",
|
|
402
|
+
"WebhookEvent",
|
|
403
|
+
"WebhookEventData",
|
|
404
|
+
"JobPrice",
|
|
405
|
+
"construct_event",
|
|
406
|
+
"verify_signature",
|
|
407
|
+
# Version
|
|
408
|
+
"__version__",
|
|
409
|
+
]
|