Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,35 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
-
# 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# ==========================================
|
| 8 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 9 |
import torch
|
|
@@ -16,41 +42,15 @@ torch.nn.Module.cuda = no_op
|
|
| 16 |
print("💉 CUDA 已屏蔽。")
|
| 17 |
|
| 18 |
# ==========================================
|
| 19 |
-
#
|
| 20 |
# ==========================================
|
| 21 |
-
sys.path.append(
|
| 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 |
|
|
@@ -83,12 +83,14 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
|
|
| 83 |
# ==========================================
|
| 84 |
try:
|
| 85 |
if gpt_path and sovits_path:
|
| 86 |
-
|
|
|
|
|
|
|
| 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"🎉 模型加载成功!(
|
| 92 |
else:
|
| 93 |
print("❌ 未找到模型文件")
|
| 94 |
except Exception as e:
|
|
@@ -141,10 +143,10 @@ def run_predict(text):
|
|
| 141 |
# 7. 界面
|
| 142 |
# ==========================================
|
| 143 |
with gr.Blocks() as app:
|
| 144 |
-
gr.Markdown(f"### GPT-SoVITS V2 (
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
-
inp = gr.Textbox(label="文本", value="
|
| 148 |
btn = gr.Button("生成")
|
| 149 |
|
| 150 |
with gr.Row():
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import shutil
|
| 4 |
|
| 5 |
# ==========================================
|
| 6 |
+
# 1. 物理层攻击:篡改模型文件
|
| 7 |
+
# ==========================================
|
| 8 |
+
print("🔨 正在执行物理覆盖手术...")
|
| 9 |
+
|
| 10 |
+
cwd = os.getcwd()
|
| 11 |
+
cpu_model_path = os.path.join(cwd, "AR", "models", "t2s_model.py")
|
| 12 |
+
gpu_model_path = os.path.join(cwd, "AR", "models", "t2s_model_flash_attn.py")
|
| 13 |
+
|
| 14 |
+
if os.path.exists(cpu_model_path) and os.path.exists(gpu_model_path):
|
| 15 |
+
try:
|
| 16 |
+
# 读取 CPU 代码
|
| 17 |
+
with open(cpu_model_path, "r", encoding="utf-8") as f:
|
| 18 |
+
cpu_code = f.read()
|
| 19 |
+
|
| 20 |
+
# 写入 GPU 文件 (鸠占鹊巢)
|
| 21 |
+
with open(gpu_model_path, "w", encoding="utf-8") as f:
|
| 22 |
+
f.write(cpu_code)
|
| 23 |
+
|
| 24 |
+
print(f"✅ 覆盖成功!\n源: {cpu_model_path}\n目标: {gpu_model_path}")
|
| 25 |
+
print("现在 't2s_model_flash_attn.py' 里的代码实际上是 CPU 版代码。")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"❌ 覆盖失败 (权限不足?): {e}")
|
| 28 |
+
else:
|
| 29 |
+
print("⚠️ 找不到模型文件路径,跳过覆盖步骤 (可能目录结构变了)")
|
| 30 |
+
|
| 31 |
+
# ==========================================
|
| 32 |
+
# 2. 基础环境设置
|
| 33 |
# ==========================================
|
| 34 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 35 |
import torch
|
|
|
|
| 42 |
print("💉 CUDA 已屏蔽。")
|
| 43 |
|
| 44 |
# ==========================================
|
| 45 |
+
# 3. 导入业务逻辑
|
| 46 |
# ==========================================
|
| 47 |
+
sys.path.append(cwd)
|
| 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 |
|
|
|
|
| 83 |
# ==========================================
|
| 84 |
try:
|
| 85 |
if gpt_path and sovits_path:
|
| 86 |
+
# 三重保险
|
| 87 |
+
core.is_half = False
|
| 88 |
+
|
| 89 |
if hasattr(core, "change_gpt_weights"):
|
| 90 |
core.change_gpt_weights(gpt_path=gpt_path)
|
| 91 |
if hasattr(core, "change_sovits_weights"):
|
| 92 |
core.change_sovits_weights(sovits_path=sovits_path)
|
| 93 |
+
print(f"🎉 模型加载成功!(HARDWARE HACKED)")
|
| 94 |
else:
|
| 95 |
print("❌ 未找到模型文件")
|
| 96 |
except Exception as e:
|
|
|
|
| 143 |
# 7. 界面
|
| 144 |
# ==========================================
|
| 145 |
with gr.Blocks() as app:
|
| 146 |
+
gr.Markdown(f"### GPT-SoVITS V2 (File System Override)")
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
+
inp = gr.Textbox(label="文本", value="别报错了,求求了。")
|
| 150 |
btn = gr.Button("生成")
|
| 151 |
|
| 152 |
with gr.Row():
|