Spaces:
Runtime error
Runtime error
app.py
CHANGED
@@ -1,42 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
3 |
|
4 |
-
# 1. 將對話轉換為語音的函數
|
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 |
-
|
13 |
-
|
14 |
dialogue = []
|
15 |
for i in range(rounds):
|
16 |
if method == "auto":
|
17 |
-
dialogue.append({"role":
|
18 |
else:
|
19 |
-
dialogue.append({"role":
|
20 |
return dialogue
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
dialogue = generate_dialogue(rounds, method)
|
25 |
audio_path = dialogue_to_audio(dialogue)
|
26 |
formatted_text = "\n".join([f"{item['role']}: {item['text']}" for item in dialogue])
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# 4. 創建 Gradio 界面並啟動
|
30 |
def main_interface():
|
31 |
interface = gr.Interface(
|
32 |
fn=generate_dialogue_with_audio,
|
33 |
inputs=[
|
34 |
gr.components.Slider(minimum=2, maximum=6, step=2, default=2, label="對話輪數"),
|
35 |
gr.components.Dropdown(choices=["auto", "manual"], label="生成方式"),
|
|
|
|
|
36 |
],
|
37 |
outputs=[
|
38 |
gr.components.Audio(type="filepath", label="對話朗讀"),
|
39 |
-
gr.components.Textbox(label="生成的對話")
|
|
|
40 |
]
|
41 |
)
|
42 |
interface.launch()
|
|
|
1 |
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()
|