125 lines
3.6 KiB
Python
125 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
快速生成语音脚本
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import json
|
|
import requests
|
|
import time
|
|
from pathlib import Path
|
|
|
|
def start_server():
|
|
"""启动Fish Speech服务器"""
|
|
print("启动Fish Speech服务器...")
|
|
|
|
fish_speech_dir = Path("/root/tts/fish-speech")
|
|
|
|
# 启动API服务器
|
|
cmd = [
|
|
sys.executable, "tools/api_server.py",
|
|
"--llama-checkpoint-path", "checkpoints/fish-speech-1.5/model.pth",
|
|
"--decoder-checkpoint-path", "checkpoints/fish-speech-1.5/firefly-gan-vq-fsq-8x1024-21hz-generator.pth",
|
|
"--device", "cpu"
|
|
]
|
|
|
|
os.chdir(fish_speech_dir)
|
|
|
|
# 在后台启动服务器
|
|
import subprocess
|
|
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
# 等待服务器启动
|
|
print("等待服务器启动...")
|
|
time.sleep(30) # 给足够时间启动
|
|
|
|
return process
|
|
|
|
def generate_audio(text, output_file):
|
|
"""生成音频"""
|
|
|
|
# 检查服务器是否运行
|
|
try:
|
|
response = requests.get("http://127.0.0.1:7860/health", timeout=5)
|
|
if response.status_code != 200:
|
|
print("服务器未准备就绪")
|
|
return False
|
|
except:
|
|
print("无法连接到服务器")
|
|
return False
|
|
|
|
# 准备请求数据
|
|
url = "http://127.0.0.1:7860/v1/tts"
|
|
|
|
# 读取参考音频
|
|
reference_audio_path = "/root/tts/ben_guanquelou.wav"
|
|
|
|
# 准备请求
|
|
data = {
|
|
"text": text,
|
|
"reference_text": "登鹳雀楼,白日依山尽,黄河入海流。欲穷千里目,更上一层楼。",
|
|
"max_new_tokens": 1024,
|
|
"chunk_length": 200,
|
|
"top_p": 0.7,
|
|
"repetition_penalty": 1.2,
|
|
"temperature": 0.7
|
|
}
|
|
|
|
files = {
|
|
"reference_audio": open(reference_audio_path, "rb")
|
|
}
|
|
|
|
try:
|
|
print(f"正在生成音频: {text}")
|
|
response = requests.post(url, data=data, files=files, timeout=300)
|
|
|
|
if response.status_code == 200:
|
|
# 保存音频
|
|
with open(output_file, "wb") as f:
|
|
f.write(response.content)
|
|
print(f"✅ 音频生成成功: {output_file}")
|
|
return True
|
|
else:
|
|
print(f"❌ 生成失败: {response.status_code} - {response.text}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"❌ 请求错误: {e}")
|
|
return False
|
|
finally:
|
|
files["reference_audio"].close()
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("=== Fish Speech 快速语音生成 ===")
|
|
|
|
# 测试文本1
|
|
text1 = "海内存知己,天涯若比邻。"
|
|
output1 = "/root/tts/audio/output/huaineizhiji_test.wav"
|
|
|
|
# 测试文本2 (来自zhuluoji.md的第一段)
|
|
text2 = "埃利泽・本 - 耶胡达,那位现代希伯来语之父,不正是现实里的约翰・哈蒙德吗?在没人说这种语言的世界里,他像偏执的疯子,用古老词汇命名现代事物。"
|
|
output2 = "/root/tts/audio/output/zhuluoji_test.wav"
|
|
|
|
# 确保输出目录存在
|
|
os.makedirs("/root/tts/audio/output", exist_ok=True)
|
|
|
|
# 生成第一个音频
|
|
print("\n🎤 生成第一个音频...")
|
|
success1 = generate_audio(text1, output1)
|
|
|
|
# 生成第二个音频
|
|
print("\n🎤 生成第二个音频...")
|
|
success2 = generate_audio(text2, output2)
|
|
|
|
if success1 and success2:
|
|
print("\n🎉 所有音频生成完成!")
|
|
print(f"📁 文件位置:")
|
|
print(f" - {output1}")
|
|
print(f" - {output2}")
|
|
else:
|
|
print("\n💔 部分或全部音频生成失败")
|
|
|
|
if __name__ == "__main__":
|
|
main() |