Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -18,41 +18,46 @@ NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'li
|
|
18 |
'乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
|
19 |
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
llm="openai",
|
41 |
story_db=db_folder,
|
42 |
verbose=True)
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
chatbot.append((user_msg, response))
|
46 |
return chatbot, None
|
47 |
|
48 |
|
49 |
-
def clear(
|
50 |
return None, None, []
|
51 |
|
52 |
|
53 |
-
def get_image(
|
54 |
-
|
55 |
-
return Image.open(f'images/{
|
56 |
|
57 |
|
58 |
with gr.Blocks() as demo:
|
@@ -74,12 +79,12 @@ with gr.Blocks() as demo:
|
|
74 |
chatbot = gr.Chatbot()
|
75 |
role_image = gr.Image(height=400, value="./images/haruhi.jpg")
|
76 |
with gr.Row():
|
77 |
-
|
78 |
user_text = gr.Textbox(label="user_text")
|
79 |
with gr.Row():
|
80 |
submit = gr.Button("Submit")
|
81 |
clean = gr.ClearButton(value="Clear")
|
82 |
-
|
83 |
'Luna', '王多鱼', 'Ron', '鸠摩智',
|
84 |
'Snape', '凉宫春日', 'Malfoy', '虚竹',
|
85 |
'萧峰', '段誉', 'Hermione', 'Dumbledore',
|
@@ -90,8 +95,8 @@ with gr.Blocks() as demo:
|
|
90 |
'Sheldon', 'Raj', 'Penny',
|
91 |
'韦小宝', '乔峰', '神里绫华',
|
92 |
'雷电将军', '于谦'], label="characters", value='凉宫春日')
|
93 |
-
role.change(get_image,
|
94 |
-
user_text.submit(fn=get_response, inputs=[
|
95 |
-
submit.click(fn=get_response, inputs=[
|
96 |
-
clean.click(clear, [
|
97 |
demo.launch(debug=True)
|
|
|
18 |
'乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
|
19 |
|
20 |
|
21 |
+
try:
|
22 |
+
os.makedirs("characters_zip")
|
23 |
+
except:
|
24 |
+
pass
|
25 |
+
try:
|
26 |
+
os.makedirs("characters")
|
27 |
+
except:
|
28 |
+
pass
|
29 |
+
AI_ROLES_OBJ = {}
|
30 |
+
for ai_role_en in NAME_DICT.values():
|
31 |
+
file_url = f"https://github.com/LC1332/Haruhi-2-Dev/raw/main/data/character_in_zip/{ai_role_en}.zip"
|
32 |
+
os.makedirs(f"characters/{ai_role_en}")
|
33 |
+
destination_file = f"characters_zip/{ai_role_en}.zip"
|
34 |
+
wget.download(file_url, destination_file)
|
35 |
+
destination_folder = f"characters/{ai_role_en}"
|
36 |
+
with zipfile.ZipFile(destination_file, 'r') as zip_ref:
|
37 |
+
zip_ref.extractall(destination_folder)
|
38 |
+
db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
|
39 |
+
system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
|
40 |
+
AI_ROLES_OBJ[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
|
41 |
llm="openai",
|
42 |
story_db=db_folder,
|
43 |
verbose=True)
|
44 |
+
|
45 |
+
|
46 |
+
def get_response(user_role, user_text, ai_role, chatbot):
|
47 |
+
ai_role_en = NAME_DICT[ai_role]
|
48 |
+
response = AI_ROLES_OBJ[ai_role_en].chat(role=user_role, text=user_text)
|
49 |
+
user_msg = user_role + ':「' + user_text + '」'
|
50 |
chatbot.append((user_msg, response))
|
51 |
return chatbot, None
|
52 |
|
53 |
|
54 |
+
def clear(user_role, user_text, chatbot):
|
55 |
return None, None, []
|
56 |
|
57 |
|
58 |
+
def get_image(ai_role):
|
59 |
+
ai_role_en = NAME_DICT[ai_role]
|
60 |
+
return Image.open(f'images/{ai_role_en}.jpg'), None, None, []
|
61 |
|
62 |
|
63 |
with gr.Blocks() as demo:
|
|
|
79 |
chatbot = gr.Chatbot()
|
80 |
role_image = gr.Image(height=400, value="./images/haruhi.jpg")
|
81 |
with gr.Row():
|
82 |
+
user_role = gr.Textbox(label="user_role")
|
83 |
user_text = gr.Textbox(label="user_text")
|
84 |
with gr.Row():
|
85 |
submit = gr.Button("Submit")
|
86 |
clean = gr.ClearButton(value="Clear")
|
87 |
+
ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
|
88 |
'Luna', '王多鱼', 'Ron', '鸠摩智',
|
89 |
'Snape', '凉宫春日', 'Malfoy', '虚竹',
|
90 |
'萧峰', '段誉', 'Hermione', 'Dumbledore',
|
|
|
95 |
'Sheldon', 'Raj', 'Penny',
|
96 |
'韦小宝', '乔峰', '神里绫华',
|
97 |
'雷电将军', '于谦'], label="characters", value='凉宫春日')
|
98 |
+
role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
|
99 |
+
user_text.submit(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
|
100 |
+
submit.click(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
|
101 |
+
clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
|
102 |
demo.launch(debug=True)
|