import requests import json # 测试获取语音列表 def test_voices(): print("测试获取语音列表...") response = requests.get("http://localhost:7860/voices") if response.status_code == 200: voices = response.json() print(f"获取到 {len(voices)} 种语音") # 打印前5种中文语音 print("中文语音示例:") chinese_voices = [v for v in voices if v['locale'].startswith('zh-')] for voice in chinese_voices[:5]: print(f" - {voice['friendly_name']} ({voice['short_name']})") return True else: print(f"获取语音列表失败: {response.status_code}") return False # 测试文本转语音 def test_tts(): print("\n测试文本转语音...") text = "欢迎使用Edge TTS API服务,这是一个测试语音。" payload = { "text": text, "voice": "zh-CN-YunxiNeural", "rate": "+0%", "volume": "+0%" } response = requests.post("http://localhost:7860/tts", json=payload, stream=True) if response.status_code == 200: with open("test_output.mp3", "wb") as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) print("语音合成成功,已保存为 test_output.mp3") return True else: print(f"语音合成失败: {response.status_code}") print(f"错误信息: {response.text}") return False if __name__ == "__main__": test_voices() test_tts()