Spaces:
Runtime error
Runtime error
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 = [
|
13 |
return dialogue
|
14 |
|
15 |
-
|
16 |
def main_function(rounds: int, method: str, role1: str, role2: str):
|
17 |
-
|
|
|
|
|
18 |
|
19 |
-
audio_path = dialogue_to_audio(
|
20 |
-
json_output = json.dumps({"dialogue":
|
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
|
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])
|