simler commited on
Commit
d6136a8
·
verified ·
1 Parent(s): 45720c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -1,32 +1,61 @@
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. 基础环境设置
@@ -39,18 +68,15 @@ def no_op(self, *args, **kwargs): return self
39
  torch.Tensor.cuda = no_op
40
  torch.nn.Module.cuda = no_op
41
 
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
 
@@ -59,7 +85,7 @@ except ImportError:
59
  sys.exit(1)
60
 
61
  # ==========================================
62
- # 4. 自动寻找模型
63
  # ==========================================
64
  def find_real_model(pattern, search_path="."):
65
  candidates = []
@@ -83,21 +109,17 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
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:
97
  print(f"⚠️ 模型加载报错: {e}")
98
 
99
  # ==========================================
100
- # 6. 推理逻辑
101
  # ==========================================
102
  import soundfile as sf
103
  import gradio as gr
@@ -108,8 +130,7 @@ REF_TEXT = "你好"
108
  REF_LANG = "中文"
109
 
110
  def run_predict(text):
111
- if not os.path.exists(REF_AUDIO):
112
- return None, "❌ 错误:请上传 ref.wav"
113
 
114
  print(f"📥 任务: {text}")
115
  try:
@@ -131,7 +152,6 @@ def run_predict(text):
131
  sr, data = result_list[0]
132
  out_path = f"out_{os.urandom(4).hex()}.wav"
133
  sf.write(out_path, data, sr)
134
- print(f"✅ 生成完毕: {out_path}")
135
  return out_path, "✅ 成功"
136
 
137
  except Exception as e:
@@ -140,19 +160,16 @@ def run_predict(text):
140
  return None, f"💥 报错: {e}"
141
 
142
  # ==========================================
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():
153
  out = gr.Audio(label="结果")
154
  log = gr.Textbox(label="日志")
155
-
156
  btn.click(run_predict, [inp], [out, log], api_name="predict")
157
 
158
  if __name__ == "__main__":
 
1
  import os
2
  import sys
 
3
 
4
  # ==========================================
5
+ # 1. 核心补丁:全局搜索并摧毁 Flash Attention
6
  # ==========================================
7
+ print("🛰️ 启动全盘扫描,寻找 GPU 毒瘤文件...")
8
 
9
+ # 定义我们要写入的“CPU 伪装代码”
10
+ # 这段代码会把所有对 GPU 模型的调用,转发给 CPU 模型
11
+ CPU_HACK_CODE = """
12
+ import sys
13
+ import logging
14
+ print("🛡️ [HACK] 成功拦截 FlashAttn 调用,强制重定向至 CPU 模型!")
15
 
16
+ # 试图导入普通的 CPU 模型
17
+ try:
18
+ # 尝试多种可能的路径
19
  try:
20
+ from AR.models.t2s_model import Text2SemanticDecoder
21
+ except ImportError:
22
+ from GPT_SoVITS.AR.models.t2s_model import Text2SemanticDecoder
23
+ except ImportError:
24
+ # 如果都找不到,手动定义一个空的 CPU 兼容类(防止报错)
25
+ import torch.nn as nn
26
+ class Text2SemanticDecoder(nn.Module):
27
+ def __init__(self, *args, **kwargs):
28
+ super().__init__()
29
+ print("⚠️ 使用了紧急备用 CPU Decoder 类")
30
+ def forward(self, *args, **kwargs):
31
+ print("⚠️ 紧急备用 Forward 被调用")
32
+ return None
33
+
34
+ # 导出这个类,骗过调用者
35
+ Text2SemanticDecoderFlashAttn = Text2SemanticDecoder
36
+ """
37
+
38
+ # 递归搜索并覆盖
39
+ target_filename = "t2s_model_flash_attn.py"
40
+ hacked_count = 0
41
+
42
+ for root, dirs, files in os.walk("."):
43
+ if target_filename in files:
44
+ full_path = os.path.join(root, target_filename)
45
+ print(f"🎯 锁定目标: {full_path}")
46
 
47
+ try:
48
+ with open(full_path, "w", encoding="utf-8") as f:
49
+ f.write(CPU_HACK_CODE)
50
+ print("✅ 已执行物理覆盖 (写入 CPU 伪装代码)")
51
+ hacked_count += 1
52
+ except Exception as e:
53
+ print(f"❌ 覆盖失败: {e}")
54
+
55
+ if hacked_count == 0:
56
+ print("⚠️ 警告:未找到任何 FlashAttn 文件!可能目录结构极其特殊。")
57
  else:
58
+ print(f"🎉 成功处理了 {hacked_count} 个 GPU 文件。")
59
 
60
  # ==========================================
61
  # 2. 基础环境设置
 
68
  torch.Tensor.cuda = no_op
69
  torch.nn.Module.cuda = no_op
70
 
 
 
71
  # ==========================================
72
+ # 3. 导入推理核心
73
  # ==========================================
74
+ sys.path.append(os.getcwd())
75
 
76
  try:
77
  import inference_webui as core
78
  print("✅ 成功导入 inference_webui")
79
 
 
80
  if hasattr(core, "is_half"): core.is_half = False
81
  if hasattr(core, "device"): core.device = "cpu"
82
 
 
85
  sys.exit(1)
86
 
87
  # ==========================================
88
+ # 4. 自动寻找模型文件
89
  # ==========================================
90
  def find_real_model(pattern, search_path="."):
91
  candidates = []
 
109
  # ==========================================
110
  try:
111
  if gpt_path and sovits_path:
 
112
  core.is_half = False
113
+ if hasattr(core, "change_gpt_weights"): core.change_gpt_weights(gpt_path=gpt_path)
114
+ if hasattr(core, "change_sovits_weights"): core.change_sovits_weights(sovits_path=sovits_path)
115
+ print(f"🎉 模型加载成功!")
 
 
 
116
  else:
117
  print("❌ 未找到模型文件")
118
  except Exception as e:
119
  print(f"⚠️ 模型加载报错: {e}")
120
 
121
  # ==========================================
122
+ # 6. 推理接口
123
  # ==========================================
124
  import soundfile as sf
125
  import gradio as gr
 
130
  REF_LANG = "中文"
131
 
132
  def run_predict(text):
133
+ if not os.path.exists(REF_AUDIO): return None, "❌ 请上传 ref.wav"
 
134
 
135
  print(f"📥 任务: {text}")
136
  try:
 
152
  sr, data = result_list[0]
153
  out_path = f"out_{os.urandom(4).hex()}.wav"
154
  sf.write(out_path, data, sr)
 
155
  return out_path, "✅ 成功"
156
 
157
  except Exception as e:
 
160
  return None, f"💥 报错: {e}"
161
 
162
  # ==========================================
163
+ # 7. 启动界面
164
  # ==========================================
165
  with gr.Blocks() as app:
166
+ gr.Markdown(f"### GPT-SoVITS V2 (CPU HACKED)")
 
167
  with gr.Row():
168
+ inp = gr.Textbox(label="文本", value="这下舒服了,终于跑通了。")
169
  btn = gr.Button("生成")
 
170
  with gr.Row():
171
  out = gr.Audio(label="结果")
172
  log = gr.Textbox(label="日志")
 
173
  btn.click(run_predict, [inp], [out, log], api_name="predict")
174
 
175
  if __name__ == "__main__":