Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import requests
|
|
5 |
from volcengine.maas import MaasService, MaasException, ChatRole
|
6 |
from collections import defaultdict
|
7 |
from copy import deepcopy
|
|
|
8 |
bot_settings=[
|
9 |
{
|
10 |
'bot_name':'保安A',
|
@@ -13,9 +14,6 @@ bot_settings=[
|
|
13 |
# 人物设定
|
14 |
你是保安A,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
|
15 |
|
16 |
-
## 故事背景
|
17 |
-
“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。
|
18 |
-
|
19 |
# 人物状态
|
20 |
健康、清醒
|
21 |
|
@@ -43,9 +41,6 @@ bot_settings=[
|
|
43 |
# 人物设定
|
44 |
你是保安B,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
|
45 |
|
46 |
-
## 故事背景
|
47 |
-
“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。
|
48 |
-
|
49 |
# 人物状态
|
50 |
健康、清醒
|
51 |
|
@@ -57,7 +52,7 @@ bot_settings=[
|
|
57 |
|
58 |
# 技能
|
59 |
- 很高的自来熟能力,善于和陌生人聊天,打探消息和情报,很容易摸清陌生人的底细
|
60 |
-
-
|
61 |
|
62 |
# 语言特点
|
63 |
你特别爱聊天,你的口头禅包括“帅哥你哪的人啊?”,“帅哥你是干什么的啊?”
|
@@ -74,7 +69,6 @@ chapter_name2chapter_info={
|
|
74 |
"stream":True,
|
75 |
'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。",
|
76 |
"context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。",
|
77 |
-
"main_context":"最近,我方情报机构得知大蛇组织在策划一个恐怖袭击计划,其威胁性足以撼动整个国家的安全。特工组织得知了这个计划的存在,他们决定派出他们最顶尖的特工,代号为“幻影”,来阻止这场灾难的发生。",
|
78 |
"task":"玩家需要打消保安的疑虑混入贵宾室**",
|
79 |
"condidations":'''
|
80 |
- 如果玩家成功获得VIP会员卡,且保安验证是真卡,则任务成功
|
@@ -90,6 +84,10 @@ chapter_name2chapter_info={
|
|
90 |
}
|
91 |
}
|
92 |
|
|
|
|
|
|
|
|
|
93 |
def select_bot(bot_name: str):
|
94 |
global bot_settings
|
95 |
for bot_setting in bot_settings:
|
@@ -100,7 +98,7 @@ def make_chat_history(history):
|
|
100 |
for part in history:
|
101 |
user, assistant = part
|
102 |
_type = 'user' if user is not None else 'assistant'
|
103 |
-
split=user.find(': ')
|
104 |
bot_name = user[:split] if user is not None else assistant[:split]
|
105 |
content=user[split:] if user is not None else assistant[split:]
|
106 |
chat_history.append({
|
@@ -110,7 +108,8 @@ def make_chat_history(history):
|
|
110 |
})
|
111 |
|
112 |
return chat_history
|
113 |
-
def respond( query, history, chapter, candidates, token):
|
|
|
114 |
chat_history=make_chat_history(history)
|
115 |
if chapter is None:
|
116 |
return "", []
|
@@ -120,26 +119,33 @@ def respond( query, history, chapter, candidates, token):
|
|
120 |
'Accept': 'text/event-stream',
|
121 |
'Authorization':f'Bearer {token}'
|
122 |
}
|
123 |
-
|
124 |
-
req_data=
|
125 |
req_data['chat_history']=chat_history
|
|
|
|
|
126 |
if query != "":
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
req_data['chat_history'].append({
|
128 |
-
'bot_name':
|
129 |
'type':'user',
|
130 |
'content':query
|
131 |
})
|
132 |
history.append([query, None])
|
133 |
yield "", history
|
134 |
|
135 |
-
req_data['bot_settings']=[
|
136 |
# 在线
|
137 |
ret=requests.get(url='http://scp1ceqsiv4mii2mol0qg.apigateway-cn-beijing.volceapi.com/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
138 |
# 离线
|
139 |
#ret=requests.get(url='http://0.0.0.0:8081/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
140 |
bot_contents = defaultdict(str)
|
141 |
content = ""
|
142 |
-
|
143 |
bot_name=''
|
144 |
for line in ret.iter_content(decode_unicode=True):
|
145 |
if content.endswith('\r\n\r\n'):
|
@@ -191,6 +197,7 @@ def clear_history(story_drop, chatbot):
|
|
191 |
chatbot = []
|
192 |
return chatbot
|
193 |
|
|
|
194 |
with gr.Blocks() as demo:
|
195 |
# 配置内容
|
196 |
with gr.Row():
|
@@ -199,14 +206,17 @@ with gr.Blocks() as demo:
|
|
199 |
with gr.Column():
|
200 |
description2 = gr.Markdown("<h2>新建章节</h2>")
|
201 |
new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1)
|
202 |
-
|
203 |
-
|
|
|
|
|
204 |
new_story_bnt = gr.Button(value="新建")
|
205 |
with gr.Column():
|
206 |
description2 = gr.Markdown("<h2>新建角色</h2>")
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
210 |
|
211 |
with gr.Row():
|
212 |
JWT_token = gr.TextArea(label='请输入你的JWT Token', lines=1, max_lines=1)
|
@@ -214,18 +224,96 @@ with gr.Blocks() as demo:
|
|
214 |
description3 = gr.Markdown("<h2>选择章节</h2>")
|
215 |
with gr.Row(equal_height=True):
|
216 |
with gr.Column():
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
with gr.Column():
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
with gr.Row():
|
221 |
with gr.Column(elem_id="chatbot", scale=2):
|
222 |
chatbot = gr.Chatbot()
|
|
|
223 |
with gr.Row():
|
224 |
chat_input = gr.Textbox(label="input")
|
225 |
|
226 |
clear = gr.Button(value="Clear chat hsitory")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
clear.click(clear_history, inputs=[story_drop, chatbot], outputs=[chatbot])
|
230 |
-
chat_input.submit(respond, inputs=[chat_input, chatbot, story_drop, candidates_drop, JWT_token], outputs=[chat_input, chatbot])
|
|
|
231 |
demo.launch()
|
|
|
5 |
from volcengine.maas import MaasService, MaasException, ChatRole
|
6 |
from collections import defaultdict
|
7 |
from copy import deepcopy
|
8 |
+
|
9 |
bot_settings=[
|
10 |
{
|
11 |
'bot_name':'保安A',
|
|
|
14 |
# 人物设定
|
15 |
你是保安A,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
|
16 |
|
|
|
|
|
|
|
17 |
# 人物状态
|
18 |
健康、清醒
|
19 |
|
|
|
41 |
# 人物设定
|
42 |
你是保安B,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
|
43 |
|
|
|
|
|
|
|
44 |
# 人物状态
|
45 |
健康、清醒
|
46 |
|
|
|
52 |
|
53 |
# 技能
|
54 |
- 很高的自来熟能力,善于和陌生人聊天,打探消息和情报,很容易摸清陌生人的底细
|
55 |
+
- 心思缜密,一旦遇到不对应的事情,就会不断询问
|
56 |
|
57 |
# 语言特点
|
58 |
你特别爱聊天,你的口头禅包括“帅哥你哪的人啊?”,“帅哥你是干什么的啊?”
|
|
|
69 |
"stream":True,
|
70 |
'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。",
|
71 |
"context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。",
|
|
|
72 |
"task":"玩家需要打消保安的疑虑混入贵宾室**",
|
73 |
"condidations":'''
|
74 |
- 如果玩家成功获得VIP会员卡,且保安验证是真卡,则任务成功
|
|
|
84 |
}
|
85 |
}
|
86 |
|
87 |
+
model_name2ep={
|
88 |
+
'Doubao-pro-4k 240515':'ep-20240514024405-7pf8j',
|
89 |
+
'Doubao-pro-4k character-240515':'ep-20240515122814-k9c6g'
|
90 |
+
}
|
91 |
def select_bot(bot_name: str):
|
92 |
global bot_settings
|
93 |
for bot_setting in bot_settings:
|
|
|
98 |
for part in history:
|
99 |
user, assistant = part
|
100 |
_type = 'user' if user is not None else 'assistant'
|
101 |
+
split=user.find(': ') if user is not None else assistant.find(': ')
|
102 |
bot_name = user[:split] if user is not None else assistant[:split]
|
103 |
content=user[split:] if user is not None else assistant[split:]
|
104 |
chat_history.append({
|
|
|
108 |
})
|
109 |
|
110 |
return chat_history
|
111 |
+
def respond( query, history, chapter, candidates, story_ep, chat_ep, token):
|
112 |
+
candidates = [json.loads(bot_info) for bot_info in candidates]
|
113 |
chat_history=make_chat_history(history)
|
114 |
if chapter is None:
|
115 |
return "", []
|
|
|
119 |
'Accept': 'text/event-stream',
|
120 |
'Authorization':f'Bearer {token}'
|
121 |
}
|
122 |
+
req_data_str=chapter
|
123 |
+
req_data=json.loads(req_data_str)
|
124 |
req_data['chat_history']=chat_history
|
125 |
+
req_data['task_ep']=story_ep
|
126 |
+
req_data['chat_ep']=chat_ep
|
127 |
if query != "":
|
128 |
+
user_name = '玩家'
|
129 |
+
for candidate in candidates:
|
130 |
+
if candidate['type'] == 'user':
|
131 |
+
user_name=candidate['bot_name']
|
132 |
+
break
|
133 |
+
|
134 |
req_data['chat_history'].append({
|
135 |
+
'bot_name':user_name,
|
136 |
'type':'user',
|
137 |
'content':query
|
138 |
})
|
139 |
history.append([query, None])
|
140 |
yield "", history
|
141 |
|
142 |
+
req_data['bot_settings']=[candidate for candidate in candidates]
|
143 |
# 在线
|
144 |
ret=requests.get(url='http://scp1ceqsiv4mii2mol0qg.apigateway-cn-beijing.volceapi.com/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
145 |
# 离线
|
146 |
#ret=requests.get(url='http://0.0.0.0:8081/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
147 |
bot_contents = defaultdict(str)
|
148 |
content = ""
|
|
|
149 |
bot_name=''
|
150 |
for line in ret.iter_content(decode_unicode=True):
|
151 |
if content.endswith('\r\n\r\n'):
|
|
|
197 |
chatbot = []
|
198 |
return chatbot
|
199 |
|
200 |
+
|
201 |
with gr.Blocks() as demo:
|
202 |
# 配置内容
|
203 |
with gr.Row():
|
|
|
206 |
with gr.Column():
|
207 |
description2 = gr.Markdown("<h2>新建章节</h2>")
|
208 |
new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1)
|
209 |
+
new_story_background = gr.TextArea(label="背景", lines=3, max_lines=3)
|
210 |
+
new_story_task = gr.TextArea(label="剧情任务", lines=1, max_lines=1)
|
211 |
+
new_story_task_condidation = gr.TextArea(label="任务条件", lines=1, max_lines=1)
|
212 |
+
|
213 |
new_story_bnt = gr.Button(value="新建")
|
214 |
with gr.Column():
|
215 |
description2 = gr.Markdown("<h2>新建角色</h2>")
|
216 |
+
new_bot_name = gr.TextArea(label="新建角色名", lines=1, max_lines=1)
|
217 |
+
new_bot_type = gr.Dropdown(choices=['user', 'assistant'], label='角色类型')
|
218 |
+
new_bot_content = gr.TextArea(label="新建角色内容", lines=3, max_lines=3)
|
219 |
+
new_bot_bnt = gr.Button(value="新建")
|
220 |
|
221 |
with gr.Row():
|
222 |
JWT_token = gr.TextArea(label='请输入你的JWT Token', lines=1, max_lines=1)
|
|
|
224 |
description3 = gr.Markdown("<h2>选择章节</h2>")
|
225 |
with gr.Row(equal_height=True):
|
226 |
with gr.Column():
|
227 |
+
with gr.Row(equal_height=True):
|
228 |
+
with gr.Column():
|
229 |
+
story_drop = gr.Dropdown([ [chapter, json.dumps(info)] for chapter, info in chapter_name2chapter_info.items()], label='确定章节', interactive=True)
|
230 |
+
|
231 |
+
with gr.Column():
|
232 |
+
story_ep=gr.Dropdown(label="故事模型",choices=[[k, v] for k , v in model_name2ep.items()])
|
233 |
+
with gr.Column():
|
234 |
+
story_display_btn=gr.Checkbox(label="显示剧情信息", value=False)
|
235 |
+
story_display = gr.TextArea( visible=False)
|
236 |
+
|
237 |
with gr.Column():
|
238 |
+
with gr.Row( equal_height=True):
|
239 |
+
with gr.Column():
|
240 |
+
candidates_drop = gr.Dropdown([[bot['bot_name'], json.dumps(bot)] for bot in bot_settings], multiselect=True, label='请确定参与对话的角色', interactive=True)
|
241 |
+
with gr.Column():
|
242 |
+
chat_ep=gr.Dropdown(label="角色模型",choices=[[k, v] for k , v in model_name2ep.items()])
|
243 |
+
with gr.Column():
|
244 |
+
candidates_display_btn=gr.Checkbox(label="显示角色信息",value=False)
|
245 |
+
candidates_display=gr.TextArea(visible=False)
|
246 |
with gr.Row():
|
247 |
with gr.Column(elem_id="chatbot", scale=2):
|
248 |
chatbot = gr.Chatbot()
|
249 |
+
|
250 |
with gr.Row():
|
251 |
chat_input = gr.Textbox(label="input")
|
252 |
|
253 |
clear = gr.Button(value="Clear chat hsitory")
|
254 |
+
def add_story(title, background, task, condidations):
|
255 |
+
choices=story_drop.choices
|
256 |
+
choices.append([
|
257 |
+
title,
|
258 |
+
json.dumps({
|
259 |
+
'background':background,
|
260 |
+
'task':task,
|
261 |
+
'condidations':condidations
|
262 |
+
})
|
263 |
+
])
|
264 |
+
return gr.Dropdown(choices=choices, interactive=True)
|
265 |
+
def add_bot(bot_name, new_bot_type, new_bot_content):
|
266 |
+
choices = candidates_drop.choices
|
267 |
+
choices.append([
|
268 |
+
bot_name,
|
269 |
+
json.dumps({
|
270 |
+
'bot_name':bot_name,
|
271 |
+
'type':new_bot_type,
|
272 |
+
'content':new_bot_content
|
273 |
+
})
|
274 |
+
])
|
275 |
+
return gr.Dropdown(choices=choices, interactive=True, multiselect=True)
|
276 |
+
def story_display_func(story_display_btn, story_drop):
|
277 |
+
if story_drop is None:
|
278 |
+
return ""
|
279 |
+
story_info = story_drop
|
280 |
+
story_info=json.loads(story_info)
|
281 |
+
text=gr.Textbox(value=f'''
|
282 |
+
## 背景
|
283 |
+
{story_info['background']}
|
284 |
+
## 任务
|
285 |
+
{story_info['task']}
|
286 |
+
## 任务条件
|
287 |
+
{story_info['condidations']}
|
288 |
+
'''
|
289 |
+
, visible=story_display_btn)
|
290 |
+
return text
|
291 |
+
def candidates_display_func(candidates_display_btn, candidates_drop):
|
292 |
+
if candidates_drop is None:
|
293 |
+
return ""
|
294 |
+
display=[
|
295 |
+
|
296 |
+
]
|
297 |
+
for candidates in candidates_drop:
|
298 |
+
bot_info = json.loads(candidates)
|
299 |
+
display.append(f'''
|
300 |
+
角色名:{bot_info['bot_name']}
|
301 |
+
角色类型:{bot_info['type']}
|
302 |
+
角色信息:{bot_info['content']}
|
303 |
+
''')
|
304 |
|
305 |
+
text=gr.Textbox(value='####'.join(display)
|
306 |
+
, visible=candidates_display_btn)
|
307 |
+
return text
|
308 |
+
|
309 |
+
story_drop.change(story_display_func, [story_display_btn, story_drop], [story_display])
|
310 |
+
candidates_drop.change(candidates_display_func, [candidates_display_btn, candidates_drop], [candidates_display])
|
311 |
+
story_display_btn.change(story_display_func, [story_display_btn, story_drop], [story_display])
|
312 |
+
candidates_display_btn.change(candidates_display_func, [candidates_display_btn, candidates_drop], [candidates_display])
|
313 |
+
|
314 |
+
new_story_bnt.click(add_story, [new_story_title, new_story_background, new_story_task, new_story_task_condidation], [story_drop])
|
315 |
+
new_bot_bnt.click( add_bot, [new_bot_name, new_bot_type, new_bot_content], [candidates_drop])
|
316 |
clear.click(clear_history, inputs=[story_drop, chatbot], outputs=[chatbot])
|
317 |
+
chat_input.submit(respond, inputs=[chat_input, chatbot, story_drop, candidates_drop, story_ep, chat_ep, JWT_token], outputs=[chat_input, chatbot])
|
318 |
+
|
319 |
demo.launch()
|