T2A 快速接入
1.获取鉴权信息


2.复制完整代码并执行
import requests
group_id = "请填写您的group_id"
api_key = "请填写您的api_key"
url = f"https://api.minimax.chat/v1/text_to_speech?GroupId={group_id}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"voice_id": "male-qn-qingse",// 如同时传入voice_id和timber_weights时,则会自动忽略voice_id,以timber_weights传递的参数为准
"text": "你好",
"model": "speech-01",
"speed": 1.0,
"vol": 1.0,
"pitch": 0,
"timber_weights": [
{
"voice_id": "male-qn-qingse",
"weight": 1
},
{
"voice_id": "female-shaonv",
"weight": 1
},
{
"voice_id": "female-yujie",
"weight": 1
},
{
"voice_id": "audiobook_male_2",
"weight": 1
}
]
}
response = requests.post(url, headers=headers, json=data)
print("trace_id", response.headers.get("Trace-Id"))
if response.status_code != 200 or "json" in response.headers["Content-Type"]:
print("调用失败", response.status_code, response.text)
exit()
with open("output.mp3", "wb") as f:
f.write(response.content)
3.构建请求头
url = f"https://api.minimax.chat/v1/text_to_speech?GroupId={group_id}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
4.构建请求内容
data = {
"voice_id": "male-qn-qingse",// 如同时传入voice_id和timber_weights时,则会自动忽略voice_id,以timber_weights传递的参数为准
"text": "你好",
"model": "speech-01",
"speed": 1.0,
"vol": 1.0,
"pitch": 0,
"timber_weights": [
{
"voice_id": "male-qn-qingse",
"weight": 1
},
{
"voice_id": "female-shaonv",
"weight": 1
},
{
"voice_id": "female-yujie",
"weight": 1
},
{
"voice_id": "audiobook_male_2",
"weight": 1
}
]
}
5.完成交互
response = requests.post(url, headers=headers, json=data)
print("trace_id", response.headers.get("Trace-Id"))
if response.status_code != 200 or "json" in response.headers["Content-Type"]:
print("调用失败", response.status_code, response.text)
exit()
with open("output.mp3", "wb") as f:
f.write(response.content)
修改于 2024-01-04 06:24:13