examples/talking-avatar/aura-model
in the Reactor cookbook, and the code below is simplified from it.
Files
aura-model/
AuraAvatar, the model class. Most of its code is in AvatarApp, which it subclasses: the room
protocol and the render loop, for any model that renders a chunk of video from a chunk of speech. AuraAvatar only
builds that model, AuraModel, which runs AURA’s own code and imports nothing from Reactor.
Model Structure describes this split.
Contract
The contract declares the tracks and the messages that the room sees:avatar_app.py (simplified)
- Tracks:
agent_audiotakes the agent’s speech in.main_videoandmain_audiosend the avatar out. - Messages:
playback_startedandplayback_finishedtell the agent what played.avatar_failedtells the client that the model failed during the session.
@event: set_avatar, clear_buffer, and user_speaking_state.
How the room reaches the model
In the room, the avatar uses LiveKit’s avatar protocol. Each signal of that protocol maps to a track, a command, or a message of the model:runtime.livekit block in reactor.yaml declares this mapping. The output tracks need no entry, because the
runtime publishes them to the room:
reactor.yaml (the livekit block)
set_avatar is not mapped to the room. The portrait arrives in the starting input of the session. The
runtime.livekit reference explains each key.
Between the app and the model
The app and the model exchange 2 dataclasses, and the model implements 2 methods:avatar_contract.py (simplified)
AvatarModel. The example’s second model, LiveAvatar in
liveavatar-model/, does exactly that, with the same avatar_app.py and avatar_contract.py.
Loading
The runtime callsload() once, when the instance starts and before any session. AvatarApp.load() reads
config.yml and calls create_model(), which AuraAvatar implements:
aura_app.py (simplified)
AuraModel.load() builds AURA’s pipeline from the weights folder, the way AURA builds it from Hugging Face, so the
model never reads the network. It then compiles the transformer and renders 6 warmup chunks, so the first call runs at
full speed. create_model() also returns a default portrait, which a session uses until set_avatar replaces it.
The render loop
Most models leave the loop to the runtime and write the three step hooks. This app writes its ownrun() instead. The
render runs on a worker thread, so commands and speech keep arriving while a chunk renders:
avatar_app.py (simplified)
Plan a chunk
The agent’s speech arrives onagent_audio faster than it plays, as one byte stream per utterance. A background task
collects it into utterances. The next chunk carries 1.0 second of one utterance as soon as that much is in, or the
last, shorter part when the utterance ends. With no speech, the next chunk is idle:
avatar_app.py (simplified)
speech_lead_s in config.yml). That is enough to keep the next
chunk ready, and an interrupt drops little rendered speech.
Render the chunk
render() calls generate() on the model, on the worker thread. When the model does not hold the portrait, it starts
a new clip from it. Then it renders the chunk from its speech:
aura_model.py (simplified)
audio_context_s in config.yml). The
model keeps its key/value cache between chunks, so each chunk continues from the frames before it.
Send the frames with their audio
send_block() sends the chunk’s frames together with the exact speech that drove them. The runtime plays the two in
lockstep, so the mouth matches the sound:
avatar_app.py (simplified)
Interrupts and playback reports
When you talk over the avatar, the agent sendslk.clear_buffer, which runs clear_buffer:
avatar_app.py (simplified)
clear_buffer runs on the event loop, so it takes effect even while a chunk renders. When that chunk is done,
send_block() drops it.
The agent uses the playback reports to know what you heard. playback_started goes back when the first frame of an
utterance plays. playback_finished goes back when the utterance has played to its end, or when clear_buffer cut it,
with how many seconds of it played.
When the model fails
An error ingenerate() during a call stops that call, not the runtime. run() catches it and calls fail():
avatar_app.py (simplified)
avatar_failed tells the client why. When the session ends, the model is
reset as after every session, and the next call starts normally.
The portrait
set_avatar takes the portrait. Only the starting input of the session can send it, so a participant in the room
cannot change the face:
avatar_app.py (simplified)
Sessions and reset
- Session start:
@session_startedcreates the session’s state from the default portrait, and starts the tasks that collect the speech and send the playback reports. - Session end:
@session_endedstops those tasks, drops the queued speech and media, and has the loop reset the model before the next session.