Real-Time AI Voice Chat

repository·main·Indexed 26 days ago

https://github.com/koljab/realtimevoicechat

A low-latency client-server system for natural spoken conversations with LLMs. It uses WebSockets to stream audio between a browser frontend and a Python backend, integrating STT, LLM, and TTS engines. Supports Docker and manual installation on Windows, Linux, and macOS, with configurable backends including Ollama and OpenAI, and TTS engines such as Coqui, Kokoro, and Orpheus.

Tokens
4.9K
Snippets
11
Records
25
Agent score
87%

What's inside realtimevoicechat

  1. Understand Licensing for Real-Time AI Voice Chat

    main

    The core codebase of this project is licensed under the MIT License.

    Important: This project integrates external components such as specific TTS engines (e.g., Coqui XTTSv2) and LLM providers. These components are subject to their own respective licensing terms. Users are responsible for ensuring compliance with the licenses of all integrated third-party components used in their implementation.

  2. Run the Real-Time AI Voice Chat Application

    main

    After installation, follow these steps to start the server and access the web interface.

    If using Docker

    The application starts automatically with docker compose up -d. Use docker compose logs -f app to monitor logs.

    If using Manual/Script Installation

    1. Activate your virtual environment.
    2. Navigate to the code directory.
    3. Run python server.py.

    Accessing the UI

    1. Open http://localhost:8000 in your browser.
    2. Grant microphone permissions.
    3. Click "Start" to begin.
  3. Install the pre-built DeepSpeed wheel for Windows

    main

    A pre-built DeepSpeed wheel is available for Windows users to avoid manual compilation. This specific wheel was built on Windows 11 with the following environment requirements:

    • PyTorch: 2.5.1
    • CUDA: 12.1
    • Python: 3.10.9
    • Enabled DeepSpeed Options: CUTLASS_OPS, SPARSE_ATTN, INFERENCE_CORE_OPS

    If your environment matches these specifications, you can install the wheel directly using pip.

    pip install .\code\deepspeed_wheel\deepspeed-0.16.1+unknown-cp310-cp310-win_amd64.whl
  4. Install Real-Time AI Voice Chat Manually (Windows/Linux/macOS)

    main

    Manual installation requires managing your own Python environment and ML dependencies.

    Windows (using install.bat)

    Run the provided script to create a venv and install PyTorch for CUDA 12.1 and DeepSpeed.

    install.bat

    Manual Steps (All Platforms)

    1. Create and activate a virtual environment.
    2. Upgrade pip.
    3. Navigate to the code directory.
    4. Install PyTorch (match your CUDA version).
    5. Install requirements.
    # 1. Create & Activate Virtual Environment
    python -m venv venv
    # Linux/macOS:
    source venv/bin/activate
    # Windows:
    .\venv\Scripts\activate
    
    # 2. Upgrade Pip
    python -m pip install --upgrade pip
    
    # 3. Navigate to Code Directory
    cd code
    
    # 4. Install PyTorch (Example for CUDA 12.1)
    pip install torch==2.5.1+cu121 torchaudio==2.5.1+cu121 torchvision --index-url https://download.pytorch.org/whl/cu121
    
    # 5. Install Other Requirements
    pip install -r requirements.txt
  5. Install Real-Time AI Voice Chat via Docker (Recommended)

    main

    The Docker installation is the recommended method for Linux users with GPUs, as it bundles the application, dependencies, and Ollama into manageable containers.

    Note: If you need to customize models or settings in code/*.py, do so before building the images.

    # 1. Clone the repository
    git clone https://github.com/KoljaB/RealtimeVoiceChat.git
    cd RealtimeVoiceChat
    
    # 2. Build the Docker images
    docker compose build
    
    # 3. Start the services (App & Ollama)
    docker compose up -d
    
    # 4. Pull the desired Ollama model into the running container
    docker compose exec ollama ollama pull hf.co/bartowski/huihui-ai_Mistral-Small-24B-Instruct-2501-abliterated-GGUF:Q4_K_M
    
    # (Optional) Verify the model is available
    docker compose exec ollama ollama list
  6. Configure SSL/HTTPS for the Server

    main

    To enable SSL in server.py:

    1. Set USE_SSL = True.
    2. Provide paths to your certificate via SSL_CERT_PATH and SSL_KEY_PATH.

    Docker Users: You must adjust docker-compose.yml to map the SSL port (e.g., 443) and mount your certificate files as volumes.

  7. Configure TTS Engine and Voice

    main

    You can customize the Text-to-Speech engine and specific voice settings by modifying the Python files in the code/ directory.

    • Engine Selection: In server.py, change START_ENGINE to one of: "coqui", "kokoro", or "orpheus".
    • Voice Settings: Adjust engine-specific settings (e.g., voice model path for Coqui, speaker ID for Orpheus, or speed) within the AudioProcessor.__init__ method in audio_module.py.
  8. Configure STT and Turn Detection

    main

    Fine-tune how the system listens and detects speech:

    • STT Settings: In transcribe.py, modify DEFAULT_RECORDER_CONFIG to change the Whisper model, language, or silence thresholds like silence_limit_seconds.
    • Turn Detection: In turndetect.py, adjust the pause duration constants within the TurnDetector.update_settings method to change how sensitive the system is to pauses in conversation.
  9. Configure LLM Backend and Model

    main

    Modify the AI's 'brain' by editing server.py and llm_module.py:

    • Provider: Set LLM_START_PROVIDER to either "ollama" or "openai" in server.py.
    • Model: Set LLM_START_MODEL in server.py (e.g., a GGUF path for Ollama or a model name for OpenAI).
    • Personality: Customize the AI's behavior by editing system_prompt.txt.

    Note for Docker users: If using Ollama, ensure you pull the model into the container after startup using docker compose exec ollama ollama pull <model_name>.

  10. Run the server with or without SSL

    main

    The server can be started using uvicorn. The behavior depends on the USE_SSL configuration variable.

    Without SSL: Starts the server on 0.0.0.0:8000.

    With SSL: Requires the following certificate files to exist in the root directory:

    • 127.0.0.1+1.pem
    • 127.0.0.1+1-key.pem

    If these files are missing, the server will exit with an error. You can generate them using mkcert.

    if __name__ == "__main__":
        if not USE_SSL:
            uvicorn.run("server:app", host="0.0.0.0", port=8000, log_config=None)
        else:
            uvicorn.run(
                "server:app",
                host="0.0.0.0",
                port=8000,
                log_config=None,
                ssl_certfile=cert_file,
                ssl_keyfile=key_file,
            )
  11. Configure the Ollama Server Service via Docker Compose

    main

    The ollama service uses the official ollama/ollama:latest image to provide the LLM backend.

    Environment Variables

    • NVIDIA_VISIBLE_DEVICES: Set to all to make GPUs visible inside the container.
    • NVIDIA_DRIVER_CAPABILITIES: Set to compute,utility.

    Volumes

    • ollama_data: Persistent volume mapped to /root/.ollama to store models and data.

    GPU Configuration

    The service requires NVIDIA GPU access via the deploy reservation block.

    services:
      ollama:
        image: ollama/ollama:latest
        container_name: realtime-voice-chat-ollama
        volumes:
          - ollama_data:/root/.ollama
        environment:
          - NVIDIA_VISIBLE_DEVICES=all
          - NVIDIA_DRIVER_CAPABILITIES=compute,utility
        deploy:
          resources:
            reservations:
              devices:
                - driver: nvidia
                  count: all
                  capabilities: [gpu, compute, utility]
        restart: unless-stopped
  12. Configure the Backend Server via Environment Variables

    main

    The server.py entrypoint uses the MAX_AUDIO_QUEUE_SIZE environment variable to control the maximum number of audio chunks allowed in the incoming queue before dropping packets to prevent lag. If not provided, it defaults to 50.

    To set this, export the variable before running the server:

    export MAX_AUDIO_QUEUE_SIZE=100
    python server.py