Update app.py
Browse files
app.py
CHANGED
@@ -1,378 +1,298 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import random
|
3 |
-
import time
|
4 |
-
|
5 |
-
css = '''
|
6 |
-
.show img {height: 320px}
|
7 |
-
.result img {height: 320px}
|
8 |
-
.qrcode img {height: 225px}
|
9 |
-
.btn {
|
10 |
-
justify-content: normal
|
11 |
-
}
|
12 |
-
'''
|
13 |
-
|
14 |
-
def get_new_question_and_options():
|
15 |
-
time.sleep(1.5)
|
16 |
-
number = random.randint(1,65536)
|
17 |
-
return {
|
18 |
-
"question_text": f"问题{number}是这样的。",
|
19 |
-
"option_A": f"问题{number}给出的选项A,吃饭",
|
20 |
-
"option_B": f"问题{number}给出的选项B,睡觉",
|
21 |
-
"option_C": f"问题{number}给出的选项C,打豆豆",
|
22 |
-
}
|
23 |
-
|
24 |
-
def get_option_result(question_text, option_text):
|
25 |
-
time.sleep(1.5)
|
26 |
-
numbers = [
|
27 |
-
random.randint(-10,10),
|
28 |
-
random.randint(-10,10),
|
29 |
-
random.randint(-10,10),
|
30 |
-
random.randint(-10,10)
|
31 |
-
]
|
32 |
-
|
33 |
-
return {
|
34 |
-
"result_text": f"问题“{question_text}”执行“{option_text}”选项,结果完成了",
|
35 |
-
"score_text": f"导致四个维度增减情况为 {numbers[0]} {numbers[1]} {numbers[2]} {numbers[3]}",
|
36 |
-
"deltas": numbers
|
37 |
-
}
|
38 |
-
|
39 |
-
def typePUA(a):
|
40 |
-
|
41 |
-
x = '0' if a[0] <= 50 else '1'
|
42 |
-
y = '0' if a[1] <= 50 else '1'
|
43 |
-
z = '0' if a[2] <= 50 else '1'
|
44 |
-
w = '0' if a[3] <= 50 else '1'
|
45 |
-
|
46 |
-
puatype = ''.join([x,y,z,w])
|
47 |
-
|
48 |
-
pua = {
|
49 |
-
"0000": "1 反PUA斗士",
|
50 |
-
"0001": "2",
|
51 |
-
"0010": "3",
|
52 |
-
"0011": "4",
|
53 |
-
"0100": "5",
|
54 |
-
"0101": "6",
|
55 |
-
"0110": "7",
|
56 |
-
"0111": "8",
|
57 |
-
"1000": "9",
|
58 |
-
"1001": "10",
|
59 |
-
"1010": "11",
|
60 |
-
"1011": "12",
|
61 |
-
"1100": "13",
|
62 |
-
"1101": "14",
|
63 |
-
"1110": "15",
|
64 |
-
"1111": "16 天生牛马打工人",
|
65 |
-
}
|
66 |
-
|
67 |
-
return pua[puatype]
|
68 |
-
|
69 |
-
with gr.Blocks(css=css).queue() as demo:
|
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 |
-
return qa["question_text"] , qa["result_text"], qa["score_text"], qa["options"][3], index, qa, result
|
299 |
-
else:
|
300 |
-
qa["question_text"] = ""
|
301 |
-
qa["result_text"] = ""
|
302 |
-
qa["score_text"] = ""
|
303 |
-
qa["options"][0] = ""
|
304 |
-
qa["options"][1] = ""
|
305 |
-
qa["options"][2] = ""
|
306 |
-
qa["options"][3] = ""
|
307 |
-
qa["canOptionA"] = True
|
308 |
-
qa["canOptionB"] = True
|
309 |
-
qa["canOptionC"] = True
|
310 |
-
qa["canOptionD"] = False
|
311 |
-
qa["canFillD"] = True
|
312 |
-
qa["canRestart"] = True
|
313 |
-
qa["canRefresh"] = True
|
314 |
-
qa["canNext"] = False
|
315 |
-
index = 2
|
316 |
-
return "", "", "", "", index, qa, result
|
317 |
-
|
318 |
-
restart.click(onRestart, None, page_index)
|
319 |
-
refresh.click(onRefresh, None, [question_text, result_text, score_text, option_D_text, qa_data, result_data])
|
320 |
-
next.click(onNext, None, [question_text, result_text, score_text, option_D_text, page_index, qa_data, result_data])
|
321 |
-
|
322 |
-
# 2 - 结果页面
|
323 |
-
else:
|
324 |
-
gr.Markdown("### 测评结果")
|
325 |
-
with gr.Row():
|
326 |
-
with gr.Column():
|
327 |
-
gr.Image(elem_classes="result", value="./src/home.png", label="测评结果图示", height=400, interactive=False)
|
328 |
-
gr.Markdown("最终分数")
|
329 |
-
with gr.Group():
|
330 |
-
gr.Slider(label="维度1", value=result["score"][0], minimum=0, maximum=100, step=1, interactive=False)
|
331 |
-
gr.Slider(label="维度2", value=result["score"][1], minimum=0, maximum=100, step=1, interactive=False)
|
332 |
-
gr.Slider(label="维度3", value=result["score"][2], minimum=0, maximum=100, step=1, interactive=False)
|
333 |
-
gr.Slider(label="维度4", value=result["score"][3], minimum=0, maximum=100, step=1, interactive=False)
|
334 |
-
with gr.Column():
|
335 |
-
with gr.Row():
|
336 |
-
gr.Number(label="您的被PUA指数", value=sum(result["score"])/4, interactive=False)
|
337 |
-
gr.Textbox(label="您的被PUA类型", value=typePUA(result["score"]), interactive=False, placeholder="测评结束后生成")
|
338 |
-
gr.TextArea(label="您的测评结果解释", placeholder="测评结束后生成", interactive=False, lines=6)
|
339 |
-
gr.TextArea(label="可能的解决方案", placeholder="测评结束后生成", interactive=False, lines=14)
|
340 |
-
restart = gr.Button(value="↩️ 重新开始", interactive=qa["canRestart"])
|
341 |
-
|
342 |
-
def onRestart(index=index):
|
343 |
-
index = 0
|
344 |
-
return index
|
345 |
-
|
346 |
-
restart.click(onRestart, None, page_index)
|
347 |
-
|
348 |
-
gr.Button(value="如果对您有用,欢迎分享网站与测评结果 🥰", variant="primary")
|
349 |
-
|
350 |
-
# # 历史测评结果
|
351 |
-
# with gr.Tab("历史"):
|
352 |
-
# pass
|
353 |
-
|
354 |
-
# # 测评原理解释
|
355 |
-
# with gr.Tab("说明"):
|
356 |
-
# pass
|
357 |
-
|
358 |
-
# 作者声明
|
359 |
-
with gr.Tab("关于"):
|
360 |
-
gr.Markdown("")
|
361 |
-
gr.Markdown("**Datawhale AI夏令营(第四期)x 浪潮信息 “源”大模型应用开发活动**")
|
362 |
-
gr.Markdown("---")
|
363 |
-
gr.Markdown("**项目名称**:别再PUA啦:基于多智能体的被PUA指数心理测评游戏")
|
364 |
-
gr.Markdown("**项目Slogan**:机智选择,远离PUA")
|
365 |
-
gr.Markdown("**项目简介**:“别再PUA啦”是一款创新的心理测评游戏,它基于多智能体技术,旨在通过互动游戏的形式,测量个体对PUA策略的抵抗力和识别能力,从而帮助用户提高自我保护意识和社交技巧。")
|
366 |
-
gr.Markdown("**项目技术栈**:Yuan 2.0,RAG,Langchain,Muiti-Agent, Gradio")
|
367 |
-
gr.Markdown("**项目演示视频**:【TODO】")
|
368 |
-
gr.Markdown("**项目构建分析与复盘博客**:【TODO】")
|
369 |
-
gr.Markdown("---")
|
370 |
-
gr.Markdown("**作者**:胡钧耀(南开大学计算机视觉在读一年级直博生,个人研究兴趣方向为视觉AIGC)")
|
371 |
-
gr.Markdown("**联系方式**:微信(LittleDream_hjy),[Bilibili](https://space.bilibili.com/2042113),[GitHub](https://github.com/JunyaoHu),[个人主页](https://junyaohu.github.io)")
|
372 |
-
with gr.Row():
|
373 |
-
gr.Image(elem_classes="qrcode", value="./src/weixin.png", label="微信", height=250)
|
374 |
-
gr.Image(elem_classes="qrcode", value="./src/zanshang.png", label="感谢支持", height=250)
|
375 |
-
|
376 |
-
if __name__ == "__main__":
|
377 |
-
# demo.launch()
|
378 |
demo.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
|
5 |
+
css = '''
|
6 |
+
.show img {height: 320px}
|
7 |
+
.result img {height: 320px}
|
8 |
+
.qrcode img {height: 225px}
|
9 |
+
.btn {
|
10 |
+
justify-content: normal
|
11 |
+
}
|
12 |
+
'''
|
13 |
+
|
14 |
+
def get_new_question_and_options():
|
15 |
+
# time.sleep(1.5)
|
16 |
+
number = random.randint(1,65536)
|
17 |
+
return {
|
18 |
+
"question_text": f"问题{number}是这样的。",
|
19 |
+
"option_A": f"问题{number}给出的选项A,吃饭",
|
20 |
+
"option_B": f"问题{number}给出的选项B,睡觉",
|
21 |
+
"option_C": f"问题{number}给出的选项C,打豆豆",
|
22 |
+
}
|
23 |
+
|
24 |
+
def get_option_result(question_text, option_text):
|
25 |
+
# time.sleep(1.5)
|
26 |
+
numbers = [
|
27 |
+
random.randint(-10,10),
|
28 |
+
random.randint(-10,10),
|
29 |
+
random.randint(-10,10),
|
30 |
+
random.randint(-10,10)
|
31 |
+
]
|
32 |
+
|
33 |
+
return {
|
34 |
+
"result_text": f"问题“{question_text}”执行“{option_text}”选项,结果完成了",
|
35 |
+
"score_text": f"导致四个维度增减情况为 {numbers[0]} {numbers[1]} {numbers[2]} {numbers[3]}",
|
36 |
+
"deltas": numbers
|
37 |
+
}
|
38 |
+
|
39 |
+
def typePUA(a):
|
40 |
+
|
41 |
+
x = '0' if a[0] <= 50 else '1'
|
42 |
+
y = '0' if a[1] <= 50 else '1'
|
43 |
+
z = '0' if a[2] <= 50 else '1'
|
44 |
+
w = '0' if a[3] <= 50 else '1'
|
45 |
+
|
46 |
+
puatype = ''.join([x,y,z,w])
|
47 |
+
|
48 |
+
pua = {
|
49 |
+
"0000": "1 反PUA斗士",
|
50 |
+
"0001": "2",
|
51 |
+
"0010": "3",
|
52 |
+
"0011": "4",
|
53 |
+
"0100": "5",
|
54 |
+
"0101": "6",
|
55 |
+
"0110": "7",
|
56 |
+
"0111": "8",
|
57 |
+
"1000": "9",
|
58 |
+
"1001": "10",
|
59 |
+
"1010": "11",
|
60 |
+
"1011": "12",
|
61 |
+
"1100": "13",
|
62 |
+
"1101": "14",
|
63 |
+
"1110": "15",
|
64 |
+
"1111": "16 天生牛马打工人",
|
65 |
+
}
|
66 |
+
|
67 |
+
return pua[puatype]
|
68 |
+
|
69 |
+
with gr.Blocks(css=css).queue() as demo:
|
70 |
+
|
71 |
+
data = gr.State({
|
72 |
+
"total": 3,
|
73 |
+
"isComfirmed": False,
|
74 |
+
"canOptionA": True,
|
75 |
+
"canOptionB": True,
|
76 |
+
"canOptionC": True,
|
77 |
+
"canOptionD": False,
|
78 |
+
"canFillD": True,
|
79 |
+
"canRestart": True,
|
80 |
+
"canRefresh": True,
|
81 |
+
"canNext": False,
|
82 |
+
})
|
83 |
+
|
84 |
+
gr.Markdown("# 😅别再PUA啦:基于多智能体的被PUA指数心理测评游戏")
|
85 |
+
gr.Markdown("### 😋机智选择,远离PUA😈")
|
86 |
+
|
87 |
+
with gr.Tab("测评"):
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
|
93 |
+
hello = gr.Markdown("👇 请输入昵称")
|
94 |
+
with gr.Group():
|
95 |
+
with gr.Row():
|
96 |
+
user_name = gr.Textbox(placeholder="输入您的昵称", value="", container=False, scale=6)
|
97 |
+
confirm = gr.Button(value="确认", scale=1)
|
98 |
+
|
99 |
+
progress_slider = gr.Slider(label=f"测评进度(共{data.value['total']}题)", value=0, minimum=0, maximum=data.value["total"], step=1, interactive=False)
|
100 |
+
|
101 |
+
with gr.Column():
|
102 |
+
gr.Markdown("当前指数")
|
103 |
+
with gr.Group():
|
104 |
+
with gr.Row():
|
105 |
+
dim1_slider = gr.Slider(label="维度1", value=50, minimum=0, maximum=100, step=1, interactive=False)
|
106 |
+
dim2_slider = gr.Slider(label="维度2", value=50, minimum=0, maximum=100, step=1, interactive=False)
|
107 |
+
with gr.Row():
|
108 |
+
dim3_slider = gr.Slider(label="维度3", value=50, minimum=0, maximum=100, step=1, interactive=False)
|
109 |
+
dim4_slider = gr.Slider(label="维度4", value=50, minimum=0, maximum=100, step=1, interactive=False)
|
110 |
+
|
111 |
+
gr.Markdown("---")
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
|
115 |
+
with gr.Column():
|
116 |
+
question_text = gr.TextArea(label="事件背景", value="", placeholder="请等待生成", interactive=False, lines=4)
|
117 |
+
result_text = gr.TextArea(label="事件结果", value="", placeholder="请根据事件背景,结合个人实际情况做出选择。注意,选择之间并无对错之分。", interactive=False, lines=3)
|
118 |
+
score_text = gr.TextArea(label="指数变更", value="", placeholder="请根据事件背景,结合个人实际情况做出选择。注意,选择之间并无对错之分。", interactive=False, lines=3)
|
119 |
+
|
120 |
+
with gr.Column():
|
121 |
+
option_A_button = gr.Button(value=f"A: ", elem_classes="btn", interactive=False)
|
122 |
+
option_B_button = gr.Button(value=f"B: ", elem_classes="btn", interactive=False)
|
123 |
+
option_C_button = gr.Button(value=f"C: ", elem_classes="btn", interactive=False)
|
124 |
+
|
125 |
+
with gr.Group():
|
126 |
+
option_D_button = gr.Button(value="D: 我选这个", elem_classes="btn", interactive=False)
|
127 |
+
option_D_text = gr.Textbox(show_label=False, value="", placeholder="我有独特的主见", container=False, interactive=False)
|
128 |
+
|
129 |
+
|
130 |
+
with gr.Column():
|
131 |
+
|
132 |
+
restart = gr.Button(value="▶️ 开始", interactive=False, variant="primary")
|
133 |
+
refresh = gr.Button(value="🔄️ 刷新", interactive=False)
|
134 |
+
next_btn = gr.Button(value="➡️ 下一题", interactive=False)
|
135 |
+
|
136 |
+
gr.Markdown("---")
|
137 |
+
gr.Markdown("### 测评结果")
|
138 |
+
with gr.Row():
|
139 |
+
gr.Image(elem_classes="result", value="./src/home.png", label="测评结果图示", interactive=False, scale=0.8)
|
140 |
+
with gr.Group():
|
141 |
+
with gr.Row():
|
142 |
+
gr.Number(label="您的被PUA指数", value=50, interactive=False)
|
143 |
+
gr.Textbox(label="您的被PUA类型", value="测评结束后生成", interactive=False, placeholder="测评结束后生成")
|
144 |
+
gr.TextArea(label="您的测评结果解释", placeholder="测评结束后生成", interactive=False, lines=5)
|
145 |
+
gr.TextArea(label="可能的解决方案", placeholder="测评结束后生成", interactive=False, lines=5)
|
146 |
+
|
147 |
+
gr.Button(value="如果对您有用,欢迎分享网站与测评结果 🥰", variant="primary")
|
148 |
+
|
149 |
+
# 历史测评结果
|
150 |
+
with gr.Tab("历史"):
|
151 |
+
pass
|
152 |
+
|
153 |
+
# 测评原理解释
|
154 |
+
with gr.Tab("说明"):
|
155 |
+
pass
|
156 |
+
|
157 |
+
# 作者声明
|
158 |
+
with gr.Tab("关于"):
|
159 |
+
gr.Markdown("")
|
160 |
+
gr.Markdown("**Datawhale AI夏令营(第四期)x 浪潮信息 “源”大模型应用开发活动**")
|
161 |
+
gr.Markdown("---")
|
162 |
+
gr.Markdown("**项目名称**:别再PUA啦:基于多智能体的被PUA指数心理测评游戏")
|
163 |
+
gr.Markdown("**项目Slogan**:机智选择,远离PUA")
|
164 |
+
gr.Markdown("**项目简介**:“别再PUA啦”是一款创新的心理测评游戏,它基于多智能体技术,旨在通过互动游戏的形式,测量个体对PUA策略的抵抗力和识别能力,从而帮助用户提高自我保护意识和社交技巧。")
|
165 |
+
gr.Markdown("**项目技术栈**:Yuan 2.0,RAG,Langchain,Muiti-Agent, Gradio")
|
166 |
+
gr.Markdown("**项目演示视频**:【TODO】")
|
167 |
+
gr.Markdown("**项目构建分析与复盘博客**:【TODO】")
|
168 |
+
gr.Markdown("---")
|
169 |
+
gr.Markdown("**作者**:胡钧耀(南开大学计算机视觉在读一年级直博生,个人研究兴趣方向为视觉AIGC)")
|
170 |
+
gr.Markdown("**联系方式**:微信(LittleDream_hjy),[Bilibili](https://space.bilibili.com/2042113),[GitHub](https://github.com/JunyaoHu),[个人主页](https://junyaohu.github.io)")
|
171 |
+
with gr.Row():
|
172 |
+
gr.Image(elem_classes="qrcode", value="./src/weixin.png", label="微信", height=250)
|
173 |
+
gr.Image(elem_classes="qrcode", value="./src/zanshang.png", label="感谢支持", height=250)
|
174 |
+
|
175 |
+
############################## 后端 ##################################
|
176 |
+
|
177 |
+
def onComfirmClick(user_name):
|
178 |
+
if user_name.strip():
|
179 |
+
return f"您好,{user_name}。请根据如下提示完成测评!小心不要被 PUA 到啦 ~", gr.update(value="已确认", interactive=False), gr.update(interactive=True)
|
180 |
+
else:
|
181 |
+
return "👇 请输入昵称", gr.update(), gr.update()
|
182 |
+
|
183 |
+
confirm.click(onComfirmClick, user_name, [hello, confirm, restart])
|
184 |
+
|
185 |
+
def onRestart():
|
186 |
+
res = get_new_question_and_options()
|
187 |
+
return res["question_text"], "", "", \
|
188 |
+
gr.update(value="A: " + res["option_A"], interactive=True), \
|
189 |
+
gr.update(value="B: " + res["option_B"], interactive=True), \
|
190 |
+
gr.update(value="C: " + res["option_C"], interactive=True), \
|
191 |
+
gr.update(value="", interactive=True), \
|
192 |
+
1, 50, 50, 50, 50, gr.update(value="➡️ 下一题", interactive=False), gr.update(visible=False), gr.update(interactive=True)
|
193 |
+
|
194 |
+
restart.click(onRestart, None, [ \
|
195 |
+
question_text, result_text, score_text, \
|
196 |
+
option_A_button, option_B_button, option_C_button, option_D_text, \
|
197 |
+
progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh])
|
198 |
+
|
199 |
+
def onRefresh():
|
200 |
+
res = get_new_question_and_options()
|
201 |
+
return res["question_text"], "", "", \
|
202 |
+
gr.update(value="A: " + res["option_A"], interactive=True), \
|
203 |
+
gr.update(value="B: " + res["option_B"], interactive=True), \
|
204 |
+
gr.update(value="C: " + res["option_C"], interactive=True), \
|
205 |
+
gr.update(value="", interactive=True), \
|
206 |
+
gr.update(), gr.update(), gr.update(), gr.update(), gr.update(interactive=False), "↩️ 重新开始", gr.update(interactive=True)
|
207 |
+
|
208 |
+
refresh.click(onRefresh, None, [ \
|
209 |
+
question_text, result_text, score_text, \
|
210 |
+
option_A_button, option_B_button, option_C_button, option_D_text, \
|
211 |
+
dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh])
|
212 |
+
|
213 |
+
def onNext(progress_slider):
|
214 |
+
if progress_slider == data.value['total']:
|
215 |
+
return "", "", "", \
|
216 |
+
gr.update(value="A: 查看测评结果", interactive=False), \
|
217 |
+
gr.update(value="B: 查看测评结果", interactive=False), \
|
218 |
+
gr.update(value="C: 查看测评结果", interactive=False), \
|
219 |
+
gr.update(value="", interactive=False), \
|
220 |
+
progress_slider, gr.update(interactive=False), "↩️ 重新开始", gr.update(interactive=False)
|
221 |
+
else:
|
222 |
+
res = get_new_question_and_options()
|
223 |
+
return res["question_text"], "", "", \
|
224 |
+
gr.update(value="A: " + res["option_A"], interactive=True), \
|
225 |
+
gr.update(value="B: " + res["option_B"], interactive=True), \
|
226 |
+
gr.update(value="C: " + res["option_C"], interactive=True), \
|
227 |
+
gr.update(value="", interactive=True), \
|
228 |
+
progress_slider+1, gr.update(interactive=False), "↩️ 重新开始", gr.update(interactive=True)
|
229 |
+
|
230 |
+
next_btn.click(onNext, progress_slider, [ \
|
231 |
+
question_text, result_text, score_text, \
|
232 |
+
option_A_button, option_B_button, option_C_button, option_D_text, \
|
233 |
+
progress_slider, next_btn, restart, refresh])
|
234 |
+
|
235 |
+
def onClickABC(btn, question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider):
|
236 |
+
option_text = btn[3:]
|
237 |
+
res = get_option_result(question_text, option_text)
|
238 |
+
|
239 |
+
dim1_slider = max(min(dim1_slider + res["deltas"][0], 100), -100)
|
240 |
+
dim2_slider = max(min(dim2_slider + res["deltas"][1], 100), -100)
|
241 |
+
dim3_slider = max(min(dim3_slider + res["deltas"][2], 100), -100)
|
242 |
+
dim4_slider = max(min(dim4_slider + res["deltas"][3], 100), -100)
|
243 |
+
|
244 |
+
if progress_slider == data.value['total']:
|
245 |
+
text = "✨查看结果"
|
246 |
+
else:
|
247 |
+
text = "➡️ 下一题"
|
248 |
+
|
249 |
+
print(res["result_text"], res["score_text"], dim1_slider, dim2_slider, dim3_slider, dim4_slider, text)
|
250 |
+
return res["result_text"], res["score_text"], dim1_slider, dim2_slider, dim3_slider, dim4_slider, gr.update(value=text, interactive=True), gr.update(value="↩️ 重新开始", visible=True), gr.update(interactive=False), \
|
251 |
+
gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
252 |
+
|
253 |
+
option_A_button.click(onClickABC, \
|
254 |
+
[option_A_button, question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider], \
|
255 |
+
[result_text, score_text, dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh, \
|
256 |
+
option_A_button, option_B_button, option_C_button, option_D_button, option_D_text])
|
257 |
+
option_B_button.click(onClickABC, \
|
258 |
+
[option_B_button, question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider], \
|
259 |
+
[result_text, score_text, dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh, \
|
260 |
+
option_A_button, option_B_button, option_C_button, option_D_button, option_D_text])
|
261 |
+
option_C_button.click(onClickABC, \
|
262 |
+
[option_C_button, question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider], \
|
263 |
+
[result_text, score_text, dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh, \
|
264 |
+
option_A_button, option_B_button, option_C_button, option_D_button, option_D_text])
|
265 |
+
|
266 |
+
def onClickD(option_D_text, question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider):
|
267 |
+
res = get_option_result(question_text, option_D_text)
|
268 |
+
|
269 |
+
dim1_slider = max(min(dim1_slider + res["deltas"][0], 100), -100)
|
270 |
+
dim2_slider = max(min(dim2_slider + res["deltas"][1], 100), -100)
|
271 |
+
dim3_slider = max(min(dim3_slider + res["deltas"][2], 100), -100)
|
272 |
+
dim4_slider = max(min(dim4_slider + res["deltas"][3], 100), -100)
|
273 |
+
|
274 |
+
if progress_slider == data.value["total"]:
|
275 |
+
text = "✨查看结果"
|
276 |
+
else:
|
277 |
+
text = "➡️ 下一题"
|
278 |
+
|
279 |
+
print(res["result_text"], res["score_text"], dim1_slider, dim2_slider, dim3_slider, dim4_slider, text)
|
280 |
+
return res["result_text"], res["score_text"], dim1_slider, dim2_slider, dim3_slider, dim4_slider, gr.update(value=text, interactive=True), gr.update(value="↩️ 重新开始", visible=True), gr.update(interactive=False), \
|
281 |
+
gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
282 |
+
|
283 |
+
option_D_button.click(onClickD, \
|
284 |
+
[option_D_text , question_text, progress_slider, dim1_slider, dim2_slider, dim3_slider, dim4_slider], \
|
285 |
+
[result_text, score_text, dim1_slider, dim2_slider, dim3_slider, dim4_slider, next_btn, restart, refresh, \
|
286 |
+
option_A_button, option_B_button, option_C_button, option_D_button, option_D_text])
|
287 |
+
|
288 |
+
def onChangeD(option_D_text):
|
289 |
+
if option_D_text.strip():
|
290 |
+
return gr.update(interactive=True)
|
291 |
+
else:
|
292 |
+
return gr.update(interactive=False)
|
293 |
+
|
294 |
+
option_D_text.change(onChangeD, option_D_text, option_D_button)
|
295 |
+
|
296 |
+
if __name__ == "__main__":
|
297 |
+
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
demo.launch(share=True)
|