chenxiYan commited on
Commit
b5dfa38
1 Parent(s): ae72434

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -22
app.py CHANGED
@@ -5,8 +5,12 @@ from chatharuhi import ChatHaruhi
5
  import wget
6
  import os
7
  import openai
8
- import asyncio
9
- import anyio
 
 
 
 
10
 
11
 
12
  NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
@@ -49,26 +53,23 @@ for ai_role_en in NAME_DICT.values():
49
  verbose=True)
50
 
51
 
52
- async def get_response(user_name, user_text, role, chatbot):
53
- role_en = NAME_DICT[role]
54
- ai_roles_obj[role_en].dialogue_history = chatbot
55
- response = ai_roles_obj[role_en].chat(role=user_name, text=user_text)
56
- user_msg = user_name + ':「' + user_text + '」'
57
  chatbot.append((user_msg, response))
58
- print("history:", chatbot)
59
- print("flag")
60
- return chatbot
61
 
62
- async def respond(user_name, user_text, role, chatbot):
63
- return await get_response(user_name, user_text, role, chatbot), None
64
 
65
 
66
- def clear(user_name, user_text, chatbot):
67
  return None, None, []
68
 
69
 
70
- def get_image(role):
71
- role_en = NAME_DICT[role]
72
  return Image.open(f'images/{role_en}.jpg'), None, None, []
73
 
74
 
@@ -91,12 +92,12 @@ with gr.Blocks() as demo:
91
  chatbot = gr.Chatbot()
92
  role_image = gr.Image(height=400, value="./images/haruhi.jpg")
93
  with gr.Row():
94
- user_name = gr.Textbox(label="user_role")
95
  user_text = gr.Textbox(label="user_text")
96
  with gr.Row():
97
  submit = gr.Button("Submit")
98
  clean = gr.ClearButton(value="Clear")
99
- role = gr.Radio(['汤师爷', '慕容复', '李云龙',
100
  'Luna', '王多鱼', 'Ron', '鸠摩智',
101
  'Snape', '凉宫春日', 'Malfoy', '虚竹',
102
  '萧峰', '段誉', 'Hermione', 'Dumbledore',
@@ -107,8 +108,8 @@ with gr.Blocks() as demo:
107
  'Sheldon', 'Raj', 'Penny',
108
  '韦小宝', '乔峰', '神里绫华',
109
  '雷电将军', '于谦'], label="characters", value='凉宫春日')
110
- role.change(get_image, role, [role_image, user_name, user_text, chatbot])
111
- user_text.submit(fn=respond, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
112
- submit.click(fn=respond, inputs=[user_name, user_text, role, chatbot], outputs=[chatbot, user_text])
113
- clean.click(clear, [user_name, user_text, chatbot], [user_name, user_text, chatbot])
114
- demo.launch(debug=True)
 
5
  import wget
6
  import os
7
  import openai
8
+ import copy
9
+
10
+ openai.api_key = "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"
11
+ os.environ["OPENAI_API_KEY"] = openai.api_key
12
+
13
+
14
 
15
 
16
  NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
 
53
  verbose=True)
54
 
55
 
56
+ async def get_response(user_role, user_text, ai_role, chatbot):
57
+ role_en = NAME_DICT[ai_role]
58
+ ai_roles_obj[role_en].dialogue_history = copy.deepcopy(chatbot)
59
+ response = ai_roles_obj[role_en].chat(role=user_role, text=user_text)
60
+ user_msg = user_role + ':「' + user_text + '」'
61
  chatbot.append((user_msg, response))
 
 
 
62
 
63
+ async def respond(user_role, user_text, ai_role, chatbot):
64
+ return await get_response(user_role, user_text, ai_role, chatbot), None
65
 
66
 
67
+ def clear(user_role, user_text, chatbot):
68
  return None, None, []
69
 
70
 
71
+ def get_image(ai_role):
72
+ role_en = NAME_DICT[ai_role]
73
  return Image.open(f'images/{role_en}.jpg'), None, None, []
74
 
75
 
 
92
  chatbot = gr.Chatbot()
93
  role_image = gr.Image(height=400, value="./images/haruhi.jpg")
94
  with gr.Row():
95
+ user_role = gr.Textbox(label="user_role")
96
  user_text = gr.Textbox(label="user_text")
97
  with gr.Row():
98
  submit = gr.Button("Submit")
99
  clean = gr.ClearButton(value="Clear")
100
+ ai_role = gr.Radio(['汤师爷', '慕容复', '李云龙',
101
  'Luna', '王多鱼', 'Ron', '鸠摩智',
102
  'Snape', '凉宫春日', 'Malfoy', '虚竹',
103
  '萧峰', '段誉', 'Hermione', 'Dumbledore',
 
108
  'Sheldon', 'Raj', 'Penny',
109
  '韦小宝', '乔峰', '神里绫华',
110
  '雷电将军', '于谦'], label="characters", value='凉宫春日')
111
+ ai_role.change(get_image, ai_role, [role_image, user_role, user_text, chatbot])
112
+ user_text.submit(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
113
+ submit.click(fn=respond, inputs=[user_role, user_text, ai_role, chatbot], outputs=[chatbot, user_text])
114
+ clean.click(clear, [user_role, user_text, chatbot], [user_role, user_text, chatbot])
115
+ demo.launch(debug=True, share=True)