Skip to content

Repository files navigation

IndexTTS FastAPI

FastAPI REST API wrapper for IndexTTS2 with OpenAI-compatible endpoints.

Features

  • 🚀 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

Installation

Prerequisites

  1. Install IndexTTS2 first:

    git clone https://github.com/index-tts/index-tts.git
    cd index-tts
    uv sync --all-extras
  2. Download the IndexTTS2 model:

    hf download IndexTeam/IndexTTS-2 --local-dir=checkpoints

Install IndexTTS FastAPI

pip install indextts-fastapi

Or from source:

git clone https://github.com/yourusername/index-tts-fastapi.git
cd index-tts-fastapi
pip install -e .

Quick Start

1. Set up environment variables (optional)

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 support

2. Run the API server

indextts-api

Or using uvicorn directly:

uvicorn indextts_fastapi.api:app --host 0.0.0.0 --port 8000

3. Access the API

  • API: http://localhost:8000
  • Interactive Docs: http://localhost:8000/docs
  • Health Check: http://localhost:8000/health

Usage Examples

OpenAI-Compatible API

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")

Direct HTTP Request

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.wav

Native API with File Upload

curl -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.wav

API Endpoints

OpenAI-Compatible Endpoints

  • POST /v1/audio/speech - Generate speech (OpenAI-compatible)
  • GET /v1/models - List available models
  • GET /v1/voices - List all available voices

Native Endpoints

  • POST /api/v1/tts - Generate speech with file upload
  • POST /api/v1/tts/json - Generate speech with JSON request
  • GET /api/v1/voices - List all available voices
  • GET /health - Health check
  • GET /model/info - Model information

Configuration

Environment Variables

VariableDefaultDescription
INDEXTTS_MODEL_DIRcheckpointsPath to model directory
INDEXTTS_CFG_PATHcheckpoints/config.yamlPath to config file
INDEXTTS_USE_FP16falseUse FP16 for lower VRAM
INDEXTTS_USE_CUDA_KERNELfalseUse CUDA kernel acceleration
INDEXTTS_USE_DEEPSPEEDfalseUse DeepSpeed acceleration (optimization only, not model parallelism)
INDEXTTS_USE_ACCELfalseUse acceleration engine
INDEXTTS_USE_TORCH_COMPILEfalseUse torch.compile optimization
INDEXTTS_VOICE_DIRexamplesVoice directory path (relative to project root or absolute)

Voice Discovery

The API automatically discovers voice files from a single configured directory. Supported formats:

  • .wav, .mp3, .flac, .m4a, .ogg, .opus

Configuration:

  • Set INDEXTTS_VOICE_DIR environment variable to specify the voice directory
  • Default: examples (relative to project root)
  • In Docker: Set to /app/examples to match the mounted volume

Voice files are identified by their filename (without extension):

  • voice_01.wav → voice ID: voice_01
  • voice_12.wav → voice ID: voice_12

List all available voices:

curl http://localhost:8000/v1/voices

Development

# 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/

Integration Examples

With LangChain

importosos.environ["OPENAI_API_BASE"] ="http://localhost:8000/v1"os.environ["OPENAI_API_KEY"] ="not-needed"# Use with LangChain (if it supports TTS)

With Custom Applications

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(...)

Differences from OpenAI TTS

  1. Voice System: Uses zero-shot voice cloning with reference audio files
  2. Speed Control: The speed parameter is accepted but not implemented
  3. Model Parameter: Both "tts-1" and "tts-1-hd" work the same way
  4. Custom Voices: Supports custom voice files via discovery or file paths

License

This package is provided as-is. Please refer to the IndexTTS2 license for model usage terms.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

Support

For issues related to:

About

Fast API wrapper for IndexTTSv2

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages