Spaces:
Runtime error
Runtime error
gr.components.Dropdown(choices=["中文", "英文"], label="語言"),
Browse files
app.py
CHANGED
@@ -50,12 +50,12 @@ def extract_json_from_response(response_text):
|
|
50 |
raise ValueError("JSON dialogue not found in the response.")
|
51 |
|
52 |
|
53 |
-
def create_chat_dialogue(rounds, role1, role2, theme="購物"):
|
54 |
openai.api_key = os.environ["OPEN_AI_KEY"]
|
55 |
|
56 |
# 初始化對話
|
57 |
sentenses_count = int(rounds) * 2
|
58 |
-
sys_content = f"
|
59 |
prompt = f"您將進行一場以{theme}為主題的對話。{role1}和{role2}將是參與者。請依次交談{rounds}輪。(1輪對話的定義是 {role1} 和 {role2} 各說一句話,總共 {sentenses_count} 句話。)以json格式儲存對話。並回傳對話JSON文件。格式為:[{{role:\"{role1}\", content: \".....\"}}, {{role:\"{role2}\", content: \".....\"}}]"
|
60 |
messages = [
|
61 |
{"role": "system", "content": sys_content},
|
@@ -84,18 +84,18 @@ def create_chat_dialogue(rounds, role1, role2, theme="購物"):
|
|
84 |
return dialogue
|
85 |
|
86 |
|
87 |
-
def generate_dialogue(rounds, method, role1, role2, theme):
|
88 |
if method == "auto":
|
89 |
dialogue = create_chat_dialogue(rounds, role1, role2, theme)
|
90 |
else:
|
91 |
dialogue = [{"role": role1, "content": "手動輸入文本 1"}, {"role": role2, "content": "手動輸入文本 2"}]
|
92 |
return dialogue
|
93 |
|
94 |
-
def main_function(password: str, theme: str, method: str, rounds: int, role1: str, role2: str):
|
95 |
if password != os.environ.get("PASSWORD", ""):
|
96 |
return "错误的密码,请重新输入。", "", ""
|
97 |
|
98 |
-
structured_dialogue = generate_dialogue(rounds, method, role1, role2, theme)
|
99 |
|
100 |
# Convert structured dialogue for Chatbot component to show "role1: content1" and "role2: content2" side by side
|
101 |
chatbot_dialogue = []
|
@@ -130,6 +130,7 @@ if __name__ == "__main__":
|
|
130 |
[
|
131 |
gr.components.Textbox(label="输入密码", type="password"),
|
132 |
gr.components.Textbox(label="對話主題"), # 加入 theme 的輸入框,設定預設值為 '購物'
|
|
|
133 |
gr.components.Dropdown(choices=["auto", "manual"], label="生成方式"),
|
134 |
gr.components.Slider(minimum=2, maximum=6, step=2, label="對話輪數"),
|
135 |
gr.components.Textbox(label="角色 1 名稱"),
|
|
|
50 |
raise ValueError("JSON dialogue not found in the response.")
|
51 |
|
52 |
|
53 |
+
def create_chat_dialogue(rounds, role1, role2, theme="購物", language):
|
54 |
openai.api_key = os.environ["OPEN_AI_KEY"]
|
55 |
|
56 |
# 初始化對話
|
57 |
sentenses_count = int(rounds) * 2
|
58 |
+
sys_content = f"你是一個{language}家教,請用{language}生成對話"
|
59 |
prompt = f"您將進行一場以{theme}為主題的對話。{role1}和{role2}將是參與者。請依次交談{rounds}輪。(1輪對話的定義是 {role1} 和 {role2} 各說一句話,總共 {sentenses_count} 句話。)以json格式儲存對話。並回傳對話JSON文件。格式為:[{{role:\"{role1}\", content: \".....\"}}, {{role:\"{role2}\", content: \".....\"}}]"
|
60 |
messages = [
|
61 |
{"role": "system", "content": sys_content},
|
|
|
84 |
return dialogue
|
85 |
|
86 |
|
87 |
+
def generate_dialogue(rounds, method, role1, role2, theme, language):
|
88 |
if method == "auto":
|
89 |
dialogue = create_chat_dialogue(rounds, role1, role2, theme)
|
90 |
else:
|
91 |
dialogue = [{"role": role1, "content": "手動輸入文本 1"}, {"role": role2, "content": "手動輸入文本 2"}]
|
92 |
return dialogue
|
93 |
|
94 |
+
def main_function(password: str, theme: str, language: str, method: str, rounds: int, role1: str, role2: str):
|
95 |
if password != os.environ.get("PASSWORD", ""):
|
96 |
return "错误的密码,请重新输入。", "", ""
|
97 |
|
98 |
+
structured_dialogue = generate_dialogue(rounds, method, role1, role2, theme, language)
|
99 |
|
100 |
# Convert structured dialogue for Chatbot component to show "role1: content1" and "role2: content2" side by side
|
101 |
chatbot_dialogue = []
|
|
|
130 |
[
|
131 |
gr.components.Textbox(label="输入密码", type="password"),
|
132 |
gr.components.Textbox(label="對話主題"), # 加入 theme 的輸入框,設定預設值為 '購物'
|
133 |
+
gr.components.Dropdown(choices=["中文", "英文"], label="語言"),
|
134 |
gr.components.Dropdown(choices=["auto", "manual"], label="生成方式"),
|
135 |
gr.components.Slider(minimum=2, maximum=6, step=2, label="對話輪數"),
|
136 |
gr.components.Textbox(label="角色 1 名稱"),
|