Spaces:
Sleeping
Sleeping
File size: 11,828 Bytes
b003f52 753292d b003f52 753292d b003f52 753292d b003f52 abc1a29 b003f52 753292d abc1a29 b003f52 16f5758 753292d 16f5758 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 16f5758 753292d b003f52 abc1a29 b003f52 b2a1b64 753292d b003f52 16f5758 753292d 16f5758 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 753292d 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 b2a1b64 753292d 16f5758 b003f52 753292d b003f52 03bc108 b003f52 753292d b003f52 62cd7b5 b003f52 753292d b003f52 753292d b003f52 753292d 16f5758 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 753292d b003f52 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 b003f52 |
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
import gradio as gr
from gradio_client import Client
# Start the Qwen API client.
client = Client("Qwen/Qwen2.5-72B-Instruct")
SYSTEM_PROMPT = (
"You are an assistant and you will ask yes or no questions about sea creatures. "
"Your goal is to guess the sea creature that the user has in mind from these questions. "
"You will never deviate from this task. Only when you find the answer, write it between <answer></answer> tags. "
"Start asking now. If your answer is wrong, continue asking. Never ask the user for the answer."
)
def start_game():
"""
Starts the game; retrieves the first question.
The question box is visible, the final guess area is cleared,
the answer buttons are visible, the evaluation and restart/continue buttons are hidden.
"""
history = []
result = client.predict(
query="",
history=history,
system=SYSTEM_PROMPT,
api_name="/model_chat"
)
assistant_message = result[1][-1][1]
history.append(("", assistant_message))
answer_buttons_update = gr.update(visible=True)
eval_buttons_update = gr.update(visible=False)
restart_update = gr.update(visible=False)
continue_update = gr.update(visible=False)
return (
gr.update(visible=True, value=assistant_message), # assistant_display
gr.update(visible=True, value=""), # final_answer_display
history, # state
answer_buttons_update, # btn_yes
answer_buttons_update, # btn_no
answer_buttons_update, # btn_dont_know
eval_buttons_update, # btn_correct
eval_buttons_update, # btn_incorrect
"", # final_state
restart_update, # btn_restart
continue_update # btn_continue
)
def process_turn(user_answer, history):
"""
Sends the user's answer to the API.
If the API's response contains <answer> tags, the final guess has been made.
- In this case, the question box is hidden,
- The final guess is displayed in large and bold,
- The answer buttons are hidden,
- The evaluation buttons are visible.
If there is no final guess, the assistant's answer (question) is updated.
"""
result = client.predict(
query=user_answer,
history=history,
system=SYSTEM_PROMPT,
api_name="/model_chat"
)
assistant_message = result[1][-1][1]
history.append((user_answer, assistant_message))
final_answer = ""
if "<answer>" in assistant_message and "</answer>" in assistant_message:
start_idx = assistant_message.index("<answer>") + len("<answer>")
end_idx = assistant_message.index("</answer>")
final_answer = assistant_message[start_idx:end_idx].strip()
if final_answer:
# Final guess made.
assistant_update = gr.update(visible=False)
final_text = f"**My guess:** **{final_answer}**"
answer_update = gr.update(visible=False)
eval_update = gr.update(visible=True)
restart_update = gr.update(visible=False)
continue_update = gr.update(visible=False)
else:
assistant_update = gr.update(visible=True, value=assistant_message)
final_text = ""
answer_update = gr.update(visible=True)
eval_update = gr.update(visible=False)
restart_update = gr.update(visible=False)
continue_update = gr.update(visible=False)
return (
assistant_update, # assistant_display
final_text, # final_answer_display
history, # state
answer_update, # btn_yes
answer_update, # btn_no
answer_update, # btn_dont_know
eval_update, # btn_correct
eval_update, # btn_incorrect
final_answer, # final_state
restart_update, # btn_restart
continue_update # btn_continue
)
def process_yes(history):
return process_turn("Yes", history)
def process_no(history):
return process_turn("No", history)
def process_dont_know(history):
return process_turn("I don't know", history)
def evaluate_correct(final_state):
"""
If the final guess is correct:
- The final text is updated to "My guess is correct! Shall we play again?"
- The evaluation buttons are hidden.
- The "Play Again" button becomes visible.
- The "Continue" button remains hidden.
"""
new_text = "**My guess is correct! Shall we play again?**"
return (
new_text,
gr.update(visible=False), # btn_correct hidden
gr.update(visible=False), # btn_incorrect hidden
gr.update(visible=True, value="Play Again"), # btn_restart visible
gr.update(visible=False) # btn_continue hidden
)
def evaluate_incorrect(final_state):
"""
If the final guess is incorrect:
- The final text is updated to "Let's continue, press the continue button."
- The evaluation buttons are hidden.
- The "Continue" button becomes visible.
"""
new_text = "**Let's continue, press the continue button.**"
return (
new_text,
gr.update(visible=False), # btn_correct hidden
gr.update(visible=False), # btn_incorrect hidden
gr.update(visible=False), # btn_restart hidden
gr.update(visible=True, value="Continue") # btn_continue visible
)
def continue_game(history):
"""
When the "Continue" button is clicked, the assistant asks a new question with the current history.
The conversation history is preserved, the question box and answer buttons are reactivated.
"""
result = client.predict(
query="",
history=history,
system=SYSTEM_PROMPT,
api_name="/model_chat"
)
assistant_message = result[1][-1][1]
history.append(("", assistant_message))
return (
gr.update(visible=True, value=assistant_message), # assistant_display
gr.update(visible=True, value=""), # final_answer_display
history, # state
gr.update(visible=True), # btn_yes
gr.update(visible=True), # btn_no
gr.update(visible=True), # btn_dont_know
gr.update(visible=False), # btn_correct
gr.update(visible=False), # btn_incorrect
"", # final_state
gr.update(visible=False), # btn_restart
gr.update(visible=False) # btn_continue
)
def restart_game():
"""
When the "Play Again" button is clicked, the game starts over (history is reset).
"""
global client
client = Client("Qwen/Qwen2.5-72B-Instruct")
return start_game()
# Custom CSS:
css = """
.question-box {
border: 2px solid #ccc;
padding: 10px;
border-radius: 5px;
margin: 10px auto;
width: 80%;
text-align: center;
}
.final-answer {
font-size: 2em;
font-weight: bold;
text-align: center;
margin: 20px;
}
.button-group {
display: flex;
justify-content: center;
gap: 20px;
}
"""
with gr.Blocks(css=css) as demo:
gr.Markdown("### TUBITECH # 9694 - Sea Creature Akinator")
# The question asked by the assistant.
assistant_display = gr.Markdown(label="Question", elem_classes="question-box")
# Final guess area.
final_answer_display = gr.Markdown("", label="Guess", elem_classes="final-answer")
# Answer buttons: Yes, No, I don't know.
with gr.Row(elem_classes="button-group"):
btn_yes = gr.Button("Yes")
btn_no = gr.Button("No")
btn_dont_know = gr.Button("I don't know")
# Evaluation buttons: Correct, Incorrect (hidden initially).
with gr.Row(elem_classes="button-group"):
btn_correct = gr.Button("Correct", visible=False)
btn_incorrect = gr.Button("Incorrect", visible=False)
# Buttons displayed in the final state:
# - If correct, "Play Again"
btn_restart = gr.Button("Play Again", visible=False)
# - If incorrect, "Continue"
btn_continue = gr.Button("Continue", visible=False)
# Hidden states:
state = gr.State([])
final_state = gr.State("")
# Start the game when the page loads.
demo.load(
fn=start_game,
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
# When answer buttons are clicked:
btn_yes.click(
fn=process_yes,
inputs=[state],
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
btn_no.click(
fn=process_no,
inputs=[state],
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
btn_dont_know.click(
fn=process_dont_know,
inputs=[state],
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
# Evaluation buttons:
btn_correct.click(
fn=evaluate_correct,
inputs=[final_state],
outputs=[
final_answer_display,
btn_correct,
btn_incorrect,
btn_restart,
btn_continue
]
)
btn_incorrect.click(
fn=evaluate_incorrect,
inputs=[final_state],
outputs=[
final_answer_display,
btn_correct,
btn_incorrect,
btn_restart,
btn_continue
]
)
# When the "Play Again" button is clicked:
btn_restart.click(
fn=restart_game,
inputs=[],
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
# When the "Continue" button is clicked:
btn_continue.click(
fn=continue_game,
inputs=[state],
outputs=[
assistant_display,
final_answer_display,
state,
btn_yes,
btn_no,
btn_dont_know,
btn_correct,
btn_incorrect,
final_state,
btn_restart,
btn_continue
]
)
demo.launch(debug=True, show_error=True)
|