DEV Community

anusha
anusha

Posted on

I Built a One-Curl Audio Translator with Telnyx AI Inference

Localizing a podcast, translating a recorded meeting, or dubbing a lecture used to require three different services stitched together. The Telnyx AI Inference API exposes STT, chat completions, and TTS on the same private backbone, which means a single Flask app can run the whole pipeline.

The Telnyx code example is:

https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-content-translator-python

It is a Python Flask app you can clone and run in a few minutes. No phone number, no webhook tunnel, no background job runner.

The Flow

You POST an audio file plus a target language. The app calls Telnyx STT to transcribe the audio in the source language, calls AI Inference to translate the transcript with a TTS-friendly system prompt, and calls Telnyx TTS to render the translated text into audio. Long transcripts are chunked at sentence boundaries so the TTS model never gets an input larger than it supports, and the chunks are concatenated into one mp3.

The response includes the job id, both transcripts, and a URL you can curl to download the dubbed audio.

Why I Like This Example

It is small enough to demo live, but it covers the parts that often get cut from an audio translation demo:

  • File upload with multipart form data and a writable temp file
  • Three Telnyx AI endpoints chained together with per-stage error handling
  • Long-transcript chunking at sentence boundaries
  • Response shape handling for TTS endpoints that can return raw audio, JSON with base64, or JSON with a fetch URL
  • Temp file cleanup so the upload does not leak disk space
  • Auto language detection (or pinned language) depending on whether you trust STT's detection

The design choice I care most about is per-stage error handling. If the TTS call fails on chunk 3 of 8, the app returns 200 partial with the transcripts still intact, rather than throwing away the STT and translation work.

Run It Locally

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/ai-content-translator-python
cp .env.example .env
pip install -r requirements.txt
python app.py
Enter fullscreen mode Exit fullscreen mode

Translate a Spanish clip into English:

curl -X POST http://localhost:5000/translate \
  -F audio=@spanish-sample.mp3 \
  -F source=es \
  -F target=en
Enter fullscreen mode Exit fullscreen mode

Download the dubbed audio:

curl -OJ http://localhost:5000/translate/<job_id>/audio
Enter fullscreen mode Exit fullscreen mode

Read the full transcripts:

curl http://localhost:5000/translate/<job_id> | python3 -m json.tool
Enter fullscreen mode Exit fullscreen mode

What To Demo

I would keep the demo short:

  1. Show the /languages endpoint to call out the supported set.
  2. POST a Spanish audio file with target=en and walk through the response shape (job_id, transcript previews, audio_url).
  3. Play the downloaded dubbed mp3.
  4. Show GET /translate/<job_id> for the full transcripts.
  5. (Optional) POST with source=auto to show language detection.

That gives viewers the full loop without getting lost in implementation details.

Where To Take It Next

The demo keeps translation jobs in memory for one hour. Production would replace the in-memory jobs dict with object storage (S3, GCS) for the audio blobs and a database (Postgres) for the metadata, run STT / translate / TTS in a queue, return 202 Accepted for long jobs, add API key auth, and cap upload size and audio length up front.

Resources

Top comments (0)