simler commited on
Commit
5731406
·
verified ·
1 Parent(s): 48d628b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -57
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import sys
3
- import shutil
4
  import requests
 
5
 
6
  # ==========================================
7
  # 1. 基础环境净化
@@ -16,61 +16,56 @@ torch.nn.Module.cuda = no_op
16
  print("💉 CUDA 已屏蔽")
17
 
18
  # ==========================================
19
- # 2. 焦土战术:全盘覆盖 + 清理缓存
20
  # ==========================================
21
- print("🔥 启动焦土战术...")
22
-
23
- # 1. 下载官方 CPU 代码
24
- CPU_CODE_URL = "https://raw.githubusercontent.com/RVC-Boss/GPT-SoVITS/main/GPT_SoVITS/AR/models/t2s_model.py"
25
- try:
26
- print("⬇️ 下载 CPU 源码...")
27
- code_resp = requests.get(CPU_CODE_URL, timeout=10)
28
- if code_resp.status_code != 200:
29
- raise Exception("下载失败")
30
- cpu_code = code_resp.text
31
- print(" CPU 源码下载完成")
32
- except Exception as e:
33
- print(f"❌ 源码下载失败: {e}")
34
- # 备用方案:写入一段最小化的 Mock 代码,防止崩溃
35
- cpu_code = "class Text2SemanticDecoder: pass\nprint('⚠️ 使用了 Mock Decoder')"
36
-
37
- # 2. 地毯式搜索并覆盖
38
- target_name = "t2s_model_flash_attn.py"
39
- hit_count = 0
40
-
41
- for root, dirs, files in os.walk("."):
42
- # A. 清理缓存 (防止尸变)
43
- if "__pycache__" in dirs:
44
- cache_path = os.path.join(root, "__pycache__")
45
- try:
46
- shutil.rmtree(cache_path)
47
- print(f"🧹 已清理缓存: {cache_path}")
48
- except: pass
49
-
50
- # B. 覆盖文件 (狡兔三窟,全部堵死)
51
- if target_name in files:
52
- full_path = os.path.join(root, target_name)
53
- try:
54
- with open(full_path, "w", encoding="utf-8") as f:
55
- f.write(cpu_code)
56
- hit_count += 1
57
- print(f"🎯 [HIT] 已覆盖毒瘤文件: {full_path}")
 
 
 
 
58
 
59
- # C. 立即验尸 (确认文件内容已变)
60
- with open(full_path, "r") as f:
61
- head = f.read(100)
62
- if "flash_attn" in head:
63
- print(" 警告:文件似乎未被成功修改!")
64
- else:
65
- print("✅ 验证通过:文件头已变为 CPU 代码。")
66
-
67
- except Exception as e:
68
- print(f"❌ 覆盖失败 {full_path}: {e}")
69
-
70
- if hit_count == 0:
71
- print("⚠️ 警告:全盘扫描未发现目标文件!")
72
- else:
73
- print(f"🎉 共处理了 {hit_count} 个文件,清理了所有缓存。")
74
 
75
  # ==========================================
76
  # 3. 导入核心逻辑
@@ -80,8 +75,10 @@ sys.path.append(os.getcwd())
80
  try:
81
  import inference_webui as core
82
  print("✅ 成功导入 inference_webui")
 
83
  if hasattr(core, "is_half"): core.is_half = False
84
  if hasattr(core, "device"): core.device = "cpu"
 
85
  except Exception as e:
86
  print(f"❌ 导入失败: {e}")
87
  sys.exit(1)
@@ -111,7 +108,7 @@ try:
111
  core.change_gpt_weights(gpt_path=gpt_path)
112
  if hasattr(core, "change_sovits_weights"):
113
  core.change_sovits_weights(sovits_path=sovits_path)
114
- print(f"🎉 模型加载成功!(CPU Mode)")
115
  else:
116
  print("❌ 未找到模型文件")
117
  except Exception as e:
@@ -163,13 +160,16 @@ def run_predict(text):
163
  # 7. 界面
164
  # ==========================================
165
  with gr.Blocks() as app:
166
- gr.Markdown(f"### GPT-SoVITS V2 (Scorched Earth Policy)")
 
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__":
 
1
  import os
2
  import sys
 
3
  import requests
