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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# WanGP Python API

`shared/api.py` provides a lightweight in-process wrapper over WanGP's existing generation path.

The main goal is to let third-party code call WanGP directly, keep the last loaded model alive across requests, receive structured progress updates, and still capture the same stdout/stderr output that would normally go to the console.

**Please note that use of the WanGP API is subject to the WanGP Terms and Conditions. Any product that integrates WanGP should clearly disclose that it uses WanGP in both its user interface and its documentation.**

## Quick Start

```python

from pathlib import Path



from shared.api import init



session = init(

    root=Path(r"C:\WanGP"),

    cli_args=["--attention", "sdpa", "--profile", "4"],

)



settings = {

    "model_type": "ltx2_22B_distilled",

    "prompt": "Cinematic shot of a neon train entering a rainy station",

    "resolution": "1280x704",

    "num_inference_steps": 8,

    "video_length": 97,

    "duration_seconds": 4,

    "force_fps": 24,

}



job = session.submit_task(settings)



for event in job.events.iter(timeout=0.2):

    if event.kind == "progress":

        progress = event.data

        print(progress.phase, progress.progress, progress.current_step, progress.total_steps)

    elif event.kind == "preview":

        preview = event.data

        if preview.image is not None:

            preview.image.save("preview.png")

    elif event.kind == "stream":

        line = event.data

        print(f"[{line.stream}] {line.text}")



result = job.result()

if result.success:

    print(result.generated_files)

else:

    for error in result.errors:

        print(error.message)

```

## Main Entry Points

- `init(...) -> WanGPSession`
  - Creates a reusable session and eagerly loads the runtime.
- `WanGPSession.submit(source) -> SessionJob`
  - Starts a job from a settings dict, a manifest list, or a saved `.json` / `.zip` file.
- `WanGPSession.submit_task(settings) -> SessionJob`
  - Preferred single-task entrypoint.
- `WanGPSession.submit_manifest(settings_list) -> SessionJob`
  - Batch entrypoint for multiple tasks.
- `SessionJob.result() -> GenerationResult`
  - Waits for completion and returns a structured result object.
- `SessionJob.cancel()`
  - Requests cancellation of the active generation.

## `init(...)` Parameters

```python

session = init(

    root=Path(r"C:\WanGP"),

    config_path=Path(r"C:\WanGP\wgp_config.json"),  # optional

    output_dir=Path(r"C:\WanGP\outputs_override"),  # optional

    callbacks=MyCallbacks(),                        # optional

    cli_args=["--attention", "sdpa"],              # optional

    console_output=True,                           # optional, default=True

    console_isatty=True,                           # optional, default=True

)

```

- `root`
  - Path to the WanGP installation folder.
  - Example: `C:\WanGP`

- `config_path`
  - Optional path to `wgp_config.json`.
  - If omitted, WanGP uses `C:\WanGP\wgp_config.json`.
  - This must point to a file named `wgp_config.json`.

- `output_dir`
  - Optional override for generated outputs.
  - If omitted, WanGP uses the output paths defined in the config file.

- `callbacks`
  - Optional callback object. See the callback section below.

- `cli_args`
  - Optional WanGP startup flags.
  - Example: `["--attention", "sdpa", "--profile", "4"]`

- `console_output`
  - Enables or disables writing WanGP stdout/stderr to the real console.
  - Default: `True`
  - The stream object always receives a copy of stdout/stderr, regardless of this setting.

- `console_isatty`
  - Controls the TTY capability reported by the API's console capture wrapper.
  - Default: `True`
  - Keep this enabled if you want tqdm or other terminal-style progress output to behave like a live console stream even when WanGP is called from another Python process.

## Accepted Input Shapes

Relative attachment paths are normalized to absolute paths when the job is submitted.

- For direct settings dictionaries and `.json` settings files, the base is the API caller's current working directory at submit time.
- For `.zip` queue files, WanGP keeps the queue bundle behavior and resolves bundled media from the extracted queue contents.
- A few WanGP string-like fields are normalized for convenience. For example, `force_fps` may be passed as `24` or `"24"`.

### Single Task

For single-task use, the intended input is the task settings dictionary itself:

