Ryanus commited on
Commit
277f96c
·
verified ·
1 Parent(s): a403eae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -8,19 +8,16 @@ import tempfile
8
  os.environ["COQUI_TOS_AGREED"] = "1"
9
 
10
  # --- 解決 PyTorch 載入 XTTS-v2 模型時的 WeightsUnpickler 錯誤 ---
11
- # PyTorch 2.6+ 出於安全考慮,預設在載入模型時會啟用 `weights_only=True`。
12
- # XTTS-v2 模型檢查點中包含自定義的配置類,這會導致 PyTorch 拒絕載入。
13
- # 這裡我們將這些類加入 PyTorch 的安全全局變數白名單,告訴 PyTorch 信任它們。
14
  import torch.serialization
15
  from TTS.tts.configs.xtts_config import XttsConfig
16
  from TTS.tts.models.xtts import XttsAudioConfig
17
  from TTS.config.shared_configs import BaseDatasetConfig
18
- from TTS.tts.models.xtts import XttsArgs # 新增這行!
19
 
20
  try:
21
  # 將所有可能導致問題的類都加入白名單
22
- torch.serialization.add_safe_globals([XttsConfig, XttsAudioConfig, BaseDatasetConfig, XttsArgs]) # 更新這行!
23
- print("已將 XttsConfig, XttsAudioConfig, BaseDatasetConfig 和 XttsArgs 加入 PyTorch 安全全局變數白名單。")
24
  except Exception as e:
25
  print(f"警告:無法將安全全局變數加入 PyTorch 白名單: {e}")
26
  print("如果遇到模型載入錯誤,請檢查 PyTorch 和 TTS 庫版本。")
@@ -91,12 +88,15 @@ SUPPORTED_LANGUAGES = [
91
  "te", # Telugu
92
  ]
93
 
 
 
 
94
 
95
  def generate_speech(text, language):
96
  """
97
  根據輸入文字和語言生成語音。
98
- 對於 XTTS-v2,需要指定語言。
99
- 此版本不包含語音克隆 (speaker_wav) 功能。
100
  """
101
  if model_load_error:
102
  return None, f"應用程式啟動錯誤:{model_load_error}"
@@ -109,13 +109,18 @@ def generate_speech(text, language):
109
  if not language:
110
  return None, "請選擇一個語言!"
111
 
 
 
 
 
112
  output_file = None
113
  try:
114
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
115
  output_file = fp.name
116
 
117
- print(f"正在為語言 '{language}' 生成語音...")
118
- tts.tts_to_file(text=text, language=language, file_path=output_file)
 
119
  print(f"語音已生成到:{output_file}")
120
 
121
  return output_file, "語音生成成功!"
@@ -138,9 +143,11 @@ iface = gr.Interface(
138
  ],
139
  title="Coqui TTS XTTS-v2 語音生成 (CPU)",
140
  description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU 上運行會非常慢。首次運行時會下載模型。",
141
- allow_flagging="never",
 
142
  )
143
 
144
  # 啟動 Gradio 應用
145
  if __name__ == "__main__":
146
  iface.launch()
 
 
8
  os.environ["COQUI_TOS_AGREED"] = "1"
9
 
10
  # --- 解決 PyTorch 載入 XTTS-v2 模型時的 WeightsUnpickler 錯誤 ---
 
 
 
11
  import torch.serialization
12
  from TTS.tts.configs.xtts_config import XttsConfig
13
  from TTS.tts.models.xtts import XttsAudioConfig
14
  from TTS.config.shared_configs import BaseDatasetConfig
15
+ from TTS.tts.models.xtts import XttsArgs
16
 
17
  try:
18
  # 將所有可能導致問題的類都加入白名單
19
+ torch.serialization.add_safe_globals([XttsConfig, XttsAudioConfig, BaseDatasetConfig, XttsArgs])
20
+ print("已將 XTTS 相關配置類加入 PyTorch 安全全局變數白名單。")
21
  except Exception as e:
22
  print(f"警告:無法將安全全局變數加入 PyTorch 白名單: {e}")
23
  print("如果遇到模型載入錯誤,請檢查 PyTorch 和 TTS 庫版本。")
 
88
  "te", # Telugu
89
  ]
90
 
91
+ # 定義預設說話者音訊檔案的路徑
92
+ # 請確保你已將 'speaker.wav' 檔案上傳到 Hugging Face Space 的根目錄
93
+ DEFAULT_SPEAKER_WAV = "speaker.wav"
94
 
95
  def generate_speech(text, language):
96
  """
97
  根據輸入文字和語言生成語音。
98
+ 對於 XTTS-v2,需要指定語言和 speaker_wav。
99
+ 此版本使用預設的 speaker.wav 檔案。
100
  """
101
  if model_load_error:
102
  return None, f"應用程式啟動錯誤:{model_load_error}"
 
109
  if not language:
110
  return None, "請選擇一個語言!"
111
 
112
+ # 檢查預設說話者音訊檔案是否存在
113
+ if not os.path.exists(DEFAULT_SPEAKER_WAV):
114
+ return None, f"錯誤:預設語音參考檔案 ({DEFAULT_SPEAKER_WAV}) 未找到。請確保該檔案已上傳到您的 Hugging Face Space 的根目錄。"
115
+
116
  output_file = None
117
  try:
118
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
119
  output_file = fp.name
120
 
121
+ print(f"正在為語言 '{language}' 生成語音,使用預設語音參考檔案: {DEFAULT_SPEAKER_WAV}...")
122
+ # DEFAULT_SPEAKER_WAV 作為 speaker_wav 參數傳遞
123
+ tts.tts_to_file(text=text, language=language, speaker_wav=DEFAULT_SPEAKER_WAV, file_path=output_file)
124
  print(f"語音已生成到:{output_file}")
125
 
126
  return output_file, "語音生成成功!"
 
143
  ],
144
  title="Coqui TTS XTTS-v2 語音生成 (CPU)",
145
  description="在 Hugging Face Space 上使用 Coqui TTS XTTS-v2 模型將文字轉換為語音。此演示使用 CPU 運行,請注意 XTTS-v2 在 CPU 上運行會非常慢。首次運行時會下載模型。",
146
+ # 將 allow_flagging 替換為 flagging_mode 以消除 Gradio 警告
147
+ flagging_mode="never",
148
  )
149
 
150
  # 啟動 Gradio 應用
151
  if __name__ == "__main__":
152
  iface.launch()
153
+