Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
import
|
|
|
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
-
# 1.
|
| 7 |
# ==========================================
|
| 8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 9 |
import torch
|
|
@@ -13,42 +14,72 @@ def no_op(self, *args, **kwargs): return self
|
|
| 13 |
torch.Tensor.cuda = no_op
|
| 14 |
torch.nn.Module.cuda = no_op
|
| 15 |
|
| 16 |
-
print("💉 CUDA
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
-
# 2.
|
| 20 |
# ==========================================
|
| 21 |
sys.path.append(os.getcwd())
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
import inference_webui as core
|
| 25 |
print("✅ 成功导入 inference_webui")
|
|
|
|
|
|
|
| 26 |
except ImportError:
|
| 27 |
print("❌ 找不到 inference_webui.py")
|
| 28 |
sys.exit(1)
|
| 29 |
|
| 30 |
-
# ==========================================
|
| 31 |
-
# 3. 核心魔法:内存热替换 (Hot Swap)
|
| 32 |
-
# ==========================================
|
| 33 |
-
# 这是解决 RuntimeError 的关键!
|
| 34 |
-
try:
|
| 35 |
-
print("🧠 正在进行模型架构手术...")
|
| 36 |
-
|
| 37 |
-
# 1. 导入 CPU 版模型类
|
| 38 |
-
from AR.models.t2s_model import Text2SemanticDecoder as CPU_Decoder
|
| 39 |
-
|
| 40 |
-
# 2. 导入 GPU 版模型模块 (不管它是否存在)
|
| 41 |
-
import AR.models.t2s_model_flash_attn as GPU_Module
|
| 42 |
-
|
| 43 |
-
# 3. 【关键步骤】把 GPU 模块里的类,强行替换成 CPU 版的类
|
| 44 |
-
# 这样,后续代码以为自己在用 GPU 版,实际上用的是 CPU 版
|
| 45 |
-
GPU_Module.Text2SemanticDecoder = CPU_Decoder
|
| 46 |
-
|
| 47 |
-
print("✅ 手术成功:FlashAttn 模型已被替换为普通 CPU 模型。")
|
| 48 |
-
|
| 49 |
-
except Exception as e:
|
| 50 |
-
print(f"⚠️ 手术警告 (如果本来就没装 FlashAttn 则忽略): {e}")
|
| 51 |
-
|
| 52 |
# ==========================================
|
| 53 |
# 4. 自动寻找模型
|
| 54 |
# ==========================================
|
|
@@ -74,17 +105,10 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
|
|
| 74 |
# ==========================================
|
| 75 |
try:
|
| 76 |
if gpt_path and sovits_path:
|
| 77 |
-
|
| 78 |
-
if hasattr(core, "
|
| 79 |
-
if hasattr(core, "
|
| 80 |
-
|
| 81 |
-
# 加载模型
|
| 82 |
-
# 由于我们上面已经做了替换,这里调用时会自动使用 CPU 类
|
| 83 |
-
if hasattr(core, "change_gpt_weights"):
|
| 84 |
-
core.change_gpt_weights(gpt_path=gpt_path)
|
| 85 |
-
if hasattr(core, "change_sovits_weights"):
|
| 86 |
-
core.change_sovits_weights(sovits_path=sovits_path)
|
| 87 |
-
print(f"🎉 模型加载成功!")
|
| 88 |
else:
|
| 89 |
print("❌ 未找到模型文件")
|
| 90 |
except Exception as e:
|
|
@@ -99,7 +123,7 @@ import numpy as np
|
|
| 99 |
|
| 100 |
REF_AUDIO = "ref.wav"
|
| 101 |
REF_TEXT = "你好"
|
| 102 |
-
REF_LANG = "中文"
|
| 103 |
|
| 104 |
def run_predict(text):
|
| 105 |
if not os.path.exists(REF_AUDIO):
|
|
@@ -110,7 +134,6 @@ def run_predict(text):
|
|
| 110 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 111 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 112 |
|
| 113 |
-
# 核心调用
|
| 114 |
generator = inference_func(
|
| 115 |
ref_wav_path=REF_AUDIO,
|
| 116 |
prompt_text=REF_TEXT,
|
|
@@ -138,16 +161,13 @@ def run_predict(text):
|
|
| 138 |
# 7. 界面
|
| 139 |
# ==========================================
|
| 140 |
with gr.Blocks() as app:
|
| 141 |
-
gr.Markdown(f"### GPT-SoVITS V2 (
|
| 142 |
-
|
| 143 |
with gr.Row():
|
| 144 |
-
inp = gr.Textbox(label="文本", value="
|
| 145 |
btn = gr.Button("生成")
|
| 146 |
-
|
| 147 |
with gr.Row():
|
| 148 |
out = gr.Audio(label="结果")
|
| 149 |
log = gr.Textbox(label="日志")
|
| 150 |
-
|
| 151 |
btn.click(run_predict, [inp], [out, log], api_name="predict")
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import importlib.util
|
| 4 |
+
import types
|
| 5 |
|
| 6 |
# ==========================================
|
| 7 |
+
# 1. 基础环境净化
|
| 8 |
# ==========================================
|
| 9 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 10 |
import torch
|
|
|
|
| 14 |
torch.Tensor.cuda = no_op
|
| 15 |
torch.nn.Module.cuda = no_op
|
| 16 |
|
| 17 |
+
print("💉 CUDA 已屏蔽。")
|
| 18 |
|
| 19 |
# ==========================================
|
| 20 |
+
# 2. 核心魔法:文件级夺舍 (File-Level Hijack)
|
| 21 |
# ==========================================
|
| 22 |
sys.path.append(os.getcwd())
|
| 23 |
|
| 24 |
+
def load_cpu_model_class():
|
| 25 |
+
"""直接从文件加载 CPU 版模型类,不依赖标准 import"""
|
| 26 |
+
try:
|
| 27 |
+
# 1. 找到 CPU 版代码文件
|
| 28 |
+
cpu_code_path = os.path.join(os.getcwd(), "AR", "models", "t2s_model.py")
|
| 29 |
+
if not os.path.exists(cpu_code_path):
|
| 30 |
+
print(f"⚠️ 找不到 CPU 代码文件: {cpu_code_path}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
# 2. 动态加载这个文件
|
| 34 |
+
spec = importlib.util.spec_from_file_location("AR.models.t2s_model", cpu_code_path)
|
| 35 |
+
cpu_module = importlib.util.module_from_spec(spec)
|
| 36 |
+
sys.modules["AR.models.t2s_model"] = cpu_module # 注册到系统
|
| 37 |
+
spec.loader.exec_module(cpu_module)
|
| 38 |
+
|
| 39 |
+
print("✅ 成功手动加载 CPU 版模型代码")
|
| 40 |
+
return cpu_module.Text2SemanticDecoder
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"❌ 加载 CPU 代码失败: {e}")
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# 1. 获取 CPU 版的类
|
| 47 |
+
CPU_Decoder_Class = load_cpu_model_class()
|
| 48 |
+
|
| 49 |
+
if CPU_Decoder_Class:
|
| 50 |
+
# 2. 导入 GPU 版模块 (让它先加载,然后我们覆盖它)
|
| 51 |
+
# 我们使用类似的技巧,或者直接 import (如果路径允许)
|
| 52 |
+
# 这里为了稳妥,我们直接预占 GPU 模块的名字
|
| 53 |
+
|
| 54 |
+
# 尝试标准导入 GPU 模块,如果失败也没关系
|
| 55 |
+
try:
|
| 56 |
+
import AR.models.t2s_model_flash_attn as gpu_mod
|
| 57 |
+
except ImportError:
|
| 58 |
+
# 如果标准导入失败,我们手动创建一个伪模块
|
| 59 |
+
gpu_mod = types.ModuleType("AR.models.t2s_model_flash_attn")
|
| 60 |
+
sys.modules["AR.models.t2s_model_flash_attn"] = gpu_mod
|
| 61 |
+
|
| 62 |
+
# 3. 【夺舍开始】用 CPU 类覆盖 GPU 类
|
| 63 |
+
gpu_mod.Text2SemanticDecoder = CPU_Decoder_Class
|
| 64 |
+
print("💉 夺舍成功:FlashAttn 模块已被 CPU 内核接管!")
|
| 65 |
+
else:
|
| 66 |
+
print("⚠️ 无法执行夺舍,后续可能会崩...")
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"⚠️ 夺舍过程异常: {e}")
|
| 70 |
+
|
| 71 |
+
# ==========================================
|
| 72 |
+
# 3. 导入业务逻辑
|
| 73 |
+
# ==========================================
|
| 74 |
try:
|
| 75 |
import inference_webui as core
|
| 76 |
print("✅ 成功导入 inference_webui")
|
| 77 |
+
if hasattr(core, "is_half"): core.is_half = False
|
| 78 |
+
if hasattr(core, "device"): core.device = "cpu"
|
| 79 |
except ImportError:
|
| 80 |
print("❌ 找不到 inference_webui.py")
|
| 81 |
sys.exit(1)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# ==========================================
|
| 84 |
# 4. 自动寻找模型
|
| 85 |
# ==========================================
|
|
|
|
| 105 |
# ==========================================
|
| 106 |
try:
|
| 107 |
if gpt_path and sovits_path:
|
| 108 |
+
core.is_half = False
|
| 109 |
+
if hasattr(core, "change_gpt_weights"): core.change_gpt_weights(gpt_path=gpt_path)
|
| 110 |
+
if hasattr(core, "change_sovits_weights"): core.change_sovits_weights(sovits_path=sovits_path)
|
| 111 |
+
print(f"🎉 模型加载成功!(CPU Mode)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
print("❌ 未找到模型文件")
|
| 114 |
except Exception as e:
|
|
|
|
| 123 |
|
| 124 |
REF_AUDIO = "ref.wav"
|
| 125 |
REF_TEXT = "你好"
|
| 126 |
+
REF_LANG = "中文"
|
| 127 |
|
| 128 |
def run_predict(text):
|
| 129 |
if not os.path.exists(REF_AUDIO):
|
|
|
|
| 134 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 135 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 136 |
|
|
|
|
| 137 |
generator = inference_func(
|
| 138 |
ref_wav_path=REF_AUDIO,
|
| 139 |
prompt_text=REF_TEXT,
|
|
|
|
| 161 |
# 7. 界面
|
| 162 |
# ==========================================
|
| 163 |
with gr.Blocks() as app:
|
| 164 |
+
gr.Markdown(f"### GPT-SoVITS V2 (File Hijack)")
|
|
|
|
| 165 |
with gr.Row():
|
| 166 |
+
inp = gr.Textbox(label="文本", value="这下真的真的要成功了。")
|
| 167 |
btn = gr.Button("生成")
|
|
|
|
| 168 |
with gr.Row():
|
| 169 |
out = gr.Audio(label="结果")
|
| 170 |
log = gr.Textbox(label="日志")
|
|
|
|
| 171 |
btn.click(run_predict, [inp], [out, log], api_name="predict")
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|