Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
import
|
| 4 |
-
import types
|
| 5 |
|
| 6 |
# ==========================================
|
| 7 |
-
# 1. 基础环境净化
|
| 8 |
# ==========================================
|
| 9 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 10 |
import torch
|
|
@@ -17,65 +16,44 @@ torch.nn.Module.cuda = no_op
|
|
| 17 |
print("💉 CUDA 已屏蔽。")
|
| 18 |
|
| 19 |
# ==========================================
|
| 20 |
-
# 2. 核心魔法:
|
| 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 |
-
|
| 47 |
-
CPU_Decoder_Class = load_cpu_model_class()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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)
|
|
@@ -105,9 +83,11 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
|
|
| 105 |
# ==========================================
|
| 106 |
try:
|
| 107 |
if gpt_path and sovits_path:
|
| 108 |
-
core.is_half = False
|
| 109 |
-
if hasattr(core, "change_gpt_weights"):
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
print(f"🎉 模型加载成功!(CPU Mode)")
|
| 112 |
else:
|
| 113 |
print("❌ 未找到模型文件")
|
|
@@ -161,13 +141,16 @@ def run_predict(text):
|
|
| 161 |
# 7. 界面
|
| 162 |
# ==========================================
|
| 163 |
with gr.Blocks() as app:
|
| 164 |
-
gr.Markdown(f"### GPT-SoVITS V2 (
|
|
|
|
| 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__":
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import logging
|
|
|
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
+
# 1. 基础环境净化 (屏蔽显卡)
|
| 7 |
# ==========================================
|
| 8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 9 |
import torch
|
|
|
|
| 16 |
print("💉 CUDA 已屏蔽。")
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
+
# 2. 核心魔法:内存类替换 (Class Swapping)
|
| 20 |
# ==========================================
|
| 21 |
sys.path.append(os.getcwd())
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
print("🧠 开始执行内存调包手术...")
|
|
|
|
| 25 |
|
| 26 |
+
# 1. 先导入 CPU 版的模型类 (这是我们想要的)
|
| 27 |
+
from AR.models.t2s_model import Text2SemanticDecoder as CPU_Decoder
|
| 28 |
+
print("✅ 成功提取 CPU 版模型类")
|
| 29 |
+
|
| 30 |
+
# 2. 再导入 GPU 版的模块 (这是我们要覆盖的)
|
| 31 |
+
import AR.models.t2s_model_flash_attn as GPU_Module
|
| 32 |
+
|
| 33 |
+
# 3. 【关键】直接把 GPU 模块里的类,换成 CPU 版的类
|
| 34 |
+
# 这样后续任何代码(包括 inference_webui)去引用 GPU_Module.Text2SemanticDecoder
|
| 35 |
+
# 实际上都会拿到 CPU_Decoder
|
| 36 |
+
GPU_Module.Text2SemanticDecoder = CPU_Decoder
|
| 37 |
+
|
| 38 |
+
print("💉 手术成功:FlashAttn 模块已被 CPU 内核接管!")
|
| 39 |
+
|
| 40 |
+
except ImportError as e:
|
| 41 |
+
print(f"⚠️ 手术失败 (可能路径不对): {e}")
|
| 42 |
+
print(f"当前目录文件: {os.listdir('.')}")
|
| 43 |
+
if os.path.exists("AR"):
|
| 44 |
+
print(f"AR目录文件: {os.listdir('AR')}")
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# ==========================================
|
| 47 |
+
# 3. 导入业务逻辑 (必须在手术后进行)
|
| 48 |
# ==========================================
|
| 49 |
try:
|
| 50 |
import inference_webui as core
|
| 51 |
print("✅ 成功导入 inference_webui")
|
| 52 |
+
|
| 53 |
+
# 强制修改全局配置
|
| 54 |
if hasattr(core, "is_half"): core.is_half = False
|
| 55 |
if hasattr(core, "device"): core.device = "cpu"
|
| 56 |
+
|
| 57 |
except ImportError:
|
| 58 |
print("❌ 找不到 inference_webui.py")
|
| 59 |
sys.exit(1)
|
|
|
|
| 83 |
# ==========================================
|
| 84 |
try:
|
| 85 |
if gpt_path and sovits_path:
|
| 86 |
+
core.is_half = False # 再次确保
|
| 87 |
+
if hasattr(core, "change_gpt_weights"):
|
| 88 |
+
core.change_gpt_weights(gpt_path=gpt_path)
|
| 89 |
+
if hasattr(core, "change_sovits_weights"):
|
| 90 |
+
core.change_sovits_weights(sovits_path=sovits_path)
|
| 91 |
print(f"🎉 模型加载成功!(CPU Mode)")
|
| 92 |
else:
|
| 93 |
print("❌ 未找到模型文件")
|
|
|
|
| 141 |
# 7. 界面
|
| 142 |
# ==========================================
|
| 143 |
with gr.Blocks() as app:
|
| 144 |
+
gr.Markdown(f"### GPT-SoVITS V2 (CPU Class Swapped)")
|
| 145 |
+
|
| 146 |
with gr.Row():
|
| 147 |
+
inp = gr.Textbox(label="文本", value="这下真的真的真的要成功了。")
|
| 148 |
btn = gr.Button("生成")
|
| 149 |
+
|
| 150 |
with gr.Row():
|
| 151 |
out = gr.Audio(label="结果")
|
| 152 |
log = gr.Textbox(label="日志")
|
| 153 |
+
|
| 154 |
btn.click(run_predict, [inp], [out, log], api_name="predict")
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|