I Built a One-Curl Audio Translator with Telnyx AI Inference
I Built a One-Curl Audio Translator with Telnyx AI Inference
我用 Telnyx AI 推理构建了一个“一键式”音频翻译器
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. 过去,本地化播客、翻译录音会议或为讲座配音通常需要将三种不同的服务拼凑在一起。Telnyx AI 推理 API 在同一个私有骨干网上提供了语音转文字(STT)、聊天补全和文字转语音(TTS)功能,这意味着一个简单的 Flask 应用就能运行整个流程。
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. Telnyx 代码示例地址:https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-content-translator-python。这是一个 Python Flask 应用,你可以在几分钟内克隆并运行它。无需电话号码,无需 Webhook 隧道,也无需后台作业运行器。
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. 你通过 POST 请求发送一个音频文件和目标语言。应用会调用 Telnyx STT 将音频转录为源语言,调用 AI 推理引擎并配合适合 TTS 的系统提示词来翻译转录文本,最后调用 Telnyx TTS 将翻译后的文本渲染为音频。长文本会在句子边界处进行分段,以确保 TTS 模型接收的输入不超过其支持范围,随后将这些片段合并为一个 MP3 文件。响应内容包括作业 ID、两种语言的转录文本,以及一个可以通过 curl 下载配音音频的 URL。
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
- 使用 multipart 表单数据上传文件及可写临时文件
- Three Telnyx AI endpoints chained together with per-stage error handling
- 将三个 Telnyx AI 端点串联起来,并具备分阶段错误处理机制
- 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
- 处理 TTS 端点的响应格式,支持返回原始音频、Base64 编码的 JSON 或带有下载 URL 的 JSON
- 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
- 自动语言检测(或指定语言),取决于你是否信任 STT 的检测结果
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. 我最看重的设计选择是分阶段错误处理。如果 TTS 调用在处理 8 个片段中的第 3 个时失败,应用会返回 200 Partial(部分成功)状态码,并保留已完成的转录文本,而不是直接丢弃 STT 和翻译工作。
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
Translate a Spanish clip into English: 将西班牙语片段翻译成英语:
curl -X POST http://localhost:5000/translate \
-F audio=@spanish-sample.mp3 \
-F source=es \
-F target=en
Download the dubbed audio: 下载配音音频:
curl -OJ http://localhost:5000/translate/<job_id>/audio
Read the full transcripts: 读取完整转录文本:
curl http://localhost:5000/translate/<job_id> | python3 -m json.tool
What To Demo
演示建议
I would keep the demo short: 我建议演示保持简短:
- Show the
/languagesendpoint to call out the supported set. - 展示
/languages端点,说明支持的语言集。 - POST a Spanish audio file with
target=enand walk through the response shape (job_id, transcript previews, audio_url). - POST 一个西班牙语音频文件并设置
target=en,演示响应结构(job_id、转录预览、audio_url)。 - Play the downloaded dubbed mp3.
- 播放下载好的配音 MP3。
- Show
GET /translate/<job_id>for the full transcripts. - 展示
GET /translate/<job_id>以获取完整转录文本。 - (Optional) POST with
source=autoto show language detection. - (可选)使用
source=auto进行 POST 请求,展示语言检测功能。
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. 该演示将翻译作业保存在内存中,有效期为一小时。在生产环境中,应将内存中的作业字典替换为对象存储(如 S3、GCS)来存放音频数据,并使用数据库(如 Postgres)存储元数据;将 STT/翻译/TTS 放入队列运行;对于长作业返回 202 Accepted;添加 API 密钥认证;并预先限制上传大小和音频长度。
Resources
资源
- Code example: https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-content-translator-python
- Telnyx AI Inference docs: https://developers.telnyx.com/docs/inference
- Telnyx Developer Docs: https://developers.telnyx.com
- Telnyx Portal: https://portal.telnyx.com