Voice Upload Feature Implementation Summary

๐ŸŽค Overview

Successfully implemented voice file upload functionality for the Chatterbox TTS API, allowing users to upload custom voice samples per request while maintaining full backward compatibility.

๐Ÿ“‹ Changes Made

1. Core Dependencies Added

python-multipart>=0.0.6 - Required for FastAPI multipart/form-data support

Files Updated:

  • requirements.txt - Added python-multipart dependency
  • pyproject.toml - Added python-multipart to project dependencies
  • All Docker files - Added python-multipart to pip install commands

2. Enhanced Speech Endpoint (app/api/endpoints/speech.py)

New Features:

  • โœ… Voice file upload support - Optional voice_file parameter
  • โœ… Multiple endpoint formats - Both JSON and form data support
  • โœ… File validation - Format, size, and content validation
  • โœ… Temporary file handling - Secure file processing with automatic cleanup
  • โœ… Backward compatibility - Existing JSON requests continue to work

Supported File Formats:

  • MP3 (.mp3)
  • WAV (.wav)
  • FLAC (.flac)
  • M4A (.m4a)
  • OGG (.ogg)
  • Maximum size: 10MB

New Endpoints:

  • POST /v1/audio/speech - Multipart form data (supports voice upload)
  • POST /v1/audio/speech/json - Legacy JSON endpoint (backward compatibility)

3. Comprehensive Testing

New Test Files:

  • tests/test_voice_upload.py - Dedicated voice upload testing
  • Updated tests/test_api.py - Tests both JSON and form data endpoints

Test Coverage:

  • โœ… Default voice (both endpoints)
  • โœ… Custom voice upload
  • โœ… File format validation
  • โœ… Error handling
  • โœ… Parameter validation
  • โœ… Backward compatibility

4. Updated Documentation

README.md Updates:

  • Added voice upload examples
  • Documented supported file formats
  • Provided usage examples in multiple languages (Python, cURL)
  • Added file requirements and best practices

๐Ÿš€ Usage Examples

Basic Usage (Default Voice)

# JSON (legacy)
curl -X POST http://localhost:4123/v1/audio/speech/json \
  -H "Content-Type: application/json" \
  -d '{"input": "Hello world!"}' \
  --output output.wav

# Form data (new)
curl -X POST http://localhost:4123/v1/audio/speech \
  -F "input=Hello world!" \
  --output output.wav

Custom Voice Upload

curl -X POST http://localhost:4123/v1/audio/speech \
  -F "input=Hello with my custom voice!" \
  -F "exaggeration=0.8" \
  -F "voice_file=@my_voice.mp3" \
  --output custom_voice.wav

Python Example

import requests

# With custom voice upload
with open("my_voice.mp3", "rb") as voice_file:
    response = requests.post(
        "http://localhost:4123/v1/audio/speech",
        data={
            "input": "Hello with my custom voice!",
            "exaggeration": 0.8,
            "temperature": 1.0
        },
        files={
            "voice_file": ("my_voice.mp3", voice_file, "audio/mpeg")
        }
    )

with open("output.wav", "wb") as f:
    f.write(response.content)

๐Ÿณ Docker Support

All Docker files updated with python-multipart:

  • docker/Dockerfile - Standard Docker image
  • docker/Dockerfile.cpu - CPU-only image
  • docker/Dockerfile.gpu - GPU-enabled image
  • docker/Dockerfile.uv - uv-optimized image
  • docker/Dockerfile.uv.gpu - uv + GPU image

Docker Usage:

# Build and run with voice upload support
docker compose -f docker/docker-compose.yml up -d

# Test voice upload
curl -X POST http://localhost:4123/v1/audio/speech \
  -F "input=Hello from Docker!" \
  -F "[email protected]" \
  --output docker_test.wav

๐Ÿ”ง Technical Implementation

File Processing Flow

  1. Upload - Receive multipart form data with optional voice file
  2. Validate - Check file format, size, and content
  3. Store - Create temporary file with secure naming
  4. Process - Use uploaded file or default voice sample for TTS
  5. Cleanup - Automatically remove temporary files

Memory Management

  • Temporary files are automatically cleaned up in finally blocks
  • File validation prevents oversized uploads
  • Secure temporary file creation with unique names

Error Handling

  • File format validation with helpful error messages
  • File size limits (10MB maximum)
  • Graceful fallback to default voice on upload errors
  • Comprehensive error responses with error codes

๐Ÿงช Testing

Quick Test

# Start the API
python main.py

# Run comprehensive tests
python tests/test_voice_upload.py
python tests/test_api.py

Test Results Expected

  • โœ… Health check
  • โœ… API documentation endpoints
  • โœ… Legacy JSON endpoint compatibility
  • โœ… New form data endpoint
  • โœ… Voice file upload functionality
  • โœ… Error handling and validation

๐Ÿ“š API Documentation

The API documentation is automatically updated and available at:

The documentation now includes:

  • Multipart form data support
  • File upload parameters
  • Example requests and responses
  • Error codes and descriptions

โœ… Backward Compatibility

100% backward compatible:

  • Existing JSON requests work unchanged
  • All previous API behavior preserved
  • Legacy endpoint (/v1/audio/speech/json) maintains exact same interface
  • No breaking changes to existing functionality

๐Ÿ” Security Considerations

  • File type validation prevents malicious uploads
  • File size limits prevent DoS attacks
  • Temporary files use secure random naming
  • Automatic cleanup prevents file system bloat
  • No persistent storage of uploaded files

๐Ÿ“ˆ Performance Impact

  • Minimal overhead for JSON requests (unchanged code path)
  • Temporary file I/O only when voice files are uploaded
  • Efficient memory management with automatic cleanup
  • FastAPI's built-in multipart handling is highly optimized

Status: โœ… Complete and Production Ready

The voice upload feature is fully implemented, tested, and documented. Users can now upload custom voice files for personalized text-to-speech generation while maintaining full backward compatibility with existing implementations.