Tianjiao-Yu commited on
Commit
b5c515d
1 Parent(s): 5a58cbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -117
app.py CHANGED
@@ -1,117 +1,117 @@
1
- import zipfile
2
- import gradio as gr
3
- from PIL import Image
4
- from chatharuhi import ChatHaruhi
5
- import requests
6
- import os
7
- import openai
8
- import copy
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
-
22
- try:
23
- os.makedirs("characters_zip")
24
- except:
25
- pass
26
- try:
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
- max_retries = 3 # 最大重试次数
40
- for attempt in range(1, max_retries+1):
41
- response = requests.get(file_url)
42
- if response.status_code == 200:
43
- with open(destination_file, "wb") as file:
44
- file.write(response.content)
45
- print(ai_role_en)
46
- break
47
- else:
48
- print(f"{ai_role_en}第{attempt}次下载失败")
49
- # wget.download(file_url, destination_file) # 503
50
- destination_folder = f"characters/{ai_role_en}"
51
- with zipfile.ZipFile(destination_file, 'r') as zip_ref:
52
- zip_ref.extractall(destination_folder)
53
- db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
54
- system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
55
- ai_roles_obj[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
56
- llm="openai",
57
- story_db=db_folder,
58
- verbose=True)
59
-
60
-
61
- async def get_response(user_role, user_text, ai_role, chatbot):
62
- role_en = NAME_DICT[ai_role]
63
- ai_roles_obj[role_en].dialogue_history = copy.deepcopy(chatbot)
64
- response = ai_roles_obj[role_en].chat(role=user_role, text=user_text)
65
- user_msg = user_role + ':「' + user_text + '」'
66
- latest_msg = (user_msg, response)
67
- print(latest_msg)
68
- chatbot.append(latest_msg)
69
- return chatbot
70
-
71
- async def respond(user_role, user_text, ai_role, chatbot):
72
- return await get_response(user_role, user_text, ai_role, chatbot), None
73
-
74
-
75
- def clear(user_role, user_text, chatbot):
76
- return None, None, []
77
-
78
-
79
- def get_image(ai_role):
80
- role_en = NAME_DICT[ai_role]
81
- return Image.open(f'images/{role_en}.jpg'), None, None, []
82
-
83
-
84
- with gr.Blocks() as demo:
85
- gr.Markdown(
86
- """
87
- # Chat凉宫春日 ChatHaruhi
88
- ## Reviving Anime Character in Reality via Large Language Model
89
-
90
- ChatHaruhi2.0的demo implemented by [Joey Yu](https://github.com/Tianjiao-Yu)
91
- """
92
- )
93
- with gr.Row():
94
- chatbot = gr.Chatbot()
95
- role_image = gr.Image(height=400, value="./images/haruhi.jpg")
96
- with gr.Row():
97
- user_role = gr.Textbox(label="user_role", scale=1)
98
- user_text = gr.Textbox(label="user_text", scale=20)
99
- with gr.Row():
100
- submit = gr.Button("Submit")
101
- clean = gr.ClearButton(value="Clear")
102
- ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
103
- 'Luna', '王多鱼', 'Ron', '鸠摩智',
104
- 'Snape', '凉宫春日', 'Malfoy', '虚竹',
105
- '萧峰', '段誉', 'Hermione', 'Dumbledore',
106
- '王语嫣',
107
- 'Harry', 'McGonagall',
108
- '白展堂', '佟湘玉', '郭芙蓉',
109
- '旅行者', '钟离', '胡桃',
110
- 'Sheldon', 'Raj', 'Penny',
111
- '韦小宝', '乔峰', '神里绫华',
112
- '雷电将军', '于谦'], label="characters", value='凉宫春日')
113
- ai_role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
114
- user_text.submit(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
115
- submit.click(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
116
- clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
117
- demo.launch(debug=True, share=True)
 
1
+ import zipfile
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from chatharuhi import ChatHaruhi
5
+ import requests
6
+ import os
7
+ import openai
8
+ import copy
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
+
22
+ try:
23
+ os.makedirs("characters_zip")
24
+ except:
25
+ pass
26
+ try:
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
+ max_retries = 3 # 最大重试次数
40
+ for attempt in range(1, max_retries+1):
41
+ response = requests.get(file_url)
42
+ if response.status_code == 200:
43
+ with open(destination_file, "wb") as file:
44
+ file.write(response.content)
45
+ print(ai_role_en)
46
+ break
47
+ else:
48
+ print(f"{ai_role_en}第{attempt}次下载失败")
49
+ # wget.download(file_url, destination_file) # 503
50
+ destination_folder = f"characters/{ai_role_en}"
51
+ with zipfile.ZipFile(destination_file, 'r') as zip_ref:
52
+ zip_ref.extractall(destination_folder)
53
+ db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
54
+ system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
55
+ ai_roles_obj[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
56
+ llm="openai",
57
+ story_db=db_folder,
58
+ verbose=True)
59
+
60
+
61
+ async def get_response(user_role, user_text, ai_role, chatbot):
62
+ role_en = NAME_DICT[ai_role]
63
+ ai_roles_obj[role_en].dialogue_history = copy.deepcopy(chatbot)
64
+ response = ai_roles_obj[role_en].chat(role=user_role, text=user_text)
65
+ user_msg = user_role + ':「' + user_text + '」'
66
+ latest_msg = (user_msg, response)
67
+ print(latest_msg)
68
+ chatbot.append(latest_msg)
69
+ return chatbot
70
+
71
+ async def respond(user_role, user_text, ai_role, chatbot):
72
+ return await get_response(user_role, user_text, ai_role, chatbot), None
73
+
74
+
75
+ def clear(user_role, user_text, chatbot):
76
+ return None, None, []
77
+
78
+
79
+ def get_image(ai_role):
80
+ role_en = NAME_DICT[ai_role]
81
+ return Image.open(f'images/{role_en}.jpg'), None, None, []
82
+
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown(
86
+ """
87
+ # Chat凉宫春日 ChatHaruhi
88
+ ## Reviving Anime Character in Reality via Large Language Model
89
+
90
+ ChatHaruhi2.0的demo implemented by [Joey Yu](https://github.com/Tianjiao-Yu)
91
+ """
92
+ )
93
+ with gr.Row():
94
+ chatbot = gr.Chatbot()
95
+ role_image = gr.Image(height=400, value="./images/haruhi.jpg")
96
+ with gr.Row():
97
+ user_role = gr.Textbox(label="user_role", scale=1)
98
+ user_text = gr.Textbox(label="user_text", scale=20)
99
+ with gr.Row():
100
+ submit = gr.Button("Submit")
101
+ clean = gr.ClearButton(value="Clear")
102
+ ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
103
+ 'Luna', '王多鱼', 'Ron', '鸠摩智',
104
+ 'Snape', '凉宫春日', 'Malfoy', '虚竹',
105
+ '萧峰', '段誉', 'Hermione', 'Dumbledore',
106
+ '王语嫣',
107
+ 'Harry', 'McGonagall',
108
+ '白展堂', '佟湘玉', '郭芙蓉',
109
+ '旅行者', '钟离', '胡桃',
110
+ 'Sheldon', 'Raj', 'Penny',
111
+ '韦小宝', '乔峰', '神里绫华',
112
+ '雷电将军', '于谦'], label="characters", value='凉宫春日')
113
+ ai_role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
114
+ user_text.submit(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
115
+ submit.click(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
116
+ clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
117
+ demo.launch(debug=False)