hhhwmws commited on
Commit
48de9af
1 Parent(s): b07f020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -30
app.py CHANGED
@@ -5,7 +5,7 @@ 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',
@@ -18,6 +18,7 @@ NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'li
18
  '乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
19
 
20
 
 
21
  try:
22
  os.makedirs("characters_zip")
23
  except:
@@ -26,35 +27,39 @@ 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):
@@ -62,8 +67,8 @@ def clear(user_role, user_text, chatbot):
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:
@@ -71,14 +76,10 @@ with gr.Blocks() as demo:
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():
@@ -102,7 +103,7 @@ with gr.Blocks() as demo:
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)
 
5
  import wget
6
  import os
7
  import openai
8
+ import copy
9
 
10
 
11
  NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
 
18
  '乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
19
 
20
 
21
+
22
  try:
23
  os.makedirs("characters_zip")
24
  except:
 
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="openai",
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):
 
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:
 
76
  """
77
  # Chat凉宫春日 ChatHaruhi
78
  ## Reviving Anime Character in Reality via Large Language Model
79
+ ChatHaruhi2.0的GLMPro 版本demo implemented by [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():
 
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)