# travisvn/openai-edge-tts architecture

> Architecture diagram of the GitHub repository travisvn/openai-edge-tts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/travisvn/openai-edge-tts
- Repository: https://github.com/travisvn/openai-edge-tts

## Overview

##### Project Type & Purpose
- This is a **backend microservice** (an OpenAI-compatible TTS API) written in Python.
- Purpose: expose `/v1/audio/speech`, `/v1/models`, `/v1/voices`, etc., translate requests into calls to Microsoft Edge’s online TTS (via `edge-tts`), optionally run audio through ffmpeg for format conversion, and return audio to the client.

##### File Structure
- Top-level files:
- `.env.example` / `.env` for configuration
- `Dockerfile` + `docker-compose.yml` for containerization
- `requirements.txt` for Python deps
- `README.md`, `LICENSE`
- `app/` directory contains core Python modules:
- `server.py` – defines HTTP endpoints & routing
- `handle_text.py` – validates/parses incoming payloads
- `tts_handler.py` – interacts with `edge-tts` library
- `utils.py` – shared helpers (e.g., mapping OpenAI voices to edge-tts voices, speed/format utilities)

##### README for Additional Insights
- Supported endpoints and their parameters
- Use of environment variables (`API_KEY`, `PORT`, defaults for voice/speed/format, feature toggles)
- Optional dependencies: `ffmpeg` for conversion
- Container workflow: Docker Compose / Docker run

##### Main Components & Relationships
1. **Client** (e.g. curl, Open WebUI, AnythingLLM)
2. **API Gateway / HTTP Server** (app/server.py)
3. **Request Validator & Text Handler** (app/handle_text.py)
4. **TTS Orchestrator** (app/tts_handler.py)
5. **Utility Layer** (app/utils.py)
6. **External Service**: Microsoft Edge Online TTS (via `edge-tts` library)
7. **(Optional) ffmpeg process** for audio format conversion
8. **Configuration Store**: Environment variables (`.env`)
9. **Container Platform**: Docker + Docker Compose
b. Data flow / interactions
- Client → HTTP Server (POST /v1/audio/speech with JSON)
- Server → handle_text → tts_handler
- tts_handler → edge-tts (external HTTP calls) → audio stream
- audio stream → [ffmpeg] → desired format
- binary audio → HTTP Response → Client
c. Architectural patterns & design principles
- **Layered**: API layer, business logic (handlers), utilities
- **Separation of Concerns**: parsing vs. orchestration vs. I/O
- **12-factor app** style: config via env vars, stateless service, containerized
d. Key technologies
- Python 3.x, `fastapi` or `flask` (depending on server.py)
- `edge-tts` library (Microsoft Edge TTS)
- `ffmpeg` CLI (optional)
- Docker & Docker Compose

##### Detailed Elements to Capture
- **Environment / Config**: show `.env` driving server port, API key enforcement, defaults
- **Container Boundary**: group all Python modules inside one Docker container
- **Library Calls**: annotate edge-tts invocation within TTS Orchestrator
- **Audio Pipeline**: illustrate branching to ffmpeg for format conversion (mp3, wav, etc.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client (curl, WebUI, etc.)"]:::external

    %% Docker Container Boundary
    subgraph DockerContainer["Docker Container"]
        direction TB
        Server["HTTP Server<br>/v1/audio/speech<br>/v1/models<br>/v1/voices"]:::code
        Handle["Request Validator & Text Handler"]:::code
        TTS["TTS Orchestrator"]:::code
        Utils["Utility Layer"]:::code
        Config["Config Store (.env)"]:::config
    end
    style DockerContainer stroke-dasharray: 5 5

    %% External Services
    EdgeTTS["Microsoft Edge Online TTS"]:::external
    FFMPEG["ffmpeg (optional)"]:::external

    %% Other Files
    Dockerfile["Dockerfile"]:::config
    DC["docker-compose.yml"]:::config
    Req["requirements.txt"]:::config
    Readme["README.md"]:::config

    %% Data Flows
    Client -->|"HTTP POST /v1/audio/speech"| Server
    Server -->|"validate & parse"| Handle
    Handle -->|"orchestrate TTS"| TTS
    TTS -->|"calls edge-tts library"| EdgeTTS
    EdgeTTS -->|"audio stream"| TTS
    TTS -->|"optional conversion"| FFMPEG
    FFMPEG -->|"converted audio"| TTS
    TTS -->|"returns binary audio"| Server
    Server -->|"HTTP Response (audio)"| Client

    %% Dependencies
    Handle -->|uses| Utils
    TTS -->|uses| Utils
    Server -->|loads vars from| Config
    TTS -->|loads vars from| Config

    %% Click Events
    click Server "https://github.com/travisvn/openai-edge-tts/blob/main/app/server.py"
    click Handle "https://github.com/travisvn/openai-edge-tts/blob/main/app/handle_text.py"
    click TTS "https://github.com/travisvn/openai-edge-tts/blob/main/app/tts_handler.py"
    click Utils "https://github.com/travisvn/openai-edge-tts/blob/main/app/utils.py"
    click Config "https://github.com/travisvn/openai-edge-tts/blob/main/.env.example"
    click Dockerfile "https://github.com/travisvn/openai-edge-tts/tree/main/Dockerfile"
    click DC "https://github.com/travisvn/openai-edge-tts/blob/main/docker-compose.yml"
    click Req "https://github.com/travisvn/openai-edge-tts/blob/main/requirements.txt"
    click Readme "https://github.com/travisvn/openai-edge-tts/blob/main/README.md"

    %% Styles
    classDef code fill:#ADD8E6,stroke:#0366d6,color:#000000
    classDef external fill:#90EE90,stroke:#228B22,color:#000000
    classDef config fill:#FFA500,stroke:#cd6100,color:#000000
```
