Python 3.8.10
Name: openai Version: 0.27.8
# 音频转文本
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)
# 音频转英文
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)
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()