4
+ import shutil
5
 
6
  # ==========================================
7
  # 1. 基础环境净化
 
16
  print("💉 CUDA 已屏蔽")
17
 
18
  # ==========================================
19
+ # 2. 核心补丁:补全 CPU 缺失文件
20
  # ==========================================
21
+ print("🏗️ 启动依赖补全计划...")
22
+
23
+ FILES_TO_PATCH = [
24
+ {
25
+ "url": "https://raw.githubusercontent.com/RVC-Boss/GPT-SoVITS/main/GPT_SoVITS/AR/models/t2s_model.py",
26
+ "path": "AR/models/t2s_model_flash_attn.py", # 覆盖目标
27
+ "desc": "模型主文件 (CPU替换GPU)"
28
+ },
29
+ {
30
+ "url": "https://raw.githubusercontent.com/RVC-Boss/GPT-SoVITS/main/GPT_SoVITS/AR/models/utils.py",
31
+ "path": "AR/models/utils.py",
32
+ "desc": "工具库 (Utils)"
33
+ },
34
+ {
35
+ "url": "https://raw.githubusercontent.com/RVC-Boss/GPT-SoVITS/main/GPT_SoVITS/AR/modules/embedding.py",
36
+ "path": "AR/modules/embedding.py",
37
+ "desc": "嵌入层 (Embedding)"
38
+ }
39
+ ]
40
+
41
+ for item in FILES_TO_PATCH:
42
+ try:
43
+ # 1. 确保目录存在
44
+ dir_name = os.path.dirname(item["path"])
45
+ if not os.path.exists(dir_name):
46
+ os.makedirs(dir_name, exist_ok=True)
47
+ print(f"📁 创建目录: {dir_name}")
48
+
49
+ # 补一个 __init__.py 确保可导入
50
+ with open(os.path.join(dir_name, "__init__.py"), "w") as f: f.write("")
51
+
52
+ # 2. 下载文件
53
+ print(f"⬇️ 下载 {item['desc']} ...")
54
+ resp = requests.get(item["url"], timeout=10)
55
+ if resp.status_code == 200:
56
+ # 3. 写入文件
57
+ with open(item["path"], "w", encoding="utf-8") as f:
58
+ f.write(resp.text)
59
+ print(f"✅ 写入成功: {item['path']}")
60
+ else:
61
+ print(f"❌ 下载失败 {item['url']}: {resp.status_code}")
62
 
63
+ except Exception as e:
64
+ print(f"⚠️ 处理 {item['path']} 时出错: {e}")
65
+
66
+ # 补充根目录 AR 的 __init__.py
67
+ if os.path.exists("AR") and not os.path.exists("AR/__init__.py"):
68
+ with open("AR/__init__.py", "w") as f: f.write("")
 
 
 
 
 
 
 
 
 
69
 
70
  # ==========================================
71
  # 3. 导入核心逻辑
 
75
  try:
76
  import inference_webui as core
77
  print("✅ 成功导入 inference_webui")
78
+
79
  if hasattr(core, "is_half"): core.is_half = False
80
  if hasattr(core, "device"): core.device = "cpu"
81
+
82
  except Exception as e:
83
  print(f"❌ 导入失败: {e}")
84
  sys.exit(1)
 
108
  core.change_gpt_weights(gpt_path=gpt_path)
109
  if hasattr(core, "change_sovits_weights"):
110
  core.change_sovits_weights(sovits_path=sovits_path)
111
+ print(f"🎉 模型加载成功!(CPU Full Patch)")
112
  else:
113
  print("❌ 未找到模型文件")
114
  except Exception as e:
 
160
  # 7. 界面
161
  # ==========================================
162
  with gr.Blocks() as app:
163
+ gr.Markdown(f"### GPT-SoVITS V2 (CPU Full Repair)")
164
+
165
  with gr.Row():
166
+ inp = gr.Textbox(label="文本", value="这下真的真的真的要成功了。")
167
  btn = gr.Button("生成")
168
+
169
  with gr.Row():
170
  out = gr.Audio(label="结果")
171
  log = gr.Textbox(label="日志")
172
+
173
  btn.click(run_predict, [inp], [out, log], api_name="predict")
174
 
175
  if __name__ == "__main__":