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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -301,12 +301,23 @@ class PodcastGenerator:
301
  else:
302
  raise gr.Error(f"Failed to generate podcast script: {str(e)}")
303
 
304
- # 嘗試解析JSON,如果失敗則返回原始文本
305
  try:
306
  return json.loads(generated_text)
307
  except json.JSONDecodeError:
308
- logger.warning("Generated text is not valid JSON. Returning raw text.")
309
- return {"raw_text": generated_text}
 
 
 
 
 
 
 
 
 
 
 
310
 
311
  async def tts_generate(self, text: str, speaker: int, speaker1: str, speaker2: str) -> str:
312
  """
@@ -388,11 +399,21 @@ class PodcastGenerator:
388
  # 生成Podcast劇本
389
  gr.Info("Generating podcast script...")
390
  start_time = time.time()
391
- podcast_json = await self.generate_script(input_text, language, api_key)
392
  end_time = time.time()
393
 
394
- if "error" in podcast_json:
395
- gr.Error(f"Failed to generate podcast script: {podcast_json['error']}")
 
 
 
 
 
 
 
 
 
 
396
  return None
397
 
398
  gr.Info(f"Successfully generated podcast script in {(end_time - start_time):.2f} seconds!")
@@ -400,7 +421,7 @@ class PodcastGenerator:
400
  # 生成Podcast音訊檔案
401
  gr.Info("Generating podcast audio files...")
402
  start_time = time.time()
403
- audio_files = await asyncio.gather(*[self.tts_generate(item['line'], item['speaker'], speaker1, speaker2) for item in podcast_json['podcast']])
404
  end_time = time.time()
405
  gr.Info(f"Successfully generated podcast audio files in {(end_time - start_time):.2f} seconds!")
406
 
 
301
  else:
302
  raise gr.Error(f"Failed to generate podcast script: {str(e)}")
303
 
304
+ # 嘗試解析JSON,如果失敗則嘗試從原始文本中提取對話
305
  try:
306
  return json.loads(generated_text)
307
  except json.JSONDecodeError:
308
+ logger.warning("Generated text is not valid JSON. Attempting to extract dialogue.")
309
+ lines = generated_text.split('\n')
310
+ podcast = []
311
+ current_speaker = 1
312
+ for line in lines:
313
+ line = line.strip()
314
+ if line:
315
+ podcast.append({
316
+ "speaker": current_speaker,
317
+ "line": line
318
+ })
319
+ current_speaker = 3 - current_speaker # Switch between 1 and 2
320
+ return {"podcast": podcast}
321
 
322
  async def tts_generate(self, text: str, speaker: int, speaker1: str, speaker2: str) -> str:
323
  """
 
399
  # 生成Podcast劇本
400
  gr.Info("Generating podcast script...")
401
  start_time = time.time()
402
+ script_result = await self.generate_script(input_text, language, api_key)
403
  end_time = time.time()
404
 
405
+ if "error" in script_result:
406
+ gr.Error(f"Failed to generate podcast script: {script_result['error']}")
407
+ return None
408
+
409
+ if "raw_text" in script_result:
410
+ gr.Warning("Generated text is not in the expected JSON format. Attempting to process raw text.")
411
+ # Here you might want to implement a fallback method to process raw text
412
+ # For now, we'll just return None
413
+ return None
414
+
415
+ if "podcast" not in script_result:
416
+ gr.Error("Generated script does not contain a 'podcast' key.")
417
  return None
418
 
419
  gr.Info(f"Successfully generated podcast script in {(end_time - start_time):.2f} seconds!")
 
421
  # 生成Podcast音訊檔案
422
  gr.Info("Generating podcast audio files...")
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