-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot_v2.py
124 lines (108 loc) · 4.37 KB
/
chatbot_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import gradio as gr
import random
import time
from tools import dialog, openai, utils, model, controller
name = 'Derek'
themes = [
# 'sudeepshouche/minimalist',
# 'bethecloud/storj_theme',
# 'ParityError/LimeFace',
# 'gradio/base',
# 'gradio/monochrome',
'derekzen/stardust'
]
with gr.Blocks(theme=random.choice(themes),
title='星尘小助手') as demo:
history = gr.State(dialog.system_prompt)
def bot(msg, history, task):
if not msg or msg.strip() == '':
print('received empty string')
print(f'history:\n{history}')
chatbot = openai.history2chat(history)
return '', history, chatbot
# add history
history.append({
"role": "user",
"content": msg,
"task": task
})
history.append({'role': 'assistant', 'content': ''})
chatbot = openai.history2chat(history)
queue = openai.create_chat(history, name)
start = time.time()
receiving = True
actions = {}
while receiving:
while queue is not None and len(queue):
content = queue.popleft()
if content == model.FINISH_TOKEN:
receiving = False
else:
history[-1]['content'] += content
start = time.time()
# yield result
chatbot = openai.history2chat(history)
chatbot[-1][1] = utils.filter_suggestion(history[-1]['content'])
yield '', history, chatbot
# timeout
if time.time() - start > model.TIMEOUT:
chatbot[-1][1] += '\n抱歉出了点问题,请重试...'
actions.update({'重试': controller.RETRY_ACTION})
history[-1]['actions'] = actions
receiving = False
yield '', history, chatbot
time.sleep(0.1)
def process_suggestions(history):
# suggestion
content = history[-1].get('content')
suggestions = []
if utils.SUGGESTION_TOKEN in content:
content, suggestions = controller.parse_suggestions(content)
history[-1]['suggestions'] = suggestions
history[-1]['content'] = content
print(f'suggestion: \n{suggestions}')
action_btns = [gr.Button.update(value=suggestion, visible=True) for suggestion in suggestions]
#actions
actions = history[-1].get('actions')
if actions:
for title, action in actions.items():
if action == controller.RETRY_ACTION:
action_btns.append(gr.Button.update(value=title, visible=True))
action_btns += [gr.Button.update(visible=False) for i in range(10-len(suggestions))]
return action_btns
def action(value):
if value == '重试':
bot_response = history.pop(-1)
user_input = history.pop(-1)
return user_input['content']
else:
return value
def on_select(evt: gr.SelectData): # SelectData is a subclass of EventData
return f"You selected 【{evt.value}】 at 【{evt.index}】 from 【{evt.target}】"
## interface
chatbot = gr.Chatbot(show_label=False)
with gr.Row():
with gr.Column(scale=0.1):
task = gr.Dropdown(['ChatGPT', 'GPT-4'], value='ChatGPT', show_label=False)
with gr.Column():
msg = gr.Textbox(placeholder='输入你的问题,然后按回车提交。', show_label=False)
with gr.Row():
action_btns = []
for i in range(10):
action_btn = gr.Button('对话区域', visible=False)
action_btn.click(action, action_btn, msg)
action_btns.append(action_btn)
new_chat = gr.Button("💬新对话")
save = gr.Button('💾保存')
statement = gr.Textbox('选中的文本')
## interactions
chatbot.select(on_select, None, statement)
msg.submit(bot, [msg, history, task], [msg, history, chatbot], show_progress=True)\
.then(process_suggestions, history, action_btns)\
.then(openai.history2chat, history, chatbot)
new_chat.click(lambda: [], [], chatbot, queue=False)
demo.queue(concurrency_count=16).launch(
show_api=False,
# share=True,
debug=True,
favicon_path='images/icon.ico')