跳转到主要内容

概述

将任何受支持语言的音频翻译为英文文本。与转录不同,此端点无论输入语言是什么,始终输出英文文本。

请求体

同步请求超时: 这个非聊天端点会等待路由到的模型完成处理。大输入、长音频或大批量请求可能超过常见的 30s 客户端默认超时,因此请将 HTTP 客户端超时设置为至少 120s
file
file
必填
要翻译的音频文件。支持的格式:flacmp3mp4mpegmpgam4aoggwavwebm。最大文件大小为 25 MB。
model
string
默认值:"whisper-1"
要使用的模型。当前仅支持 whisper-1
prompt
string
用于引导模型风格或续写上一段内容的可选文本。应使用英文。
response_format
string
默认值:"json"
输出格式。可选值:jsontextsrtverbose_jsonvtt
temperature
number
采样温度,取值范围在 0 到 1 之间。较高的值(如 0.8)会产生更随机的输出,而较低的值(如 0.2)会使输出更集中且更具确定性。

响应

text
string
英文翻译文本。
对于 verbose_json 格式,响应还包括:
language
string
输入音频检测出的语言。
duration
number
输入音频的时长,单位为秒。
segments
array
带时间戳的翻译文本分段。
curl -X POST "https://api.tokenlab.sh/v1/audio/translations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -F "file=@german_audio.mp3" \
  -F "model=whisper-1"
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://api.tokenlab.sh/v1"
)

with open("german_audio.mp3", "rb") as audio_file:
    response = client.audio.translations.create(
        model="whisper-1",
        file=audio_file
    )

print(response.text)
import OpenAI from 'openai';
import fs from 'fs';

const client = new OpenAI({
  apiKey: 'sk-your-api-key',
  baseURL: 'https://api.tokenlab.sh/v1'
});

const response = await client.audio.translations.create({
  model: 'whisper-1',
  file: fs.createReadStream('german_audio.mp3')
});

console.log(response.text);
<?php
$ch = curl_init('https://api.tokenlab.sh/v1/audio/translations');

$file = new CURLFile('german_audio.mp3', 'audio/mpeg', 'german_audio.mp3');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer sk-your-api-key'
    ],
    CURLOPT_POSTFIELDS => [
        'file' => $file,
        'model' => 'whisper-1'
    ]
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo $data['text'];
{
  "text": "Hello, my name is Wolfgang and I come from Germany. Where are you from?"
}
{
  "task": "translate",
  "language": "german",
  "duration": 8.470000267028809,
  "text": "Hello, my name is Wolfgang and I come from Germany. Where are you from?",
  "segments": [
    {
      "id": 0,
      "seek": 0,
      "start": 0.0,
      "end": 4.0,
      "text": " Hello, my name is Wolfgang and I come from Germany.",
      "tokens": [50364, 2425, 11, 452, 1315, 307, 25329, 293, 286, 808, 490, 5765, 13, 50564],
      "temperature": 0.0,
      "avg_logprob": -0.45,
      "compression_ratio": 1.0,
      "no_speech_prob": 0.0
    }
  ]
}

翻译与转录

功能翻译转录
输出语言始终为英文与输入相同
使用场景将外语音频转换为英文保留原始语言
language 参数不适用可选提示
翻译端点会自动检测源语言并将其翻译为英文。转录中的 language 参数会被忽略。