> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 语言翻译

> 使用在 Qualcomm NPU 上运行的神经机器翻译模型进行跨语言文本翻译。

翻译服务使用神经机器翻译模型在语言对之间转换文本，完全在 Qualcomm NPU 上运行。支持批量翻译——可以在单个请求中翻译多个字符串。

## 前提条件

确保 Audio Analytics 容器已部署并正在运行。有关设置说明，请参阅[运行 Audio Analytics 容器](/zh/ai-workflows/audio-analytics-overview)。

## 可用模型

查询模型端点以获取可用翻译模型列表：

```bash theme={null}
curl http://localhost:8085/audio-analytics/v1/api/translations/models
```

当前可用的模型有：

| 模型           | 翻译方向      |
| ------------ | --------- |
| `opus-en-es` | 英语 → 西班牙语 |
| `opus-es-en` | 西班牙语 → 英语 |
| `opus-en-zh` | 英语 → 中文   |
| `opus-zh-en` | 中文 → 英语   |

在翻译请求中使用响应中的 `name` 值。

## 端点

| 方法     | 端点                        | 描述        |
| ------ | ------------------------- | --------- |
| `GET`  | `/translations/models`    | 列出可用的翻译模型 |
| `POST` | `/translations/translate` | 翻译文本      |

## 翻译文本

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/translations/translate \
  -H "Content-Type: application/json" \
  -d '{
    "text": ["Hello world", "How are you?"],
    "model": "opus-en-es",
    "source_language": "en",
    "target_language": "es"
  }'
```

**参数：**

* `text`（必填）：要翻译的字符串数组
* `model`（必填）：翻译模型名称
* `source_language`（必填）：与所选模型匹配的源语言代码（例如 `opus-en-es` 对应 `en`）
* `target_language`（必填）：目标语言代码

**响应：**

```json theme={null}
{
  "translations": [
    {
      "translated_text": "Hola mundo",
      "source_language": "en",
      "target_language": "es"
    },
    {
      "translated_text": "¿Cómo estás?",
      "source_language": "en",
      "target_language": "es"
    }
  ]
}
```

## 批量翻译

通过传入文本数组，在单个请求中翻译多个字符串：

```bash theme={null}
curl -X POST http://localhost:8085/audio-analytics/v1/api/translations/translate \
  -H "Content-Type: application/json" \
  -d '{
    "text": ["Hello world", "How are you?", "Good morning"],
    "model": "opus-en-es",
    "source_language": "en",
    "target_language": "es"
  }'
```

**响应：**

```json theme={null}
{
  "translations": [
    {"translated_text": "Hola mundo", "source_language": "en", "target_language": "es"},
    {"translated_text": "¿Cómo estás?", "source_language": "en", "target_language": "es"},
    {"translated_text": "Buenos días.", "source_language": "en", "target_language": "es"}
  ]
}
```

结果按输入数组的相同顺序返回。

## Python 示例

```python theme={null}
import requests

def translate(texts, model="opus-en-es", source_language="en", target_language="es"):
    url = "http://localhost:8085/audio-analytics/v1/api/translations/translate"
    response = requests.post(url, json={
        "text": texts,
        "model": model,
        "source_language": source_language,
        "target_language": target_language
    }, timeout=30)
    response.raise_for_status()
    return [t["translated_text"] for t in response.json()["translations"]]

results = translate(["Hello world", "How are you?"])
print(results)
```

## 后续步骤

<CardGroup cols={2}>
  <Card title="语音识别" icon="microphone" href="/zh/ai-workflows/asr-container">
    在设备端将音频转录为文本
  </Card>

  <Card title="文本转语音" icon="volume-high" href="/zh/ai-workflows/tts-container">
    在设备端将文本合成为语音
  </Card>
</CardGroup>
