REST API with OpenAI compatible endpoints for both Speech-to-Text (STT) and Text-to-Speech (TTS).
3.1K
This project provides a local, Dockerized REST API with OpenAI compatible endpoints for both Speech-to-Text (STT) and Text-to-Speech (TTS).
Faster-Whisper for transcription.Kokoro-82M for speech synthesis./v1/audio/transcriptions (STT) and /v1/audio/speech (TTS) endpoints.docker-compose.Systran/faster-whisper-large-v3: Best quality, multilingual.Systran/faster-distil-whisper-small.en: Good quality, English-only, faster.Kokoro-82M is used by default via the kokoro library. The language is configured via an environment variable.If you plan to run the application or tests locally (outside Docker), you must install the following system libraries:
sudo apt-get update && sudo apt-get install -y ffmpeg espeak-ng
brew install ffmpeg espeak
ffmpeg and ensure it's in your PATH.espeak-ng and ensure it's in your PATH (and PHONEMIZER_ESPEAK_LIBRARY env var might be needed).Using docker-compose is the recommended way to run this service.
If you prefer to run the service directly on your host system:
Clone the repository:
git clone https://github.com/dlaszlo/speech-service.git
cd speech-service
Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
Install dependencies:
pip install -r requirements/cpu.txt
Set environment variables and start the server:
export STT_MODEL_NAME=Systran/faster-distil-whisper-small.en
export STT_COMPUTE_TYPE=int8
export TTS_MODEL_NAME=hexgrad/Kokoro-82M
export TTS_LANG_CODE=a
python -m uvicorn src.main:app --host 0.0.0.0 --port 8000
Or use the provided helper script:
./bin/start.sh
docker-compose.ymlCreate or edit your docker-compose.yml file and select the appropriate service configuration for your hardware.
services:
speech-service:
image: dlaszlo/speech-service:gpu-latest
container_name: speech-service
ports:
- "8000:8000"
volumes:
- huggingface_cache:/data/huggingface
environment:
- STT_MODEL_NAME=Systran/faster-distil-whisper-small.en
# Compute type: float16 or int8_float16 for GPU recommended
- STT_COMPUTE_TYPE=int8_float16
- TTS_MODEL_NAME=hexgrad/Kokoro-82M
- TTS_LANG_CODE=a
- HF_HOME=/data/huggingface
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
restart: unless-stopped
volumes:
huggingface_cache:
driver: local
The CPU image supports both x86_64 and ARM64 (Raspberry Pi / Apple Silicon) automatically.
services:
speech-service:
image: dlaszlo/speech-service:cpu-latest
container_name: speech-service
ports:
- "8000:8000"
volumes:
- huggingface_cache:/data/huggingface
environment:
- STT_MODEL_NAME=Systran/faster-distil-whisper-small.en
# Compute type: int8 usually works well on ARM too
- STT_COMPUTE_TYPE=int8
- TTS_MODEL_NAME=hexgrad/Kokoro-82M
- TTS_LANG_CODE=a
- HF_HOME=/data/huggingface
restart: unless-stopped
volumes:
huggingface_cache:
driver: local
The API is available at http://localhost:8000.
Check if the service is running and what STT model is loaded.
curl http://localhost:8000/health
This endpoint is compatible with the OpenAI STT API.
curl -X POST "http://localhost:8000/v1/audio/transcriptions" \
-H "Content-Type: multipart/form-data" \
-F "file=@test_speech.wav" \
-F "model=Systran/faster-distil-whisper-small.en"
Response:
{"text":"This is the transcribed text from the audio file."}
You can switch the STT model at runtime without restarting the container.
curl -X POST "http://localhost:8000/v1/models/stt/download" \
-H "Content-Type: application/json" \
-d '{"model_id": "ctranslate2-expert/faster-whisper-medium.en"}'
This endpoint is compatible with OpenAI TTS API. It generates audio in various formats.
curl -X POST "http://localhost:8000/v1/audio/speech" \
-H "Content-Type: application/json" \
-d '{"model": "hexgrad/Kokoro-82M", "input": "The quick brown fox jumps over the lazy dog. This is a test of the English text to speech generation.", "voice": "af_sarah", "response_format": "mp3", "speed": 1.0}' \
--output test_speech.mp3
Request Parameters:
model (Required): The model to use (e.g., hexgrad/Kokoro-82M).input (Required): The text to be synthesized. Maximum length: 4096 characters.voice (Required): The voice to use (e.g., af_sarah, af_heart, am_michael, etc.).response_format (Optional): The audio format. Supported formats: mp3, opus, aac, flac, wav, pcm. Default: mp3.speed (Optional): The speed of generated audio. Range: 0.25 to 4.0. Default: 1.0.
stream_format (Optional): The streaming format. Supported: audio (default), sse (Server-Sent Events).instructions (Optional): Additional voice control instructions.
You can switch the TTS language or model version at runtime.
curl -X POST "http://localhost:8000/v1/models/tts/download" \
-H "Content-Type: application/json" \
-d '{"lang_code": "b", "model_id": "hexgrad/Kokoro-82M"}'
lang_code: The language code (e.g., b for British English).model_id: (Optional) The Hugging Face model repository ID.The project uses pytest for testing. The tests run in-process using FastAPI TestClient, so you do not need to start the server manually before running them.
The tests cover:
The easiest way to run the full test suite (handling virtualenv and dependencies automatically) is:
./bin/run_tests.sh
Install Test Dependencies:
pip install -r requirements/tests.txt
Run All Tests:
pytest tests/
Run Specific Tests:
pytest tests/test_stt.py
pytest tests/test_stt_edge_cases.py
pytest tests/test_tts_basic.py
pytest tests/test_tts_audio_streaming.py
pytest tests/test_tts_sse_streaming.py
docker pull dlaszlo/speech-service:cpu-latest