Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tests/
/dgl/*
!/dgl/.gitkeep
logs/
input/
output/
.vscode/
.idea/
Expand Down
6 changes: 3 additions & 3 deletions docs/dgl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,7 @@ return {cover = cover, clip = clip, latent = latent}
`media::save(path, media)` writes these exact bytes, with no re-encoding or
metadata changes. It checks file-write permission and the extension against the
MIME type. Omit the save calls for in-memory results only. The server's DGL
editor displays returned media from the existing job result; ordinary tensors
remain available for inspection.
editor displays returned media and ordinary tensor metadata in the job result.

| Encoding | MIME type | Requirements |
| --- | --- | --- |
Expand Down Expand Up @@ -1892,7 +1891,8 @@ clears the registration. The sampling pass counter starts at one on each
run. Progress can report model loading in tensors, sampling in steps, video
upscaling in frames, or a stage with no known total.
Without a progress callback, the DGL execution layer displays sampling and
weight-loading progress bars. With a callback, the host controls their display.
weight-loading progress bars. Set `sd_dgl_ctx_set(ctx, "console_progress", "on")`
on an idle context to display those bars alongside a progress callback.
The default observer handles previews. Preview mode `proj` uses latent projection. Modes `tae` and `vae`
use the decoder supplied to `preview(decoder=...)`, falling back to projection. A positive interval
Expand Down
1 change: 1 addition & 0 deletions examples/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ add_executable(${TARGET}
routes_sdapi.cpp
routes_sdcpp.cpp
routes_dgl.cpp
routes_media.cpp
)
if(APPLE)
sd_set_macos_rpaths(${TARGET})
Expand Down
71 changes: 52 additions & 19 deletions examples/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,46 @@ and are not parsed by the native server API.

### DGL editor

Open the Web UI using the local server address and select **DGL** in the header.
Select a saved script or choose **New**. **Duplicate** saves a copy of the
current editor text under a new name. Edit **File name** before saving to save
under a different name. **Run** submits the editor text to the generation queue,
so you can run unsaved edits. The results panel shows text, scalar and tensor
information and returned encoded media. PNG appears as an image, WebM as a
video player, and AVI as a download. WebM encoding is silent; use AVI for
a PCM16 soundtrack.
Open the Web UI and select **DGL** in the header. The workspace follows the
server's `--listen-ip` setting. The default `127.0.0.1` limits access to the
server machine. When the server listens on a LAN interface, open the Web UI
using the server's LAN IP and port to use the workspace from another device.
Use **Search scripts** to filter the script list, then select a saved script or
choose **New**. **Save** updates the current file or asks for a name for a new
script. Use **Save as…** to save a copy. The editor reopens the last saved script
used in that browser when it is still available. **Run** submits the current
editor text to the generation queue, including unsaved edits. The status bar
below the editor shows progress and keeps the final status and elapsed time.
The server terminal displays weight-loading throughput and sampling speed.

When a script returns values, **View result** appears in the status bar. Open it
to inspect text, scalars, tensor metadata, and encoded media. PNG appears as an
image, WebM as a video player, and AVI as a download. WebM encoding is silent.
Use AVI for a PCM16 soundtrack.

Use `image::encode` or `video::encode` and return their `{data, mime_type}`
dicts, either directly or as named fields of the root dict. To save the same
encoded output, call `media::save("output/cover.png", cover)` before returning
it. Saving is optional. The job result contains the same encoded bytes, and
polling does not encode them again. Tensor frames can be inspected separately
with **Inspect tensor frames**; they are not fetched automatically.
polling returns them without re-encoding.

The editor underlines supported media paths and shows compact previews below
their lines. Previews align with the code indentation, and files on the same
line appear side by side when space permits. Media loads near the visible area
of the editor. An output path without a file stays collapsed, and a completed
run refreshes its preview when the file becomes available. Drag the handle in
a preview's lower-right corner to resize it, or double-click the handle to reset it.
Click an image or video preview to open it at a larger size. Expanded videos
open at the current playback position. Click the picture to play or pause, or
drag the timeline below it to seek. AVI path previews support MJPEG and
uncompressed 24-bit RGB video with up to 64 MiB of PCM or 32-bit float audio.
The speaker button mutes the preview. To adjust volume in an inline preview,
hover or focus the speaker button to open the volume slider.
Use a relative path under the server's working directory, such as
`assets/reference.png` or `output/clip.webm`. Turn off **Media view** in the
editor toolbar to hide the preview. The server previews files under `input/`,
`output/`, and `assets/`. Start the server
with `--no-media-view` to disable file previews at the server.

DGL jobs share an execution context for the lifetime of the server. Compatible
scripts can reuse loaded model components across runs. The context can release
Expand Down Expand Up @@ -433,29 +458,37 @@ configured defaults.
| --- | --- | --- |
| `--listen-ip <ip>` | `127.0.0.1` | Server listen address |
| `--listen-port <port>` | `1234` | Server listen port |
| `--dgl-dir <path>` | `dgl` | Script directory for the local DGL editor |
| `--dgl-dir <path>` | `dgl` | Script directory for the DGL editor |
| `--no-media-view` | Off | Disable the media view endpoint |
| `--max-pending-jobs <count>` | `64` | Maximum async jobs queued or generating |
| `--completed-ttl-seconds <seconds>` | `600` | Retention time for completed async jobs |

### Access

By default any program on the machine, and any web page open in a browser on
it, can call the server. To limit that:
The default listener accepts connections from the server machine. A listener
bound to a LAN interface also accepts connections from devices that can reach
it. You can open the Web UI from a link on another site. Browser API requests
are same-origin by default. Use an IP address, `localhost`, or the hostname
configured as the listen address when connecting directly.

| Option | Default | Purpose |
| --- | --- | --- |
| `--api-key <key>` | none | Key clients must send as `Authorization: Bearer <key>` or `X-Api-Key: <key>`. Separate multiple keys with commas. |
| `--api-key-file <file>` | none | The same, one key per line, so keys don't show in the process list |
| `--cors-origins <origins>` | any | Web origins allowed to read the server's answers in a browser, comma-separated |
| `--cors-origins <origins>` | none | Additional browser origins allowed to make cross-origin requests, comma-separated |

With a key, the page at `/` still loads, but the bundled Web UI can't send
the key, so its calls get `401`; it is meant for use without a key.
With a key, the page at `/` still loads. Enter the key in **Settings → Server API Key**
and select **Apply**. The Web UI saves it for that server in the current browser tab.
Leave **Base URL** blank when connecting to the server that serves the Web UI.
DGL scripts and media previews use the server that serves the Web UI.
Authentication failures return `401` with a `WWW-Authenticate` Bearer challenge.
Protected responses include `Cache-Control: no-store`.

`--cors-origins` only decides which web pages may read the answers: other
pages get no CORS headers, but their requests still reach the server. To
refuse requests, set a key as well.
The server rejects requests from other browser origins with `403` unless their
origin is listed in `--cors-origins`. For a reverse proxy that preserves the
public Host header, include its full public origin in that list, such as
`https://sd.example.com`. The origin policy applies to DGL and media requests
as well as the generation APIs.

Startup fails if an access option is missing its value, a list is empty
(such as `--api-key ,`), or the key file contains no keys.
Expand Down
43 changes: 27 additions & 16 deletions examples/server/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,8 @@ Field types:
the elapsed generation time in seconds, including output encoding and excluding
queue time.

`progress` is available independently of the preview setting, for both image
and video jobs. Continue polling until `status` is terminal: a sampling pass
`progress` is available independently of the preview setting for image, video,
and local DGL jobs. Continue polling until `status` is terminal: a sampling pass
can finish before decoding, ADetailer, or output encoding finishes.

| Field | Type | Notes |
Expand Down Expand Up @@ -858,6 +858,7 @@ Top-level fields:
| `loras` | `array<object>` | Available LoRA entries |
| `upscalers` | `array<object>` | Available highres upscalers, built-in and model-backed |
| `preview_modes` | `array<string>` | Available preview modes, e.g. `["none", "proj", "tae", "vae"]` |
| `media_view` | `boolean` | Whether the media view endpoint is enabled |
| `limits` | `object` | Shared queue and size limits |

`model`
Expand Down Expand Up @@ -1071,11 +1072,12 @@ Typical status codes:

#### `POST /sdcpp/v1/jobs/{id}/cancel`

Cancels a queued job immediately, or requests interruption of a generating image or
video job. A generating job returns `200 OK` with `cancel_requested: true` and
remains `generating` until execution stops at a cancellation checkpoint. Continue
polling until its status becomes `cancelled`. Cancelled jobs have no result.
Repeated cancellation requests are safe; terminal jobs are returned unchanged.
Cancels a queued image, video, or DGL job immediately, or requests interruption
if it is already running. No request body is required. A running job returns
`200 OK` with `cancel_requested: true` and remains `generating` until it reaches
a cancellation checkpoint. Poll `GET /sdcpp/v1/jobs/{id}` for the final status.
Cancelled jobs have no result. Repeated cancellation requests are safe and
return the current status of terminal jobs.

Typical status codes:

Expand Down Expand Up @@ -1827,20 +1829,31 @@ Example cancelled job:
- `429 Too Many Requests` when the job queue is full
- `500 Internal Server Error` for unexpected server exceptions during submission

## Media view

`GET /sdcpp/v1/view?root=assets&path=reference.png` serves an image or video
over the configured listener. `root` selects `input`, `output`, or `assets`
in the server's working directory. `path` is a slash-separated relative path
within that directory. Supported formats are PNG, JPEG, GIF, WebP, WebM, MP4,
and AVI. The endpoint supports HTTP byte ranges and sends `Cache-Control: no-store`.

The server returns `403` when access is denied or the server was started with
`--no-media-view`. Unavailable paths, links, and unsupported files return
`404`.

## DGL editor API

These endpoints are available to the local, same-origin Web UI. The server uses
the working directory's ./dgl directory unless --dgl-dir is set.
These endpoints follow the server's access rules and `--listen-ip`. The server
uses `./dgl` in the working directory unless `--dgl-dir` is set.

| Method and path | Request or response |
| --- | --- |
| GET /sdcpp/v1/dgl/scripts | Returns directory and a list of scripts |
| GET /sdcpp/v1/dgl/script?name=example.dgl | Returns the script as source |
| POST /sdcpp/v1/dgl/script | Saves UTF-8 source with the given name. Pass previous_source to update an existing script. A conflict returns HTTP 409. |
| POST /sdcpp/v1/dgl/jobs | Queues the source string and returns a job ID with HTTP 202 |
| GET /sdcpp/v1/dgl/jobs/{id} | Returns the job status, error or result with tensor descriptions and inline encoded media |
| POST /sdcpp/v1/dgl/jobs/{id}/cancel | Cancels a queued job or requests cancellation of a running job. Send `Content-Type: application/json`. |
| GET /sdcpp/v1/dgl/jobs/{id}/outputs/{index}?frame=0 | Returns a PNG of an image output or a video frame. Output and frame indexes start at zero. |
| POST /sdcpp/v1/dgl/jobs | Queues the source string and returns a job ID and `poll_url` with HTTP 202 |
| GET /sdcpp/v1/jobs/{id} | Returns job status and progress, plus any error or result with tensor descriptions and inline encoded media |
| POST /sdcpp/v1/jobs/{id}/cancel | Cancels a queued job or requests cancellation of a running job |

Failed jobs report `error.code` and `error.message`. When execution reports
`out_of_memory`, the message includes the memory failure details available
Expand Down Expand Up @@ -1878,9 +1891,7 @@ The name of a root media dict is `result`.

The script encodes media once. After execution releases the generation lock,
the server converts returned bytes to Base64 once, before publishing
`completed`. Polling returns the stored result; it does not encode a video,
convert Base64 again, or require frame requests. The frame endpoint remains
available for explicit tensor inspection. Native `/sdcpp/v1/vid_gen` is unchanged.
`completed`. Polling returns the stored metadata and inline Base64 media.

Inline media share a 64 MiB raw-byte budget per job. An item that exceeds the
remaining budget has no `b64_json`; it instead contains
Expand Down
Loading
Loading