simler commited on
Commit
28b5db4
·
verified ·
1 Parent(s): 62f3cea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -31
app.py CHANGED
@@ -12,12 +12,9 @@ torch.nn.Module.cuda = no_op
12
 
13
  print("💉 CUDA 补丁已注入")
14
 
15
- # --- 2. 导入推理函数 (适配扁平结构) ---
16
  sys.path.append(os.getcwd())
17
-
18
- # 尝试导入核心逻辑
19
  try:
20
- # 先试试直接导入
21
  import inference_webui as core
22
  print("✅ 成功导入 inference_webui")
23
  except ImportError:
@@ -32,20 +29,38 @@ if hasattr(core, "get_tts_model"):
32
  elif hasattr(core, "get_tts_wav"):
33
  inference_func = core.get_tts_wav
34
  print("👉 使用旧版函数: get_tts_wav")
35
- else:
36
- # 如果都找不到,打印所有函数名供调试
37
- print("❌ 没找到推理函数!可用函数如下:")
38
- print([d for d in dir(core) if "__" not in d])
39
 
40
- # --- 3. 自动寻找模型 (修复 .lock 问题) ---
41
  def find_real_model(pattern, search_path="."):
 
 
 
42
  for root, dirs, files in os.walk(search_path):
43
  for file in files:
44
- # 关键修改:排除 .lock 文件
45
- if pattern in file and not file.endswith(".lock"):
46
  path = os.path.join(root, file)
47
- print(f"🔍 发现模型: {path}")
48
- return path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  return None
50
 
51
  gpt_path = find_real_model("s1v3.ckpt")
@@ -57,51 +72,39 @@ if not sovits_path: sovits_path = find_real_model("s2G")
57
  # --- 4. 加载模型 ---
58
  try:
59
  if gpt_path and sovits_path:
60
- # 调用 inference_webui 里的加载函数
61
  if hasattr(core, "change_gpt_weights"):
62
  core.change_gpt_weights(gpt_path=gpt_path)
63
  if hasattr(core, "change_sovits_weights"):
64
  core.change_sovits_weights(sovits_path=sovits_path)
65
- print(" 模型加载完成!")
66
  else:
67
  print("❌ 依然没找到模型,请检查 Files 里的文件下载情况。")
68
  except Exception as e:
69
- print(f"⚠️ 模型加载报错 (可能内存不足): {e}")
70
 
71
  # --- 5. 推理逻辑 ---
72
  import soundfile as sf
73
- import numpy as np
74
  import gradio as gr
75
 
76
  REF_AUDIO = "ref.wav"
77
  REF_TEXT = "你好"
78
- REF_LANG = "zh"
79
 
80
  def run_predict(text):
81
  if not os.path.exists(REF_AUDIO):
82
  return None, "❌ 错误:请上传 ref.wav"
83
 
84
- if not inference_func:
85
- return None, "❌ 错误:未找到推理函数"
86
-
87
  print(f"📥 任务: {text}")
88
  try:
89
- # 构造参数 - 这是最通用的参数列表
90
- # 如果是旧版函数 get_tts_wav,它通常接受以下参数
91
- # ref_wav_path, prompt_text, prompt_language, text, text_language
92
-
93
- # 尝试调用
94
  generator = inference_func(
95
  ref_wav_path=REF_AUDIO,
96
  prompt_text=REF_TEXT,
97
- prompt_language=REF_LANG,
98
  text=text,
99
  text_language="zh",
100
  how_to_cut="凑四句一切",
101
  top_k=5, top_p=1, temperature=1, ref_free=False
102
  )
103
 
104
- # 处理结果
105
  result_list = list(generator)
106
  if result_list:
107
  sr, data = result_list[0]
@@ -116,11 +119,12 @@ def run_predict(text):
116
 
117
  # --- 6. 界面 ---
118
  with gr.Blocks() as app:
119
- gr.Markdown(f"### GPT-SoVITS 终极适配版")
120
- gr.Markdown(f"GPT: `{gpt_path}` \n SoVITS: `{sovits_path}`")
 
121
 
122
  with gr.Row():
123
- inp = gr.Textbox(label="文本", value="测试一下语音合成。")
124
  btn = gr.Button("生成")
125
 
