Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,8 @@ from chatharuhi import ChatHaruhi
|
|
5 |
import wget
|
6 |
import os
|
7 |
import openai
|
8 |
-
|
|
|
9 |
|
10 |
|
11 |
NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
|
@@ -18,6 +19,7 @@ NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'li
|
|
18 |
'乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
|
19 |
|
20 |
|
|
|
21 |
try:
|
22 |
os.makedirs("characters_zip")
|
23 |
except:
|
@@ -26,41 +28,48 @@ try:
|
|
26 |
os.makedirs("characters")
|
27 |
except:
|
28 |
pass
|
29 |
-
|
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 |
try:
|
33 |
-
|
34 |
except:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
|
42 |
system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
|
43 |
-
|
44 |
llm="openai",
|
45 |
story_db=db_folder,
|
46 |
verbose=True)
|
47 |
|
48 |
|
49 |
-
def get_response(
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
chatbot.append((user_msg, response))
|
54 |
-
|
|
|
|
|
55 |
|
|
|
|
|
56 |
|
57 |
-
|
|
|
58 |
return None, None, []
|
59 |
|
60 |
|
61 |
-
def get_image(
|
62 |
-
|
63 |
-
return Image.open(f'
|
64 |
|
65 |
|
66 |
with gr.Blocks() as demo:
|
@@ -68,13 +77,13 @@ with gr.Blocks() as demo:
|
|
68 |
"""
|
69 |
# Chat凉宫春日 ChatHaruhi
|
70 |
## Reviving Anime Character in Reality via Large Language Model
|
71 |
-
|
72 |
ChatHaruhi2.0的demo implemented by [chenxi](https://github.com/todochenxi)
|
73 |
-
|
74 |
更多信息见项目github链接 [https://github.com/LC1332/Chat-Haruhi-Suzumiya](https://github.com/LC1332/Chat-Haruhi-Suzumiya)
|
75 |
-
|
76 |
如果觉得有趣请拜托为我们点上star. If you find it interesting, please be kind enough to give us a star.
|
77 |
-
|
78 |
user_role 为角色扮演的人物 请尽量设置为与剧情相关的人物 且不要与主角同名
|
79 |
"""
|
80 |
)
|
@@ -82,12 +91,12 @@ with gr.Blocks() as demo:
|
|
82 |
chatbot = gr.Chatbot()
|
83 |
role_image = gr.Image(height=400, value="./images/haruhi.jpg")
|
84 |
with gr.Row():
|
85 |
-
|
86 |
user_text = gr.Textbox(label="user_text")
|
87 |
with gr.Row():
|
88 |
submit = gr.Button("Submit")
|
89 |
clean = gr.ClearButton(value="Clear")
|
90 |
-
|
91 |
'Luna', '王多鱼', 'Ron', '鸠摩智',
|
92 |
'Snape', '凉宫春日', 'Malfoy', '虚竹',
|
93 |
'萧峰', '段誉', 'Hermione', 'Dumbledore',
|
@@ -98,8 +107,8 @@ with gr.Blocks() as demo:
|
|
98 |
'Sheldon', 'Raj', 'Penny',
|
99 |
'韦小宝', '乔峰', '神里绫华',
|
100 |
'雷电将军', '于谦'], label="characters", value='凉宫春日')
|
101 |
-
|
102 |
-
user_text.submit(fn=
|
103 |
-
submit.click(fn=
|
104 |
-
clean.click(clear, [
|
105 |
-
demo.launch(debug=True)
|
|
|
5 |
import wget
|
6 |
import os
|
7 |
import openai
|
8 |
+
import asyncio
|
9 |
+
import anyio
|
10 |
|
11 |
|
12 |
NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
|
|
|
19 |
'乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
|
20 |
|
21 |
|
22 |
+
|
23 |
try:
|
24 |
os.makedirs("characters_zip")
|
25 |
except:
|
|
|
28 |
os.makedirs("characters")
|
29 |
except:
|
30 |
pass
|
31 |
+
ai_roles_obj = {}
|
32 |
for ai_role_en in NAME_DICT.values():
|
33 |
file_url = f"https://github.com/LC1332/Haruhi-2-Dev/raw/main/data/character_in_zip/{ai_role_en}.zip"
|
34 |
try:
|
35 |
+
os.makedirs(f"characters/{ai_role_en}")
|
36 |
except:
|
37 |
+
pass
|
38 |
+
if f"{ai_role_en}.zip" not in os.listdir(f"characters_zip"):
|
39 |
+
destination_file = f"characters_zip/{ai_role_en}.zip"
|
40 |
+
wget.download(file_url, destination_file)
|
41 |
+
destination_folder = f"characters/{ai_role_en}"
|
42 |
+
with zipfile.ZipFile(destination_file, 'r') as zip_ref:
|
43 |
+
zip_ref.extractall(destination_folder)
|
44 |
db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
|
45 |
system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
|
46 |
+
ai_roles_obj[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
|
47 |
llm="openai",
|
48 |
story_db=db_folder,
|
49 |
verbose=True)
|
50 |
|
51 |
|
52 |
+
async def get_response(user_name, user_text, role, chatbot):
|
53 |
+
role_en = NAME_DICT[role]
|
54 |
+
ai_roles_obj[role_en].dialogue_history = chatbot
|
55 |
+
response = ai_roles_obj[role_en].chat(role=user_name, text=user_text)
|
56 |
+
user_msg = user_name + ':「' + user_text + '」'
|
57 |
chatbot.append((user_msg, response))
|
58 |
+
print("history:", chatbot)
|
59 |
+
print("flag")
|
60 |
+
return chatbot
|
61 |
|
62 |
+
async def respond(user_name, user_text, role, chatbot):
|
63 |
+
return await get_response(user_name, user_text, role, chatbot), None
|
64 |
|
65 |
+
|
66 |
+
def clear(user_name, user_text, chatbot):
|
67 |
return None, None, []
|
68 |
|
69 |
|
70 |
+
def get_image(role):
|
71 |
+
role_en = NAME_DICT[role]
|
72 |
+
return Image.open(f'images/{role_en}.jpg'), None, None, []
|
73 |
|
74 |
|
75 |
with gr.Blocks() as demo:
|
|
|
77 |
"""
|
78 |
# Chat凉宫春日 ChatHaruhi
|
79 |
## Reviving Anime Character in Reality via Large Language Model
|
80 |
+
|
81 |
ChatHaruhi2.0的demo implemented by [chenxi](https://github.com/todochenxi)
|
82 |
+
|
83 |
更多信息见项目github链接 [https://github.com/LC1332/Chat-Haruhi-Suzumiya](https://github.com/LC1332/Chat-Haruhi-Suzumiya)
|
84 |
+
|
85 |
如果觉得有趣请拜托为我们点上star. If you find it interesting, please be kind enough to give us a star.
|
86 |
+
|
87 |
user_role 为角色扮演的人物 请尽量设置为与剧情相关的人物 且不要与主角同名
|
88 |
"""
|
89 |
)
|
|
|
91 |
chatbot = gr.Chatbot()
|
92 |
role_image = gr.Image(height=400, value="./images/haruhi.jpg")
|
93 |
with gr.Row():
|
94 |
+
user_name = gr.Textbox(label="user_role")
|
95 |
user_text = gr.Textbox(label="user_text")
|
96 |
with gr.Row():
|
97 |
submit = gr.Button("Submit")
|
98 |
clean = gr.ClearButton(value="Clear")
|
99 |
+
role = gr.Radio(['汤师爷', '慕容复', '李云龙',
|
100 |
'Luna', '王多鱼', 'Ron', '鸠摩智',
|
101 |
'Snape', '凉宫春日', 'Malfoy', '虚竹',
|
102 |
'萧峰', '段誉', 'Hermione', 'Dumbledore',
|
|
|
107 |
'Sheldon', 'Raj', 'Penny',
|
108 |
'韦小宝', '乔峰', '神里绫华',
|
109 |
'雷电将军', '于谦'], label="characters", value='凉宫春日')
|
110 |
+
role.change(get_image, role, [role_image, user_name, user_text, chatbot])
|
111 |
+
user_text.submit(fn=respond, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
|
112 |
+
submit.click(fn=respond, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
|
113 |
+
clean.click(clear, [user_name, user_text, chatbot], [user_name, user_text, chatbot])
|
114 |
+
demo.launch(debug=True, share=True)
|