# MuseTalk

[MuseTalk 1.5](https://github.com/TMElyralab/MuseTalk) animates a prepared portrait's mouth from speech audio. In SGLang-Omni it runs in process as the `speech_video` stage of a streaming cascade: the stage reads the agent's reply audio and publishes one JPEG per frame at 25 fps on the `jpeg_frame` port.

```text
input:pcm16 -> vad -> asr -> dialog -> llm (Inkling-2-Mini) -> tts (Qwen3-TTS) -> speech_video (MuseTalk 1.5) -> jpeg_frame
```

MuseTalk is released under the MIT license. The BiSeNet face-parsing weights used to prepare an avatar were trained on CelebAMask-HQ and are for non-commercial research only.

## Prerequisites

Install SGLang-Omni by following [Installation](../get_started/installation.md).

The bundled profile `qwen3-asr-inkling-2-mini-vivian-tts-tool-musetalk` runs on 2× H100:

| GPU | Runs | Measured memory |
|---|---|---|
| GPU 0 | Inkling-2-Mini (`chat`, port 8003) | 67.2 GB |
| GPU 1 | Qwen3-ASR-1.7B (`asr`, port 8002), Qwen3-TTS-12Hz-1.7B-Base (`tts`, port 8005), the `speech_video` stage | 26.9 GB, of which 3.4 GB is the stage |

Build the Inkling-2-Mini venv once and download the cascade checkpoints, as in the [Full-Duplex Guide](../developer_reference/full_duplex_guide.md#streaming-cascade):

```bash
examples/streaming_cascade/inkling_chat_service.sh install
hf download thinkingmachines/Inkling-2-Mini --revision b85c3cfa615fc413cc9af3379f491b963f3b586a
hf download Qwen/Qwen3-ASR-1.7B
hf download Qwen/Qwen3-TTS-12Hz-1.7B-Base
hf download Qwen/Qwen3-ForcedAligner-0.6B
```

The profile's `aligner` node uses a forced-alignment worker on port 8010, which runs from a venv of its own:

```bash
uv venv aligner-venv -p 3.12
uv pip install --python aligner-venv/bin/python qwen-asr fastapi uvicorn regex httpx msgpack msgspec numpy pydantic websockets
```

## Prepare an avatar

Prepare the portrait once on a GPU. The source is a video, an image, or a directory of PNG frames; the output directory must not exist or must be empty:

```bash
python -m sglang_omni.models.musetalk.prepare /path/to/portrait.mp4 /path/to/prepared/avatar
```

The MuseTalk, VAE, Whisper-tiny, face detector, landmark and face-parsing weights download from Hugging Face on first use. One portrait image took about 24 s on one H100. The directory holds `full_imgs/`, `mask/`, `coords.pkl`, `mask_coords.pkl` and `latents.pt`. Add `--device` to choose the torch device; it defaults to CUDA when available.

## Launch

Start the forced-alignment worker on GPU 1 and wait until `curl http://127.0.0.1:8010/health` answers `{"status":"ready"}`:

```bash
PYTHONPATH=. aligner-venv/bin/python examples/streaming_cascade/alignment_worker.py --model Qwen/Qwen3-ForcedAligner-0.6B --device cuda:1 --language English --port 8010
```

Then start the cascade from the repository root with the prepared avatar:

```bash
sgl-omni serve --config examples/streaming_cascade/profiles/qwen3-asr-inkling-2-mini-vivian-tts-tool-musetalk/launch.yaml \
  --speech_video.factory.avatar_dir /path/to/prepared/avatar --enable-realtime --port 8765
```

The server starts the `chat`, `asr` and `tts` services in order, reusing a service already answering on its port, then the cascade. The `speech_video` stage compiles the MuseTalk UNet and VAE decoder with `torch.compile` and renders four warm-up chunks; it was ready in about 11 s with a warm compile cache and about 47 s cold. To run without the alignment worker, append `--session_nodes.aligner.method asr --session_nodes.aligner.alignment_url ""`.

Check readiness in another terminal:

```bash
curl -fS http://127.0.0.1:8765/v1/realtime/capabilities
```

The server is ready when the response contains `"interaction":"turn_based"` and lists the `jpeg_frame` port under `plugin_output_ports`.

## Talk to the avatar

Start the cascade playground against the server:

```bash
python playground/cascade/app.py --api-base http://127.0.0.1:8765 --port 8090
```

Once the terminal prints `Running on`, open <http://localhost:8090> and click **Start talking**. If the server is remote, first run `ssh -N -L 8090:127.0.0.1:8090 USER@SERVER` on your own machine; open the page through `localhost`, or the browser will not grant microphone access.

The playground draws the `jpeg_frame` stream in place of the orb. While the agent is silent the portrait holds its idle picture; when a reply starts, the page holds the reply's audio until its first frame is due, so voice and mouth start together. Speak over a reply to interrupt it: the audio stops, the reply's frames the page has not shown are dropped, and the frames rendered past the cut close the mouth.

## Stage options

Set these under `stages.speech_video` in `launch.yaml`, or on the command line as `--speech_video.factory.<option>` and `--speech_video.gpu`:

| Option | Default | Bundled profile | Effect |
|---|---|---|---|
| `avatar_dir` | required | none | The prepared avatar directory. |
| `speech_sample_rate_hz` | required | `24000` | Sample rate of the `tts` node's `pcm_f32le` audio; 24000 for `openai_speech`. |
| `chunk_ms` | `200` | `200` | Audio per render call; a whole number of 40 ms frames. |
| `render_lead_ms` | `chunk_ms` | `80` | How far past the current frame a reply starts, which gives its first chunk time to render. |
| `tail_ms` | `0` | `200` | Rendering continues this long after a reply so the mouth closes before the picture holds. |
| `width`, `height` | `512`, `512` | `512`, `512` | Output frame size; the avatar scaled to `width` must be `height` high. |
| `max_open_sessions` | `1` | `1` | Sessions the stage holds; its one renderer draws one chunk at a time. |
| `gpu` | `0` | `1` | GPU of the stage process. |

The `jpeg_frame` port advertises `reply_media_wait_ms = chunk_ms + 80 + render_lead_ms` (360 ms in the bundled profile), the latest time a reply's first frame is due; the playground holds a reply's audio at most that long.

## Performance

Measured on 2× H100 with the bundled profile, the `speech_video` stage sharing GPU 1 with ASR and TTS:

| Metric | Value |
|---|---|
| Render time per 200 ms chunk (5 frames) | p50 31 ms, p95 48 ms, max 50 ms |
| First frame after the reply's first audio | 50–86 ms |

## Troubleshooting

- **Startup fails without an avatar.** `avatar_dir` has no default; without `--speech_video.factory.avatar_dir`, the `speech_video` stage fails to start and the error names the missing argument.
- **A Session ends with an error.** An error while rendering or publishing a chunk fails that chunk's Session; the stage keeps serving its other Sessions. Check the server log for the exception.
- **A second conversation is refused.** The bundled profile serves one Session at a time (`max_open_sessions: 1`), so a second Session is rejected with `admission_rejected` while the first is open. Close the first conversation before starting another.