126
  with gr.Row():
 
12
 
13
  print("💉 CUDA 补丁已注入")
14
 
15
+ # --- 2. 导入推理函数 ---
16
  sys.path.append(os.getcwd())
 
 
17
  try:
 
18
  import inference_webui as core
19
  print("✅ 成功导入 inference_webui")
20
  except ImportError:
 
29
  elif hasattr(core, "get_tts_wav"):
30
  inference_func = core.get_tts_wav
31
  print("👉 使用旧版函数: get_tts_wav")
 
 
 
 
32
 
33
+ # --- 3. 自动寻找模型 (V5 - 增加文件大小检测) ---
34
  def find_real_model(pattern, search_path="."):
35
+ print(f"🔍 开始搜索 {pattern} ...")
36
+ candidates = []
37
+
38
  for root, dirs, files in os.walk(search_path):
39
  for file in files:
40
+ # 必须包含文件
41
+ if pattern in file:
42
  path = os.path.join(root, file)
43
+
44
+ # 排除垃圾文件
45
+ if file.endswith(".lock"): continue
46
+ if file.endswith(".metadata"): continue
47
+
48
+ # 关键:检查文件大小 (小于 10MB 的肯定是假模型)
49
+ size_mb = os.path.getsize(path) / (1024 * 1024)
50
+ if size_mb < 10:
51
+ print(f" Skipping 小文件: {file} ({size_mb:.2f} MB)")
52
+ continue
53
+
54
+ candidates.append((path, size_mb))
55
+
56
+ if candidates:
57
+ # 按大小降序排序,选最大的那个,绝对没错
58
+ candidates.sort(key=lambda x: x[1], reverse=True)
59
+ best_path = candidates[0][0]
60
+ print(f"✅ 选中最大文件: {best_path} ({candidates[0][1]:.2f} MB)")
61
+ return best_path
62
+
63
+ print(f"❌ 未找到任何匹配 {pattern} 的大文件")
64
  return None
65
 
66
  gpt_path = find_real_model("s1v3.ckpt")
 
72
  # --- 4. 加载模型 ---
73
  try:
74
  if gpt_path and sovits_path:
 
75
  if hasattr(core, "change_gpt_weights"):
76
  core.change_gpt_weights(gpt_path=gpt_path)
77
  if hasattr(core, "change_sovits_weights"):
78
  core.change_sovits_weights(sovits_path=sovits_path)
79
+ print("🎉 模型加载完成!")
80
  else:
81
  print("❌ 依然没找到模型,请检查 Files 里的文件下载情况。")
82
  except Exception as e:
83
+ print(f"⚠️ 模型加载报错: {e}")
84
 
85
  # --- 5. 推理逻辑 ---
86
  import soundfile as sf
 
87
  import gradio as gr
88
 
89
  REF_AUDIO = "ref.wav"
90
  REF_TEXT = "你好"
 
91
 
92
  def run_predict(text):
93
  if not os.path.exists(REF_AUDIO):
94
  return None, "❌ 错误:请上传 ref.wav"
95
 
 
 
 
96
  print(f"📥 任务: {text}")
97
  try:
 
 
 
 
 
98
  generator = inference_func(
99
  ref_wav_path=REF_AUDIO,
100
  prompt_text=REF_TEXT,
101
+ prompt_language="zh",
102
  text=text,
103
  text_language="zh",
104
  how_to_cut="凑四句一切",
105
  top_k=5, top_p=1, temperature=1, ref_free=False
106
  )
107
 
 
108
  result_list = list(generator)
109
  if result_list:
110
  sr, data = result_list[0]
 
119
 
120
  # --- 6. 界面 ---
121
  with gr.Blocks() as app:
122
+ gr.Markdown(f"### GPT-SoVITS V2 CPU Worker")
123
+ gr.Markdown(f"GPT: `{os.path.basename(gpt_path) if gpt_path else 'Missing'}`")
124
+ gr.Markdown(f"SoVITS: `{os.path.basename(sovits_path) if sovits_path else 'Missing'}`")
125
 
126
  with gr.Row():
127
+ inp = gr.Textbox(label="文本", value="终于功了,这真是不容易啊。")
128
  btn = gr.Button("生成")
129
 
130
  with gr.Row():