zerogen 0.2.1__tar.gz → 0.4.0__tar.gz
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.
- zerogen-0.4.0/PKG-INFO +507 -0
- zerogen-0.4.0/README.md +477 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/pyproject.toml +1 -1
- {zerogen-0.2.1 → zerogen-0.4.0}/pyproject.toml.orig +1 -1
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/__init__.py +17 -3
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/_client.py +49 -12
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/_errors.py +60 -2
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/_options.py +2 -1
- zerogen-0.4.0/src/zerogen/_version.py +1 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/__init__.py +6 -0
- zerogen-0.4.0/src/zerogen/resources/_downloads.py +298 -0
- zerogen-0.4.0/src/zerogen/resources/assets.py +739 -0
- zerogen-0.4.0/src/zerogen/resources/attachments.py +342 -0
- zerogen-0.4.0/src/zerogen/resources/folders.py +410 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/labels.py +50 -38
- zerogen-0.4.0/src/zerogen/resources/luts.py +63 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/naming_presets.py +26 -11
- zerogen-0.4.0/src/zerogen/resources/projects.py +75 -0
- zerogen-0.4.0/src/zerogen/resources/runs.py +537 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/uploads.py +86 -3
- zerogen-0.4.0/src/zerogen/types/__init__.py +327 -0
- zerogen-0.4.0/src/zerogen/types/_requests.py +923 -0
- zerogen-0.4.0/src/zerogen/types/_responses.py +1332 -0
- zerogen-0.2.1/PKG-INFO +0 -271
- zerogen-0.2.1/README.md +0 -241
- zerogen-0.2.1/src/zerogen/_version.py +0 -1
- zerogen-0.2.1/src/zerogen/resources/assets.py +0 -445
- zerogen-0.2.1/src/zerogen/resources/folders.py +0 -248
- zerogen-0.2.1/src/zerogen/resources/projects.py +0 -32
- zerogen-0.2.1/src/zerogen/resources/runs.py +0 -222
- zerogen-0.2.1/src/zerogen/types/__init__.py +0 -147
- zerogen-0.2.1/src/zerogen/types/_requests.py +0 -259
- zerogen-0.2.1/src/zerogen/types/_responses.py +0 -721
- {zerogen-0.2.1 → zerogen-0.4.0}/LICENSE +0 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/_models.py +0 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/_pagination.py +0 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/py.typed +0 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/models.py +0 -0
- {zerogen-0.2.1 → zerogen-0.4.0}/src/zerogen/resources/skills.py +0 -0
zerogen-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zerogen
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: The official Python library for the ZeroGen API
|
|
5
|
+
Keywords: zerogen,generative-ai,image-generation,video-generation,api
|
|
6
|
+
Author: ZeroGen LLC
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Dist: anyio>=4.5
|
|
21
|
+
Requires-Dist: httpx>=0.27,<1
|
|
22
|
+
Requires-Dist: pydantic>=2.11,<3
|
|
23
|
+
Requires-Dist: typing-extensions>=4.12
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Project-URL: Homepage, https://github.com/zerogeneration/zerogen-python
|
|
26
|
+
Project-URL: Documentation, https://docs.zerogen.com
|
|
27
|
+
Project-URL: Issues, https://github.com/zerogeneration/zerogen-python/issues
|
|
28
|
+
Project-URL: Changelog, https://github.com/zerogeneration/zerogen-python/blob/main/CHANGELOG.md
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# ZeroGen Python library
|
|
32
|
+
|
|
33
|
+
The official Python client for the [ZeroGen API](https://docs.zerogen.com). It
|
|
34
|
+
runs on Python 3.10 or later, has a synchronous and an asynchronous client, and
|
|
35
|
+
is built on [httpx](https://www.python-httpx.org) and
|
|
36
|
+
[pydantic](https://docs.pydantic.dev).
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install zerogen
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The ZeroGen API refuses requests from browsers, so keep your API key on your
|
|
43
|
+
server. Upgrading from 0.3? See [the migration notes](CHANGELOG.md#migrating-from-03).
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
Create an API key in ZeroGen under **Settings → API keys**, and set it as
|
|
48
|
+
`ZEROGEN_API_KEY`.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import os
|
|
52
|
+
|
|
53
|
+
from zerogen import ZeroGen
|
|
54
|
+
|
|
55
|
+
zerogen = ZeroGen() # Reads ZEROGEN_API_KEY.
|
|
56
|
+
|
|
57
|
+
run = zerogen.runs.create_and_wait(
|
|
58
|
+
{"model": "openai/gpt-image-2/generate", "prompt": "A lighthouse at dawn"}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
os.makedirs("downloads", exist_ok=True)
|
|
62
|
+
for output in run.outputs:
|
|
63
|
+
if output.asset:
|
|
64
|
+
print(output.asset.id, output.asset.mime_type)
|
|
65
|
+
zerogen.runs.download_output_to(run.id, output.asset.id, "downloads")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`create_and_wait` starts a run and returns it when it ends. A run that fails
|
|
69
|
+
returns too, so check its `status`, and each output's: a failed or cancelled run
|
|
70
|
+
can still hold succeeded outputs. Each output's asset is in your organization's
|
|
71
|
+
Library, in the run's project: your team can open it in ZeroGen, you can
|
|
72
|
+
[download it](#downloads), and you can send its ID as an input to a later run.
|
|
73
|
+
To start a run without waiting, use `create`, then `get` or `wait_for` with its
|
|
74
|
+
`id`.
|
|
75
|
+
|
|
76
|
+
A request body is a dict with the API's own field names, such as `projectId`
|
|
77
|
+
and `maxCostMicroUsd`, exactly as the
|
|
78
|
+
[API reference](https://docs.zerogen.com/api-reference/overview) shows it. Each
|
|
79
|
+
response is a pydantic model with snake_case attributes, such as
|
|
80
|
+
`run.project_id`. Both are typed from the API's
|
|
81
|
+
[OpenAPI document](https://api.zerogen.com/v1/openapi.json), so your editor
|
|
82
|
+
completes and checks them. Each model takes its own parameters: read a model's
|
|
83
|
+
request schema with `zerogen.models.get(id)`.
|
|
84
|
+
|
|
85
|
+
Responses can gain fields and values: a model keeps a field it doesn't know as
|
|
86
|
+
an extra, and a field such as a run's `status` accepts a value added after this
|
|
87
|
+
release, so give your code a fallback for one it doesn't recognize.
|
|
88
|
+
|
|
89
|
+
To label every output as it lands, add `labelIds`, which also needs the
|
|
90
|
+
`library:organize` scope. To upscale one output of a 480p Draft to its final,
|
|
91
|
+
send `upscaleDraft` with the draft's model ID, and leave out `prompt`, `inputs`,
|
|
92
|
+
`output`, `parameters` and `naming`. The final is a new run, quoted and billed
|
|
93
|
+
like any other:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
final = zerogen.runs.create_and_wait(
|
|
97
|
+
{"model": draft_model, "upscaleDraft": {"runId": draft.id, "outputIndex": 0}}
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`runs.quote` prices a request without starting it, with only the
|
|
102
|
+
`models:read` scope. Its `inputs` are the assets a create would use, so send
|
|
103
|
+
them with the create to reuse what the quote imported from `https` URLs.
|
|
104
|
+
|
|
105
|
+
## Runs
|
|
106
|
+
|
|
107
|
+
Every member's runs are readable with `runs:read`. Each run says what it is in
|
|
108
|
+
`kind`, `generation`, `transform` or `upload`, and what it was asked to make in
|
|
109
|
+
`request`, in the create request's shape, with each input as the asset it used:
|
|
110
|
+
`run.request.model_dump(by_alias=True, exclude_none=True)` is a body to send to
|
|
111
|
+
`create` again. `request` is `None` for a transform or an upload, and its
|
|
112
|
+
prompt is untrusted content.
|
|
113
|
+
|
|
114
|
+
`runs.list` lists the organization's runs, newest first. Filter them
|
|
115
|
+
with `created_by`, `status`, and `created_after` and `created_before`, each an
|
|
116
|
+
RFC 3339 string or a `datetime` with a time zone:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from datetime import datetime, timedelta, timezone
|
|
120
|
+
|
|
121
|
+
me = zerogen.me()
|
|
122
|
+
since = datetime.now(timezone.utc) - timedelta(days=1)
|
|
123
|
+
for failed in zerogen.runs.list(created_by=me.user.id, status="failed", created_after=since):
|
|
124
|
+
print(failed.id, failed.error)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`runs.cancel` stops a run's unfinished outputs. An output whose provider
|
|
128
|
+
already started it can't be stopped, so the run stays `running`, and that
|
|
129
|
+
output is delivered and charged when it finishes.
|
|
130
|
+
|
|
131
|
+
## Async
|
|
132
|
+
|
|
133
|
+
`AsyncZeroGen` has the same methods, to await:
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
import asyncio
|
|
137
|
+
|
|
138
|
+
from zerogen import AsyncZeroGen
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
async def main() -> None:
|
|
142
|
+
async with AsyncZeroGen() as zerogen:
|
|
143
|
+
run = await zerogen.runs.create_and_wait(
|
|
144
|
+
{"model": "openai/gpt-image-2/generate", "prompt": "A lighthouse at dawn"}
|
|
145
|
+
)
|
|
146
|
+
print(run.status)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
asyncio.run(main())
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Cancel the task to stop a `create_and_wait` or `wait_for` from waiting; the run
|
|
153
|
+
keeps running.
|
|
154
|
+
|
|
155
|
+
## Progress
|
|
156
|
+
|
|
157
|
+
A wait ends when the run's `ended_at` is set, whatever its status, so a status
|
|
158
|
+
added after this release can't keep it waiting. `has_ended(run)` makes the same
|
|
159
|
+
check. To show a run's progress while you wait, pass `wait_for` an `on_update`
|
|
160
|
+
function, which gets the run after each read, and a shorter `poll`:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from zerogen.types import Run
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def report(run: Run) -> None:
|
|
167
|
+
done = sum(output.status == "succeeded" for output in run.outputs)
|
|
168
|
+
print(f"{run.status}: {done} of {len(run.outputs)} outputs")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
run = zerogen.runs.create(request)
|
|
172
|
+
run = zerogen.runs.wait_for(run.id, poll=5, on_update=report)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`poll` is how many seconds each read may wait for the run to end, from 1 to 60,
|
|
176
|
+
and 60 by default. The API answers as soon as the run ends, so a shorter `poll`
|
|
177
|
+
only reports progress more often, at a request each time. With `AsyncZeroGen`,
|
|
178
|
+
`on_update` can also be a coroutine function.
|
|
179
|
+
|
|
180
|
+
To stop waiting from another thread, such as when someone presses Cancel, pass
|
|
181
|
+
`ZeroGen`'s `wait_for` a `threading.Event` as `cancel`. When it's set,
|
|
182
|
+
`wait_for` raises `WaitCancelledError` at once, even partway through a read.
|
|
183
|
+
With `AsyncZeroGen`, cancel the task instead. Either way, the run keeps
|
|
184
|
+
running; to stop it, call `runs.cancel`.
|
|
185
|
+
|
|
186
|
+
## Inputs
|
|
187
|
+
|
|
188
|
+
Give a run an input by asset ID or by `https` URL. To upload a file, use
|
|
189
|
+
`uploads.upload`, which returns when the asset is ready:
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
upload = zerogen.uploads.upload("storyboard.png")
|
|
193
|
+
|
|
194
|
+
run = zerogen.runs.create_and_wait(
|
|
195
|
+
{
|
|
196
|
+
"model": "openai/gpt-image-2/edit",
|
|
197
|
+
"prompt": "Turn the sky into a sunset",
|
|
198
|
+
"inputs": [{"role": "source", "asset": upload.asset_id}],
|
|
199
|
+
}
|
|
200
|
+
)
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`upload` takes a path, the file's `bytes`, or a binary file object that can
|
|
204
|
+
seek. It takes the media type from the filename's extension, or from
|
|
205
|
+
`content_type`. It sends the file's SHA-256 digest, so a file the project
|
|
206
|
+
already has is reused instead of uploaded again.
|
|
207
|
+
|
|
208
|
+
## Transforms
|
|
209
|
+
|
|
210
|
+
A transform makes a free Derived copy of an asset from a recipe: a frame from a
|
|
211
|
+
video, a trimmed, faster, reversed or silent clip, a turn, flip or crop, a LUT
|
|
212
|
+
grade or color correction, a smaller size, another format, or audio of a
|
|
213
|
+
video's sound. It needs the `runs:create` and `runs:read` scopes, because it
|
|
214
|
+
returns a run, whose output's asset is the copy:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
copy = zerogen.assets.transform_and_wait(
|
|
218
|
+
video.id, {"recipe": {"trim": {"startSeconds": 2, "endSeconds": 8}, "removeAudio": True}}
|
|
219
|
+
)
|
|
220
|
+
clip = copy.outputs[0].asset
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
A recipe has one form, chosen by the source: `FrameRecipe`, `ImageRecipe`,
|
|
224
|
+
`VideoRecipe` or `AudioRecipe`, together `TransformRecipe`. For each step, see
|
|
225
|
+
[Transforms](https://docs.zerogen.com/developers/transforms). `transform` starts
|
|
226
|
+
one without waiting. Your earlier run of the same recipe on the same asset, for
|
|
227
|
+
the same destination, comes back with `reused` set instead of running again,
|
|
228
|
+
unless it failed. To grade with a LUT, name one from `luts.list()` as
|
|
229
|
+
`lut.lutId`. Test keys can't transform assets.
|
|
230
|
+
|
|
231
|
+
## Declared provenance
|
|
232
|
+
|
|
233
|
+
When you upload a file made outside ZeroGen, such as a ComfyUI result, the
|
|
234
|
+
upload can declare what it was made from and with. ZeroGen records the
|
|
235
|
+
declaration as a no-charge run, so the asset's lineage shows its sources, and
|
|
236
|
+
its details show the tool, prompt and workflow as a declaration, unchecked:
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
saved = zerogen.uploads.upload(
|
|
240
|
+
"render.png",
|
|
241
|
+
declaration={
|
|
242
|
+
"tool": {
|
|
243
|
+
"name": "comfyui",
|
|
244
|
+
"version": "0.43.0",
|
|
245
|
+
"integration": {"name": "zerogen-nodes", "version": "0.1.0"},
|
|
246
|
+
},
|
|
247
|
+
"sources": [{"asset": upload.asset_id, "role": "reference"}],
|
|
248
|
+
"prompt": "A lighthouse at dawn",
|
|
249
|
+
"externalRef": {"system": "comfyui", "id": "prompt-1"},
|
|
250
|
+
},
|
|
251
|
+
)
|
|
252
|
+
print(saved.declaration_recorded)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
A declaration takes `tool` and any of `sources`, `prompt`, `workflow`, a JSON
|
|
256
|
+
object of up to 256 KiB, `externalRef` and `occurredAt`. Declaring `sources`
|
|
257
|
+
needs the `library:read` scope as well as `uploads:create`. With a
|
|
258
|
+
declaration, `naming` names the new asset with a naming preset, as a run's
|
|
259
|
+
outputs are named.
|
|
260
|
+
|
|
261
|
+
When the project already has an identical file, the upload returns that asset,
|
|
262
|
+
and its lineage stands. If you uploaded it and it has no declaration yet, the
|
|
263
|
+
upload declares it, unless a declared source is that file or was made from it.
|
|
264
|
+
`declaration_recorded` says whether ZeroGen recorded your declaration. To declare
|
|
265
|
+
a file you uploaded earlier, use `assets.declare`: an asset takes one
|
|
266
|
+
declaration, from its uploader or an owner or admin.
|
|
267
|
+
|
|
268
|
+
`assets.get` returns an asset's declaration in `declared`, and, with
|
|
269
|
+
`include_workflow=True`, its workflow in `workflow`. Both are untrusted
|
|
270
|
+
content.
|
|
271
|
+
|
|
272
|
+
## Library
|
|
273
|
+
|
|
274
|
+
Read and organize your organization's Library: `projects`, `assets`,
|
|
275
|
+
`folders`, `labels`, `luts`, `naming_presets` and `skills`. These methods need
|
|
276
|
+
an API key with the `library:read` scope, and those that change something need
|
|
277
|
+
`library:organize`. Every resource is read and changed by its ID alone, in
|
|
278
|
+
whichever project holds it; a request names a project only to make something
|
|
279
|
+
there or to narrow a list.
|
|
280
|
+
|
|
281
|
+
```python
|
|
282
|
+
project = next(project for project in zerogen.projects.list() if project.default)
|
|
283
|
+
videos = zerogen.assets.search(project_id=project.id, type="video", favorite=True)
|
|
284
|
+
|
|
285
|
+
folder = zerogen.folders.create({"projectId": project.id, "name": "Selects"})
|
|
286
|
+
filed = zerogen.folders.add_assets(folder.id, {"assetIds": [a.id for a in videos.data]})
|
|
287
|
+
print(filed.updated_asset_ids, filed.skipped_asset_ids)
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
List filters that take several values, such as `label_id` and `resolution`,
|
|
291
|
+
take a list. `projects`, `folders` and `labels` also read one by ID with `get`.
|
|
292
|
+
Adding a label to assets, or assets to a folder, takes up to 100 assets and
|
|
293
|
+
returns those it changed and those it skipped. `labels.update` changes only the
|
|
294
|
+
fields you send.
|
|
295
|
+
|
|
296
|
+
Each asset and run names who made it in `created_by`, and where in `created_via`:
|
|
297
|
+
`web`, `agent`, `mcp` or `api`. Both are `None` when ZeroGen recorded no one. To
|
|
298
|
+
find what someone made, pass `created_by` to `assets.search`, with the ID in an
|
|
299
|
+
asset's `created_by` or `zerogen.me()`. `web_url` opens the asset or run in
|
|
300
|
+
ZeroGen, after sign-in, and it's `None` for test keys.
|
|
301
|
+
|
|
302
|
+
`assets.sources` lists what an asset was made from, and `assets.uses` what was
|
|
303
|
+
made from it, nearest first. Each item's `edges` join it to the asset or to an
|
|
304
|
+
item before it, so you can rebuild the graph as you page.
|
|
305
|
+
|
|
306
|
+
`assets.delete` moves an asset to Recently deleted, where it leaves search, its
|
|
307
|
+
folders and run inputs until `assets.restore` puts it back. `folders.delete`
|
|
308
|
+
deletes a folder and its subfolders. You choose what happens to their assets:
|
|
309
|
+
`"keep"` preserves them, and `"trash"` sends them to Recently deleted, including
|
|
310
|
+
assets in other folders. Both deletes need the `library:delete` scope, which has
|
|
311
|
+
to be granted explicitly, and test keys can't use it. Within an hour,
|
|
312
|
+
`folders.restore` undoes your own folder deletion:
|
|
313
|
+
|
|
314
|
+
```python
|
|
315
|
+
deleted = zerogen.folders.delete(folder.id, assets="keep")
|
|
316
|
+
zerogen.folders.restore({"eventId": deleted.event_id})
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
To file into a folder path, such as a shot's `SH010/anim`, use
|
|
320
|
+
`folders.ensure_path`. It returns the last folder, creating each one on the
|
|
321
|
+
way that isn't there yet, and reuses a folder of the same name, so clients
|
|
322
|
+
saving into the same path at once share its folders:
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
anim = zerogen.folders.ensure_path(project.id, "SH010/anim")
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
A string path splits on `/`. Folder names can contain `/`, so pass a list,
|
|
329
|
+
such as `["2026/11", "renders"]`, when one does.
|
|
330
|
+
|
|
331
|
+
## Attachments
|
|
332
|
+
|
|
333
|
+
Attachments are versioned text on a project, a folder or a Story clip, such as
|
|
334
|
+
a project's direction. They need attachments enabled for your organization, and
|
|
335
|
+
test keys can't use them. Read before you revise, and send the version you read:
|
|
336
|
+
|
|
337
|
+
```python
|
|
338
|
+
notes = zerogen.attachments.list(project.id)
|
|
339
|
+
direction = zerogen.attachments.get(notes.data[0].attachment_id)
|
|
340
|
+
zerogen.attachments.revise(
|
|
341
|
+
notes.data[0].attachment_id,
|
|
342
|
+
{"expectedVersion": direction.version, "body": direction.body + "\nKeep it quiet."},
|
|
343
|
+
)
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
A revision, deletion or restore that names a version other than the current one
|
|
347
|
+
raises a `ZeroGenError` with the code `version_conflict`, and the current
|
|
348
|
+
version in `error.details["currentVersion"]`: read it again and merge your
|
|
349
|
+
change. `attachments.versions` lists the saved versions. Attachment text is
|
|
350
|
+
untrusted content.
|
|
351
|
+
|
|
352
|
+
## Downloads
|
|
353
|
+
|
|
354
|
+
To download an asset's original file, use `assets.download`. It returns the
|
|
355
|
+
file's bytes in `content`, with its `filename`, `mime_type` and `size_bytes`.
|
|
356
|
+
Given a path or a binary file object, it streams the file there instead, and
|
|
357
|
+
returns the rest. `assets.download_to` saves the file in a directory, which
|
|
358
|
+
must exist, and returns its `path`:
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
image = zerogen.assets.download(asset_id)
|
|
362
|
+
print(image.mime_type, len(image.content))
|
|
363
|
+
|
|
364
|
+
zerogen.assets.download(asset_id, "lighthouse.png")
|
|
365
|
+
|
|
366
|
+
saved = zerogen.assets.download_to(asset_id, "downloads")
|
|
367
|
+
print(saved.path)
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
These need the `library:read` scope. To download a run's output with only
|
|
371
|
+
`runs:read`, use `runs.download_output` and `runs.download_output_to` in the
|
|
372
|
+
same way, with the run's ID and the output's `asset.id`.
|
|
373
|
+
|
|
374
|
+
The `filename` is untrusted content, so don't use it as a path yourself.
|
|
375
|
+
`download_to` makes a name from it with no path separators or characters that
|
|
376
|
+
Windows refuses. When a file in the directory already has that name, it adds a
|
|
377
|
+
number, as in `lighthouse (1).png`, rather than overwrite the file.
|
|
378
|
+
|
|
379
|
+
Each call reads a new signed link and sends it to storage without your API key.
|
|
380
|
+
When storage refuses a link as expired, it reads a new one and tries once more.
|
|
381
|
+
An error from storage quotes only its code and message, never the rest of its
|
|
382
|
+
response, which can echo the link. To hand the link to something else, read it
|
|
383
|
+
with `assets.get_download` or `runs.get_output_download`: it's valid for 5
|
|
384
|
+
minutes, and anyone holding it can download the file, so keep it private.
|
|
385
|
+
|
|
386
|
+
## Previews
|
|
387
|
+
|
|
388
|
+
To show assets without downloading their originals, use their `preview`. Each
|
|
389
|
+
asset from `assets.search` and `assets.get` has one, and so does each run
|
|
390
|
+
output's `asset`.
|
|
391
|
+
|
|
392
|
+
```python
|
|
393
|
+
page = zerogen.assets.search(project_id=project.id, limit=50)
|
|
394
|
+
tiles = [
|
|
395
|
+
{
|
|
396
|
+
"id": asset.id,
|
|
397
|
+
"image": asset.preview.image512_url if asset.preview else None,
|
|
398
|
+
"clip": asset.preview.video_url if asset.preview else None,
|
|
399
|
+
}
|
|
400
|
+
for asset in page.data
|
|
401
|
+
]
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
`image96_url` and `image512_url` are WebP images up to 96 and 512 pixels wide.
|
|
405
|
+
`video_url` is a silent WebM clip of a video's first six seconds; it's `None`
|
|
406
|
+
for other assets and until the clip is made. `preview` itself is `None` until
|
|
407
|
+
the images are made, which can take up to about 15 minutes after a run, and for
|
|
408
|
+
assets with none, such as audio.
|
|
409
|
+
|
|
410
|
+
The URLs are signed storage URLs, so load them without your API key: a browser
|
|
411
|
+
can load one as an `<img>` element's `src`. They work until `expires_at`, 5
|
|
412
|
+
minutes later, and anyone holding them can load the previews until then, so
|
|
413
|
+
keep them out of logs. After `expires_at`, read the page again for new ones:
|
|
414
|
+
one `assets.search` call refreshes every tile on it, where `assets.get` would
|
|
415
|
+
take a request for each. For a single asset or a run, read it again.
|
|
416
|
+
|
|
417
|
+
## Lists
|
|
418
|
+
|
|
419
|
+
Every list method returns one page, and iterating the page reads every item on
|
|
420
|
+
every page after it:
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
for model in zerogen.models.list(capability="video"):
|
|
424
|
+
print(model.id)
|
|
425
|
+
|
|
426
|
+
page = zerogen.runs.list(limit=20)
|
|
427
|
+
print(page.data, page.next_cursor)
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
With `AsyncZeroGen`, iterate a list with `async for`, or await it for one page.
|
|
431
|
+
|
|
432
|
+
## Errors
|
|
433
|
+
|
|
434
|
+
A request the API refuses raises a `ZeroGenError`. Branch on its `code`, which
|
|
435
|
+
is stable; its `message` is for people. A `rate_limited` or
|
|
436
|
+
`permission_denied` error also says why in `reason`, such as `running_cap` or
|
|
437
|
+
`scope_required`, and `details` holds the error's other facts:
|
|
438
|
+
|
|
439
|
+
```python
|
|
440
|
+
from zerogen import ZeroGenError
|
|
441
|
+
|
|
442
|
+
try:
|
|
443
|
+
zerogen.runs.create({"model": "openai/gpt-image-2/generate", "prompt": ""})
|
|
444
|
+
except ZeroGenError as error:
|
|
445
|
+
print(error.status, error.code, error.reason, error.param, error.request_id)
|
|
446
|
+
if error.reason == "scope_required":
|
|
447
|
+
print("The key needs", (error.details or {}).get("scope"))
|
|
448
|
+
raise
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
The API can add codes and reasons, so handle one you don't know as you would
|
|
452
|
+
the error without it, by its `retryable` and `status`. A request that gets no
|
|
453
|
+
response, because the connection failed or the request timed out, raises a
|
|
454
|
+
`ZeroGenConnectionError`. For every code and reason, see
|
|
455
|
+
[Errors](https://docs.zerogen.com/developers/errors).
|
|
456
|
+
|
|
457
|
+
## Retries and idempotency
|
|
458
|
+
|
|
459
|
+
The client retries connection errors and errors the API marks `retryable`,
|
|
460
|
+
twice by default, with backoff. For `429` responses, it waits for
|
|
461
|
+
`Retry-After`, unless that's more than a minute.
|
|
462
|
+
|
|
463
|
+
Every `POST`, `PATCH` and `DELETE` sends an `Idempotency-Key`, and a call's
|
|
464
|
+
retries reuse it, so a retried create never starts a second run. To retry
|
|
465
|
+
safely across calls or processes, pass your own key:
|
|
466
|
+
|
|
467
|
+
```python
|
|
468
|
+
zerogen.runs.create(request, idempotency_key=job.id)
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## Options
|
|
472
|
+
|
|
473
|
+
```python
|
|
474
|
+
import os
|
|
475
|
+
|
|
476
|
+
zerogen = ZeroGen(
|
|
477
|
+
api_key=os.environ["ZEROGEN_API_KEY"],
|
|
478
|
+
max_retries=2,
|
|
479
|
+
timeout=120, # Seconds per attempt. Waits on a run add their wait.
|
|
480
|
+
)
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
| Option | Default | Description |
|
|
484
|
+
| --- | --- | --- |
|
|
485
|
+
| `api_key` | `ZEROGEN_API_KEY` | The API key. |
|
|
486
|
+
| `base_url` | `ZEROGEN_BASE_URL`, then `https://api.zerogen.com/v1` | The API's base URL. |
|
|
487
|
+
| `max_retries` | `2` | Retries for each request. |
|
|
488
|
+
| `timeout` | `120` | Seconds httpx waits on each step of an attempt. A request that waits on a run gets its wait added. |
|
|
489
|
+
| `default_headers` | None | Headers to send with every request. |
|
|
490
|
+
| `user_agent` | None | Your app's name and version, such as `zerogen-nodes/0.1`, which the `User-Agent` header names before this library's. |
|
|
491
|
+
| `http_client` | A new `httpx.Client` or `httpx.AsyncClient` | The httpx client to send requests with, such as one with your proxy settings. |
|
|
492
|
+
|
|
493
|
+
Every method also takes request options as keyword arguments: `timeout`,
|
|
494
|
+
`max_retries`, `headers`, and `idempotency_key` for a `POST`, `PATCH` or
|
|
495
|
+
`DELETE`. Close a client you're done with, or use it as a context manager.
|
|
496
|
+
|
|
497
|
+
## Test keys
|
|
498
|
+
|
|
499
|
+
A test key runs generation requests on placeholder providers and charges
|
|
500
|
+
nothing. Its runs can rehearse failures with the `test` field, and it reaches
|
|
501
|
+
only the API sandbox project. It can't transform, delete or restore assets, or
|
|
502
|
+
use attachments. For details, see
|
|
503
|
+
[Test keys](https://docs.zerogen.com/test-keys).
|
|
504
|
+
|
|
505
|
+
## License
|
|
506
|
+
|
|
507
|
+
MIT
|