Sign inSign up

dattm24/edge-tts-fastapi

By dattm24

Updated 20 days ago

Edge tts running on fastapi with swagger

Image
Machine learning & AI
0

418

dattm24/edge-tts-fastapi repository overview

edge-tts-fastapi

A free, OpenAI-compatible text-to-speech API backed by Microsoft Edge Neural TTS. Drop-in replacement for applications using OpenAI's TTS endpoint.

Website & Documentation

Features

  • OpenAI Compatible API — Works with any client that supports OpenAI's /v1/audio/speech endpoint
  • Free Azure Neural TTS — Powered by Microsoft Edge's edge-tts library, no API keys or credits needed
  • 11 Vocal Presets — alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse
  • 6 Audio Formats — mp3, opus, aac, flac, wav, pcm
  • SSE Streaming — Real-time audio streaming via Server-Sent Events
  • Speed Control — Adjust playback speed from 0.5x to 2.0x
  • Expanded API Support — ElevenLabs and Azure Cognitive Services compatible endpoints
  • Text Preprocessing — Automatic markdown, emoji, and HTML cleanup for natural-sounding output
  • Voice Listing — Query available voices by language/locale

Quick Start

Docker
docker run -d \
  -p 5050:5050 \
  -e DEFAULT_VOICE=en-US-AriaNeural \
  -e DEFAULT_RESPONSE_FORMAT=mp3 \
  -e DEFAULT_SPEED=1.0 \
  --name edge-tts-fastapi \
  dattm24/edge-tts-fastapi
Install FFmpeg (optional, for non-mp3 formats)
docker run -d \
  -p 5050:5050 \
  -e INSTALL_FFMPEG_ARG=true \
  --name edge-tts-fastapi \
  dattm24/edge-tts-fastapi
Docker Compose
services:
  tts:
    image: dattm24/edge-tts-fastapi
    container_name: edge-tts-fastapi
    restart: unless-stopped
    ports:
      - "5050:5050"
    environment:
      DEFAULT_VOICE: en-US-AriaNeural
      DEFAULT_RESPONSE_FORMAT: mp3
      DEFAULT_SPEED: 1.0

API Reference

Generate Speech
POST /v1/audio/speech

Generate audio from text. Accepts JSON body.

Request Body:

{
  "model": "tts-1",
  "input": "Hello, world!",
  "voice": "nova",
  "response_format": "mp3",
  "speed": 1.0
}
ParameterTypeDefaultDescription
modelstringtts-1TTS model name
inputstring(required)Text to synthesize
voicestringen-US-AvaNeuralVoice ID or Azure Neural Voice name
response_formatstringmp3Output audio format
speednumber1.0Playback speed (0.5 – 2.0)

Available Voices (Preset IDs):

Preset IDAzure Neural VoiceLanguage
alloyen-US-JennyNeuralEnglish (US, Female)
ashen-US-AndrewNeuralEnglish (US, Male)
balladen-GB-ThomasNeuralEnglish (GB, Male)
coralen-AU-NatashaNeuralEnglish (AU, Female)
echoen-US-GuyNeuralEnglish (US, Male)
fableen-GB-SoniaNeuralEnglish (GB, Female)
novaen-US-AriaNeuralEnglish (US, Female)
onyxen-US-EricNeuralEnglish (US, Male)
sageen-US-JennyNeuralEnglish (US, Female)
shimmeren-US-EmmaNeuralEnglish (US, Female)
verseen-US-BrianNeuralEnglish (US, Male)

Or use any Azure Neural Voice name directly (e.g. zh-CN-XiaoxiaoNeural).

Response: Binary audio file with Content-Type matching the requested format.

cURL Example:

curl -X POST http://localhost:5050/v1/audio/speech \
  -H "Content-Type: application/json" \
  -d '{"input": "Hello, world!", "voice": "nova", "response_format": "mp3"}' \
  --output speech.mp3

OpenAI Python Client Example:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:5050/v1",
    api_key="fake-key"
)

response = client.audio.speech.create(
    model="tts-1",
    voice="nova",
    input="Hello, world!",
    response_format="mp3",
    speed=1.0
)

response.stream_to_file("output.mp3")
SSE Streaming

Add "stream_format": "sse" to request body for real-time streaming:

curl -X POST http://localhost:5050/v1/audio/speech \
  -H "Content-Type: application/json" \
  -d '{"input": "Hello, world!", "voice": "nova", "stream_format": "sse"}'

Response format (SSE events):

{"type": "speech.audio.delta", "audio": "base64EncodedChunk"}
{"type": "speech.audio.delta", "audio": "base64EncodedChunk"}
{"type": "speech.audio.done", "usage": {"input_tokens": 3}}
List Available Models
GET /v1/models

Response:

{
  "models": [
    {"id": "tts-1", "name": "Text-to-speech v1"},
    {"id": "tts-1-hd", "name": "Text-to-speech v1 HD"},
    {"id": "gpt-4o-mini-tts", "name": "GPT-4o mini TTS"}
  ]
}
List Voices
GET /v1/voices
GET /v1/voices/all

Returns available voices filtered by optional ?language=en-US query parameter.

Expanded API Endpoints

Compatible with third-party client integrations. Requires EXPAND_API=True.

EndpointMethodDescription
/elevenlabs/v1/text-to-speech/{voice_id}POSTElevenLabs-style TTS (expects text field)
/azure/cognitiveservices/v1POSTAzure SSML-style TTS (expects SSML body)
/v1/audio/speech, /audio/speechPOSTOpenAI-compatible speech generation

Environment Variables

VariableTypeDefaultDescription
PORTint5050Server port
HOSTstring0.0.0.0Bind address
DEFAULT_VOICEstringen-US-AvaNeuralDefault voice
DEFAULT_RESPONSE_FORMATstringmp3Output audio format
DEFAULT_SPEEDfloat1.0Playback speed
DEFAULT_LANGUAGEstringen-USDefault language for voice listing
REQUIRE_API_KEYboolFalseRequire X-API-Key header
REMOVE_FILTERboolFalseSkip markdown/emoji text preprocessing
EXPAND_APIboolTrueEnable ElevenLabs & Azure endpoints
DETAILED_ERROR_LOGGINGboolTrueVerbose error output in logs

License

[Add your license here]


Built with edge-tts, FastAPI, and ❤️.

Website & Documentation

Tag summary

Content type

Image

Digest

sha256:f95b68165

Size

78.9 MB

Last updated

20 days ago

docker pull dattm24/edge-tts-fastapi