hhhwmws commited on
Commit
6ca4f4f
1 Parent(s): a056459

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+ 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
+ try:
33
+ os.makedirs(f"characters/{ai_role_en}")
34
+ except:
35
+ pass
36
+ destination_file = f"characters_zip/{ai_role_en}.zip"
37
+ wget.download(file_url, destination_file)
38
+ destination_folder = f"characters/{ai_role_en}"
39
+ with zipfile.ZipFile(destination_file, 'r') as zip_ref:
40
+ zip_ref.extractall(destination_folder)
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
+ AI_ROLES_OBJ[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
44
+ llm="GLMPro",
45
+ story_db=db_folder,
46
+ verbose=True)
47
+
48
+
49
+ def get_response(user_role, user_text, ai_role, chatbot, history=None):
50
+ ai_role_en = NAME_DICT[ai_role]
51
+
52
+ AI_ROLES_OBJ[ai_role_en].dialogue_history = history
53
+ response = AI_ROLES_OBJ[ai_role_en].chat(role=user_role, text=user_text)
54
+
55
+ user_msg = user_role + ':「' + user_text + '」'
56
+ chatbot.append((user_msg, response))
57
+ return chatbot, None
58
+
59
+
60
+ def clear(user_role, user_text, chatbot):
61
+ return None, None, []
62
+
63
+
64
+ def get_image(ai_role):
65
+ ai_role_en = NAME_DICT[ai_role]
66
+ return Image.open(f'./images/{ai_role_en}.jpg'), None, None, []
67
+
68
+
69
+ with gr.Blocks() as demo:
70
+ gr.Markdown(
71
+ """
72
+ # Chat凉宫春日 ChatHaruhi
73
+ ## Reviving Anime Character in Reality via Large Language Model
74
+
75
+ ChatHaruhi2.0 的GLMPro 版本demo implemented by [Weishi MI](https://github.com/hhhwmws0117) and [chenxi](https://github.com/todochenxi)
76
+
77
+ 更多信息见项目github链接 [https://github.com/LC1332/Chat-Haruhi-Suzumiya](https://github.com/LC1332/Chat-Haruhi-Suzumiya)
78
+
79
+ 如果觉得有趣请拜托为我们点上star. If you find it interesting, please be kind enough to give us a star.
80
+
81
+ user_role 为角色扮演的人物 请尽量设置为与剧情相关的人物 且不要与主角同名
82
+ """
83
+ )
84
+ with gr.Row():
85
+ chatbot = gr.Chatbot()
86
+ role_image = gr.Image(height=400, value="./images/haruhi.jpg")
87
+ with gr.Row():
88
+ user_role = gr.Textbox(label="user_role")
89
+ user_text = gr.Textbox(label="user_text")
90
+ with gr.Row():
91
+ submit = gr.Button("Submit")
92
+ clean = gr.ClearButton(value="Clear")
93
+ ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
94
+ 'Luna', '王多鱼', 'Ron', '鸠摩智',
95
+ 'Snape', '凉宫春日', 'Malfoy', '虚竹',
96
+ '萧峰', '段誉', 'Hermione', 'Dumbledore',
97
+ '王语嫣',
98
+ 'Harry', 'McGonagall',
99
+ '白展堂', '佟湘玉', '郭芙蓉',
100
+ '旅行者', '钟离', '胡桃',
101
+ 'Sheldon', 'Raj', 'Penny',
102
+ '韦小宝', '乔峰', '神里绫华',
103
+ '雷电将军', '于谦'], label="characters", value='凉宫春日')
104
+ ai_role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
105
+ user_text.submit(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
106
+ submit.click(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
107
+ clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
108
+ demo.launch(debug=True)