nukopy commited on
Commit
4575c7d
·
1 Parent(s): c848d89

feat: add GPU support for transcription and improve error handling

Browse files

- Introduced a new function `transcribe_one_with_gpu` to enable GPU acceleration for audio transcription.
- Enhanced error handling during Whisper model loading to provide clearer messages and support for unsupported platforms.
- Updated the `make_prompt` function to utilize the new GPU-enabled transcription function.

Files changed (1) hide show
  1. apps/audio_cloning/vallex/main.py +54 -7
apps/audio_cloning/vallex/main.py CHANGED
@@ -170,17 +170,20 @@ if not os.path.exists(OUTPUT_DIR_WHISPER):
170
  try:
171
  logger.info("Loading Whisper model...")
172
  model_name = "tiny"
173
- whisper_model = whisper.load_model(model_name, download_root=OUTPUT_DIR_WHISPER).to(
174
- device
175
- )
176
  logger.info("Whisper model loaded successfully")
 
 
 
 
 
177
  except Exception as e:
178
  logger.error("Error on loading Whisper model: %s", e)
179
  raise Exception(
180
  "\n Whisper download failed or damaged, please go to "
181
- "'https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt'"
182
- "\n manually download model and put it to {} .".format(os.getcwd() + "/whisper")
183
- )
184
 
185
  # Initialize Voice Presets
186
  logger.info("Initializing Voice Presets...")
@@ -235,6 +238,50 @@ def transcribe_one(model, audio_path):
235
  return lang, text_pr
236
 
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  def make_npz_prompt(name, uploaded_audio, recorded_audio, transcript_content):
239
  global model, text_collater, text_tokenizer, audio_tokenizer
240
  clear_prompts()
@@ -290,7 +337,7 @@ def make_prompt(name, wav, sr, save=True):
290
  wav = wav.unsqueeze(0)
291
  assert wav.ndim and wav.size(0) == 1
292
  torchaudio.save(f"./prompts/{name}.wav", wav, sr)
293
- lang, text = transcribe_one(whisper_model, f"./prompts/{name}.wav")
294
  lang_token = lang2token[lang]
295
  text = lang_token + text + lang_token
296
  with open(f"./prompts/{name}.txt", "w", encoding="utf-8") as f:
 
170
  try:
171
  logger.info("Loading Whisper model...")
172
  model_name = "tiny"
173
+ whisper_model = whisper.load_model(model_name, download_root=OUTPUT_DIR_WHISPER)
 
 
174
  logger.info("Whisper model loaded successfully")
175
+ except NotImplementedError as e:
176
+ logger.error("Error on loading Whisper model: %s", e)
177
+ raise Exception(
178
+ f"Whisper model {model_name} is not supported on this platform."
179
+ ) from e
180
  except Exception as e:
181
  logger.error("Error on loading Whisper model: %s", e)
182
  raise Exception(
183
  "\n Whisper download failed or damaged, please go to "
184
+ f"'https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/{model_name}.pt'"
185
+ f"\n manually download model and put it to {OUTPUT_DIR_WHISPER}."
186
+ ) from e
187
 
188
  # Initialize Voice Presets
189
  logger.info("Initializing Voice Presets...")
 
238
  return lang, text_pr
239
 
240
 
241
+ @spaces.GPU(duration=120)
242
+ def transcribe_one_with_gpu(model, audio_path):
243
+ model.eval()
244
+
245
+ # ZeroGPU では GPU 初期化/移動は関数内で
246
+ if torch.cuda.is_available():
247
+ model = model.to("cuda", non_blocking=True)
248
+ use_fp16 = True
249
+ dev = torch.device("cuda")
250
+ else:
251
+ use_fp16 = False
252
+ dev = torch.device("cpu")
253
+
254
+ # 推論は grad 無効に(速くて軽い)
255
+ with torch.inference_mode():
256
+ # 30 秒にパディング/トリム
257
+ audio = whisper.load_audio(audio_path)
258
+ audio = whisper.pad_or_trim(audio)
259
+
260
+ # ログメルを作成(最初は CPU の密テンソル想定)
261
+ mel = whisper.log_mel_spectrogram(audio)
262
+ mel = mel.to(dev, non_blocking=True)
263
+
264
+ # 言語推定
265
+ _, probs = model.detect_language(mel)
266
+ lang = max(probs, key=probs.get)
267
+ print(f"Detected language: {lang}")
268
+
269
+ # デコード
270
+ options = whisper.DecodingOptions(
271
+ temperature=1.0,
272
+ best_of=5,
273
+ fp16=use_fp16,
274
+ sample_len=150,
275
+ )
276
+ result = whisper.decode(model, mel, options)
277
+
278
+ text_pr = result.text
279
+ if text_pr.strip(" ")[-1] not in "?!.,。,?!。、":
280
+ text_pr += "."
281
+ return lang, text_pr
282
+
283
+
284
+ @spaces.GPU(duration=120)
285
  def make_npz_prompt(name, uploaded_audio, recorded_audio, transcript_content):
286
  global model, text_collater, text_tokenizer, audio_tokenizer
287
  clear_prompts()
 
337
  wav = wav.unsqueeze(0)
338
  assert wav.ndim and wav.size(0) == 1
339
  torchaudio.save(f"./prompts/{name}.wav", wav, sr)
340
+ lang, text = transcribe_one_with_gpu(whisper_model, f"./prompts/{name}.wav")
341
  lang_token = lang2token[lang]
342
  text = lang_token + text + lang_token
343
  with open(f"./prompts/{name}.txt", "w", encoding="utf-8") as f: