Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
|
4 |
-
# Qwen API
|
5 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
6 |
|
7 |
SYSTEM_PROMPT = (
|
8 |
-
"
|
9 |
-
"
|
10 |
-
"
|
11 |
-
"
|
12 |
)
|
13 |
|
14 |
def start_game():
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
"""
|
20 |
history = []
|
21 |
result = client.predict(
|
@@ -30,31 +30,31 @@ def start_game():
|
|
30 |
answer_buttons_update = gr.update(visible=True)
|
31 |
eval_buttons_update = gr.update(visible=False)
|
32 |
restart_update = gr.update(visible=False)
|
33 |
-
|
34 |
|
35 |
return (
|
36 |
gr.update(visible=True, value=assistant_message), # assistant_display
|
37 |
gr.update(visible=True, value=""), # final_answer_display
|
38 |
history, # state
|
39 |
-
answer_buttons_update, #
|
40 |
-
answer_buttons_update, #
|
41 |
-
answer_buttons_update, #
|
42 |
-
eval_buttons_update, #
|
43 |
-
eval_buttons_update, #
|
44 |
"", # final_state
|
45 |
restart_update, # btn_restart
|
46 |
-
|
47 |
)
|
48 |
|
49 |
def process_turn(user_answer, history):
|
50 |
"""
|
51 |
-
|
52 |
-
|
53 |
-
-
|
54 |
-
-
|
55 |
-
-
|
56 |
-
-
|
57 |
-
|
58 |
"""
|
59 |
result = client.predict(
|
60 |
query=user_answer,
|
@@ -65,85 +65,85 @@ def process_turn(user_answer, history):
|
|
65 |
assistant_message = result[1][-1][1]
|
66 |
history.append((user_answer, assistant_message))
|
67 |
final_answer = ""
|
68 |
-
if "<
|
69 |
-
start_idx = assistant_message.index("<
|
70 |
-
end_idx = assistant_message.index("</
|
71 |
final_answer = assistant_message[start_idx:end_idx].strip()
|
72 |
if final_answer:
|
73 |
-
# Final
|
74 |
assistant_update = gr.update(visible=False)
|
75 |
-
final_text = f"**
|
76 |
answer_update = gr.update(visible=False)
|
77 |
eval_update = gr.update(visible=True)
|
78 |
restart_update = gr.update(visible=False)
|
79 |
-
|
80 |
else:
|
81 |
assistant_update = gr.update(visible=True, value=assistant_message)
|
82 |
final_text = ""
|
83 |
answer_update = gr.update(visible=True)
|
84 |
eval_update = gr.update(visible=False)
|
85 |
restart_update = gr.update(visible=False)
|
86 |
-
|
87 |
return (
|
88 |
assistant_update, # assistant_display
|
89 |
final_text, # final_answer_display
|
90 |
history, # state
|
91 |
-
answer_update, #
|
92 |
-
answer_update, #
|
93 |
-
answer_update, #
|
94 |
-
eval_update, #
|
95 |
-
eval_update, #
|
96 |
final_answer, # final_state
|
97 |
restart_update, # btn_restart
|
98 |
-
|
99 |
)
|
100 |
|
101 |
-
def
|
102 |
-
return process_turn("
|
103 |
|
104 |
-
def
|
105 |
-
return process_turn("
|
106 |
|
107 |
-
def
|
108 |
-
return process_turn("
|
109 |
|
110 |
def evaluate_correct(final_state):
|
111 |
"""
|
112 |
-
|
113 |
-
-
|
114 |
-
-
|
115 |
-
- "
|
116 |
-
-
|
117 |
"""
|
118 |
-
new_text = "**
|
119 |
return (
|
120 |
new_text,
|
121 |
-
gr.update(visible=False), #
|
122 |
-
gr.update(visible=False), #
|
123 |
-
gr.update(visible=True, value="
|
124 |
-
gr.update(visible=False) #
|
125 |
)
|
126 |
|
127 |
def evaluate_incorrect(final_state):
|
128 |
"""
|
129 |
-
|
130 |
-
-
|
131 |
-
-
|
132 |
-
-
|
133 |
"""
|
134 |
-
new_text = "**
|
135 |
return (
|
136 |
new_text,
|
137 |
-
gr.update(visible=False), #
|
138 |
-
gr.update(visible=False), #
|
139 |
-
gr.update(visible=False), # btn_restart
|
140 |
-
gr.update(visible=True, value="
|
141 |
)
|
142 |
|
143 |
def continue_game(history):
|
144 |
"""
|
145 |
-
|
146 |
-
|
147 |
"""
|
148 |
result = client.predict(
|
149 |
query="",
|
@@ -157,25 +157,25 @@ def continue_game(history):
|
|
157 |
gr.update(visible=True, value=assistant_message), # assistant_display
|
158 |
gr.update(visible=True, value=""), # final_answer_display
|
159 |
history, # state
|
160 |
-
gr.update(visible=True), #
|
161 |
-
gr.update(visible=True), #
|
162 |
-
gr.update(visible=True), #
|
163 |
-
gr.update(visible=False), #
|
164 |
-
gr.update(visible=False), #
|
165 |
"", # final_state
|
166 |
gr.update(visible=False), # btn_restart
|
167 |
-
gr.update(visible=False) #
|
168 |
)
|
169 |
|
170 |
def restart_game():
|
171 |
"""
|
172 |
-
"
|
173 |
"""
|
174 |
global client
|
175 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
176 |
return start_game()
|
177 |
|
178 |
-
#
|
179 |
css = """
|
180 |
.question-box {
|
181 |
border: 2px solid #ccc;
|
@@ -199,130 +199,130 @@ css = """
|
|
199 |
"""
|
200 |
|
201 |
with gr.Blocks(css=css) as demo:
|
202 |
-
gr.Markdown("###
|
203 |
|
204 |
-
#
|
205 |
-
assistant_display = gr.Markdown(label="
|
206 |
-
# Final
|
207 |
-
final_answer_display = gr.Markdown("", label="
|
208 |
|
209 |
-
#
|
210 |
with gr.Row(elem_classes="button-group"):
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
|
215 |
-
#
|
216 |
with gr.Row(elem_classes="button-group"):
|
217 |
-
|
218 |
-
|
219 |
|
220 |
-
#
|
221 |
-
# -
|
222 |
-
btn_restart = gr.Button("
|
223 |
-
# -
|
224 |
-
|
225 |
|
226 |
-
#
|
227 |
state = gr.State([])
|
228 |
final_state = gr.State("")
|
229 |
|
230 |
-
#
|
231 |
demo.load(
|
232 |
fn=start_game,
|
233 |
outputs=[
|
234 |
assistant_display,
|
235 |
final_answer_display,
|
236 |
state,
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
final_state,
|
243 |
btn_restart,
|
244 |
-
|
245 |
]
|
246 |
)
|
247 |
|
248 |
-
#
|
249 |
-
|
250 |
-
fn=
|
251 |
inputs=[state],
|
252 |
outputs=[
|
253 |
assistant_display,
|
254 |
final_answer_display,
|
255 |
state,
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
final_state,
|
262 |
btn_restart,
|
263 |
-
|
264 |
]
|
265 |
)
|
266 |
-
|
267 |
-
fn=
|
268 |
inputs=[state],
|
269 |
outputs=[
|
270 |
assistant_display,
|
271 |
final_answer_display,
|
272 |
state,
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
final_state,
|
279 |
btn_restart,
|
280 |
-
|
281 |
]
|
282 |
)
|
283 |
-
|
284 |
-
fn=
|
285 |
inputs=[state],
|
286 |
outputs=[
|
287 |
assistant_display,
|
288 |
final_answer_display,
|
289 |
state,
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
final_state,
|
296 |
btn_restart,
|
297 |
-
|
298 |
]
|
299 |
)
|
300 |
|
301 |
-
#
|
302 |
-
|
303 |
fn=evaluate_correct,
|
304 |
inputs=[final_state],
|
305 |
outputs=[
|
306 |
final_answer_display,
|
307 |
-
|
308 |
-
|
309 |
btn_restart,
|
310 |
-
|
311 |
]
|
312 |
)
|
313 |
-
|
314 |
fn=evaluate_incorrect,
|
315 |
inputs=[final_state],
|
316 |
outputs=[
|
317 |
final_answer_display,
|
318 |
-
|
319 |
-
|
320 |
btn_restart,
|
321 |
-
|
322 |
]
|
323 |
)
|
324 |
|
325 |
-
# "
|
326 |
btn_restart.click(
|
327 |
fn=restart_game,
|
328 |
inputs=[],
|
@@ -330,33 +330,33 @@ with gr.Blocks(css=css) as demo:
|
|
330 |
assistant_display,
|
331 |
final_answer_display,
|
332 |
state,
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
final_state,
|
339 |
btn_restart,
|
340 |
-
|
341 |
]
|
342 |
)
|
343 |
|
344 |
-
#
|
345 |
-
|
346 |
fn=continue_game,
|
347 |
inputs=[state],
|
348 |
outputs=[
|
349 |
assistant_display,
|
350 |
final_answer_display,
|
351 |
state,
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
final_state,
|
358 |
btn_restart,
|
359 |
-
|
360 |
]
|
361 |
)
|
362 |
|
|
|
1 |
import gradio as gr
|
2 |
from gradio_client import Client
|
3 |
|
4 |
+
# Start the Qwen API client.
|
5 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
6 |
|
7 |
SYSTEM_PROMPT = (
|
8 |
+
"You are an assistant and you will ask yes or no questions about sea creatures. "
|
9 |
+
"Your goal is to guess the sea creature that the user has in mind from these questions. "
|
10 |
+
"You will never deviate from this task. Only when you find the answer, write it between <answer></answer> tags. "
|
11 |
+
"Start asking now. If your answer is wrong, continue asking. Never ask the user for the answer."
|
12 |
)
|
13 |
|
14 |
def start_game():
|
15 |
"""
|
16 |
+
Starts the game; retrieves the first question.
|
17 |
+
The question box is visible, the final guess area is cleared,
|
18 |
+
the answer buttons are visible, the evaluation and restart/continue buttons are hidden.
|
19 |
"""
|
20 |
history = []
|
21 |
result = client.predict(
|
|
|
30 |
answer_buttons_update = gr.update(visible=True)
|
31 |
eval_buttons_update = gr.update(visible=False)
|
32 |
restart_update = gr.update(visible=False)
|
33 |
+
continue_update = gr.update(visible=False)
|
34 |
|
35 |
return (
|
36 |
gr.update(visible=True, value=assistant_message), # assistant_display
|
37 |
gr.update(visible=True, value=""), # final_answer_display
|
38 |
history, # state
|
39 |
+
answer_buttons_update, # btn_yes
|
40 |
+
answer_buttons_update, # btn_no
|
41 |
+
answer_buttons_update, # btn_dont_know
|
42 |
+
eval_buttons_update, # btn_correct
|
43 |
+
eval_buttons_update, # btn_incorrect
|
44 |
"", # final_state
|
45 |
restart_update, # btn_restart
|
46 |
+
continue_update # btn_continue
|
47 |
)
|
48 |
|
49 |
def process_turn(user_answer, history):
|
50 |
"""
|
51 |
+
Sends the user's answer to the API.
|
52 |
+
If the API's response contains <answer> tags, the final guess has been made.
|
53 |
+
- In this case, the question box is hidden,
|
54 |
+
- The final guess is displayed in large and bold,
|
55 |
+
- The answer buttons are hidden,
|
56 |
+
- The evaluation buttons are visible.
|
57 |
+
If there is no final guess, the assistant's answer (question) is updated.
|
58 |
"""
|
59 |
result = client.predict(
|
60 |
query=user_answer,
|
|
|
65 |
assistant_message = result[1][-1][1]
|
66 |
history.append((user_answer, assistant_message))
|
67 |
final_answer = ""
|
68 |
+
if "<answer>" in assistant_message and "</answer>" in assistant_message:
|
69 |
+
start_idx = assistant_message.index("<answer>") + len("<answer>")
|
70 |
+
end_idx = assistant_message.index("</answer>")
|
71 |
final_answer = assistant_message[start_idx:end_idx].strip()
|
72 |
if final_answer:
|
73 |
+
# Final guess made.
|
74 |
assistant_update = gr.update(visible=False)
|
75 |
+
final_text = f"**My guess:** **{final_answer}**"
|
76 |
answer_update = gr.update(visible=False)
|
77 |
eval_update = gr.update(visible=True)
|
78 |
restart_update = gr.update(visible=False)
|
79 |
+
continue_update = gr.update(visible=False)
|
80 |
else:
|
81 |
assistant_update = gr.update(visible=True, value=assistant_message)
|
82 |
final_text = ""
|
83 |
answer_update = gr.update(visible=True)
|
84 |
eval_update = gr.update(visible=False)
|
85 |
restart_update = gr.update(visible=False)
|
86 |
+
continue_update = gr.update(visible=False)
|
87 |
return (
|
88 |
assistant_update, # assistant_display
|
89 |
final_text, # final_answer_display
|
90 |
history, # state
|
91 |
+
answer_update, # btn_yes
|
92 |
+
answer_update, # btn_no
|
93 |
+
answer_update, # btn_dont_know
|
94 |
+
eval_update, # btn_correct
|
95 |
+
eval_update, # btn_incorrect
|
96 |
final_answer, # final_state
|
97 |
restart_update, # btn_restart
|
98 |
+
continue_update # btn_continue
|
99 |
)
|
100 |
|
101 |
+
def process_yes(history):
|
102 |
+
return process_turn("Yes", history)
|
103 |
|
104 |
+
def process_no(history):
|
105 |
+
return process_turn("No", history)
|
106 |
|
107 |
+
def process_dont_know(history):
|
108 |
+
return process_turn("I don't know", history)
|
109 |
|
110 |
def evaluate_correct(final_state):
|
111 |
"""
|
112 |
+
If the final guess is correct:
|
113 |
+
- The final text is updated to "My guess is correct! Shall we play again?"
|
114 |
+
- The evaluation buttons are hidden.
|
115 |
+
- The "Play Again" button becomes visible.
|
116 |
+
- The "Continue" button remains hidden.
|
117 |
"""
|
118 |
+
new_text = "**My guess is correct! Shall we play again?**"
|
119 |
return (
|
120 |
new_text,
|
121 |
+
gr.update(visible=False), # btn_correct hidden
|
122 |
+
gr.update(visible=False), # btn_incorrect hidden
|
123 |
+
gr.update(visible=True, value="Play Again"), # btn_restart visible
|
124 |
+
gr.update(visible=False) # btn_continue hidden
|
125 |
)
|
126 |
|
127 |
def evaluate_incorrect(final_state):
|
128 |
"""
|
129 |
+
If the final guess is incorrect:
|
130 |
+
- The final text is updated to "Let's continue, press the continue button."
|
131 |
+
- The evaluation buttons are hidden.
|
132 |
+
- The "Continue" button becomes visible.
|
133 |
"""
|
134 |
+
new_text = "**Let's continue, press the continue button.**"
|
135 |
return (
|
136 |
new_text,
|
137 |
+
gr.update(visible=False), # btn_correct hidden
|
138 |
+
gr.update(visible=False), # btn_incorrect hidden
|
139 |
+
gr.update(visible=False), # btn_restart hidden
|
140 |
+
gr.update(visible=True, value="Continue") # btn_continue visible
|
141 |
)
|
142 |
|
143 |
def continue_game(history):
|
144 |
"""
|
145 |
+
When the "Continue" button is clicked, the assistant asks a new question with the current history.
|
146 |
+
The conversation history is preserved, the question box and answer buttons are reactivated.
|
147 |
"""
|
148 |
result = client.predict(
|
149 |
query="",
|
|
|
157 |
gr.update(visible=True, value=assistant_message), # assistant_display
|
158 |
gr.update(visible=True, value=""), # final_answer_display
|
159 |
history, # state
|
160 |
+
gr.update(visible=True), # btn_yes
|
161 |
+
gr.update(visible=True), # btn_no
|
162 |
+
gr.update(visible=True), # btn_dont_know
|
163 |
+
gr.update(visible=False), # btn_correct
|
164 |
+
gr.update(visible=False), # btn_incorrect
|
165 |
"", # final_state
|
166 |
gr.update(visible=False), # btn_restart
|
167 |
+
gr.update(visible=False) # btn_continue
|
168 |
)
|
169 |
|
170 |
def restart_game():
|
171 |
"""
|
172 |
+
When the "Play Again" button is clicked, the game starts over (history is reset).
|
173 |
"""
|
174 |
global client
|
175 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
176 |
return start_game()
|
177 |
|
178 |
+
# Custom CSS:
|
179 |
css = """
|
180 |
.question-box {
|
181 |
border: 2px solid #ccc;
|
|
|
199 |
"""
|
200 |
|
201 |
with gr.Blocks(css=css) as demo:
|
202 |
+
gr.Markdown("### Sea Creature Assistant - Yes/No Game")
|
203 |
|
204 |
+
# The question asked by the assistant.
|
205 |
+
assistant_display = gr.Markdown(label="Question", elem_classes="question-box")
|
206 |
+
# Final guess area.
|
207 |
+
final_answer_display = gr.Markdown("", label="Guess", elem_classes="final-answer")
|
208 |
|
209 |
+
# Answer buttons: Yes, No, I don't know.
|
210 |
with gr.Row(elem_classes="button-group"):
|
211 |
+
btn_yes = gr.Button("Yes")
|
212 |
+
btn_no = gr.Button("No")
|
213 |
+
btn_dont_know = gr.Button("I don't know")
|
214 |
|
215 |
+
# Evaluation buttons: Correct, Incorrect (hidden initially).
|
216 |
with gr.Row(elem_classes="button-group"):
|
217 |
+
btn_correct = gr.Button("Correct", visible=False)
|
218 |
+
btn_incorrect = gr.Button("Incorrect", visible=False)
|
219 |
|
220 |
+
# Buttons displayed in the final state:
|
221 |
+
# - If correct, "Play Again"
|
222 |
+
btn_restart = gr.Button("Play Again", visible=False)
|
223 |
+
# - If incorrect, "Continue"
|
224 |
+
btn_continue = gr.Button("Continue", visible=False)
|
225 |
|
226 |
+
# Hidden states:
|
227 |
state = gr.State([])
|
228 |
final_state = gr.State("")
|
229 |
|
230 |
+
# Start the game when the page loads.
|
231 |
demo.load(
|
232 |
fn=start_game,
|
233 |
outputs=[
|
234 |
assistant_display,
|
235 |
final_answer_display,
|
236 |
state,
|
237 |
+
btn_yes,
|
238 |
+
btn_no,
|
239 |
+
btn_dont_know,
|
240 |
+
btn_correct,
|
241 |
+
btn_incorrect,
|
242 |
final_state,
|
243 |
btn_restart,
|
244 |
+
btn_continue
|
245 |
]
|
246 |
)
|
247 |
|
248 |
+
# When answer buttons are clicked:
|
249 |
+
btn_yes.click(
|
250 |
+
fn=process_yes,
|
251 |
inputs=[state],
|
252 |
outputs=[
|
253 |
assistant_display,
|
254 |
final_answer_display,
|
255 |
state,
|
256 |
+
btn_yes,
|
257 |
+
btn_no,
|
258 |
+
btn_dont_know,
|
259 |
+
btn_correct,
|
260 |
+
btn_incorrect,
|
261 |
final_state,
|
262 |
btn_restart,
|
263 |
+
btn_continue
|
264 |
]
|
265 |
)
|
266 |
+
btn_no.click(
|
267 |
+
fn=process_no,
|
268 |
inputs=[state],
|
269 |
outputs=[
|
270 |
assistant_display,
|
271 |
final_answer_display,
|
272 |
state,
|
273 |
+
btn_yes,
|
274 |
+
btn_no,
|
275 |
+
btn_dont_know,
|
276 |
+
btn_correct,
|
277 |
+
btn_incorrect,
|
278 |
final_state,
|
279 |
btn_restart,
|
280 |
+
btn_continue
|
281 |
]
|
282 |
)
|
283 |
+
btn_dont_know.click(
|
284 |
+
fn=process_dont_know,
|
285 |
inputs=[state],
|
286 |
outputs=[
|
287 |
assistant_display,
|
288 |
final_answer_display,
|
289 |
state,
|
290 |
+
btn_yes,
|
291 |
+
btn_no,
|
292 |
+
btn_dont_know,
|
293 |
+
btn_correct,
|
294 |
+
btn_incorrect,
|
295 |
final_state,
|
296 |
btn_restart,
|
297 |
+
btn_continue
|
298 |
]
|
299 |
)
|
300 |
|
301 |
+
# Evaluation buttons:
|
302 |
+
btn_correct.click(
|
303 |
fn=evaluate_correct,
|
304 |
inputs=[final_state],
|
305 |
outputs=[
|
306 |
final_answer_display,
|
307 |
+
btn_correct,
|
308 |
+
btn_incorrect,
|
309 |
btn_restart,
|
310 |
+
btn_continue
|
311 |
]
|
312 |
)
|
313 |
+
btn_incorrect.click(
|
314 |
fn=evaluate_incorrect,
|
315 |
inputs=[final_state],
|
316 |
outputs=[
|
317 |
final_answer_display,
|
318 |
+
btn_correct,
|
319 |
+
btn_incorrect,
|
320 |
btn_restart,
|
321 |
+
btn_continue
|
322 |
]
|
323 |
)
|
324 |
|
325 |
+
# When the "Play Again" button is clicked:
|
326 |
btn_restart.click(
|
327 |
fn=restart_game,
|
328 |
inputs=[],
|
|
|
330 |
assistant_display,
|
331 |
final_answer_display,
|
332 |
state,
|
333 |
+
btn_yes,
|
334 |
+
btn_no,
|
335 |
+
btn_dont_know,
|
336 |
+
btn_correct,
|
337 |
+
btn_incorrect,
|
338 |
final_state,
|
339 |
btn_restart,
|
340 |
+
btn_continue
|
341 |
]
|
342 |
)
|
343 |
|
344 |
+
# When the "Continue" button is clicked:
|
345 |
+
btn_continue.click(
|
346 |
fn=continue_game,
|
347 |
inputs=[state],
|
348 |
outputs=[
|
349 |
assistant_display,
|
350 |
final_answer_display,
|
351 |
state,
|
352 |
+
btn_yes,
|
353 |
+
btn_no,
|
354 |
+
btn_dont_know,
|
355 |
+
btn_correct,
|
356 |
+
btn_incorrect,
|
357 |
final_state,
|
358 |
btn_restart,
|
359 |
+
btn_continue
|
360 |
]
|
361 |
)
|
362 |
|