simler commited on
Commit
5ceccd0
·
verified ·
1 Parent(s): 46a4752

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -1,18 +1,25 @@
1
  import os
2
  import sys
3
- import types
4
 
5
  # ==========================================
6
- # 1. 核心欺骗:制造“假”的 Flash Attention
7
  # ==========================================
8
- # 这一步必须在所有 imports 之前!
9
- # 我们创建一个空的模块,骗过系统的 import 检查
10
- # 但因为里面没有 functional 接口,模型会报错并回退到普通模式
11
- dummy_module = types.ModuleType("flash_attn")
12
- sys.modules["flash_attn"] = dummy_module
13
- sys.modules["flash_attn.flash_attn_interface"] = dummy_module
14
 
15
- print("💉 已注入 Flash Attention 假模块,强制开启 CPU 兼容模式。")
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # ==========================================
18
  # 2. 屏蔽 CUDA
@@ -24,7 +31,6 @@ torch.cuda.device_count = lambda: 0
24
  def no_op(self, *args, **kwargs): return self
25
  torch.Tensor.cuda = no_op
26
  torch.nn.Module.cuda = no_op
27
-
28
  print("💉 CUDA 已屏蔽。")
29
 
30
  # ==========================================
@@ -36,10 +42,9 @@ try:
36
  import inference_webui as core
37
  print("✅ 成功导入 inference_webui")
38
 
39
- # 强制关闭半精度
40
- if hasattr(core, "is_half"):
41
- core.is_half = False
42
- print("✅ 强制禁用半精度")
43
 
44
  except ImportError:
45
  print("❌ 找不到 inference_webui.py")
@@ -70,14 +75,14 @@ sovits_path = find_real_model("s2Gv2ProPlus.pth") or find_real_model("s2G")
70
  # ==========================================
71
  try:
72
  if gpt_path and sovits_path:
73
- # 再次确保
74
- core.is_half = False
75
 
76
  if hasattr(core, "change_gpt_weights"):
77
  core.change_gpt_weights(gpt_path=gpt_path)
78
  if hasattr(core, "change_sovits_weights"):
79
  core.change_sovits_weights(sovits_path=sovits_path)
80
- print(f"🎉 模型加载成功!(CPU模式)")
81
  else:
82
  print("❌ 未找到模型文件")
83
  except Exception as e:
@@ -100,10 +105,10 @@ def run_predict(text):
100
 
101
  print(f"📥 任务: {text}")
102
  try:
 
103
  inference_func = getattr(core, "get_tts_model", getattr(core, "get_tts_wav", None))
104
  if not inference_func: return None, "❌ 找不到推理函数"
105
 
106
- # 核心调用
107
  generator = inference_func(
108
  ref_wav_path=REF_AUDIO,
109
  prompt_text=REF_TEXT,
@@ -131,11 +136,10 @@ def run_predict(text):
131
  # 7. 界面
132
  # ==========================================
133
  with gr.Blocks() as app:
134
- gr.Markdown(f"### GPT-SoVITS CPU 终极版")
135
- gr.Markdown(f"Status: FlashAttn Disabled, CUDA Disabled, FP32 Mode")
136
 
137
  with gr.Row():
138
- inp = gr.Textbox(label="文本", value="如果听到这句话,说明你成功了!")
139
  btn = gr.Button("生成")
140
 
141
  with gr.Row():
 
1
  import os
2
  import sys
3
+ import importlib.util
4
 
5
  # ==========================================
6
+ # 1. 核弹级隐身术:彻底隐藏 Flash Attention
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
 
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
  # ==========================================
 
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
 
49
  except ImportError:
50
  print("❌ 找不到 inference_webui.py")
 
75
  # ==========================================
76
  try:
77
  if gpt_path and sovits_path:
78
+ # 三重保险:修改 core 里的全局变量
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:
 
105
 
106
  print(f"📥 任务: {text}")
107
  try:
108
+ # 自动识别函数
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
  # 7. 界面
137
  # ==========================================
138
  with gr.Blocks() as app:
139
+ gr.Markdown(f"### GPT-SoVITS V2 (CPU Stealth Mode)")
 
140
 
141
  with gr.Row():
142
+ inp = gr.Textbox(label="文本", value="这次一定行!")
143
  btn = gr.Button("生成")
144
 
145
  with gr.Row():