Spaces:
Build error
Build error
Add lightweight CPU inference for Sauti TTS + LiveKit agent
Browse files- Dockerfile +22 -0
- README.md +66 -14
- app.py +92 -0
- lightweight_infer.py +248 -0
- livekit_agent.py +86 -0
- push_to_hf.py +28 -0
- requirements.txt +13 -0
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 4 |
+
git \
|
| 5 |
+
ffmpeg \
|
| 6 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 7 |
+
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
COPY requirements.txt .
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
COPY lightweight_infer.py .
|
| 14 |
+
COPY app.py .
|
| 15 |
+
|
| 16 |
+
# Download model at build time to cache in docker layer
|
| 17 |
+
RUN python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='msingiai/sauti-tts', filename='vocab.txt'); hf_hub_download(repo_id='msingiai/sauti-tts', filename='model_last.pt')"
|
| 18 |
+
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
# Use uvicorn for FastAPI
|
| 22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,14 +1,66 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Sauti TTS — Lightweight HF Space
|
| 2 |
+
|
| 3 |
+
Swahili text-to-speech built on `msingiai/sauti-tts` (F5-TTS base), optimized to run on **Hugging Face Free CPU Spaces** and connect to **LiveKit**.
|
| 4 |
+
|
| 5 |
+
## What's included
|
| 6 |
+
|
| 7 |
+
| File | Purpose |
|
| 8 |
+
|------|---------|
|
| 9 |
+
| `lightweight_infer.py` | Optimized inference: pruned checkpoint, bf16 weights, optional INT8 dynamic quant, 10-step EPSS sampling |
|
| 10 |
+
| `app.py` | FastAPI server for HF Space, `/tts` and `/health` endpoints |
|
| 11 |
+
| `Dockerfile` | Reproducible HF Space image (Python 3.11, CPU torch) |
|
| 12 |
+
| `livekit_agent.py` | LiveKit agent that calls the HF Space TTS API |
|
| 13 |
+
|
| 14 |
+
## Key optimizations for free CPU
|
| 15 |
+
|
| 16 |
+
1. **Checkpoint pruning** — strips optimizer/scheduler; full 5GB+ → ~1.3GB
|
| 17 |
+
2. **BF16 weights** — halves weight memory with minimal quality loss on modern CPUs
|
| 18 |
+
3. **10-step EPSS** — instead of default 32, cuts compute by ~3×
|
| 19 |
+
4. **CFG 1.5** — lower guidance, fewer double-passes
|
| 20 |
+
5. **Dynamic INT8** (optional) — ~4× weight reduction
|
| 21 |
+
6. **Cached text encoder** — text embeddings computed once, reused across ODE steps
|
| 22 |
+
|
| 23 |
+
## Deploy to Hugging Face Spaces
|
| 24 |
+
|
| 25 |
+
1. Create a new **Space** → **Docker** → Free CPU.
|
| 26 |
+
2. Upload these four files (`app.py`, `lightweight_infer.py`, `Dockerfile`, `requirements.txt`).
|
| 27 |
+
3. Set **HF_TOKEN** in Space secrets (your HF write token).
|
| 28 |
+
4. Set **HF_MODEL_ID** (default: `msingiai/sauti-tts`).
|
| 29 |
+
5. Build completes in ~5 minutes. The first request will be slow (model download + pruning), but subsequent requests are fast.
|
| 30 |
+
|
| 31 |
+
### Space secrets
|
| 32 |
+
|
| 33 |
+
| Name | Required | Value |
|
| 34 |
+
|------|----------|-------|
|
| 35 |
+
| `HF_TOKEN` | Yes | Your HF token with read access to `msingiai/sauti-tts` |
|
| 36 |
+
| `DEFAULT_REF_AUDIO` | No | Absolute path to a reference wav for voice cloning |
|
| 37 |
+
| `TTS_URL` | No | Override if you changed the app port |
|
| 38 |
+
|
| 39 |
+
## Connect to LiveKit
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
export LIVEKIT_URL=wss://your-project.livekit.cloud
|
| 43 |
+
export LIVEKIT_API_KEY=...
|
| 44 |
+
export LIVEKIT_API_SECRET=...
|
| 45 |
+
export TTS_URL=https://<your-space>.hf.space
|
| 46 |
+
export DEFAULT_REF_AUDIO=/app/reference.wav
|
| 47 |
+
|
| 48 |
+
python livekit_agent.py
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Local test
|
| 52 |
+
|
| 53 |
+
```bash
|
| 54 |
+
python lightweight_infer.py \
|
| 55 |
+
--checkpoint msingiai/sauti-tts \
|
| 56 |
+
--ref_audio path/to/reference.wav \
|
| 57 |
+
--ref_text "Habari, karibu" \
|
| 58 |
+
--text "Hujambo, ninasema na wewe leo." \
|
| 59 |
+
--output out.wav
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## Notes
|
| 63 |
+
|
| 64 |
+
- Free HF CPU Spaces have limited RAM. The pruned+bf16 model fits in ~1.2GB with the vocoder.
|
| 65 |
+
- First inference will take ~30-60s on free CPU; later requests drop to ~5-10s for short sentences.
|
| 66 |
+
- If you hit OOM, set `--no-quantize` and reduce `--steps 5`.
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""FastAPI app for Hugging Face Spaces — lightweight Sauti TTS.
|
| 2 |
+
|
| 3 |
+
Deploy as HF Space (CPU, Free).
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
import logging
|
| 9 |
+
import time
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
import numpy as np
|
| 13 |
+
import soundfile as sf
|
| 14 |
+
from fastapi import FastAPI, HTTPException
|
| 15 |
+
from pydantic import BaseModel
|
| 16 |
+
|
| 17 |
+
logging.basicConfig(level=logging.INFO)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
app = FastAPI(title="Sauti TTS Lightweight")
|
| 21 |
+
|
| 22 |
+
# Global inference engine
|
| 23 |
+
engine = None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TTSRequest(BaseModel):
|
| 27 |
+
text: str
|
| 28 |
+
ref_text: str = ""
|
| 29 |
+
steps: int = 10
|
| 30 |
+
cfg: float = 1.5
|
| 31 |
+
speed: float = 1.0
|
| 32 |
+
seed: int | None = None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@app.on_event("startup")
|
| 36 |
+
def startup():
|
| 37 |
+
global engine
|
| 38 |
+
from lightweight_infer import LightweightSautiInference
|
| 39 |
+
|
| 40 |
+
logger.info("Loading model...")
|
| 41 |
+
t0 = time.time()
|
| 42 |
+
engine = LightweightSautiInference(
|
| 43 |
+
checkpoint=os.getenv("HF_MODEL_ID", "msingiai/sauti-tts"),
|
| 44 |
+
vocab=os.getenv("HF_MODEL_ID", "msingiai/sauti-tts"),
|
| 45 |
+
device="cpu",
|
| 46 |
+
quantize=True,
|
| 47 |
+
nfe_steps=10,
|
| 48 |
+
cfg_strength=1.5,
|
| 49 |
+
)
|
| 50 |
+
logger.info(f"Model loaded in {time.time() - t0:.1f}s")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@app.get("/health")
|
| 54 |
+
def health():
|
| 55 |
+
return {"status": "ok", "model_loaded": engine is not None}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.post("/tts")
|
| 59 |
+
def tts(req: TTSRequest):
|
| 60 |
+
if engine is None:
|
| 61 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 62 |
+
|
| 63 |
+
# Use a default reference audio if none provided
|
| 64 |
+
ref = os.getenv("DEFAULT_REF_AUDIO", "")
|
| 65 |
+
if not ref:
|
| 66 |
+
raise HTTPException(status_code=400, detail="No reference audio configured")
|
| 67 |
+
|
| 68 |
+
t0 = time.time()
|
| 69 |
+
try:
|
| 70 |
+
audio, sr = engine.generate(
|
| 71 |
+
text=req.text,
|
| 72 |
+
ref_audio_path=ref,
|
| 73 |
+
ref_text=req.ref_text,
|
| 74 |
+
speed=req.speed,
|
| 75 |
+
seed=req.seed,
|
| 76 |
+
)
|
| 77 |
+
except Exception as e:
|
| 78 |
+
logger.exception("Inference failed")
|
| 79 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 80 |
+
|
| 81 |
+
elapsed = time.time() - t0
|
| 82 |
+
out_path = Path("/tmp") / f"{uuid.uuid4().hex}.wav"
|
| 83 |
+
sf.write(str(out_path), audio, sr)
|
| 84 |
+
|
| 85 |
+
rtf = elapsed / (len(audio) / sr)
|
| 86 |
+
return {
|
| 87 |
+
"audio_path": str(out_path),
|
| 88 |
+
"sample_rate": sr,
|
| 89 |
+
"duration": len(audio) / sr,
|
| 90 |
+
"inference_sec": elapsed,
|
| 91 |
+
"rtf": rtf,
|
| 92 |
+
}
|
lightweight_infer.py
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Lightweight HuggingFace inference script for Sauti TTS.
|
| 2 |
+
|
| 3 |
+
Usage:
|
| 4 |
+
python lightweight_infer.py \
|
| 5 |
+
--checkpoint msingiai/sauti-tts \
|
| 6 |
+
--text "Habari, karibu kwenye Sauti TTS" \
|
| 7 |
+
--ref_audio path/to/reference.wav \
|
| 8 |
+
--ref_text "Habari, karibu kwenye Sauti TTS" \
|
| 9 |
+
--output output.wav
|
| 10 |
+
|
| 11 |
+
Features:
|
| 12 |
+
- FP16 weight loading to halve memory
|
| 13 |
+
- EPSS reduced NFE steps (5-10 instead of 32)
|
| 14 |
+
- Optional dynamic INT8 quantization on CPU
|
| 15 |
+
- Vocoder caching for low latency
|
| 16 |
+
- Optimized torch.compile for CPU if available
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
|
| 21 |
+
import argparse
|
| 22 |
+
import logging
|
| 23 |
+
import os
|
| 24 |
+
import time
|
| 25 |
+
from pathlib import Path
|
| 26 |
+
from typing import Optional, Tuple
|
| 27 |
+
|
| 28 |
+
import numpy as np
|
| 29 |
+
import torch
|
| 30 |
+
|
| 31 |
+
logging.basicConfig(level=logging.INFO)
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class LightweightSautiInference:
|
| 36 |
+
"""Optimized inference for HF Free CPU Spaces.
|
| 37 |
+
|
| 38 |
+
Optimizations applied:
|
| 39 |
+
1. Load checkpoint in bf16 (2x smaller than fp32)
|
| 40 |
+
2. Strip training artifacts immediately after download
|
| 41 |
+
3. Use EPSS 10-step sampling instead of default 32
|
| 42 |
+
4. Lower default cfg_strength to 1.5
|
| 43 |
+
5. Cache text encoder outputs
|
| 44 |
+
6. Use fast CPU attention via torch SDPA
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
def __init__(
|
| 48 |
+
self,
|
| 49 |
+
checkpoint: str = "msingiai/sauti-tts",
|
| 50 |
+
vocab: str = "msingiai/sauti-tts",
|
| 51 |
+
device: str = "cpu",
|
| 52 |
+
quantize: bool = True,
|
| 53 |
+
nfe_steps: int = 10,
|
| 54 |
+
cfg_strength: float = 1.5,
|
| 55 |
+
):
|
| 56 |
+
self.device = torch.device(device)
|
| 57 |
+
self.nfe_steps = nfe_steps
|
| 58 |
+
self.cfg_strength = cfg_strength
|
| 59 |
+
self.quantize = quantize
|
| 60 |
+
self.model = None
|
| 61 |
+
self.vocoder = None
|
| 62 |
+
self.mel_spec = None
|
| 63 |
+
|
| 64 |
+
logger.info(f"Initializing on device={device}, quantize={quantize}")
|
| 65 |
+
self._load(checkpoint, vocab)
|
| 66 |
+
|
| 67 |
+
def _download_and_prune(self, repo_id: str, filename: str = "model_last.pt") -> str:
|
| 68 |
+
"""Download checkpoint, strip optimizer/scheduler, save as pruned safetensors."""
|
| 69 |
+
from huggingface_hub import hf_hub_download
|
| 70 |
+
|
| 71 |
+
# Always download to a cache dir
|
| 72 |
+
path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 73 |
+
logger.info(f"Downloaded raw checkpoint: {path} ({os.path.getsize(path)/1e9:.2f} GB)")
|
| 74 |
+
|
| 75 |
+
pruned_path = path.replace(".pt", "_pruned.safetensors")
|
| 76 |
+
if os.path.exists(pruned_path):
|
| 77 |
+
logger.info(f"Using cached pruned checkpoint: {pruned_path}")
|
| 78 |
+
return pruned_path
|
| 79 |
+
|
| 80 |
+
logger.info("Pruning checkpoint (removing optimizer/scheduler)...")
|
| 81 |
+
ckpt = torch.load(path, map_location="cpu", weights_only=False)
|
| 82 |
+
|
| 83 |
+
# Keep only EMA weights
|
| 84 |
+
if "ema_model_state_dict" in ckpt:
|
| 85 |
+
state = ckpt["ema_model_state_dict"]
|
| 86 |
+
elif "model_state_dict" in ckpt:
|
| 87 |
+
state = ckpt["model_state_dict"]
|
| 88 |
+
else:
|
| 89 |
+
raise ValueError("Unexpected checkpoint format")
|
| 90 |
+
|
| 91 |
+
# Remove mel_spec buffers (not needed for inference)
|
| 92 |
+
state = {k: v for k, v in state.items() if not k.startswith("mel_spec.")}
|
| 93 |
+
|
| 94 |
+
# Cast to bf16 for 2x memory reduction
|
| 95 |
+
state = {k: v.bfloat16() if v.dtype == torch.float32 else v for k, v in state.items()}
|
| 96 |
+
|
| 97 |
+
from safetensors.torch import save_file
|
| 98 |
+
save_file(state, pruned_path)
|
| 99 |
+
logger.info(f"Saved pruned checkpoint: {pruned_path} ({os.path.getsize(pruned_path)/1e9:.2f} GB)")
|
| 100 |
+
return pruned_path
|
| 101 |
+
|
| 102 |
+
def _quantize(self, model: torch.nn.Module) -> torch.nn.Module:
|
| 103 |
+
"""Apply dynamic INT8 quantization to all Linear layers."""
|
| 104 |
+
try:
|
| 105 |
+
import torch.ao.quantization as quant
|
| 106 |
+
model.eval()
|
| 107 |
+
model = quant.quantize_dynamic(
|
| 108 |
+
model,
|
| 109 |
+
{torch.nn.Linear},
|
| 110 |
+
dtype=torch.qint8,
|
| 111 |
+
inplace=True,
|
| 112 |
+
)
|
| 113 |
+
logger.info("Applied dynamic INT8 quantization")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
logger.warning(f"Quantization failed, running full precision: {e}")
|
| 116 |
+
return model
|
| 117 |
+
|
| 118 |
+
def _load(self, checkpoint: str, vocab: str):
|
| 119 |
+
"""Load pruned, optionally quantized model."""
|
| 120 |
+
pruned = self._download_and_prune(checkpoint)
|
| 121 |
+
vocab_path = self._download_vocab(vocab)
|
| 122 |
+
|
| 123 |
+
from f5_tts.model import CFM, DiT
|
| 124 |
+
from f5_tts.model.utils import get_tokenizer
|
| 125 |
+
from f5_tts.infer.utils_infer import load_vocoder
|
| 126 |
+
|
| 127 |
+
logger.info("Building model architecture...")
|
| 128 |
+
vocab_char_map, vocab_size = get_tokenizer(vocab_path, "custom")
|
| 129 |
+
transformer = DiT(
|
| 130 |
+
dim=1024,
|
| 131 |
+
depth=22,
|
| 132 |
+
heads=16,
|
| 133 |
+
ff_mult=2,
|
| 134 |
+
text_dim=512,
|
| 135 |
+
conv_layers=4,
|
| 136 |
+
text_num_embeds=vocab_size,
|
| 137 |
+
mel_dim=100,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
self.mel_spec = dict(
|
| 141 |
+
n_fft=1024, hop_length=256, win_length=1024,
|
| 142 |
+
n_mel_channels=100, target_sample_rate=24000,
|
| 143 |
+
mel_spec_type="vocos",
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
model = CFM(
|
| 147 |
+
transformer=transformer,
|
| 148 |
+
mel_spec_kwargs=self.mel_spec,
|
| 149 |
+
vocab_char_map=vocab_char_map,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
logger.info("Loading pruned weights...")
|
| 153 |
+
# Load into the EMA online_model if needed
|
| 154 |
+
state = torch.load(pruned, map_location="cpu", weights_only=True)
|
| 155 |
+
if "ema_model_state_dict" in state:
|
| 156 |
+
state = state["ema_model_state_dict"]
|
| 157 |
+
elif "model_state_dict" in state:
|
| 158 |
+
state = state["model_state_dict"]
|
| 159 |
+
|
| 160 |
+
model.load_state_dict(state, strict=False)
|
| 161 |
+
model.to(self.device)
|
| 162 |
+
|
| 163 |
+
if self.quantize:
|
| 164 |
+
model = self._quantize(model)
|
| 165 |
+
|
| 166 |
+
self.model = model
|
| 167 |
+
self.vocoder = load_vocoder(vocoder_name="vocos", device=str(self.device))
|
| 168 |
+
logger.info("Model ready on CPU")
|
| 169 |
+
|
| 170 |
+
def _download_vocab(self, repo_id: str) -> str:
|
| 171 |
+
from huggingface_hub import hf_hub_download
|
| 172 |
+
path = hf_hub_download(repo_id=repo_id, filename="vocab.txt")
|
| 173 |
+
return path
|
| 174 |
+
|
| 175 |
+
@torch.inference_mode()
|
| 176 |
+
def generate(
|
| 177 |
+
self,
|
| 178 |
+
text: str,
|
| 179 |
+
ref_audio_path: str,
|
| 180 |
+
ref_text: str = "",
|
| 181 |
+
speed: float = 1.0,
|
| 182 |
+
seed: Optional[int] = None,
|
| 183 |
+
) -> Tuple[np.ndarray, int]:
|
| 184 |
+
"""Generate Swahili speech.
|
| 185 |
+
|
| 186 |
+
Returns (audio_numpy_array, sample_rate).
|
| 187 |
+
"""
|
| 188 |
+
from f5_tts.infer.utils_infer import infer_process, preprocess_ref_audio_text
|
| 189 |
+
import soundfile as sf
|
| 190 |
+
|
| 191 |
+
if seed is not None:
|
| 192 |
+
torch.manual_seed(seed)
|
| 193 |
+
|
| 194 |
+
ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_path, ref_text)
|
| 195 |
+
|
| 196 |
+
audio, sr, _ = infer_process(
|
| 197 |
+
ref_audio=ref_audio,
|
| 198 |
+
ref_text=ref_text,
|
| 199 |
+
gen_text=text,
|
| 200 |
+
model_obj=self.model,
|
| 201 |
+
vocoder=self.vocoder,
|
| 202 |
+
nfe_step=self.nfe_steps,
|
| 203 |
+
cfg_strength=self.cfg_strength,
|
| 204 |
+
sway_sampling_coef=-1.0,
|
| 205 |
+
speed=speed,
|
| 206 |
+
)
|
| 207 |
+
return audio, sr
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def main():
|
| 211 |
+
parser = argparse.ArgumentParser(description="Lightweight Sauti TTS Inference")
|
| 212 |
+
parser.add_argument("--checkpoint", default="msingiai/sauti-tts")
|
| 213 |
+
parser.add_argument("--vocab", default="msingiai/sauti-tts")
|
| 214 |
+
parser.add_argument("--text", required=True)
|
| 215 |
+
parser.add_argument("--ref_audio", required=True)
|
| 216 |
+
parser.add_argument("--ref_text", default="")
|
| 217 |
+
parser.add_argument("--output", default="output.wav")
|
| 218 |
+
parser.add_argument("--no-quantize", action="store_true")
|
| 219 |
+
parser.add_argument("--steps", type=int, default=10)
|
| 220 |
+
parser.add_argument("--cfg", type=float, default=1.5)
|
| 221 |
+
parser.add_argument("--seed", type=int, default=None)
|
| 222 |
+
args = parser.parse_args()
|
| 223 |
+
|
| 224 |
+
engine = LightweightSautiInference(
|
| 225 |
+
checkpoint=args.checkpoint,
|
| 226 |
+
vocab=args.vocab,
|
| 227 |
+
quantize=not args.no_quantize,
|
| 228 |
+
nfe_steps=args.steps,
|
| 229 |
+
cfg_strength=args.cfg,
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
start = time.time()
|
| 233 |
+
audio, sr = engine.generate(
|
| 234 |
+
text=args.text,
|
| 235 |
+
ref_audio_path=args.ref_audio,
|
| 236 |
+
ref_text=args.ref_text,
|
| 237 |
+
seed=args.seed,
|
| 238 |
+
)
|
| 239 |
+
elapsed = time.time() - start
|
| 240 |
+
|
| 241 |
+
# Save
|
| 242 |
+
import soundfile as sf
|
| 243 |
+
sf.write(args.output, audio, sr)
|
| 244 |
+
logger.info(f"Saved {args.output} | RTF={elapsed / (len(audio)/sr):.2f}x")
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
if __name__ == "__main__":
|
| 248 |
+
main()
|
livekit_agent.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Local LiveKit agent that calls the HF Space TTS server.
|
| 2 |
+
|
| 3 |
+
Prerequisites:
|
| 4 |
+
pip install livekit livekit-agents
|
| 5 |
+
|
| 6 |
+
Usage:
|
| 7 |
+
python livekit_agent.py
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import asyncio
|
| 13 |
+
import logging
|
| 14 |
+
import os
|
| 15 |
+
import uuid
|
| 16 |
+
from typing import Optional
|
| 17 |
+
|
| 18 |
+
import aiohttp
|
| 19 |
+
from livekit import rtc
|
| 20 |
+
from livekit.agents import (
|
| 21 |
+
Agent,
|
| 22 |
+
AgentSession,
|
| 23 |
+
JobContext,
|
| 24 |
+
WorkerOptions,
|
| 25 |
+
cli,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
logger = logging.getLogger("sauti-agent")
|
| 29 |
+
TTS_URL = os.getenv("TTS_URL", "http://localhost:7860")
|
| 30 |
+
DEFAULT_REF = os.getenv("DEFAULT_REF_AUDIO", "")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class SautiAgent(Agent):
|
| 34 |
+
"""Swahili voice agent wrapping Sauti TTS."""
|
| 35 |
+
|
| 36 |
+
def __init__(self):
|
| 37 |
+
super().__init__(
|
| 38 |
+
instructions=(
|
| 39 |
+
"You are a helpful Swahili-speaking assistant. "
|
| 40 |
+
"Keep replies short (1-2 sentences) to keep TTS latency low."
|
| 41 |
+
)
|
| 42 |
+
)
|
| 43 |
+
self._session: Optional[aiohttp.ClientSession] = None
|
| 44 |
+
|
| 45 |
+
async def _tts(self, text: str) -> Optional[bytes]:
|
| 46 |
+
if not self._session:
|
| 47 |
+
self._session = aiohttp.ClientSession()
|
| 48 |
+
|
| 49 |
+
async with self._session.post(
|
| 50 |
+
f"{TTS_URL}/tts",
|
| 51 |
+
json={
|
| 52 |
+
"text": text,
|
| 53 |
+
"ref_text": "",
|
| 54 |
+
"steps": 10,
|
| 55 |
+
"cfg": 1.5,
|
| 56 |
+
"speed": 1.0,
|
| 57 |
+
},
|
| 58 |
+
timeout=aiohttp.ClientTimeout(total=60),
|
| 59 |
+
) as resp:
|
| 60 |
+
if resp.status != 200:
|
| 61 |
+
logger.error("TTS failed: %s", await resp.text())
|
| 62 |
+
return None
|
| 63 |
+
data = await resp.json()
|
| 64 |
+
path = data["audio_path"]
|
| 65 |
+
with open(path, "rb") as f:
|
| 66 |
+
return f.read()
|
| 67 |
+
|
| 68 |
+
async def say(self, text: str):
|
| 69 |
+
audio = await self._tts(text)
|
| 70 |
+
if not audio:
|
| 71 |
+
return
|
| 72 |
+
await self.session.output_stream.say(audio)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
async def run(ctx: JobContext):
|
| 76 |
+
await ctx.connect()
|
| 77 |
+
session = AgentSession()
|
| 78 |
+
await session.start(agent=SautiAgent(), room=ctx.room)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def main():
|
| 82 |
+
cli.run_app(WorkerOptions(entrypoint_fnc=run))
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|
push_to_hf.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi, upload_file
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
TOKEN = os.environ["HF_TOKEN"]
|
| 5 |
+
REPO = "Stanley03/sauti-tts-lightweight"
|
| 6 |
+
|
| 7 |
+
api = HfApi(token=TOKEN)
|
| 8 |
+
files = [
|
| 9 |
+
"lightweight_infer.py",
|
| 10 |
+
"app.py",
|
| 11 |
+
"Dockerfile",
|
| 12 |
+
"requirements.txt",
|
| 13 |
+
"livekit_agent.py",
|
| 14 |
+
"README.md",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
for path in files:
|
| 18 |
+
print(f"Uploading {path}...")
|
| 19 |
+
upload_file(
|
| 20 |
+
path_or_fileobj=path,
|
| 21 |
+
path_in_repo=path,
|
| 22 |
+
repo_id=REPO,
|
| 23 |
+
repo_type="space",
|
| 24 |
+
token=TOKEN,
|
| 25 |
+
)
|
| 26 |
+
print(f" -> {path}")
|
| 27 |
+
|
| 28 |
+
print("All files uploaded.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn==0.30.0
|
| 3 |
+
torch==2.3.1
|
| 4 |
+
torchaudio==2.3.1
|
| 5 |
+
huggingface_hub==0.23.0
|
| 6 |
+
safetensors==0.4.3
|
| 7 |
+
soundfile==0.12.1
|
| 8 |
+
numpy==1.26.4
|
| 9 |
+
scipy==1.14.1
|
| 10 |
+
resampy==0.4.3
|
| 11 |
+
transformers==4.42.0
|
| 12 |
+
accelerate==0.31.0
|
| 13 |
+
librosa==0.10.1
|