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
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
"""Public realtime sessions: :class:`AsyncRealtime` and its synchronous twin :class:`Realtime`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import concurrent.futures
|
|
7
|
+
import contextlib
|
|
8
|
+
import inspect
|
|
9
|
+
import os
|
|
10
|
+
import queue
|
|
11
|
+
import threading
|
|
12
|
+
import time
|
|
13
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
|
|
14
|
+
from typing import TYPE_CHECKING, Any, Protocol, TypeVar
|
|
15
|
+
|
|
16
|
+
from deapi_sdk._exceptions import RealtimeError
|
|
17
|
+
from deapi_sdk._polling import AsyncJob, Job, _timeout_error
|
|
18
|
+
from deapi_sdk._realtime import _protocol as protocol
|
|
19
|
+
from deapi_sdk._realtime._discovery import parse_websocket_config
|
|
20
|
+
from deapi_sdk._realtime._engine import RECONNECTED, Closed, Engine, SinkItem
|
|
21
|
+
from deapi_sdk._types import JobStatus
|
|
22
|
+
from deapi_sdk._version import __version__
|
|
23
|
+
from deapi_sdk.types.common import JobResult, JobStatusEvent
|
|
24
|
+
from deapi_sdk.types.websocket import WebSocketConfig
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from types import TracebackType
|
|
28
|
+
|
|
29
|
+
from deapi_sdk._client import AsyncHTTPClient, BaseClient, SyncHTTPClient
|
|
30
|
+
|
|
31
|
+
_T = TypeVar("_T")
|
|
32
|
+
|
|
33
|
+
ProgressCallback = Callable[[JobStatusEvent], Any]
|
|
34
|
+
"""Called with every status event of the awaited job. The async API also accepts ``async def`` callbacks."""
|
|
35
|
+
|
|
36
|
+
_TERMINAL = (JobStatus.DONE, JobStatus.ERROR)
|
|
37
|
+
# After the socket reports a terminal status, re-check the job over HTTP this often until it agrees.
|
|
38
|
+
_CONFIRM_INTERVAL = 1.0
|
|
39
|
+
# Longest single blocking wait in the sync API, so Ctrl+C stays responsive.
|
|
40
|
+
_SYNC_WAIT_SLICE = 0.5
|
|
41
|
+
_EXECUTOR_SHUTDOWN_TIMEOUT = 5.0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class HasRequestId(Protocol):
|
|
45
|
+
@property
|
|
46
|
+
def request_id(self) -> str: ...
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
JobRef = str | HasRequestId
|
|
50
|
+
"""A submitted job: a ``Job`` / ``AsyncJob`` handle or its ``request_id``."""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _request_id_of(job: JobRef) -> str:
|
|
54
|
+
request_id = job if isinstance(job, str) else job.request_id
|
|
55
|
+
if not request_id:
|
|
56
|
+
raise ValueError("request_id must not be empty")
|
|
57
|
+
return request_id
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _is_terminal(result: JobResult) -> bool:
|
|
61
|
+
return result.status in _TERMINAL
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _env(name: str) -> str | None:
|
|
65
|
+
return os.environ.get(name, "").strip() or None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _Settings:
|
|
69
|
+
"""Connection settings: argument, then environment, then the account's ``GET /websocket`` response.
|
|
70
|
+
|
|
71
|
+
Nothing here does I/O. When an argument/environment value is missing, :meth:`apply`
|
|
72
|
+
fills the gaps from the discovery response at connect time (once; the result is cached).
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __init__(
|
|
76
|
+
self,
|
|
77
|
+
http: BaseClient,
|
|
78
|
+
client_id: str | int | None,
|
|
79
|
+
ws_url: str | None,
|
|
80
|
+
app_key: str | None,
|
|
81
|
+
connect_timeout: float,
|
|
82
|
+
) -> None:
|
|
83
|
+
config = http._config
|
|
84
|
+
if connect_timeout <= 0:
|
|
85
|
+
raise ValueError("connect_timeout must be positive")
|
|
86
|
+
self.client_id: str | None = (str(client_id).strip() if client_id is not None else None) or _env(
|
|
87
|
+
"DEAPI_CLIENT_ID"
|
|
88
|
+
)
|
|
89
|
+
self._ws_url = ws_url or _env("DEAPI_WS_URL")
|
|
90
|
+
self._app_key = app_key or _env("DEAPI_WS_APP_KEY")
|
|
91
|
+
if self._ws_url is not None:
|
|
92
|
+
protocol.check_ws_url(self._ws_url)
|
|
93
|
+
self.channel: str | None = None
|
|
94
|
+
self.url: str | None = None
|
|
95
|
+
self.discovered = False
|
|
96
|
+
self.auth_url = f"{config.base_url.rstrip('/')}{protocol.BROADCASTING_AUTH_PATH}"
|
|
97
|
+
self.discovery_url: str = http._resolve_endpoint("websocket")
|
|
98
|
+
self.user_agent = config._user_agent
|
|
99
|
+
self.connect_timeout = connect_timeout
|
|
100
|
+
if self.client_id and self._ws_url and self._app_key:
|
|
101
|
+
self._finish(self.client_id, self._ws_url, self._app_key, protocol.channel_for(self.client_id))
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def ready(self) -> bool:
|
|
105
|
+
return self.url is not None
|
|
106
|
+
|
|
107
|
+
def _finish(self, client_id: str, ws_url: str, app_key: str, channel: str) -> None:
|
|
108
|
+
self.client_id = client_id
|
|
109
|
+
self.channel = channel
|
|
110
|
+
self.url = protocol.build_url(ws_url, app_key, __version__)
|
|
111
|
+
|
|
112
|
+
def apply(self, discovered: WebSocketConfig) -> None:
|
|
113
|
+
"""Fill every missing field from the discovery response (explicit values keep precedence)."""
|
|
114
|
+
conn = discovered.connection
|
|
115
|
+
if self.client_id:
|
|
116
|
+
client_id, channel = self.client_id, protocol.channel_for(self.client_id)
|
|
117
|
+
else:
|
|
118
|
+
# The server's channel is authoritative for the account it announced.
|
|
119
|
+
client_id, channel = discovered.client_id.strip(), discovered.channel
|
|
120
|
+
self._finish(client_id, self._ws_url or conn.url, self._app_key or conn.key, channel)
|
|
121
|
+
if discovered.auth_endpoint:
|
|
122
|
+
self.auth_url = discovered.auth_endpoint
|
|
123
|
+
self.discovered = True
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _auth_signature(resp: Any) -> str:
|
|
127
|
+
auth = resp.get("auth") if isinstance(resp, dict) else None
|
|
128
|
+
if not isinstance(auth, str) or not auth:
|
|
129
|
+
raise RealtimeError("Channel authorization response did not include an auth signature")
|
|
130
|
+
return auth
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _raise_closed(item: Closed) -> None:
|
|
134
|
+
if item.error is not None:
|
|
135
|
+
raise item.error
|
|
136
|
+
raise RealtimeError("The realtime session was closed")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _check_wait_args(max_wait: float, poll_interval: float) -> None:
|
|
140
|
+
if max_wait <= 0:
|
|
141
|
+
raise ValueError("max_wait must be positive")
|
|
142
|
+
if poll_interval <= 0:
|
|
143
|
+
raise ValueError("poll_interval must be positive")
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class AsyncRealtime:
|
|
147
|
+
"""Real-time job status updates over one WebSocket connection (asyncio).
|
|
148
|
+
|
|
149
|
+
Create it with :meth:`AsyncDeapiClient.realtime` and use it as an async
|
|
150
|
+
context manager::
|
|
151
|
+
|
|
152
|
+
async with client.realtime(client_id="...") as rt:
|
|
153
|
+
job = await client.images.generate(...)
|
|
154
|
+
result = await rt.wait(job, on_progress=lambda e: print(e.progress))
|
|
155
|
+
|
|
156
|
+
The connection subscribes to the private channel of ``client_id`` (the Client
|
|
157
|
+
ID shown in your deAPI dashboard), reconnects automatically with backoff and
|
|
158
|
+
re-authorizes the channel after every reconnect.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
def __init__(
|
|
162
|
+
self,
|
|
163
|
+
http: AsyncHTTPClient,
|
|
164
|
+
*,
|
|
165
|
+
client_id: str | int | None = None,
|
|
166
|
+
ws_url: str | None = None,
|
|
167
|
+
app_key: str | None = None,
|
|
168
|
+
connect_timeout: float = 30.0,
|
|
169
|
+
) -> None:
|
|
170
|
+
self._http = http
|
|
171
|
+
self._settings = _Settings(http, client_id, ws_url, app_key, connect_timeout)
|
|
172
|
+
self._engine = Engine(
|
|
173
|
+
url=self._settings.url,
|
|
174
|
+
channel=self._settings.channel,
|
|
175
|
+
authorize=self._authorize,
|
|
176
|
+
user_agent=self._settings.user_agent,
|
|
177
|
+
connect_timeout=connect_timeout,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def client_id(self) -> str | None:
|
|
182
|
+
"""The Client ID, or ``None`` until :meth:`connect` when it was not passed or set in the environment."""
|
|
183
|
+
return self._settings.client_id
|
|
184
|
+
|
|
185
|
+
def _discovered(self, config: WebSocketConfig) -> None:
|
|
186
|
+
self._settings.apply(config)
|
|
187
|
+
self._engine.configure(self._settings.url or "", self._settings.channel or "")
|
|
188
|
+
|
|
189
|
+
@property
|
|
190
|
+
def connected(self) -> bool:
|
|
191
|
+
"""True while the channel subscription is live (False while reconnecting or after close)."""
|
|
192
|
+
return self._engine.connected
|
|
193
|
+
|
|
194
|
+
async def connect(self) -> None:
|
|
195
|
+
"""Open the connection and subscribe. Raises :class:`RealtimeError` if that fails.
|
|
196
|
+
|
|
197
|
+
Connection parameters that were not supplied are fetched first from ``GET /websocket``.
|
|
198
|
+
"""
|
|
199
|
+
if not self._settings.ready:
|
|
200
|
+
resp = await self._http.get(self._settings.discovery_url)
|
|
201
|
+
self._discovered(parse_websocket_config(resp))
|
|
202
|
+
await self._engine.start()
|
|
203
|
+
|
|
204
|
+
async def close(self) -> None:
|
|
205
|
+
"""Close the connection. Pending :meth:`wait` calls and :meth:`events` iterators stop. Idempotent."""
|
|
206
|
+
await self._engine.close()
|
|
207
|
+
|
|
208
|
+
async def __aenter__(self) -> AsyncRealtime:
|
|
209
|
+
await self.connect()
|
|
210
|
+
return self
|
|
211
|
+
|
|
212
|
+
async def __aexit__(
|
|
213
|
+
self,
|
|
214
|
+
exc_type: type[BaseException] | None,
|
|
215
|
+
exc: BaseException | None,
|
|
216
|
+
tb: TracebackType | None,
|
|
217
|
+
) -> None:
|
|
218
|
+
await self.close()
|
|
219
|
+
|
|
220
|
+
async def _authorize(self, socket_id: str, channel: str) -> str:
|
|
221
|
+
resp = await self._http.post(self._settings.auth_url, json={"socket_id": socket_id, "channel_name": channel})
|
|
222
|
+
return _auth_signature(resp)
|
|
223
|
+
|
|
224
|
+
async def wait(
|
|
225
|
+
self,
|
|
226
|
+
job: JobRef,
|
|
227
|
+
*,
|
|
228
|
+
on_progress: ProgressCallback | None = None,
|
|
229
|
+
max_wait: float = 300.0,
|
|
230
|
+
poll_interval: float = 10.0,
|
|
231
|
+
) -> JobResult:
|
|
232
|
+
"""Wait for a job to finish and return its final :class:`JobResult`.
|
|
233
|
+
|
|
234
|
+
Status events from the socket drive ``on_progress`` and completion; the
|
|
235
|
+
returned result always comes from the job status endpoint (fresh
|
|
236
|
+
``result_url``, all fields). Failed jobs are usually not announced over
|
|
237
|
+
the socket, so the status is also checked over HTTP right away, every
|
|
238
|
+
``poll_interval`` seconds, after each reconnect and as soon as the socket
|
|
239
|
+
reports ``done`` or ``error``. A failed job is
|
|
240
|
+
returned with ``status == "error"``, like :meth:`AsyncJob.wait`.
|
|
241
|
+
|
|
242
|
+
Raises :class:`JobTimeoutError` after ``max_wait`` seconds.
|
|
243
|
+
"""
|
|
244
|
+
_check_wait_args(max_wait, poll_interval)
|
|
245
|
+
self._engine.require_started()
|
|
246
|
+
request_id = _request_id_of(job)
|
|
247
|
+
handle = AsyncJob(request_id, self._http, self._http._resolve_endpoint("request_status"))
|
|
248
|
+
inbox: asyncio.Queue[SinkItem] = asyncio.Queue()
|
|
249
|
+
sink_id = self._engine.add_sink(request_id, inbox.put_nowait)
|
|
250
|
+
loop = asyncio.get_running_loop()
|
|
251
|
+
try:
|
|
252
|
+
deadline = loop.time() + max_wait
|
|
253
|
+
result = await handle.status()
|
|
254
|
+
if _is_terminal(result):
|
|
255
|
+
return result
|
|
256
|
+
next_check = loop.time() + poll_interval
|
|
257
|
+
done_seen = False # the socket reported done/error; confirm over HTTP until the endpoint agrees
|
|
258
|
+
while True:
|
|
259
|
+
now = loop.time()
|
|
260
|
+
if now >= deadline:
|
|
261
|
+
raise _timeout_error(request_id, max_wait)
|
|
262
|
+
item: SinkItem | None = None
|
|
263
|
+
with contextlib.suppress(TimeoutError):
|
|
264
|
+
# asyncio.timeout(), not wait_for(): keeps task cancellation reliable on 3.11
|
|
265
|
+
async with asyncio.timeout(min(next_check, deadline) - now):
|
|
266
|
+
item = await inbox.get()
|
|
267
|
+
if isinstance(item, Closed):
|
|
268
|
+
_raise_closed(item)
|
|
269
|
+
if isinstance(item, JobStatusEvent):
|
|
270
|
+
if on_progress is not None:
|
|
271
|
+
outcome = on_progress(item)
|
|
272
|
+
if inspect.isawaitable(outcome):
|
|
273
|
+
await outcome
|
|
274
|
+
if item.status not in _TERMINAL:
|
|
275
|
+
continue
|
|
276
|
+
done_seen = True
|
|
277
|
+
next_check = min(next_check, loop.time())
|
|
278
|
+
if item is None or item is RECONNECTED or loop.time() >= next_check:
|
|
279
|
+
result = await handle.status()
|
|
280
|
+
if _is_terminal(result):
|
|
281
|
+
return result
|
|
282
|
+
next_check = loop.time() + (_CONFIRM_INTERVAL if done_seen else poll_interval)
|
|
283
|
+
finally:
|
|
284
|
+
self._engine.remove_sink(sink_id)
|
|
285
|
+
|
|
286
|
+
async def events(self) -> AsyncIterator[JobStatusEvent]:
|
|
287
|
+
"""Iterate over status events of every job on the channel, as they arrive.
|
|
288
|
+
|
|
289
|
+
Only events received after iteration starts are yielded; the iterator
|
|
290
|
+
ends when the session is closed and raises if the connection fails for good.
|
|
291
|
+
Events sent while the connection was down are not replayed.
|
|
292
|
+
"""
|
|
293
|
+
self._engine.require_started()
|
|
294
|
+
inbox: asyncio.Queue[SinkItem] = asyncio.Queue()
|
|
295
|
+
sink_id = self._engine.add_sink(None, inbox.put_nowait)
|
|
296
|
+
try:
|
|
297
|
+
while True:
|
|
298
|
+
item = await inbox.get()
|
|
299
|
+
if isinstance(item, Closed):
|
|
300
|
+
if item.error is not None:
|
|
301
|
+
raise item.error
|
|
302
|
+
return
|
|
303
|
+
if isinstance(item, JobStatusEvent):
|
|
304
|
+
yield item
|
|
305
|
+
finally:
|
|
306
|
+
self._engine.remove_sink(sink_id)
|
|
307
|
+
|
|
308
|
+
def __repr__(self) -> str:
|
|
309
|
+
return f"AsyncRealtime(client_id={self.client_id!r}, connected={self.connected})"
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
class _LoopThread:
|
|
313
|
+
"""A private asyncio event loop running in a daemon thread."""
|
|
314
|
+
|
|
315
|
+
def __init__(self) -> None:
|
|
316
|
+
self._loop = asyncio.new_event_loop()
|
|
317
|
+
self._thread = threading.Thread(target=self._loop.run_forever, name="deapi-realtime", daemon=True)
|
|
318
|
+
self._thread.start()
|
|
319
|
+
|
|
320
|
+
def run(self, coro: Awaitable[_T]) -> _T:
|
|
321
|
+
future = asyncio.run_coroutine_threadsafe(_as_coroutine(coro), self._loop)
|
|
322
|
+
while True:
|
|
323
|
+
try:
|
|
324
|
+
return future.result(timeout=_SYNC_WAIT_SLICE)
|
|
325
|
+
except concurrent.futures.TimeoutError:
|
|
326
|
+
continue
|
|
327
|
+
except BaseException:
|
|
328
|
+
future.cancel()
|
|
329
|
+
raise
|
|
330
|
+
|
|
331
|
+
def stop(self) -> None:
|
|
332
|
+
if self._loop.is_closed():
|
|
333
|
+
return
|
|
334
|
+
# Let in-flight worker-thread calls (channel authorization) finish, bounded.
|
|
335
|
+
shutdown = asyncio.run_coroutine_threadsafe(self._loop.shutdown_default_executor(), self._loop)
|
|
336
|
+
with contextlib.suppress(concurrent.futures.TimeoutError):
|
|
337
|
+
shutdown.result(timeout=_EXECUTOR_SHUTDOWN_TIMEOUT)
|
|
338
|
+
self._loop.call_soon_threadsafe(self._loop.stop)
|
|
339
|
+
self._thread.join()
|
|
340
|
+
self._loop.close()
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
async def _as_coroutine(awaitable: Awaitable[_T]) -> _T:
|
|
344
|
+
return await awaitable
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _get(inbox: queue.Queue[SinkItem], timeout: float) -> SinkItem | None:
|
|
348
|
+
"""Blocking get with a timeout, in short slices so KeyboardInterrupt is not delayed."""
|
|
349
|
+
end = time.monotonic() + timeout
|
|
350
|
+
while True:
|
|
351
|
+
remaining = end - time.monotonic()
|
|
352
|
+
if remaining <= 0:
|
|
353
|
+
return None
|
|
354
|
+
try:
|
|
355
|
+
return inbox.get(timeout=min(remaining, _SYNC_WAIT_SLICE))
|
|
356
|
+
except queue.Empty:
|
|
357
|
+
continue
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
class Realtime:
|
|
361
|
+
"""Real-time job status updates over one WebSocket connection (synchronous).
|
|
362
|
+
|
|
363
|
+
Create it with :meth:`DeapiClient.realtime` and use it as a context manager::
|
|
364
|
+
|
|
365
|
+
with client.realtime(client_id="...") as rt:
|
|
366
|
+
job = client.images.generate(...)
|
|
367
|
+
result = rt.wait(job, on_progress=lambda e: print(e.progress))
|
|
368
|
+
|
|
369
|
+
The connection runs in a background thread; ``on_progress`` callbacks and
|
|
370
|
+
:meth:`events` run in the calling thread. See :class:`AsyncRealtime` for the
|
|
371
|
+
connection behaviour.
|
|
372
|
+
"""
|
|
373
|
+
|
|
374
|
+
def __init__(
|
|
375
|
+
self,
|
|
376
|
+
http: SyncHTTPClient,
|
|
377
|
+
*,
|
|
378
|
+
client_id: str | int | None = None,
|
|
379
|
+
ws_url: str | None = None,
|
|
380
|
+
app_key: str | None = None,
|
|
381
|
+
connect_timeout: float = 30.0,
|
|
382
|
+
) -> None:
|
|
383
|
+
self._http = http
|
|
384
|
+
self._settings = _Settings(http, client_id, ws_url, app_key, connect_timeout)
|
|
385
|
+
self._engine = Engine(
|
|
386
|
+
url=self._settings.url,
|
|
387
|
+
channel=self._settings.channel,
|
|
388
|
+
authorize=self._authorize,
|
|
389
|
+
user_agent=self._settings.user_agent,
|
|
390
|
+
connect_timeout=connect_timeout,
|
|
391
|
+
)
|
|
392
|
+
self._thread: _LoopThread | None = None
|
|
393
|
+
self._closed = False
|
|
394
|
+
self._lock = threading.Lock()
|
|
395
|
+
|
|
396
|
+
@property
|
|
397
|
+
def client_id(self) -> str | None:
|
|
398
|
+
"""The Client ID, or ``None`` until :meth:`connect` when it was not passed or set in the environment."""
|
|
399
|
+
return self._settings.client_id
|
|
400
|
+
|
|
401
|
+
def _discovered(self, config: WebSocketConfig) -> None:
|
|
402
|
+
self._settings.apply(config)
|
|
403
|
+
self._engine.configure(self._settings.url or "", self._settings.channel or "")
|
|
404
|
+
|
|
405
|
+
@property
|
|
406
|
+
def connected(self) -> bool:
|
|
407
|
+
"""True while the channel subscription is live (False while reconnecting or after close)."""
|
|
408
|
+
return self._engine.connected
|
|
409
|
+
|
|
410
|
+
def connect(self) -> None:
|
|
411
|
+
"""Open the connection and subscribe. Raises :class:`RealtimeError` if that fails.
|
|
412
|
+
|
|
413
|
+
Connection parameters that were not supplied are fetched first from ``GET /websocket``
|
|
414
|
+
(a blocking HTTP call in the calling thread).
|
|
415
|
+
"""
|
|
416
|
+
with self._lock:
|
|
417
|
+
started = self._thread is not None
|
|
418
|
+
if not started and not self._settings.ready:
|
|
419
|
+
self._discovered(parse_websocket_config(self._http.get(self._settings.discovery_url)))
|
|
420
|
+
with self._lock:
|
|
421
|
+
if self._thread is not None:
|
|
422
|
+
raise RealtimeError("This realtime session was already started; create a new one to reconnect.")
|
|
423
|
+
self._thread = _LoopThread()
|
|
424
|
+
try:
|
|
425
|
+
self._thread.run(self._engine.start())
|
|
426
|
+
except BaseException:
|
|
427
|
+
self.close()
|
|
428
|
+
raise
|
|
429
|
+
|
|
430
|
+
def close(self) -> None:
|
|
431
|
+
"""Close the connection and stop its thread. Pending waits and event iterators stop. Idempotent."""
|
|
432
|
+
with self._lock:
|
|
433
|
+
thread, closing = self._thread, not self._closed
|
|
434
|
+
self._closed = True
|
|
435
|
+
if thread is None or not closing:
|
|
436
|
+
return
|
|
437
|
+
try:
|
|
438
|
+
thread.run(self._engine.close())
|
|
439
|
+
finally:
|
|
440
|
+
thread.stop()
|
|
441
|
+
|
|
442
|
+
def __enter__(self) -> Realtime:
|
|
443
|
+
self.connect()
|
|
444
|
+
return self
|
|
445
|
+
|
|
446
|
+
def __exit__(
|
|
447
|
+
self,
|
|
448
|
+
exc_type: type[BaseException] | None,
|
|
449
|
+
exc: BaseException | None,
|
|
450
|
+
tb: TracebackType | None,
|
|
451
|
+
) -> None:
|
|
452
|
+
self.close()
|
|
453
|
+
|
|
454
|
+
async def _authorize(self, socket_id: str, channel: str) -> str:
|
|
455
|
+
# Runs on the realtime thread; the blocking HTTP call goes to a worker thread.
|
|
456
|
+
resp = await asyncio.to_thread(
|
|
457
|
+
self._http.post, self._settings.auth_url, json={"socket_id": socket_id, "channel_name": channel}
|
|
458
|
+
)
|
|
459
|
+
return _auth_signature(resp)
|
|
460
|
+
|
|
461
|
+
def wait(
|
|
462
|
+
self,
|
|
463
|
+
job: JobRef,
|
|
464
|
+
*,
|
|
465
|
+
on_progress: ProgressCallback | None = None,
|
|
466
|
+
max_wait: float = 300.0,
|
|
467
|
+
poll_interval: float = 10.0,
|
|
468
|
+
) -> JobResult:
|
|
469
|
+
"""Wait for a job to finish and return its final :class:`JobResult`.
|
|
470
|
+
|
|
471
|
+
Same semantics as :meth:`AsyncRealtime.wait`; ``on_progress`` runs in the calling thread.
|
|
472
|
+
"""
|
|
473
|
+
_check_wait_args(max_wait, poll_interval)
|
|
474
|
+
self._engine.require_started()
|
|
475
|
+
request_id = _request_id_of(job)
|
|
476
|
+
handle = Job(request_id, self._http, self._http._resolve_endpoint("request_status"))
|
|
477
|
+
inbox: queue.Queue[SinkItem] = queue.Queue()
|
|
478
|
+
sink_id = self._engine.add_sink(request_id, inbox.put_nowait)
|
|
479
|
+
try:
|
|
480
|
+
deadline = time.monotonic() + max_wait
|
|
481
|
+
result = handle.status()
|
|
482
|
+
if _is_terminal(result):
|
|
483
|
+
return result
|
|
484
|
+
next_check = time.monotonic() + poll_interval
|
|
485
|
+
done_seen = False # the socket reported done/error; confirm over HTTP until the endpoint agrees
|
|
486
|
+
while True:
|
|
487
|
+
now = time.monotonic()
|
|
488
|
+
if now >= deadline:
|
|
489
|
+
raise _timeout_error(request_id, max_wait)
|
|
490
|
+
item = _get(inbox, min(next_check, deadline) - now)
|
|
491
|
+
if isinstance(item, Closed):
|
|
492
|
+
_raise_closed(item)
|
|
493
|
+
if isinstance(item, JobStatusEvent):
|
|
494
|
+
if on_progress is not None:
|
|
495
|
+
on_progress(item)
|
|
496
|
+
if item.status not in _TERMINAL:
|
|
497
|
+
continue
|
|
498
|
+
done_seen = True
|
|
499
|
+
next_check = min(next_check, time.monotonic())
|
|
500
|
+
if item is None or item is RECONNECTED or time.monotonic() >= next_check:
|
|
501
|
+
result = handle.status()
|
|
502
|
+
if _is_terminal(result):
|
|
503
|
+
return result
|
|
504
|
+
next_check = time.monotonic() + (_CONFIRM_INTERVAL if done_seen else poll_interval)
|
|
505
|
+
finally:
|
|
506
|
+
self._engine.remove_sink(sink_id)
|
|
507
|
+
|
|
508
|
+
def events(self) -> Iterator[JobStatusEvent]:
|
|
509
|
+
"""Iterate over status events of every job on the channel, as they arrive (blocking).
|
|
510
|
+
|
|
511
|
+
Same semantics as :meth:`AsyncRealtime.events`.
|
|
512
|
+
"""
|
|
513
|
+
self._engine.require_started()
|
|
514
|
+
inbox: queue.Queue[SinkItem] = queue.Queue()
|
|
515
|
+
sink_id = self._engine.add_sink(None, inbox.put_nowait)
|
|
516
|
+
try:
|
|
517
|
+
while True:
|
|
518
|
+
item = _get(inbox, float("inf"))
|
|
519
|
+
if isinstance(item, Closed):
|
|
520
|
+
if item.error is not None:
|
|
521
|
+
raise item.error
|
|
522
|
+
return
|
|
523
|
+
if isinstance(item, JobStatusEvent):
|
|
524
|
+
yield item
|
|
525
|
+
finally:
|
|
526
|
+
self._engine.remove_sink(sink_id)
|
|
527
|
+
|
|
528
|
+
def __repr__(self) -> str:
|
|
529
|
+
return f"Realtime(client_id={self.client_id!r}, connected={self.connected})"
|
deapi_sdk/_types.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class JobStatus(StrEnum):
|
|
7
|
+
PENDING = "pending"
|
|
8
|
+
PROCESSING = "processing"
|
|
9
|
+
DONE = "done"
|
|
10
|
+
ERROR = "error"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InferenceType(StrEnum):
|
|
14
|
+
TXT2IMG = "txt2img"
|
|
15
|
+
IMG2IMG = "img2img"
|
|
16
|
+
TXT2AUDIO = "txt2audio"
|
|
17
|
+
TXT2MUSIC = "txt2music"
|
|
18
|
+
VIDEO2TEXT = "video2text"
|
|
19
|
+
AUDIO2TEXT = "audio2text"
|
|
20
|
+
IMG2TXT = "img2txt"
|
|
21
|
+
IMG2VIDEO = "img2video"
|
|
22
|
+
TXT2VIDEO = "txt2video"
|
|
23
|
+
TXT2EMBEDDING = "txt2embedding"
|
|
24
|
+
VIDEO_FILE2TEXT = "video_file2text"
|
|
25
|
+
AUDIO_FILE2TEXT = "audio_file2text"
|
|
26
|
+
IMAGE_UPSCALE = "img-upscale"
|
|
27
|
+
IMAGE_REMOVE_BG = "img-rmbg"
|
|
28
|
+
VIDEO_UPSCALE = "vid-upscale"
|
|
29
|
+
AUDIO2VIDEO = "audio2video"
|
|
30
|
+
VIDEO2DESC = "video2desc"
|
|
31
|
+
VIDEO_FILE2DESC = "video_file2desc"
|
deapi_sdk/_version.py
ADDED
deapi_sdk/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from deapi_sdk.resources.account import AccountResource, AsyncAccountResource
|
|
4
|
+
from deapi_sdk.resources.audio import AsyncAudio, Audio
|
|
5
|
+
from deapi_sdk.resources.embeddings import AsyncEmbeddings, Embeddings
|
|
6
|
+
from deapi_sdk.resources.images import AsyncImages, Images
|
|
7
|
+
from deapi_sdk.resources.keys import AsyncKeysResource, KeysResource
|
|
8
|
+
from deapi_sdk.resources.models import AsyncModels, Models
|
|
9
|
+
from deapi_sdk.resources.ocr import OCR, AsyncOCR
|
|
10
|
+
from deapi_sdk.resources.prompts import AsyncPrompts, Prompts
|
|
11
|
+
from deapi_sdk.resources.transcription import AsyncTranscription, Transcription
|
|
12
|
+
from deapi_sdk.resources.video import AsyncVideo, Video
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"OCR",
|
|
16
|
+
"AccountResource",
|
|
17
|
+
"AsyncAccountResource",
|
|
18
|
+
"AsyncAudio",
|
|
19
|
+
"AsyncEmbeddings",
|
|
20
|
+
"AsyncImages",
|
|
21
|
+
"AsyncKeysResource",
|
|
22
|
+
"AsyncModels",
|
|
23
|
+
"AsyncOCR",
|
|
24
|
+
"AsyncPrompts",
|
|
25
|
+
"AsyncTranscription",
|
|
26
|
+
"AsyncVideo",
|
|
27
|
+
"Audio",
|
|
28
|
+
"Embeddings",
|
|
29
|
+
"Images",
|
|
30
|
+
"KeysResource",
|
|
31
|
+
"Models",
|
|
32
|
+
"Prompts",
|
|
33
|
+
"Transcription",
|
|
34
|
+
"Video",
|
|
35
|
+
]
|