youngtsai commited on
Commit
656113e
1 Parent(s): 58978b9
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -2,51 +2,50 @@ import gradio as gr
2
  from gtts import gTTS
3
  import json
4
 
5
- def dialogue_to_audio(dialogue):
6
- text = " ".join([item["text"] for item in dialogue])
7
- tts = gTTS(text=text, lang='en')
8
- file_path = "temp_audio.mp3"
9
- tts.save(file_path)
10
- return file_path
11
-
12
  def generate_dialogue(rounds: int, method: str, role1: str, role2: str) -> list:
13
  roles = [role1, role2]
14
  dialogue = []
15
  for i in range(rounds):
16
  if method == "auto":
17
- dialogue.append({"role": roles[i % 2], "text": f"自動文本 {i + 1}"})
18
  else:
19
- dialogue.append({"role": roles[i % 2], "text": ""})
20
  return dialogue
21
 
22
- def generate_dialogue_with_audio(rounds: int, method: str, role1: str, role2: str):
23
  dialogue = generate_dialogue(rounds, method, role1, role2)
 
24
  audio_path = dialogue_to_audio(dialogue)
25
- formatted_text = "\n".join([f"{item['role']}: {item['text']}" for item in dialogue])
26
- json_output = json.dumps(dialogue, ensure_ascii=False, indent=4)
27
 
28
  # 儲存對話為 JSON 文件
29
  with open("dialogue_output.json", "w", encoding="utf-8") as f:
30
  f.write(json_output)
31
 
32
- return audio_path, formatted_text, "dialogue_output.json"
 
 
 
33
 
34
- def main_interface():
35
- interface = gr.Interface(
36
- fn=generate_dialogue_with_audio,
37
- inputs=[
 
 
 
 
 
 
 
38
  gr.components.Slider(minimum=2, maximum=6, step=2, default=2, label="對話輪數"),
39
  gr.components.Dropdown(choices=["auto", "manual"], label="生成方式"),
40
  gr.components.Textbox(default="A", label="角色 1 名稱"),
41
  gr.components.Textbox(default="B", label="角色 2 名稱"),
42
- ],
43
- outputs=[
 
44
  gr.components.Audio(type="filepath", label="對話朗讀"),
45
- gr.components.Textbox(label="生成的對話"),
46
  gr.components.File(label="下載對話 JSON 文件")
47
  ]
48
- )
49
- interface.launch()
50
-
51
- if __name__ == "__main__":
52
- main_interface()
 
2
  from gtts import gTTS
3
  import json
4
 
 
 
 
 
 
 
 
5
  def generate_dialogue(rounds: int, method: str, role1: str, role2: str) -> list:
6
  roles = [role1, role2]
7
  dialogue = []
8
  for i in range(rounds):
9
  if method == "auto":
10
+ dialogue.append((roles[i % 2], f"自動文本 {i + 1}"))
11
  else:
12
+ dialogue.append((roles[i % 2], ""))
13
  return dialogue
14
 
15
+ def main_function(rounds: int, method: str, role1: str, role2: str):
16
  dialogue = generate_dialogue(rounds, method, role1, role2)
17
+
18
  audio_path = dialogue_to_audio(dialogue)
19
+ json_output = json.dumps({"dialogue": dialogue}, ensure_ascii=False, indent=4)
 
20
 
21
  # 儲存對話為 JSON 文件
22
  with open("dialogue_output.json", "w", encoding="utf-8") as f:
23
  f.write(json_output)
24
 
25
+ # 將對話格式化為 Chatbot 能接受的格式
26
+ formatted_dialogue = [{"role": item[0], "content": item[1]} for item in dialogue]
27
+
28
+ return formatted_dialogue, audio_path, "dialogue_output.json"
29
 
30
+ def dialogue_to_audio(dialogue):
31
+ text = " ".join([item[1] for item in dialogue])
32
+ tts = gTTS(text=text, lang='en')
33
+ file_path = "temp_audio.mp3"
34
+ tts.save(file_path)
35
+ return file_path
36
+
37
+ if __name__ == "__main__":
38
+ gr.Interface(
39
+ main_function,
40
+ [
41
  gr.components.Slider(minimum=2, maximum=6, step=2, default=2, label="對話輪數"),
42
  gr.components.Dropdown(choices=["auto", "manual"], label="生成方式"),
43
  gr.components.Textbox(default="A", label="角色 1 名稱"),
44
  gr.components.Textbox(default="B", label="角色 2 名稱"),
45
+ ],
46
+ [
47
+ gr.components.Chatbot(label="生成的對話"),
48
  gr.components.Audio(type="filepath", label="對話朗讀"),
 
49
  gr.components.File(label="下載對話 JSON 文件")
50
  ]
51
+ ).launch()