8000 GitHub - elonchen/KoGen: A lightweight Text-to-Speech (TTS) API service combining Kokoro with Gentle · GitHub
[go: up one dir, main page]

Skip to content
7FFF
elonchenPublic

About

A lightweight Text-to-Speech (TTS) API service combining Kokoro with Gentle

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

KoGen - Kokoro TTS API

A lightweight, CPU-optimized Text-to-Speech (TTS) API service combining Kokoro-82M (advanced TTS synthesis) with Gentle (precise audio alignment) for professional-grade speech generation and synchronization.

简体中文文档 | English

🎯 Features

  • Multi-language Support: Supports multiple languages including English and Chinese
  • Multiple Voice Options: Various voice profiles (e.g., af_heart) for diverse speech synthesis
  • Speed Control: Adjustable speech speed parameter for customized output
  • Audio Alignment: Optional integration with Gentle for precise word-level timing alignment
  • CPU-Only Optimization: Designed to run efficiently on CPU-only environments
  • Docker Support: Complete Docker and Docker Compose setup for easy deployment
  • Base64 Audio Output: Audio returned as base64-encoded data for easy integration
  • CORS Enabled: Cross-Origin Resource Sharing enabled for web applications
  • FastAPI Framework: Modern, fast, and easy-to-use REST API

📋 Requirements

  • Python 3.11+
  • Docker & Docker Compose (for containerized deployment)
  • 4GB+ RAM (recommended for smooth CPU operation)

🚀 Quick Start

Option 1: Docker Compose (Recommended)

docker-compose up -d

This will start:

  • Kokoro TTS API on http://localhost:8000
  • Gentle Audio Alignment Service on http://localhost:8765

Option 2: Local Installation

  1. Install dependencies:
pip install -r requirements.txt
  1. (Optional) Install and run Gentle for audio alignment:

    Using Docker:

    docker run -d -p 8765:8765 lowerquality/gentle

    Or manually:

    • Clone the Gentle repository: git clone https://github.com/lowerquality/gentle.git
    • Follow the installation instructions in the Gentle repository
    • Run Gentle on port 8765
  2. Run the API:

uvicorn tts_api:app --host 0.0.0.0 --port 8000

If Gentle is not running, the API will still work but audio alignment (align: true) will fail.

📡 API Endpoints

GET /

Returns service information and usage instructions.

Response:

{
  "message": "Kokoro-82M TTS API is running",
  "gentle_url": "http://gentle:8765/transcriptions?async=false",
  "usage": "POST /tts with JSON: {text, voice, speed, lang}"
}

POST /tts

Synthesizes text to speech.

Request Body:

{
  "text": "Hello, this is a test.",
  "voice": "af_heart",
  "speed": 1.0,
  "lang": "a",
  "align": false
}

Parameters:

  • text (string, required): The text to synthesize
  • voice (string, optional): Voice profile name. Default: "af_heart"
  • speed (float, optional): Speech speed multiplier. Default: 1.0
  • lang (string, optional): Language code. Default: "a" (English). Use "z" for Chinese
  • align (boolean, optional): Enable audio alignment with Gentle. Default: false

Response:

{
  "audio_base64": "UklGRi4A...",
  "align_data": {
    "words": [...],
    "transcript": "Hello, this is a test."
  }
}

🔧 Configuration

Environment Variables

  • GENTLE_URL: URL of the Gentle alignment service. Default: http://gentle:8765/transcriptions?async=false
  • LANG_CODE: Default language code. Default: "a" (English)

Supported Languages

  • "a": English
  • "z": Chinese
  • Other language codes may be supported by the Kokoro model

📦 Project Structure

KoGen/
├── tts_api.py              # Main FastAPI application
├── Dockerfile.cpu          # CPU-optimized Docker image
├── docker-compose.yml      # Docker Compose configuration
├── requirements.txt        # Python dependencies
├── cache/                  # Model cache directory
└── data/                   # Output audio and logs directory

🎵 Audio Output

  • Format: WAV
  • Sample Rate: 24000 Hz
  • Encoding: Base64 (in JSON response)
  • Multiple Segments: Automatically merged into a single audio file

🔄 Audio Alignment

When align: true is set, the API:

  1. Generates audio using Kokoro TTS
  2. Sends the audio to Gentle for word-level alignment
  3. Returns both the audio and alignment data with precise timing information

This is useful for:

  • Subtitle generation
  • Karaoke applications
  • Precise audio-text synchronization

🐳 Docker Details

Dockerfile.cpu

  • Base image: python:3.11-slim
  • Includes: espeak-ng, libsndfile1, ffmpeg, and other audio processing tools
  • Pre-loads Kokoro model during build to reduce first-run latency
  • Exposes port 8000

Docker Compose Services

  • kokoro: TTS API service (port 8000)
  • gentle: Audio alignment service (port 8765)

📝 Example Usage

Using cURL

curl -X POST http://localhost:8000/tts \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Hello world",
    "voice": "af_heart",
    "speed": 1.0,
    "lang": "a",
    "align": false
  }'

Using Python

import requests
import base64

response = requests.post('http://localhost:8000/tts', json={
    'text': 'Hello world',
    'voice': 'af_heart',
    'speed': 1.0,
    'lang': 'a',
    'align': False
})

data = response.json()
audio_bytes = base64.b64decode(data['audio_base64'])

# Save to file
with open('output.wav', 'wb') as f:
    f.write(audio_bytes)

🛠️ Troubleshooting

Model Loading Issues

  • Ensure sufficient disk space for model cache (~2GB)
  • Check that /root/.cache/huggingface is properly mounted in Docker

Audio Quality Issues

  • Verify the language code matches your input text
  • Adjust the speed parameter if audio sounds distorted
  • Ensure the voice profile is valid for the selected language

Alignment Failures

  • Verify Gentle service is running and accessible
  • Check that the transcript matches the generated audio closely
  • Ensure audio quality is sufficient for alignment

📄 License

This project uses the Kokoro TTS model. Please refer to the model's license for usage terms.

🤝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

📞 Support

For issues and questions, please open an issue on the project repository.

About

A lightweight Text-to-Speech (TTS) API service combining Kokoro with Gentle

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0