```python

settings = {

    "model_type": "qwen_image_20B",

    "prompt": "A red bicycle parked in front of a bakery",

    "resolution": "1024x1024",

    "num_inference_steps": 4,

    "image_mode": 1,

}



job = session.submit_task(settings)

```

### Manifest

`submit_manifest(...)` accepts a list of settings dictionaries:

```python

settings_list = [

    {

        "model_type": "qwen_image_20B",

        "prompt": "A quiet library at sunrise",

        "resolution": "1024x1024",

        "num_inference_steps": 4,

        "image_mode": 1,

    },

    {

        "model_type": "qwen_image_20B",

        "prompt": "A rainy alley with neon signs",

        "resolution": "1024x1024",

        "num_inference_steps": 4,

        "image_mode": 1,

    },

]



job = session.submit_manifest(settings_list)

```

### Saved Queue / Settings File

`submit(...)` also accepts:

- a `.json` settings file path
- a `.zip` saved queue path

Example:

```python

job = session.submit(Path(r"C:\WanGP\my_queue.zip"))

```

## Streaming Events

Each job exposes `job.events`, a `SessionStream`.

The stream yields `SessionEvent` objects:

```python

SessionEvent(

    kind="progress",

    data=ProgressUpdate(...),

    timestamp=1710000000.0,

)

```

Known `kind` values:

- `started`
  - Job accepted and session processing started.
- `progress`
  - Structured progress update.
- `preview`
  - RGB preview update.
- `stream`
  - One stdout/stderr line.
- `status`
  - WanGP status message.
- `info`
  - WanGP informational message.
- `output`
  - Raw output refresh event from WanGP.
- `refresh_models`
  - Raw model-refresh event from WanGP.
- `completed`
  - Final `GenerationResult`.
- `error`
  - One `GenerationError` record.

## Returned Objects

### `GenerationResult`

Returned by `job.result()`:

```python

GenerationResult(

    success=False,

    generated_files=[

        r"C:\WanGP\outputs\clip_001.mp4",

    ],

    errors=[

        GenerationError(

            message="Task 2 failed validation",

            task_index=2,

            task_id=2,

            stage="validation",

        ),

    ],

    total_tasks=3,

    successful_tasks=2,

    failed_tasks=1,

)

```

Fields:

- `success: bool`
  - `True` only when every submitted task completed without error.
- `generated_files: list[str]`
  - Absolute paths to every file generated by the job, including partial-success runs.
- `errors: list[GenerationError]`
  - Structured error records collected during the run.
- `total_tasks: int`
  - Number of tasks submitted in the job.
- `successful_tasks: int`
  - Number of tasks that completed successfully.
- `failed_tasks: int`
  - Number of tasks that failed or were cancelled.

`job.result()` does not raise generation-task failures. Instead, inspect `result.success` and `result.errors`.

### `GenerationError`

Delivered through `error` events, `on_error(...)`, and `GenerationResult.errors`:

```python

GenerationError(

    message="Task 2 did not complete successfully",

    task_index=2,

    task_id=2,

    stage="generation",

)

```

Fields:

- `message: str`
  - Human-readable error message.
- `task_index: int | None`
  - One-based task index when the error is associated with a specific task.
- `task_id: Any`
  - Task identifier from the manifest when available.
- `stage: str | None`
  - Error stage such as `validation`, `generation`, `cancelled`, or `runtime`.

### `ProgressUpdate`

Delivered through `progress` events and `on_progress(...)`:

```python

ProgressUpdate(

    phase="inference",

    status="Prompt 1/1 | Denoising | 7.2s",

    progress=54,

    current_step=4,

    total_steps=8,

    raw_phase="Denoising",

    unit=None,

)

```

Fields:

- `phase: str`
  - Normalized phase. Typical values:
  - `loading_model`
  - `encoding_text`
  - `inference`
  - `decoding`
  - `downloading_output`
  - `cancelled`
- `status: str`
  - Human-readable status string produced by WanGP.
- `progress: int`
  - Estimated percentage from `0` to `100`.
- `current_step: int | None`
  - Current inference step when available.
- `total_steps: int | None`
  - Total inference steps when available.
- `raw_phase: str | None`
  - Original WanGP phase label before normalization.
- `unit: str | None`
  - Optional progress unit if WanGP provides one.

