# 语音模型 调用示例

**本文中，我们将介绍如何使用 Python 调用** [**OpenKEY**](https://openkey.cloud/) **提供的ChatGPT API接口来调用 ChatGPT 语音模型，即 Whisper 和 TTS。**

**系统环境：**

> Python 3.8.10
>
> Name: openai Version: 0.27.8

#### 1. Whisper 模型

Whisper 模型主要功能为转录和翻译，它们可以用于：

* 将音频转录为任何语言。
* 将音频翻译并转录成英语。

目前文件上传限制为 25 MB，并支持以下输入文件类型：mp3、mp4、mpeg、mpga、m4a、wav 和 webm。

{% hint style="info" %}
注意：接口返回格式仅支持json格式，其他尚未适配调用会报错。
{% endhint %}

**1.1 音频转文本**

```python
# 音频转文本
import requests

headers = {
    'Authorization': f'Bearer sk-xxxx', # 注：key为OpenKey创建的令牌
}
url = "https://openkey.cloud/v1/audio/transcriptions" # 注意这里是/transcriptions
file_path = r"./src/voice_250.mp3"  # 文件地址
files = {'file':open(file_path, "rb")}
query = {
            "model":"whisper-1",
            "language":"zh",  # 简体汉语
            "response_format":"json", # 注意：OpenKey接口目前仅支持json一种格式，否则会报错
        }
response = requests.post(url=url, data=query,files=files, headers=headers)
print(response.text)
```

终端输出如下：

<figure><img src="/files/X89mlVskf2CeUyMbMHtm" alt=""><figcaption></figcaption></figure>

**1.2 音频转英文**

```
# 音频转英文
import requests

headers = {
    'Authorization': f'Bearer sk-xxxx', # 注：key为OpenKey创建的令牌
}
url = "https://openkey.cloud/v1/audio/translations" # 注意这里是/translations
file_path = r"./src/voice_250.mp3" # 文件地址
files = {'file':open(file_path, "rb")}
query = {
            "model":"whisper-1",
            "response_format":"json", # 注意：OpenKey接口目前仅支持json一种格式，否则会报错
            "prompt":"This is English"
        }
response = requests.post(url=url, data=query,files=files, headers=headers)
print(response.text)
```

终端输出如下：

<figure><img src="/files/YLrsCZFxF9HLsnHqjmLb" alt=""><figcaption></figcaption></figure>

#### 2. TTS 模型

TTS 模型主要的功能是将文字转语音，具体模型有以下两个：

* **tts-1**: 这是最新的TTS模型，主要针对实时文本转语音使用案例进行了性能优化，速度更快。
* **tts-1-hd**: 与tts-1类似，但更注重语音质量，适用于对语音质量要求更高的场景。

```python
import requests
import json

headers = {
    'Authorization': f'Bearer sk-xxxx', # 注：key为OpenKey创建的令牌
    'Content-Type':'application/json'
}
url = "https://openkey.cloud/v1/audio/speech"
input_text = "《三国演义》以东汉末年三国时期为背景，展现了刘备、关羽、张飞等英雄豪杰的壮丽故事。他们桃园结义，共同抵抗董卓、曹操等反动势力，最终形成了三足鼎立的局面。小说描写了忠义仁爱、智勇双全的形象，同时也展现了权谋斗争和人性的复杂性。《三国演义》是一部具有高历史价值和文学艺术价值的作品，塑造了许多永垂史册的英雄形象，对中国传统文化产生了深远影响。"
query = {
            "model":"tts-1-hd",
            "input":input_text,
            "voice":"alloy",
            "response_format":"mp3",
            "speed":1,
        }
response = requests.post(url=url, data=json.dumps(query), headers=headers)
# 保存文件
f = open("./src/tts-1-hd.mp3", "wb")
f.write(response.content)
f.close()
```

**实现效果如下：**

{% embed url="<https://terobox.oss-cn-hongkong.aliyuncs.com/openkey/tts-1-hd.mp3>" %}

OpenKey完全兼容OpenAI接口协议，具体调用方法参考官方文档：

<https://platform.openai.com/docs/introduction>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.openkey.cloud/api/model-voice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
