Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,9 @@ import sys
|
|
| 3 |
import logging
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
-
# 1.
|
| 7 |
# ==========================================
|
| 8 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 9 |
import torch
|
| 10 |
torch.cuda.is_available = lambda: False
|
| 11 |
torch.cuda.device_count = lambda: 0
|
|
@@ -13,42 +13,42 @@ 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 |
-
# 1. 先手动导入普通的 CPU 版模型
|
| 25 |
-
import AR.models.t2s_model as cpu_model
|
| 26 |
-
print("✅ 成功加载 CPU 版模型架构")
|
| 27 |
-
|
| 28 |
-
# 2. 关键一步:把 GPU 版模型的“坑位”占了!
|
| 29 |
-
# 当后续代码想导入 t2s_model_flash_attn 时,系统会直接给它这个 CPU 版
|
| 30 |
-
sys.modules["AR.models.t2s_model_flash_attn"] = cpu_model
|
| 31 |
-
print("💉 已将 FlashAttn 模型偷换为 CPU 模型")
|
| 32 |
-
|
| 33 |
-
except ImportError as e:
|
| 34 |
-
print(f"⚠️ 预加载模型失败 (可能是路径不对): {e}")
|
| 35 |
-
# 不退出,赌一把后面的逻辑
|
| 36 |
-
|
| 37 |
-
# ==========================================
|
| 38 |
-
# 3. 导入业务逻辑
|
| 39 |
-
# ==========================================
|
| 40 |
try:
|
| 41 |
import inference_webui as core
|
| 42 |
print("✅ 成功导入 inference_webui")
|
| 43 |
-
|
| 44 |
-
# 强制修改全局配置 (如果存在)
|
| 45 |
-
if hasattr(core, "is_half"): core.is_half = False
|
| 46 |
-
if hasattr(core, "device"): core.device = "cpu"
|
| 47 |
-
|
| 48 |
except ImportError:
|
| 49 |
print("❌ 找不到 inference_webui.py")
|
| 50 |
sys.exit(1)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# ==========================================
|
| 53 |
# 4. 自动寻找模型
|
| 54 |
# ==========================================
|
|
@@ -74,14 +74,17 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
|
|
| 74 |
# ==========================================
|
| 75 |
try:
|
| 76 |
if gpt_path and sovits_path:
|
| 77 |
-
#
|
| 78 |
-
core.is_half = False
|
|
|
|
| 79 |
|
|
|
|
|
|
|
| 80 |
if hasattr(core, "change_gpt_weights"):
|
| 81 |
core.change_gpt_weights(gpt_path=gpt_path)
|
| 82 |
if hasattr(core, "change_sovits_weights"):
|
| 83 |
core.change_sovits_weights(sovits_path=sovits_path)
|
| 84 |
-
print(f"🎉 模型加载成功!
|
| 85 |
else:
|
| 86 |
print("❌ 未找到模型文件")
|
| 87 |
except Exception as e:
|
|
@@ -104,7 +107,6 @@ def run_predict(text):
|
|
| 104 |
|
| 105 |
print(f"📥 任务: {text}")
|
| 106 |
try:
|
| 107 |
-
# 自动识别函数
|
| 108 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 109 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 110 |
|
|
@@ -136,7 +138,7 @@ def run_predict(text):
|
|
| 136 |
# 7. 界面
|
| 137 |
# ==========================================
|
| 138 |
with gr.Blocks() as app:
|
| 139 |
-
gr.Markdown(f"### GPT-SoVITS V2 (
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
inp = gr.Textbox(label="文本", value="这下舒服了,终于跑通了。")
|
|
|
|
| 3 |
import logging
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
+
# 1. 基础环境设置
|
| 7 |
# ==========================================
|
| 8 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 9 |
import torch
|
| 10 |
torch.cuda.is_available = lambda: False
|
| 11 |
torch.cuda.device_count = lambda: 0
|
|
|
|
| 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 |
# ==========================================
|
| 75 |
try:
|
| 76 |
if gpt_path and sovits_path:
|
| 77 |
+
# 强制全局配置为 CPU 模式
|
| 78 |
+
if hasattr(core, "is_half"): core.is_half = False
|
| 79 |
+
if hasattr(core, "device"): core.device = "cpu"
|
| 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:
|
|
|
|
| 107 |
|
| 108 |
print(f"📥 任务: {text}")
|
| 109 |
try:
|
|
|
|
| 110 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 111 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 112 |
|
|
|
|
| 138 |
# 7. 界面
|
| 139 |
# ==========================================
|
| 140 |
with gr.Blocks() as app:
|
| 141 |
+
gr.Markdown(f"### GPT-SoVITS V2 (CPU Hot-Swap)")
|
| 142 |
|
| 143 |
with gr.Row():
|
| 144 |
inp = gr.Textbox(label="文本", value="这下舒服了,终于跑通了。")
|