silk-road commited on
Commit
0df524c
1 Parent(s): 1ad4a2e

Upload 2 files

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