FastAPI REST API wrapper for IndexTTS2 with OpenAI-compatible endpoints.
- 🚀 FastAPI-based REST API for IndexTTS2
- 🤖 OpenAI-compatible endpoints - drop-in replacement for OpenAI TTS
- 🎤 Dynamic voice discovery - automatically finds all available voices
- 📦 Standalone package - easy to install and deploy
- 🔧 Configurable via environment variables
- 📝 Interactive API docs at
/docs
Install IndexTTS2 first:
git clone https://github.com/index-tts/index-tts.git cd index-tts uv sync --all-extrasDownload the IndexTTS2 model:
hf download IndexTeam/IndexTTS-2 --local-dir=checkpoints
pip install indextts-fastapiOr from source:
git clone https://github.com/yourusername/index-tts-fastapi.git
cd index-tts-fastapi
pip install -e .export INDEXTTS_MODEL_DIR="checkpoints"export INDEXTTS_CFG_PATH="checkpoints/config.yaml"export INDEXTTS_USE_FP16="true"# Enabled by default for lower VRAM usageexport INDEXTTS_USE_DEEPSPEED="true"# Enabled by default for multi-GPU supportindextts-apiOr using uvicorn directly:
uvicorn indextts_fastapi.api:app --host 0.0.0.0 --port 8000- API:
http://localhost:8000 - Interactive Docs:
http://localhost:8000/docs - Health Check:
http://localhost:8000/health
fromopenaiimportOpenAIclient=OpenAI(
base_url="http://localhost:8000/v1",
api_key="not-needed"
)
response=client.audio.speech.create(
model="tts-1",
voice="alloy",
input="Hello from IndexTTS2!"
)
response.stream_to_file("output.mp3")curl -X POST "http://localhost:8000/v1/audio/speech" \
-H "Content-Type: application/json" \
-d '{ "model": "tts-1", "input": "Hello, world!", "voice": "alloy", "response_format": "wav" }' \
--output output.wavcurl -X POST "http://localhost:8000/api/v1/tts" \
-F "text=Hello, this is a test" \
-F "spk_audio_prompt=@path/to/voice.wav" \
-o output.wavPOST /v1/audio/speech- Generate speech (OpenAI-compatible)GET /v1/models- List available modelsGET /v1/voices- List all available voices
POST /api/v1/tts- Generate speech with file uploadPOST /api/v1/tts/json- Generate speech with JSON requestGET /api/v1/voices- List all available voicesGET /health- Health checkGET /model/info- Model information
| Variable | Default | Description |
|---|---|---|
INDEXTTS_MODEL_DIR | checkpoints | Path to model directory |
INDEXTTS_CFG_PATH | checkpoints/config.yaml | Path to config file |
INDEXTTS_USE_FP16 | false | Use FP16 for lower VRAM |
INDEXTTS_USE_CUDA_KERNEL | false | Use CUDA kernel acceleration |
INDEXTTS_USE_DEEPSPEED | false | Use DeepSpeed acceleration (optimization only, not model parallelism) |
INDEXTTS_USE_ACCEL | false | Use acceleration engine |
INDEXTTS_USE_TORCH_COMPILE | false | Use torch.compile optimization |
INDEXTTS_VOICE_DIR | examples | Voice directory path (relative to project root or absolute) |
The API automatically discovers voice files from a single configured directory. Supported formats:
.wav,.mp3,.flac,.m4a,.ogg,.opus
Configuration:
- Set
INDEXTTS_VOICE_DIRenvironment variable to specify the voice directory - Default:
examples(relative to project root) - In Docker: Set to
/app/examplesto match the mounted volume
Voice files are identified by their filename (without extension):
voice_01.wav→ voice ID:voice_01voice_12.wav→ voice ID:voice_12
List all available voices:
curl http://localhost:8000/v1/voices# Clone the repository
git clone https://github.com/yourusername/index-tts-fastapi.git
cd index-tts-fastapi
# Install in development mode
pip install -e ".[dev]"# Run tests
pytest
# Format code
black indextts_fastapi/
ruff check indextts_fastapi/importosos.environ["OPENAI_API_BASE"] ="http://localhost:8000/v1"os.environ["OPENAI_API_KEY"] ="not-needed"# Use with LangChain (if it supports TTS)Any application using OpenAI SDK can be configured:
fromopenaiimportOpenAIclient=OpenAI(
base_url="http://localhost:8000/v1",
api_key="not-needed"
)
# Use normallyresponse=client.audio.speech.create(...)- Voice System: Uses zero-shot voice cloning with reference audio files
- Speed Control: The
speedparameter is accepted but not implemented - Model Parameter: Both "tts-1" and "tts-1-hd" work the same way
- Custom Voices: Supports custom voice files via discovery or file paths
This package is provided as-is. Please refer to the IndexTTS2 license for model usage terms.
Contributions are welcome! Please feel free to submit a Pull Request.
- IndexTTS2 - The underlying TTS model
- FastAPI - The web framework
- OpenAI - For the API compatibility standard
For issues related to:
- This FastAPI wrapper: Open an issue in this repository
- IndexTTS2 model: See IndexTTS2 repository