Open Avatar Chat Documentation
repository·main·Indexed 25 days ago
https://github.com/humanaigc-engineering/openavatarchatA modular implementation of interactive digital human dialogues supporting multi-modal interactions (text, voice, video). It allows developers to swap core components including ASR, LLM, TTS, and avatar technologies such as LiteAvatar, LAM, MuseTalk, and FlashHead. Features include a Beta Chat Agent mode integrated with OpenClaw for tool calling, personality memory, and visual perception, as well as Docker Compose deployment options.
What's inside Open Avatar Chat
- Open Avatar Chat utilizes a modular Handler architecture where each Handler is responsible for a specific stage in the conversation workflow. This modularity allows developers to swap or extend different components of the pipeline, such as speech recognition, language modeling, or avatar rendering.
Overview of MuseTalk Avatar Handler
mainMuseTalk is a diffusion-based real-time digital human lip-sync driving solution. It consumes anAVATAR_AUDIOaudio stream from upstream TTS (Text-to-Speech) and uses a multi-threaded pipeline to generate lip-synced video frames and audio frames. The output consists ofAVATAR_VIDEOandAVATAR_AUDIO, which are provided to downstream Client Handlers for rendering.Overview of Open Avatar Chat
mainOpen Avatar Chat is a modular implementation of interactive digital human dialogue systems. It is designed to run a complete digital human conversation system on a single PC.
Key features include:
- Multimodal Interaction: Supports text, voice, and video for natural human-computer dialogue.
- Modular Architecture: Highly modular design allows for flexible replacement of core components such as ASR (Automatic Speech Recognition), LLM (Large Language Model), TTS (Text-to-Speech), and Avatar technologies.
- Diverse Digital Human Avatars: Supports multiple technologies including
LiteAvatar,LAM,MuseTalk, andFlashHead. - Low Latency Optimization: Optimized via VAD (Voice Activity Detection), speech buffering, and frame rate control, achieving an average response time of approximately 2.2 seconds.
MuseTalk Backpressure and Error Handling
mainTo prevent memory exhaustion and handle runtime errors, the MuseTalk pipeline uses the following strategies:
Backpressure Control
- Output Queue: If the
_output_queuedepth exceedsbatch_size * 5,_collect_batch()pauses collection. - Frame ID Queue: The Frame Collector allocates
frame_idbased on actual FPS; the inference thread must acquire aframe_idbefore processing.
Fault Tolerance
- Inference Errors: If a GPU inference error occurs, the pipeline returns zero frames/latents to prevent a total crash (this may result in temporary black faces).
- Audio Overflow: If the
add_audio()queue is full, data is dropped after a 1-second timeout. - Resource Cleanup:
create_context()uses try/except blocks to prevent Processor leaks, anddestroy_context()follows a strict stop-then-release sequence.
- Output Queue: If the
Get Started with Open Avatar Chat
mainOpen Avatar Chat is a modular interactive digital human conversation system designed to run a complete digital human dialogue system on a single PC. It features multimodal interaction (text, audio, video), a modular architecture for swapping ASR, LLM, TTS, and Avatar components, and is optimized for low latency (~2.2s average response time).Understand the Open Avatar Chat Handler architecture
mainOpen Avatar Chat utilizes a modular Handler architecture. EachHandleris responsible for managing a specific stage within the conversation pipeline. This modularity allows developers to swap different implementations for speech recognition, language modeling, voice synthesis, and avatar rendering.Understand MuseTalk Concurrency and Thread Safety
mainThe MuseTalk implementation uses a hybrid concurrency model to balance GPU and CPU resources:
- GPU Operations: Serialized via
_inference_lockin theMuseTalkAlgoV15instance to prevent hardware contention. - CPU Operations: Tasks like
res2combined()and frame collection run in parallel across multipleProcessorinstances. - Multi-Session Support: A
MuseTalkProcessorPoolmanages multipleProcessorinstances. Each processor has its own independent queues and worker threads, allowing multiple concurrent user sessions.
Key Locks:
_inference_lock(MuseTalkAlgoV15): Protects all GPU operations._lock(MuseTalkProcessorPool): Protectsacquire/releaseof processors._generation_lock(AvatarMuseTalkProcessor): Protects_generation_id._frame_id_lock(AvatarMuseTalkProcessor): Protects queue clearing operations._stream_key_lock(AvatarMuseTalkContext): Protects_current_tts_stream_key.
- GPU Operations: Serialized via
Understand the MuseTalk Avatar Data Flow
mainThe MuseTalk Avatar Handler processes audio from a TTS engine to generate synchronized video frames. The data flow follows this pipeline:
- Input: Receives TTS audio (expected:
AVATAR_AUDIO, 24kHz,float32, shape[1, N]). - Handler Processing:
HandlerAvatarMuseTalk.handle()detects stream key changes, validates input, and slices audio into 1-second segments (based onoutput_audio_sample_rate). - Processor Pipeline:
- Feature Extraction: Resamples audio from 24kHz to 16kHz using
librosa, extracts Whisper features, and aligns frames. - Frame Generation: Uses UNet and VAE (optionally multi-threaded) to generate latents and decode them into video frames.
- Composition: Combines processed frames into a final format.
- Frame Collection: Acts as the output clock, emitting video and audio frames synchronized to the configured
fps.
- Feature Extraction: Resamples audio from 24kHz to 16kHz using
- Output: Emits
AVATAR_VIDEOandAVATAR_AUDIOvia callbacks to the downstream client (e.g., WebRTC).
- Input: Receives TTS audio (expected:
Understand the MuseTalk Avatar Handler Pipeline
mainThe MuseTalk Avatar Handler uses a multi-threaded pipeline to transform audio into video frames. The pipeline consists of five main stages connected by queues:
- Feature Extractor Worker: Resamples audio (e.g., 24kHz to 16kHz), extracts Whisper features via GPU, and splits data into frame-aligned chunks.
- UNet Worker: Collects chunks into batches and performs UNet inference on the GPU to generate latents.
- VAE Worker: Decodes UNet latents into facial crops using the VAE on the GPU.
- Compose Worker: Performs CPU-bound image composition (
res2combined) to merge facial crops into full frames. - Frame Collector Worker: Acts as the output metronome, ensuring frames are output at the correct
fps. It handles both speaking frames and idle static frames.
Output Callbacks:
on_video_frame()on_audio_frame()on_speech_end()
Understand MuseTalk Architecture and Threading Model
mainThe MuseTalk implementation uses a multi-session processor pool architecture. The
AvatarMuseTalkProcessormanages several daemon worker threads for a pipeline:_feature_extractor_worker: Whisper feature extraction._frame_generator_worker: Single-threaded inference (UNet+VAE)._frame_generator_unet_worker: Multi-threaded UNet inference._frame_generator_vae_worker: Multi-threaded VAE inference._compose_worker: Res2Combined synthesis._frame_collector_worker: Periodic frame output (FPS control).
Threading Contexts:
handle(): Runs in the engine'shandler_pumperthread (one per handler).on_signal(): Runs in the globalsignal_distribute_thread.on_speech_endcallback: Runs in the_frame_collector_workerthread.
Queue Flow:
_audio_queue→_whisper_queue→[_unet_queue]→_compose_queue→_output_queue(with_frame_id_queuemanaged by the collector). All internal queues are unboundedQueue()objects.Understand the Open Avatar Chat modular architecture
mainOpen Avatar Chat uses a modular Handler architecture. You can compose different digital human dialogue systems by combining various ASR, LLM, TTS, and Avatar modules via configuration files.
The core modules are:
- Client Handler: Manages WebRTC audio/video stream ingestion and transmission.
- VAD Handler: Performs Voice Activity Detection to identify when a user starts and stops speaking.
- ASR Handler: Converts user speech into text.
- LLM Handler: Performs language model inference to generate dialogue responses.
- Agent Handler (Beta): A multi-turn tool-calling Agent that can replace the traditional LLM Handler.
- TTS Handler: Synthesizes speech from the generated response text.
- Avatar Handler: Drives the digital human by generating corresponding facial animations based on the audio.
MuseTalk Thread Safety and Concurrency
mainThe MuseTalk handler implements several mechanisms to ensure stability during concurrent use:
- GPU Serialization: All GPU operations are serialized through
_inference_lockto allow safe sharing across multiple sessions. - TTS Stream Protection: The
_current_tts_stream_keyis protected by_stream_key_lock. - Race Condition Prevention: A
generation_idmechanism is used to resolve race conditions betweenadd_audio()andinterrupt()calls. - Queue Integrity:
_frame_id_lockprotects queue clearing operations to prevent conflicts betweeninterrupt()and_clear_queues().
- GPU Serialization: All GPU operations are serialized through