Skip to main content

🚀 Try this Example

View the complete source code on GitHub. Steps to run:
  1. Install fal:
  1. Authenticate (if not already done):
  1. Copy the code below into kokoro.py
  1. Run the app:
Or clone this repository:
Before you run, make sure you have:
  • Authenticated with fal: fal auth login
  • Activated your virtual environment (recommended): python -m venv venv && source venv/bin/activate (macOS/Linux) or venv\Scripts\activate (Windows)

Key Features

  • Multi-Language Support: American English, British English, Japanese with native voices
  • CPU-Efficient Deployment: Lightweight 82M parameter model runs efficiently on CPU
  • Multiple Endpoints: Language-specific endpoints with shared generation logic
  • Voice Variety: Multiple voice options for each supported language
  • Audio Streaming: Generator-based audio processing for memory efficiency
  • Character-Based Billing: Usage-based pricing tied to text length
  • Advanced Validation: Custom error handling with user-friendly messages
  • Audio File Management: Temporary file handling and CDN integration

When to Use CPU Deployment

CPU deployment is ideal when:
  • Models are lightweight (< 100M parameters)
  • Inference is fast enough on CPU
  • Cost optimization is important
  • GPU resources are not required
  • Multiple concurrent requests can share CPU resources efficiently

Project Setup

Language-Specific Input Models

Define input models for each supported language with appropriate voice options:

Language-Specific Output Models

Application Configuration for CPU Deployment

Shared Generation Logic

Create a reusable generation method that handles all languages:

Multiple Endpoint Definitions

Define language-specific endpoints using the shared generation logic:

Key Concepts and Best Practices

CPU-Efficient Deployment

Why CPU for TTS:
  • Kokoro is only 82M parameters - runs efficiently on CPU
  • Lower cost compared to GPU instances
  • Sufficient performance for real-time TTS
  • Better resource utilization for multiple concurrent requests

Audio Streaming and Memory Management

Generator-based processing:

Character-Based Billing

Audio File Handling

Multi-Language Architecture

Pipeline initialization:
Language-specific voice options:

Advanced Features

Custom Validation

Backwards Compatibility

Flexible Text Processing

Deployment and Usage

Running the Service

Making Requests

American English:
British English:
Japanese:

Use Cases

  • Content Creation: Generate voiceovers for videos and podcasts
  • Accessibility: Convert text content to audio for visually impaired users
  • E-Learning: Create educational content with natural-sounding narration
  • Customer Service: Generate dynamic audio responses for chatbots
  • Multilingual Applications: Support global audiences with native-sounding voices
  • Book Reading: Convert written content to audiobooks

Performance Optimizations

Memory Efficiency

Cost Optimization

Key Takeaways

  • CPU deployment is ideal for lightweight models like Kokoro (82M parameters)
  • Multi-language support requires separate pipelines and voice models
  • Character-based billing aligns costs with resource usage
  • Audio streaming handles long texts efficiently without memory issues
  • Temporary file handling with CDN upload provides fast, reliable audio delivery
  • Multiple endpoints with shared logic offer flexibility while maintaining DRY principles
  • Custom validation provides better user experience with clear error messages
This pattern is perfect for building production-ready TTS services that need to support multiple languages and voices while maintaining cost efficiency and high performance through CPU-optimized deployment.