youngtsai commited on
Commit
52d5ca6
1 Parent(s): 830e7f1
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -3,29 +3,26 @@ from gtts import gTTS
3
  import json
4
 
5
  def generate_dialogue(rounds, method, role1, role2):
6
- # 這裡只是一個示例,你可能需要更高級的生成方式
7
  if method == "auto":
8
- dialogue = []
9
- for i in range(rounds):
10
- dialogue.append((role1 if i % 2 == 0 else role2, f"自動文本 {i+1}"))
11
  else:
12
- dialogue = [(role1, "手動輸入文本 1"), (role2, "手動輸入文本 2")]
13
  return dialogue
14
 
15
-
16
  def main_function(rounds: int, method: str, role1: str, role2: str):
17
- dialogue = generate_dialogue(rounds, method, role1, role2)
 
 
18
 
19
- audio_path = dialogue_to_audio(dialogue)
20
- json_output = json.dumps({"dialogue": dialogue}, ensure_ascii=False, indent=4)
21
 
22
  # 儲存對話為 JSON 文件
23
  file_name = "dialogue_output.txt"
24
  with open(file_name, "w", encoding="utf-8") as f:
25
  f.write(json_output)
26
 
27
- return dialogue, audio_path, file_name
28
-
29
 
30
  def dialogue_to_audio(dialogue):
31
  text = " ".join([item[1] for item in dialogue])
 
3
  import json
4
 
5
  def generate_dialogue(rounds, method, role1, role2):
 
6
  if method == "auto":
7
+ dialogue = [{"role": role1 if i % 2 == 0 else role2, "content": f"自動文本 {i+1}"} for i in range(rounds)]
 
 
8
  else:
9
+ dialogue = [{"role": role1, "content": "手動輸入文本 1"}, {"role": role2, "content": "手動輸入文本 2"}]
10
  return dialogue
11
 
 
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 文件
21
  file_name = "dialogue_output.txt"
22
  with open(file_name, "w", encoding="utf-8") as f:
23
  f.write(json_output)
24
 
25
+ return chatbot_dialogue, audio_path, file_name
 
26
 
27
  def dialogue_to_audio(dialogue):
28
  text = " ".join([item[1] for item in dialogue])