Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
-
# 1.
|
| 7 |
-
# ==========================================
|
| 8 |
-
# 这一步必须在所有 import 之前!
|
| 9 |
-
|
| 10 |
-
# 方法 A: 告诉 Python 这个库已经“加载失败”了
|
| 11 |
-
sys.modules["flash_attn"] = None
|
| 12 |
-
|
| 13 |
-
# 方法 B: 劫持 find_spec,让 transformers 以为这个包根本没安装
|
| 14 |
-
# 这是修复 V12 报错的关键!
|
| 15 |
-
original_find_spec = importlib.util.find_spec
|
| 16 |
-
def patched_find_spec(name, package=None):
|
| 17 |
-
if name == "flash_attn":
|
| 18 |
-
return None # 告诉调用者:查无此人
|
| 19 |
-
return original_find_spec(name, package)
|
| 20 |
-
importlib.util.find_spec = patched_find_spec
|
| 21 |
-
|
| 22 |
-
print("💉 已启动隐身模式:Flash Attention 已从系统中隐藏。")
|
| 23 |
-
|
| 24 |
-
# ==========================================
|
| 25 |
-
# 2. 屏蔽 CUDA
|
| 26 |
# ==========================================
|
| 27 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 28 |
import torch
|
|
@@ -31,18 +12,36 @@ torch.cuda.device_count = lambda: 0
|
|
| 31 |
def no_op(self, *args, **kwargs): return self
|
| 32 |
torch.Tensor.cuda = no_op
|
| 33 |
torch.nn.Module.cuda = no_op
|
|
|
|
| 34 |
print("💉 CUDA 已屏蔽。")
|
| 35 |
|
| 36 |
# ==========================================
|
| 37 |
-
#
|
| 38 |
# ==========================================
|
| 39 |
sys.path.append(os.getcwd())
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
import inference_webui as core
|
| 43 |
print("✅ 成功导入 inference_webui")
|
| 44 |
|
| 45 |
-
#
|
| 46 |
if hasattr(core, "is_half"): core.is_half = False
|
| 47 |
if hasattr(core, "device"): core.device = "cpu"
|
| 48 |
|
|
@@ -75,14 +74,14 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
|
|
| 75 |
# ==========================================
|
| 76 |
try:
|
| 77 |
if gpt_path and sovits_path:
|
| 78 |
-
#
|
| 79 |
core.is_half = False
|
| 80 |
|
| 81 |
if hasattr(core, "change_gpt_weights"):
|
| 82 |
core.change_gpt_weights(gpt_path=gpt_path)
|
| 83 |
if hasattr(core, "change_sovits_weights"):
|
| 84 |
core.change_sovits_weights(sovits_path=sovits_path)
|
| 85 |
-
print(f"🎉 模型加载成功!")
|
| 86 |
else:
|
| 87 |
print("❌ 未找到模型文件")
|
| 88 |
except Exception as e:
|
|
@@ -109,6 +108,7 @@ def run_predict(text):
|
|
| 109 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 110 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 111 |
|
|
|
|
| 112 |
generator = inference_func(
|
| 113 |
ref_wav_path=REF_AUDIO,
|
| 114 |
prompt_text=REF_TEXT,
|
|
@@ -136,10 +136,10 @@ 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="
|
| 143 |
btn = gr.Button("生成")
|
| 144 |
|
| 145 |
with gr.Row():
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import logging
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
+
# 1. 基础环境净化 (屏蔽显卡)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ==========================================
|
| 8 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
| 9 |
import torch
|
|
|
|
| 12 |
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. 核心魔法:狸猫换太子 (Module Swapping)
|
| 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 |
|
|
|
|
| 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"🎉 模型加载成功!(Using CPU Path)")
|
| 85 |
else:
|
| 86 |
print("❌ 未找到模型文件")
|
| 87 |
except Exception as e:
|
|
|
|
| 108 |
inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
|
| 109 |
if not inference_func: return None, "❌ 找不到推理函数"
|
| 110 |
|
| 111 |
+
# 核心调用
|
| 112 |
generator = inference_func(
|
| 113 |
ref_wav_path=REF_AUDIO,
|
| 114 |
prompt_text=REF_TEXT,
|
|
|
|
| 136 |
# 7. 界面
|
| 137 |
# ==========================================
|
| 138 |
with gr.Blocks() as app:
|
| 139 |
+
gr.Markdown(f"### GPT-SoVITS V2 (Module Swapped)")
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
+
inp = gr.Textbox(label="文本", value="这下舒服了,终于跑通了。")
|
| 143 |
btn = gr.Button("生成")
|
| 144 |
|
| 145 |
with gr.Row():
|