-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_game.py
298 lines (269 loc) · 11.4 KB
/
app_game.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import json
import gradio as gr
from gradio.components import Chatbot
import threading
import agentscope
from agentscope.agents import DialogAgent
from agentscope.agents.user_agent import UserAgent
from agentscope.message import Msg
from utils import format_welcome_html
import utils
from dotenv import find_dotenv, load_dotenv
_ = load_dotenv(find_dotenv())
uid = threading.current_thread().name
tutor_avatar = 'assets/tutor.png'
summarize_avatar = 'assets/summarize.png'
extract_avatar = 'assets/extract.png'
design_avatar = 'assets/design.png'
painter_avatar = 'assets/painter.png'
product_avatar = 'assets/product.png'
user_avatar = 'assets/user.png'
requirement = 'default'
i = 0
j = 0
history = ''
tutor = True
extracted_msg = ''
def init_game(state):
model_configs = json.load(open('configs/model_configs.json', 'r'))
model_configs[0]["api_key"] = os.environ.get("DASHSCOPE_API_KEY")
model_configs[1]["api_key"] = os.environ.get("DASHSCOPE_API_KEY")
agents = agentscope.init(
model_configs=model_configs,
agent_configs="configs/agent_configs_poem.json",
)
user_agent = UserAgent()
state['tutor_agent'] = agents[0]
state['summarize_agent'] = agents[1]
state['extract_agent'] = agents[2]
state['design_agent'] = agents[3]
state['painter'] = agents[4]
state['product_agent'] = agents[5]
return state
# 创建 Gradio 界面
demo = gr.Blocks(css='assets/appBot.css')
with demo:
warning_html_code = """
<div class="hint" style="background-color: rgba(255, 255, 0, 0.15); padding: 10px; margin: 10px 0; border-radius: 5px; border: 1px solid #ffcc00;">
<p>\N{fire} Powered by <a href="https://github.com/modelscope/agentscope">AgentScope</a></p>
</div>
"""
gr.HTML(warning_html_code)
state = gr.State({'session_seed': uid})
tabs = gr.Tabs(visible=True)
with tabs:
welcome_tab = gr.Tab('软件介绍', id=0)
with welcome_tab:
user_chat_bot_cover = gr.HTML(format_welcome_html())
with gr.Row():
new_button = gr.Button(value='🚀开始设计', variant='primary')
game_tabs = gr.Tabs(visible=False)
with game_tabs:
main_tab = gr.Tab('主界面', id=0)
with main_tab:
with gr.Row():
with gr.Column(min_width=270):
user_chatbot = gr.Chatbot(
elem_classes="app-chatbot",
avatar_images=[summarize_avatar, design_avatar, extract_avatar],
label="记录和提示区",
show_label=True,
bubble_full_width=False,
)
with gr.Column(min_width=270):
user_chatsys = gr.Chatbot(
value=[[
'您好,欢迎来到产品设计大师,先由我们专业的销售导购和您交流您的需求,你只需要做出简单选择即可,输入任意字符开始',
None]],
elem_classes="app-chatbot",
avatar_images=[tutor_avatar, user_avatar, product_avatar],
label="系统栏",
show_label=True,
bubble_full_width=False,
)
with gr.Row():
with gr.Column(scale=12):
user_chat_input = gr.Textbox(
label='user_chat_input',
show_label=False,
placeholder='你的想法是?输入exit/null退出当前会话')
with gr.Column(min_width=70, scale=1):
send_button = gr.Button('📣发送', variant='primary')
with gr.Row():
return_welcome_button = gr.Button(value="↩️返回首页")
with gr.Row():
image_preview = gr.Image('assets/logo.png', width=300) # 设置图片路径和宽度
def game_ui():
return {tabs: gr.update(visible=False), game_tabs: gr.update(visible=True)}
def welcome_ui():
return {tabs: gr.update(visible=True), game_tabs: gr.update(visible=False)}
def send_message(chatbot, chatsys, user_input, _state):
# 将发送的消息添加到聊天历史
global tutor
global history
global extracted_msg
global i
global j
tutor_agent = _state['tutor_agent']
summarize_agent = _state['summarize_agent']
extract_agent = _state['extract_agent']
design_agent = _state['design_agent']
painter = _state['painter']
product_agent = _state['product_agent']
chatsys.append((None, user_input))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
user_chat_input: '',
}
if tutor:
if 'exit' in user_input or len(user_input) == 0:
tutor = False
if i > 1:
print("here is the summarize part")
chatbot.append(("下面由总结助手进行总结,请稍后。。。", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
if len(history) > 5000:
history = history[-5000:]
summarize_msg = summarize_agent(history)
chatbot.append((None, f"{summarize_msg.content}"))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
print("here is the extract part")
chatbot.append(("进行关键词抽取,请稍后。。。", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
extract_msg = extract_agent(summarize_msg)
chatbot.append((f"{extract_msg.content}", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
requirement = extract_msg.content
else:
print("using default")
chatsys.append((f"""需求信息不完全""", None))
requirement = "白色,地中海风格"
chatbot.append((f"""下面使用缺省样式:{requirement}进行设计。。。""", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
extracted_msg = requirement
print("extracted-msg: ",extracted_msg)
print("here is the prompt part")
msg = Msg(
name="user",
role="user",
content="儿童学习桌椅," + requirement
)
chatbot.append(("生成英文提示词。。。", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
design_msg = design_agent(msg)
chatbot.append((f"{design_msg.content}", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
chatbot.append(("下面设计师根据您的需求进行图片生成,耗时较长,请您耐心等候。。。", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
xx = painter(design_msg)
image_url = xx.url[0]
# xmsg = Msg(
# name="user",
# role="user",
# content=xx.content,
# url=image_url,
# )
# chatbot.append((xmsg, None))
chatbot.append((None, f"""生成的图片如下:\n
<a href="{image_url}">
<img src="{image_url}" alt="{image_url}">
</a>
"""))
# f"""<img src="{image_url}"></img>"""
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
else:
tutor_msg = tutor_agent(user_input)
# tutor_msg.role = "assistant"
history = history + tutor_msg.content
chatsys.append((f"{tutor_msg.content}", None))
i = i + 1
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
else:
if 'exit' in user_input or len(user_input) == 0:
print("here is the finish part")
chatsys.append(("谢谢您的使用,有任何建议请联系:[email protected]", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
else:
j = j + 1
if j > 1:
if "开始" in user_input or "重新" in user_input:
tutor = True
#product_msg = product_agent("再见")
#chatsys.append((f"{product_msg.content}", None))
chatsys.append(("让我们再次启动设计流程!", None))
tutor_msg = product_agent("再见")
chatsys.append((f"{tutor_msg.content}", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
else:
product_msg = product_agent(user_input+extracted_msg)
chatsys.append((f"{product_msg.content}", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
else:
chatsys.append((
"下面由我们的产品经理为您服务和答疑,请您输入您的问题交流。您也可以通过关键词'重新','开始'重新回到导购环节",
None))
print("user_input last is", user_input)
product_msg = product_agent("你好")
chatsys.append((f"{product_msg.content}", None))
yield {
user_chatbot: chatbot,
user_chatsys: chatsys,
}
# change ui
new_button.click(game_ui, outputs=[tabs, game_tabs])
return_welcome_button.click(welcome_ui, outputs=[tabs, game_tabs])
# submit message
send_button.click(
send_message,
inputs=[user_chatbot, user_chatsys, user_chat_input, state],
outputs=[user_chatbot, user_chatsys, user_chat_input]
)
user_chat_input.submit(
send_message,
inputs=[user_chatbot, user_chatsys, user_chat_input, state],
outputs=[user_chatbot, user_chatsys, user_chat_input]
)
demo.load(init_game, inputs=[state], outputs=[state])
demo.queue()
demo.launch(share=True)