youngtsai commited on
Commit
f8edc08
1 Parent(s): 52d5ca6
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -12,9 +12,9 @@ def generate_dialogue(rounds, method, role1, role2):
12
  def main_function(rounds: int, method: str, role1: str, role2: str):
13
  structured_dialogue = generate_dialogue(rounds, method, role1, role2)
14
 
15
- chatbot_dialogue = [(item["role"], item["content"]) for item in structured_dialogue]
16
 
17
- audio_path = dialogue_to_audio(chatbot_dialogue)
18
  json_output = json.dumps({"dialogue": structured_dialogue}, ensure_ascii=False, indent=4)
19
 
20
  # 儲存對話為 JSON 文件
@@ -25,8 +25,8 @@ def main_function(rounds: int, method: str, role1: str, role2: str):
25
  return chatbot_dialogue, audio_path, file_name
26
 
27
  def dialogue_to_audio(dialogue):
28
- text = " ".join([item[1] for item in dialogue])
29
- tts = gTTS(text=text, lang='en')
30
  file_path = "temp_audio.mp3"
31
  tts.save(file_path)
32
  return file_path
@@ -41,7 +41,7 @@ if __name__ == "__main__":
41
  gr.components.Textbox(label="角色 2 名稱", default="B"),
42
  ],
43
  [
44
- gr.components.Chatbot(label="生成的對話"),
45
  gr.components.Audio(type="filepath", label="對話朗讀"),
46
  gr.components.File(label="下載對話 JSON 文件")
47
  ]
 
12
  def main_function(rounds: int, method: str, role1: str, role2: str):
13
  structured_dialogue = generate_dialogue(rounds, method, role1, role2)
14
 
15
+ chatbot_dialogue = structured_dialogue # 使用结构化对话
16
 
17
+ audio_path = dialogue_to_audio([(item["role"], item["content"]) for item in structured_dialogue])
18
  json_output = json.dumps({"dialogue": structured_dialogue}, ensure_ascii=False, indent=4)
19
 
20
  # 儲存對話為 JSON 文件
 
25
  return chatbot_dialogue, audio_path, file_name
26
 
27
  def dialogue_to_audio(dialogue):
28
+ text = " ".join([f"{item['role']}: {item['content']}" for item in dialogue])
29
+ tts = gTTS(text=text, lang='zh-tw')
30
  file_path = "temp_audio.mp3"
31
  tts.save(file_path)
32
  return file_path
 
41
  gr.components.Textbox(label="角色 2 名稱", default="B"),
42
  ],
43
  [
44
+ gr.components.Chatbot(roles=["A", "B"], label="生成的對話"), # 确保此处的角色与默认值相匹配
45
  gr.components.Audio(type="filepath", label="對話朗讀"),
46
  gr.components.File(label="下載對話 JSON 文件")
47
  ]