chenxiYan commited on
Commit
b51ea9d
1 Parent(s): 90be1d3

Create haruhiv2.py

Browse files
Files changed (1) hide show
  1. haruhiv2.py +87 -0
haruhiv2.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def get_response(user_name, user_text, role, chatbot):
22
+ # 下载role zip 并解压
23
+ role_en = NAME_DICT[role]
24
+ if not os.path.exists("characters_zip"):
25
+ os.makedirs("characters_zip")
26
+ if not os.path.exists("characters"):
27
+ os.makedirs("characters")
28
+ if NAME_DICT[role] not in os.listdir("./characters"):
29
+ file_url = f"https://github.com/LC1332/Haruhi-2-Dev/raw/main/data/character_in_zip/{role_en}.zip"
30
+ os.makedirs(f"characters/{role_en}")
31
+ destination_file = f"characters_zip/{role_en}.zip"
32
+ wget.download(file_url, destination_file)
33
+ destination_folder = f"characters/{role_en}"
34
+ with zipfile.ZipFile(destination_file, 'r') as zip_ref:
35
+ zip_ref.extractall(destination_folder)
36
+
37
+ db_folder = f"./characters/{role_en}/content/{role_en}"
38
+ system_prompt = f"./characters/{role_en}/content/system_prompt.txt"
39
+ haruhi = ChatHaruhi(system_prompt=system_prompt,
40
+ llm="openai",
41
+ story_db=db_folder,
42
+ verbose=True)
43
+ response = haruhi.chat(role=user_name, text=user_text)
44
+ chatbot.append((user_text, response))
45
+ return chatbot
46
+
47
+
48
+ def clear(user_name, user_text, chatbot):
49
+ return None, None, []
50
+
51
+
52
+ def get_image(role):
53
+ role_en = NAME_DICT[role]
54
+ return Image.open(f'images/{role_en}.jpg')
55
+
56
+
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown(
59
+ """
60
+ haruhi 2.0 readme
61
+ """
62
+ )
63
+ with gr.Row():
64
+ chatbot = gr.Chatbot()
65
+ role_image = gr.Image(height=400, value="./images/haruhi.jpg")
66
+ with gr.Row():
67
+ user_name = gr.Textbox(label="user_role")
68
+ user_text = gr.Textbox(label="user_text")
69
+ with gr.Row():
70
+ submit = gr.Button("Submit")
71
+ clean = gr.ClearButton(value="Clear")
72
+ role = gr.Radio(['汤师爷', '慕容复', '李云龙',
73
+ 'Luna', '王多鱼', 'Ron', '鸠摩智',
74
+ 'Snape', '凉宫春日', 'Malfoy', '虚竹',
75
+ '萧峰', '段誉', 'Hermione', 'Dumbledore',
76
+ '王语嫣',
77
+ 'Harry', 'McGonagall',
78
+ '白展堂', '佟湘玉', '郭芙蓉',
79
+ '旅行者', '钟离', '胡桃',
80
+ 'Sheldon', 'Raj', 'Penny',
81
+ '韦小宝', '乔峰', '神里绫华',
82
+ '雷电将军', '于谦'], label="characters", value='凉宫春日')
83
+ role.change(get_image, role, role_image)
84
+ user_text.submit(fn=get_response, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot])
85
+ submit.click(fn=get_response, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot])
86
+ clean.click(clear, [user_name, user_text, chatbot], [user_name, user_text, chatbot])
87
+ demo.launch(share=True, debug=True)