# KaziVoice Production Deployment Guide

This guide covers deployment options for KaziVoice across bare-metal Linux servers, Docker containers, and Kubernetes environments.

---

## 1. System Requirements

| Resource | Minimum | Recommended (Production) |
| :--- | :--- | :--- |
| **CPU** | 2 vCPUs (x86_64, AVX-2) | 4+ vCPUs (x86_64 with AVX-512) |
| **RAM** | 4 GB | 8–16 GB |
| **Disk Space** | 10 GB free | 25 GB free (for model caching) |
| **OS** | Ubuntu 22.04+ / Debian 12 | Ubuntu 24.04 LTS / Debian 12 |
| **Runtime** | Python 3.10–3.12 | Python 3.12 + FFmpeg 6.0+ |

---

## 2. Bare-Metal / Systemd Deployment

### Step 1: Install System Audio Packages
```bash
sudo apt update && sudo apt install -y ffmpeg libsndfile1 build-essential git
```

### Step 2: Set up Virtual Environment & Install Package
```bash
python3 -m venv /home/boxd/.venv-kaziai
source /home/boxd/.venv-kaziai/bin/activate
pip install --upgrade pip setuptools wheel
pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu

cd /home/boxd/voicellm
pip install -e .
```

### Step 3: Configure Systemd Unit
The supplied unit assumes user `boxd`, checkout `/home/boxd/voicellm`, and environment `/home/boxd/.venv-kaziai`; adjust those paths and user names for another host. Copy `systemd/voicellm.service` to `/etc/systemd/system/voicellm.service`:
```bash
sudo cp systemd/voicellm.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable voicellm
sudo systemctl start voicellm
sudo systemctl status voicellm
```

---

## 3. Docker & Docker Compose Deployment

### Build & Run via Docker Compose
```bash
# Build and launch in detached mode
docker compose up -d --build

# View container logs
docker compose logs -f voicellm

# Check health probe
curl -f http://localhost:8050/healthz
```

---

## 4. Production Reverse Proxy (Nginx) Configuration

```nginx
upstream voicellm_backend {
    server 127.0.0.1:8050;
    keepalive 32;
}

server {
    listen 80;
    server_name tts.yourdomain.com;

    client_max_body_size 10M;

    location / {
        proxy_pass http://voicellm_backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 90s;
    }
}
```

---

## 5. Security & Rate Limiting

In production, enable API key authentication and configure rate limits in `.env`:
```env
VOICELLM_API_KEY_ENABLED=true
VOICELLM_API_KEYS=["sk-prod-kenya-enterprise-speech-key-998811"]
VOICELLM_RATE_LIMIT_ENABLED=true
VOICELLM_RATE_LIMIT_PER_MINUTE=180
```
Clients must provide the key via the `X-API-Key` header or `Authorization: Bearer <key>`.

## Container and readiness behavior

Validate with `docker compose config --quiet` before building. The image installs the application and its web assets into `/opt/venv`, and runs as an unprivileged user. Optional audio samples live at `/app/audio_samples`, configured through `VOICELLM_AUDIO_SAMPLES_DIR`. Model downloads use the runtime user's Hugging Face cache. Preserve that cache with a volume if cold-start downloads are undesirable. Each worker loads its own model and cache.

Use `/healthz` for liveness and `/readyz` for traffic readiness. Readiness stays at 503 until the configured default voice backend is available and its local model, if applicable, has loaded. Startup warmup can be disabled for lazy loading, but a local default is not ready until its first model load. Metrics are JSON, and disabled metrics return 404. Worker count, request deadline, cache enablement, rate-limit enablement, CPU thread count, and device selection are effective settings.

## Local OmniVoice worker

Voice cloning uses a separate, offline OmniVoice process communicating over a
private Unix socket. Install the reviewed runtime and pinned assets using
[the OmniVoice guide](OMNIVOICE_CLONING.md), enable `voicellm-omni.service`, and
wait for its health response to report ready before enabling
`VOICELLM_OMNI_ENABLED=true` in the API service. The included worker unit restricts
network and filesystem access and hides user credentials. Keep references and
render databases outside public static directories.

Use one API process with this SQLite render queue. Private workspace keys are
browser credentials and have no recovery/SSO workflow. Backup and retention
policies should explicitly cover private recordings and completed render files;
deleting a voice does not delete previously completed renders. The model weights
retain non-commercial terms; review tokenizer terms as well.
