Ai-Teacher/tts_test.py
2025-03-07 22:17:52 +08:00

119 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import base64
import json
import io
from IPython.display import Audio
import soundfile as sf
# 假设您的FastAPI服务在本地运行
BASE_URL = "http://feng-arch.cn:31006"
def tts_file_response(text, voice="af_heart", speed=1.0, lang_code="z"):
"""
发送请求并直接获取音频文件
"""
url = f"{BASE_URL}/tts"
payload = {
"text": text,
"voice": voice,
"speed": speed,
"return_type": "file"
}
response = requests.post(url, json=payload)
if response.status_code == 200:
# 保存音频文件
with open("received_audio.wav", "wb") as f:
f.write(response.content)
print("音频已保存为 received_audio.wav")
# 如果在Jupyter Notebook中可以直接播放
return Audio(data=response.content, rate=24000)
else:
print(f"错误: {response.status_code}")
print(response.text)
return None
def tts_base64_response(text, voice="af_heart", speed=1.0, lang_code="z"):
"""
发送请求并获取base64编码的音频数据
"""
url = f"{BASE_URL}/tts"
payload = {
"text": text,
"voice": voice,
"speed": speed,
"return_type": "base64"
}
response = requests.post(url, json=payload)
if response.status_code == 200:
data = response.json()
# 获取base64编码的音频
audio_base64 = data.get("audio_base64")
# 解码base64数据
audio_data = base64.b64decode(audio_base64)
# 保存音频文件
with open("received_audio.wav", "wb") as f:
f.write(audio_data)
print("音频已保存为 received_audio.wav")
# 如果在Jupyter Notebook中可以直接播放
return Audio(data=audio_data, rate=24000)
else:
print(f"错误: {response.status_code}")
print(response.text)
return None
def get_available_voices(lang_code="z"):
"""
获取指定语言的可用声音列表
Name Traits Target Quality Training Duration Overall Grade SHA256
af_heart 🚺❤️ A 0ab5709b
af_alloy 🚺 B MM minutes C 6d877149
af_aoede 🚺 B H hours C+ c03bd1a4
af_bella 🚺🔥 A HH hours A- 8cb64e02
af_jessica 🚺 C MM minutes D cdfdccb8
af_kore 🚺 B H hours C+ 8bfbc512
af_nicole 🚺🎧 B HH hours B- c5561808
af_nova 🚺 B MM minutes C e0233676
af_river 🚺 C MM minutes D e149459b
af_sarah 🚺 B H hours C+ 49bd364e
af_sky 🚺 B M minutes 🤏 C- c799548a
am_adam 🚹 D H hours F+ ced7e284
am_echo 🚹 C MM minutes D 8bcfdc85
am_eric 🚹 C MM minutes D ada66f0e
am_fenrir 🚹 B H hours C+ 98e507ec
am_liam 🚹 C MM minutes D c8255075
am_michael 🚹 B H hours C+ 9a443b79
am_onyx 🚹 C MM minutes D e8452be1
am_puck 🚹 B H hours C+ dd1d8973
am_santa 🚹 C M minutes 🤏 D- 7f2f7582
Name Traits Target Quality Training Duration Overall Grade SHA256
zf_xiaobei 🚺 C MM minutes D 9b76be63
zf_xiaoni 🚺 C MM minutes D 95b49f16
zf_xiaoxiao 🚺 C MM minutes D cfaf6f2d
zf_xiaoyi 🚺 C MM minutes D b5235dba
zm_yunjian 🚹 C MM minutes D 76cbf8ba
zm_yunxi 🚹 C MM minutes D dbe6e1ce
zm_yunxia 🚹 C MM minutes D bb2b03b0
zm_yunyang 🚹 C MM minutes D 5238ac22
"""
# 示例使用
if __name__ == "__main__":
text = "你能解决什么问题"
# 获取音频文件
audio = tts_file_response(text, voice="zf_xiaoxiao")
# 或者获取base64编码的音频
# audio = tts_base64_response(text)