Jiangxz01 commited on
Commit
6c5cfef
·
verified ·
1 Parent(s): 15ffe7c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -330,7 +330,7 @@ class PodcastGenerator:
330
  speaker2 (str): 第二位說話者的語音設定。
331
 
332
  返回:
333
- str: 生成的臨時音訊檔案的檔名。
334
 
335
  此方法使用 Edge TTS 將文字轉換爲語音,並將結果儲存爲臨時音訊檔案。
336
  根據指定的說話者編號選擇相應的語音設定。
@@ -346,11 +346,16 @@ class PodcastGenerator:
346
  # 儲存語音檔案
347
  await speech.save(temp_filename)
348
  return temp_filename
 
 
 
349
  except Exception as e:
350
- # 如果出錯,刪除臨時檔案並丟擲異常
 
 
 
351
  if os.path.exists(temp_filename):
352
  os.remove(temp_filename)
353
- raise e
354
 
355
  async def combine_audio_files(self, audio_files: List[str]) -> str:
356
  """
@@ -423,7 +428,15 @@ class PodcastGenerator:
423
  start_time = time.time()
424
  audio_files = await asyncio.gather(*[self.tts_generate(item['line'], item['speaker'], speaker1, speaker2) for item in script_result['podcast']])
425
  end_time = time.time()
426
- gr.Info(f"Successfully generated podcast audio files in {(end_time - start_time):.2f} seconds!")
 
 
 
 
 
 
 
 
427
 
428
  # 合併音訊檔案
429
  combined_audio = await self.combine_audio_files(audio_files)
@@ -493,6 +506,12 @@ async def process_input(input_text: str, input_file, language: str, speaker1: st
493
  speaker1 = voice_names[speaker1]
494
  speaker2 = voice_names[speaker2]
495
 
 
 
 
 
 
 
496
  # 如果提供了輸入檔案,則從檔案中提取文字
497
  if input_file:
498
  input_text = await TextExtractor.extract_text(input_file.name)
 
330
  speaker2 (str): 第二位說話者的語音設定。
331
 
332
  返回:
333
+ str: 生成的臨時音訊檔案的檔名,或者 None 如果生成失敗。
334
 
335
  此方法使用 Edge TTS 將文字轉換爲語音,並將結果儲存爲臨時音訊檔案。
336
  根據指定的說話者編號選擇相應的語音設定。
 
346
  # 儲存語音檔案
347
  await speech.save(temp_filename)
348
  return temp_filename
349
+ except edge_tts.exceptions.NoAudioReceived:
350
+ logger.error(f"No audio received for text: '{text[:50]}...' with voice: {voice}")
351
+ return None
352
  except Exception as e:
353
+ logger.error(f"Error generating audio for text: '{text[:50]}...' with voice: {voice}. Error: {str(e)}")
354
+ return None
355
+ finally:
356
+ # 如果檔案存在但生成失敗,刪除臨時檔案
357
  if os.path.exists(temp_filename):
358
  os.remove(temp_filename)
 
359
 
360
  async def combine_audio_files(self, audio_files: List[str]) -> str:
361
  """
 
428
  start_time = time.time()
429
  audio_files = await asyncio.gather(*[self.tts_generate(item['line'], item['speaker'], speaker1, speaker2) for item in script_result['podcast']])
430
  end_time = time.time()
431
+
432
+ # Filter out None values (failed TTS generations)
433
+ audio_files = [file for file in audio_files if file is not None]
434
+
435
+ if not audio_files:
436
+ gr.Error("Failed to generate any audio files. Please check your language and voice settings.")
437
+ return None
438
+
439
+ gr.Info(f"Successfully generated {len(audio_files)} out of {len(script_result['podcast'])} audio files in {(end_time - start_time):.2f} seconds!")
440
 
441
  # 合併音訊檔案
442
  combined_audio = await self.combine_audio_files(audio_files)
 
506
  speaker1 = voice_names[speaker1]
507
  speaker2 = voice_names[speaker2]
508
 
509
+ # Check if the selected voices are compatible with the chosen language
510
+ if language != "Auto Detect":
511
+ if not (speaker1.startswith(language[:2].lower()) and speaker2.startswith(language[:2].lower())):
512
+ gr.Error(f"Selected voices may not be compatible with the chosen language: {language}")
513
+ return None
514
+
515
  # 如果提供了輸入檔案,則從檔案中提取文字
516
  if input_file:
517
  input_text = await TextExtractor.extract_text(input_file.name)