Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,146 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
|
| 4 |
-
# --- 1. CPU
|
| 5 |
-
# 这几行代码必须放在所有 import 之前
|
| 6 |
-
# 它的作用是告诉程序:“别找显卡了,我就用 CPU”
|
| 7 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 8 |
import torch
|
| 9 |
torch.cuda.is_available = lambda : False
|
| 10 |
device = "cpu"
|
| 11 |
-
is_half = False # CPU 不支持半精度,必须强制 False
|
| 12 |
|
| 13 |
-
# --- 2.
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
import soundfile as sf
|
| 20 |
-
import numpy as np
|
| 21 |
-
from tools.i18n.i18n import I18nAuto
|
| 22 |
-
from GPT_SoVITS.inference_webui import change_gpt_weights, change_sovits_weights, get_tts_model
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# --- 3.
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# --- 4.
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
print(f"📥
|
| 56 |
|
| 57 |
-
# 检查参考音频是否存在
|
| 58 |
-
if not os.path.exists(REF_AUDIO_PATH):
|
| 59 |
-
return None, f"错误:找不到参考音频 {REF_AUDIO_PATH},请上传!"
|
| 60 |
-
|
| 61 |
try:
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
result_list = list(generator)
|
| 77 |
if result_list:
|
| 78 |
sampling_rate, audio_data = result_list[0]
|
| 79 |
-
output_path = f"
|
| 80 |
sf.write(output_path, audio_data, sampling_rate)
|
| 81 |
-
return output_path, "
|
| 82 |
-
|
| 83 |
except Exception as e:
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
# ---
|
|
|
|
| 87 |
with gr.Blocks() as app:
|
| 88 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
msg = gr.Textbox(label="日志")
|
| 94 |
|
| 95 |
-
btn =
|
| 96 |
-
btn.click(predict_worker, [inp], [out, msg], api_name="predict")
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
app.queue().launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import glob
|
| 4 |
|
| 5 |
+
# --- 1. 强制 CPU 模式 ---
|
|
|
|
|
|
|
| 6 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 7 |
import torch
|
| 8 |
torch.cuda.is_available = lambda : False
|
| 9 |
device = "cpu"
|
|
|
|
| 10 |
|
| 11 |
+
# --- 2. 暴力解决路径问题 ---
|
| 12 |
+
# 打印当前目录结构,方便调试
|
| 13 |
+
print(f"📂 当前工作目录: {os.getcwd()}")
|
| 14 |
+
print(f"📂 根目录文件: {os.listdir('.')}")
|
| 15 |
|
| 16 |
+
# 把当前目录加入 Python 搜索路径
|
| 17 |
+
sys.path.append(os.getcwd())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# 尝试找到 GPT_SoVITS 文件夹并加入路径
|
| 20 |
+
for root, dirs, files in os.walk("."):
|
| 21 |
+
if "GPT_SoVITS" in dirs:
|
| 22 |
+
gpt_sovits_path = os.path.join(root, "GPT_SoVITS")
|
| 23 |
+
sys.path.append(root) # 将父目录加入路径
|
| 24 |
+
print(f"✅ 找到核心模块路径: {root}")
|
| 25 |
+
break
|
| 26 |
|
| 27 |
+
# --- 3. 导入核心类 (不依赖 inference_webui) ---
|
| 28 |
+
try:
|
| 29 |
+
# 直接导入底层的推理类,绕过 UI 层的报错
|
| 30 |
+
from GPT_SoVITS.TTS_infer_pack.TTS import TTS, TTS_Config
|
| 31 |
+
from tools.i18n.i18n import I18nAuto
|
| 32 |
+
print("✅ 成功导入核心 TTS 类!")
|
| 33 |
+
except ImportError as e:
|
| 34 |
+
print(f"❌ 导入失败,尝试备用路径... 错误: {e}")
|
| 35 |
+
# 备用方案:如果上面失败,可能是路径层级问题,尝试直接 import
|
| 36 |
+
try:
|
| 37 |
+
from TTS_infer_pack.TTS import TTS, TTS_Config
|
| 38 |
+
print("✅ 备用路径导入成功!")
|
| 39 |
+
except Exception as e2:
|
| 40 |
+
print(f"❌ 彻底失败,请查看 Files 结构。错误: {e2}")
|
| 41 |
|
| 42 |
+
# --- 4. 自动寻找模型文件 ---
|
| 43 |
+
def find_file(pattern, search_path="."):
|
| 44 |
+
for root, dirs, files in os.walk(search_path):
|
| 45 |
+
for file in files:
|
| 46 |
+
if pattern in file:
|
| 47 |
+
path = os.path.join(root, file)
|
| 48 |
+
print(f"🔍 发现模型: {path}")
|
| 49 |
+
return path
|
| 50 |
+
return None
|
| 51 |
|
| 52 |
+
gpt_path = find_file("s1v3.ckpt")
|
| 53 |
+
sovits_path = find_file("s2Gv2ProPlus.pth")
|
| 54 |
+
|
| 55 |
+
# 如果没找到 V2,找 V1
|
| 56 |
+
if not gpt_path: gpt_path = find_file("s1bert")
|
| 57 |
+
if not sovits_path: sovits_path = find_file("s2G")
|
| 58 |
+
|
| 59 |
+
# --- 5. 初始化 TTS 管道 ---
|
| 60 |
+
# 这是最核心的部分,手动启动推理引擎
|
| 61 |
+
tts_config = TTS_Config("GPT_SoVITS/configs/tts_infer.yaml")
|
| 62 |
+
tts_config.device = "cpu"
|
| 63 |
+
tts_config.is_half = False
|
| 64 |
+
|
| 65 |
+
if gpt_path and sovits_path:
|
| 66 |
+
tts_config.t2s_weights_path = gpt_path
|
| 67 |
+
tts_config.vits_weights_path = sovits_path
|
| 68 |
+
else:
|
| 69 |
+
print("⚠️ 警告:未找到模型文件,后续推理可能会失败!")
|
| 70 |
+
|
| 71 |
+
# 实例化管道
|
| 72 |
+
tts_pipeline = TTS(tts_config)
|
| 73 |
+
print("🚀 TTS 管道初始化完成!")
|
| 74 |
+
|
| 75 |
+
# --- 6. 定义推理函数 ---
|
| 76 |
+
import soundfile as sf
|
| 77 |
+
import numpy as np
|
| 78 |
+
|
| 79 |
+
# 这里一定要改!
|
| 80 |
+
REF_AUDIO_PATH = "ref.wav" # 请确保你上传了这个文件
|
| 81 |
+
REF_TEXT = "你好" # 你的参考音频说了啥
|
| 82 |
+
REF_LANG = "zh"
|
| 83 |
+
|
| 84 |
+
def predict_worker(text):
|
| 85 |
+
if not os.path.exists(REF_AUDIO_PATH):
|
| 86 |
+
return None, "❌ 错误:请先上传 ref.wav!"
|
| 87 |
|
| 88 |
+
print(f"📥 处理: {text}")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
+
# 手动调用推理管道
|
| 92 |
+
# 参数含义: ref_audio, ref_text, ref_lang, target_text, target_lang, ...
|
| 93 |
+
# 注意:run 方法返回的是一个 generator
|
| 94 |
+
req = {
|
| 95 |
+
"text": text,
|
| 96 |
+
"text_lang": "zh",
|
| 97 |
+
"ref_audio_path": REF_AUDIO_PATH,
|
| 98 |
+
"prompt_text": REF_TEXT,
|
| 99 |
+
"prompt_lang": REF_LANG,
|
| 100 |
+
"top_k": 5,
|
| 101 |
+
"top_p": 1,
|
| 102 |
+
"temperature": 1,
|
| 103 |
+
"text_split_method": "cut4", # 凑四句一切
|
| 104 |
+
"batch_size": 1,
|
| 105 |
+
"speed_factor": 1.0,
|
| 106 |
+
"fragment_interval": 0.3,
|
| 107 |
+
"seed": -1,
|
| 108 |
+
"return_fragment": False,
|
| 109 |
+
"parallel_infer": True,
|
| 110 |
+
"repetition_penalty": 1.35
|
| 111 |
+
}
|
| 112 |
|
| 113 |
+
# 调用核心 run 函数
|
| 114 |
+
generator = tts_pipeline.run(req)
|
| 115 |
+
|
| 116 |
+
# 获取结果
|
| 117 |
result_list = list(generator)
|
| 118 |
if result_list:
|
| 119 |
sampling_rate, audio_data = result_list[0]
|
| 120 |
+
output_path = f"out_{os.urandom(4).hex()}.wav"
|
| 121 |
sf.write(output_path, audio_data, sampling_rate)
|
| 122 |
+
return output_path, "✅ 成功"
|
| 123 |
+
|
| 124 |
except Exception as e:
|
| 125 |
+
import traceback
|
| 126 |
+
traceback.print_exc()
|
| 127 |
+
return None, f"💥 报错: {e}"
|
| 128 |
|
| 129 |
+
# --- 7. 界面 ---
|
| 130 |
+
import gradio as gr
|
| 131 |
with gr.Blocks() as app:
|
| 132 |
+
gr.Markdown("# ⚡ GPT-SoVITS 纯净核心版")
|
| 133 |
+
gr.Markdown(f"模型: `{gpt_path}` | `{sovits_path}`")
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
inp = gr.Textbox(label="文本", value="测试一下语音合成效果。")
|
| 137 |
+
btn = gr.Button("生成")
|
| 138 |
|
| 139 |
with gr.Row():
|
| 140 |
+
out = gr.Audio(label="音频")
|
| 141 |
+
log = gr.Textbox(label="日志")
|
|
|
|
| 142 |
|
| 143 |
+
btn.click(predict_worker, [inp], [out, log], api_name="predict")
|
|
|
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
app.queue().launch()
|