chenxiYan commited on
Commit
1744914
1 Parent(s): 83936fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -31
app.py CHANGED
@@ -18,41 +18,46 @@ NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'li
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
- user_msg = user_name + ':「' + user_text + '」'
 
 
 
 
45
  chatbot.append((user_msg, response))
46
  return chatbot, None
47
 
48
 
49
- def clear(user_name, user_text, chatbot):
50
  return None, None, []
51
 
52
 
53
- def get_image(role):
54
- role_en = NAME_DICT[role]
55
- return Image.open(f'images/{role_en}.jpg'), None, None, []
56
 
57
 
58
  with gr.Blocks() as demo:
@@ -74,12 +79,12 @@ with gr.Blocks() as demo:
74
  chatbot = gr.Chatbot()
75
  role_image = gr.Image(height=400, value="./images/haruhi.jpg")
76
  with gr.Row():
77
- user_name = gr.Textbox(label="user_role")
78
  user_text = gr.Textbox(label="user_text")
79
  with gr.Row():
80
  submit = gr.Button("Submit")
81
  clean = gr.ClearButton(value="Clear")
82
- role = gr.Radio(['汤师爷', '慕容复', '李云龙',
83
  'Luna', '王多鱼', 'Ron', '鸠摩智',
84
  'Snape', '凉宫春日', 'Malfoy', '虚竹',
85
  '萧峰', '段誉', 'Hermione', 'Dumbledore',
@@ -90,8 +95,8 @@ with gr.Blocks() as demo:
90
  'Sheldon', 'Raj', 'Penny',
91
  '韦小宝', '乔峰', '神里绫华',
92
  '雷电将军', '于谦'], label="characters", value='凉宫春日')
93
- role.change(get_image, role, [role_image, user_name, user_text, chatbot])
94
- user_text.submit(fn=get_response, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
95
- submit.click(fn=get_response, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
96
- clean.click(clear, [user_name, user_text, chatbot], [user_name, user_text, chatbot])
97
  demo.launch(debug=True)
 
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
+ os.makedirs(f"characters/{ai_role_en}")
33
+ destination_file = f"characters_zip/{ai_role_en}.zip"
34
+ wget.download(file_url, destination_file)
35
+ destination_folder = f"characters/{ai_role_en}"
36
+ with zipfile.ZipFile(destination_file, 'r') as zip_ref:
37
+ zip_ref.extractall(destination_folder)
38
+ db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
39
+ system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
40
+ AI_ROLES_OBJ[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
41
  llm="openai",
42
  story_db=db_folder,
43
  verbose=True)
44
+
45
+
46
+ def get_response(user_role, user_text, ai_role, chatbot):
47
+ ai_role_en = NAME_DICT[ai_role]
48
+ response = AI_ROLES_OBJ[ai_role_en].chat(role=user_role, text=user_text)
49
+ user_msg = user_role + ':「' + user_text + '」'
50
  chatbot.append((user_msg, response))
51
  return chatbot, None
52
 
53
 
54
+ def clear(user_role, user_text, chatbot):
55
  return None, None, []
56
 
57
 
58
+ def get_image(ai_role):
59
+ ai_role_en = NAME_DICT[ai_role]
60
+ return Image.open(f'images/{ai_role_en}.jpg'), None, None, []
61
 
62
 
63
  with gr.Blocks() as demo:
 
79
  chatbot = gr.Chatbot()
80
  role_image = gr.Image(height=400, value="./images/haruhi.jpg")
81
  with gr.Row():
82
+ user_role = gr.Textbox(label="user_role")
83
  user_text = gr.Textbox(label="user_text")
84
  with gr.Row():
85
  submit = gr.Button("Submit")
86
  clean = gr.ClearButton(value="Clear")
87
+ ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
88
  'Luna', '王多鱼', 'Ron', '鸠摩智',
89
  'Snape', '凉宫春日', 'Malfoy', '虚竹',
90
  '萧峰', '段誉', 'Hermione', 'Dumbledore',
 
95
  'Sheldon', 'Raj', 'Penny',
96
  '韦小宝', '乔峰', '神里绫华',
97
  '雷电将军', '于谦'], label="characters", value='凉宫春日')
98
+ role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
99
+ user_text.submit(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
100
+ submit.click(fn=get_response, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
101
+ clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
102
  demo.launch(debug=True)