### `PreviewUpdate`

Delivered through `preview` events and `on_preview(...)`:

```python

PreviewUpdate(

    image=<PIL.Image.Image image mode=RGB size=800x200>,

    phase="inference",

    status="Prompt 1/1 | Denoising",

    progress=54,

    current_step=4,

    total_steps=8,

)

```

Fields:

- `image: PIL.Image.Image | None`
  - RGB preview image generated from WanGP's latent preview payload.
- `phase`, `status`, `progress`, `current_step`, `total_steps`
  - Same interpretation as `ProgressUpdate`.

### `StreamMessage`

Delivered through `stream` events and `on_stream(...)`:

```python

StreamMessage(

    stream="stdout",

    text="New video saved to Path: C:\\WanGP\\outputs\\clip_001.mp4",

)

```

Fields:

- `stream: str`
  - Usually `stdout` or `stderr`.
- `text: str`
  - One redirected line of console output.

### `SessionEvent`

Generic event wrapper:

```python

SessionEvent(

    kind="stream",

    data=StreamMessage(stream="stdout", text="Model loaded"),

    timestamp=1710000000.0,

)

```

Fields:

- `kind: str`
  - Event type.
- `data: Any`
  - Payload object for that event.
- `timestamp: float`
  - Event creation time.

## Callback Object

You can pass a callback object to `init(...)` or `WanGPSession(...)`.

Supported callback methods:

- `on_progress(progress_update)`
  - Called when WanGP emits a structured progress update.
  - Use this for progress bars, step counters, and status text.

- `on_preview(preview_update)`
  - Called when a preview image is available.
  - Use this when you want live RGB preview frames during inference.

- `on_stream(stream_message)`
  - Called for every redirected stdout/stderr line.
  - This is the programmatic equivalent of watching the terminal output.

- `on_status(text)`
  - Called for WanGP status messages.
  - Use this if you want coarse status without parsing full progress objects.

- `on_info(text)`
  - Called for informational messages.

- `on_output(data)`
  - Called for raw WanGP output refresh events.
  - This is a low-level hook and is usually not needed by third-party integrations.

- `on_complete(result)`
  - Called when the job finishes.
  - Receives a `GenerationResult`.

- `on_error(error)`
  - Called each time WanGP reports a task or runtime error.
  - Receives a `GenerationError`.

- `on_event(session_event)`
  - Generic catch-all event hook.
  - Called alongside the specific callback above, not instead of it.

Example:

```python

class Callbacks:

    def on_progress(self, progress):

        print("progress:", progress.progress, progress.phase)



    def on_preview(self, preview):

        if preview.image is not None:

            preview.image.save("latest_preview.png")



    def on_stream(self, line):

        print(f"[{line.stream}] {line.text}")



    def on_complete(self, result):

        print("success:", result.success)

        print("generated:", result.generated_files)



    def on_error(self, error):

        print("error:", error.message)

```

Full signature example:

```python

from shared.api import GenerationError, GenerationResult, PreviewUpdate, ProgressUpdate, SessionEvent, StreamMessage





class VerboseCallbacks:

    def on_progress(self, progress: ProgressUpdate) -> None:

        print("progress", progress.progress, progress.current_step, progress.total_steps)



    def on_preview(self, preview: PreviewUpdate) -> None:

        print("preview", preview.phase, preview.image.size if preview.image is not None else None)



    def on_stream(self, line: StreamMessage) -> None:

        print(line.stream, line.text)



    def on_status(self, text: str) -> None:

        print("status", text)



    def on_info(self, text: str) -> None:

        print("info", text)



    def on_output(self, data: object) -> None:

        print("output", data)



    def on_complete(self, result: GenerationResult) -> None:

        print("success", result.success)

        print("files", result.generated_files)



    def on_error(self, error: GenerationError) -> None:

        print("error", error.stage, error.task_index, error.message)



    def on_event(self, event: SessionEvent) -> None:

        print("event", event.kind)

```

## Cancellation

```python

job = session.submit_task(settings)

job.cancel()

```

Cancellation is cooperative and forwards WanGP's normal abort signal to the active model. A cancelled run completes with `result.success == False` and a cancellation entry in `result.errors`.