hhhwmws commited on
Commit
317fcb3
1 Parent(s): 795e828

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import zipfile
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from chatharuhi import ChatHaruhi
5
+ import wget
6
+ import os
7
+ import openai
8
+ import copy
9
+
10
+
11
+ NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
12
+ 'Ron': 'Ron', '鸠摩智': 'jiumozhi', 'Snape': 'Snape',
13
+ '凉宫春日': 'haruhi', 'Malfoy': 'Malfoy', '虚竹': 'xuzhu', '萧峰': 'xiaofeng', '段誉': 'duanyu',
14
+ 'Hermione': 'Hermione', 'Dumbledore': 'Dumbledore', '王语嫣': 'wangyuyan',
15
+ 'Harry': 'Harry', 'McGonagall': 'McGonagall', '白展堂': 'baizhantang', '佟湘玉': 'tongxiangyu',
16
+ '郭芙蓉': 'guofurong', '旅行者': 'wanderer', '钟离': 'zhongli',
17
+ '胡桃': 'hutao', 'Sheldon': 'Sheldon', 'Raj': 'Raj', 'Penny': 'Penny', '韦小宝': 'weixiaobao',
18
+ '乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
19
+
20
+
21
+
22
+ try:
23
+ os.makedirs("characters_zip")
24
+ except:
25
+ pass
26
+ try:
27
+ os.makedirs("characters")
28
+ except:
29
+ pass
30
+ ai_roles_obj = {}
31
+ for ai_role_en in NAME_DICT.values():
32
+ file_url = f"https://github.com/LC1332/Haruhi-2-Dev/raw/main/data/character_in_zip/{ai_role_en}.zip"
33
+ try:
34
+ os.makedirs(f"characters/{ai_role_en}")
35
+ except:
36
+ pass
37
+ if f"{ai_role_en}.zip" not in os.listdir(f"characters_zip"):
38
+ destination_file = f"characters_zip/{ai_role_en}.zip"
39
+ wget.download(file_url, destination_file)
40
+ destination_folder = f"characters/{ai_role_en}"
41
+ with zipfile.ZipFile(destination_file, 'r') as zip_ref:
42
+ zip_ref.extractall(destination_folder)
43
+ db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
44
+ system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
45
+ ai_roles_obj[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
46
+ llm="spark",
47
+ story_db=db_folder,
48
+ verbose=True)
49
+
50
+
51
+ async def get_response(user_role, user_text, ai_role, chatbot):
52
+ role_en = NAME_DICT[ai_role]
53
+ ai_roles_obj[role_en].dialogue_history = copy.deepcopy(chatbot)
54
+ response = ai_roles_obj[role_en].chat(role=user_role, text=user_text)
55
+ user_msg = user_role + ':「' + user_text + '」'
56
+ latest_msg = (user_msg, response)
57
+ print(latest_msg)
58
+ chatbot.append(latest_msg)
59
+ return chatbot
60
+
61
+ async def respond(user_role, user_text, ai_role, chatbot):
62
+ return await get_response(user_role, user_text, ai_role, chatbot), None
63
+
64
+
65
+ def clear(user_role, user_text, chatbot):
66
+ return None, None, []
67
+
68
+
69
+ def get_image(ai_role):
70
+ role_en = NAME_DICT[ai_role]
71
+ return Image.open(f'images/{role_en}.jpg'), None, None, []
72
+
73
+
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown(
76
+ """
77
+ # Chat凉宫春日 ChatHaruhi
78
+ ## Reviving Anime Character in Reality via Large Language Model
79
+ ChatHaruhi2.0的星火大模型版本demo implemented by [Weishi MI](https://github.com/hhhwmws0117) and [chenxi](https://github.com/todochenxi)
80
+ 更多信息见项目github链接 [https://github.com/LC1332/Chat-Haruhi-Suzumiya](https://github.com/LC1332/Chat-Haruhi-Suzumiya)
81
+ 如果觉得有趣请拜托为我们点上star. If you find it interesting, please be kind enough to give us a star.
82
+ user_role 为用户扮演的人物 请尽量设置为与剧情相关的人物 且不要与主角同名
83
+ """
84
+ )
85
+ with gr.Row():
86
+ chatbot = gr.Chatbot()
87
+ role_image = gr.Image(height=400, value="./images/haruhi.jpg")
88
+ with gr.Row():
89
+ user_role = gr.Textbox(label="user_role")
90
+ user_text = gr.Textbox(label="user_text")
91
+ with gr.Row():
92
+ submit = gr.Button("Submit")
93
+ clean = gr.ClearButton(value="Clear")
94
+ ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
95
+ 'Luna', '王多鱼', 'Ron', '鸠摩智',
96
+ 'Snape', '凉宫春日', 'Malfoy', '虚竹',
97
+ '萧峰', '段誉', 'Hermione', 'Dumbledore',
98
+ '王语嫣',
99
+ 'Harry', 'McGonagall',
100
+ '白展堂', '佟湘玉', '郭芙蓉',
101
+ '旅行者', '钟离', '胡桃',
102
+ 'Sheldon', 'Raj', 'Penny',
103
+ '韦小宝', '乔峰', '神里绫华',
104
+ '雷电将军', '于谦'], label="characters", value='凉宫春日')
105
+ ai_role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
106
+ user_text.submit(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
107
+ submit.click(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
108
+ clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
109
+ demo.launch(debug=True)