Spaces:
Sleeping
Sleeping
File size: 11,617 Bytes
1275a62 b003f52 753292d b003f52 753292d b003f52 abc1a29 b003f52 753292d abc1a29 b003f52 16f5758 753292d 16f5758 4502572 b003f52 753292d b003f52 753292d b003f52 16f5758 753292d b003f52 abc1a29 b003f52 b2a1b64 753292d b003f52 4502572 b003f52 753292d b003f52 753292d b003f52 753292d b003f52 753292d 16f5758 4502572 16f5758 b003f52 753292d 16f5758 4502572 16f5758 4502572 16f5758 b003f52 03bc108 b003f52 4502572 37c6655 4502572 8784854 4502572 8784854 4502572 b003f52 8784854 b003f52 4502572 b003f52 4502572 b003f52 4502572 b003f52 37c6655 8784854 4502572 1275a62 4502572 1275a62 8784854 4502572 b003f52 8784854 4502572 b003f52 4502572 16f5758 b003f52 4502572 b003f52 4502572 b003f52 4502572 b003f52 16f5758 753292d 16f5758 753292d 16f5758 b003f52 4502572 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 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 753292d 16f5758 b003f52 1275a62 b44067e 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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
import base64
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():
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):
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:
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,
final_text,
history,
answer_update,
answer_update,
answer_update,
eval_update,
eval_update,
final_answer,
restart_update,
continue_update
)
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):
new_text = "**My guess is correct! Shall we play again?**"
return (
new_text,
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=True, value="Play Again"),
gr.update(visible=False)
)
def evaluate_incorrect(final_state):
new_text = "**Let's continue, press the continue button.**"
return (
new_text,
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=True, value="Continue")
)
def continue_game(history):
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),
gr.update(visible=True, value=""),
history,
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
"",
gr.update(visible=False),
gr.update(visible=False)
)
def restart_game():
global client
client = Client("Qwen/Qwen2.5-72B-Instruct")
return start_game()
css = """
body, .gradio-container {
font-family: 'Roboto', sans-serif;
background-color: #f0f2f5;
background-image: url('https://cdn.glitch.global/e803869d-6fae-4322-93f8-f15353a38bf3/pexels-jeremy-bishop-1260133-2397651.jpg?v=1740128669007') !important;
background-size: cover !important;
background-position: center !important;
background-repeat: no-repeat !important;
background-attachment: fixed !important;
color: #333; /* Varsayılan yazı rengi */
}
/* Tüm yazıların rengini siyah yapmak için */
* {
color: black !important; /* Butonlar dışındaki tüm yazıları siyah yapar */
}
/* Üstteki başlık için stil */
.header-text {
text-align: center;
margin-top: 20px;
font-size: 2em;
color: #4261a8;
}
/* Özel buton sınıfımız: my-button */
.my-button {
background-color: #4261a8 !important;
color: #fff !important;
border: none !important;
transition: transform 0.3s, background-color 0.3s !important;
}
.my-button:hover {
background-color: #1c4196 !important;
transform: scale(1.05);
}
/* Soru kutusu */
.question-box {
border: 2px solid #ccc;
padding: 10px;
border-radius: 5px;
margin: 10px auto;
width: 80%;
text-align: center;
background-color: rgba(173, 216, 230, 0);
}
/* Gradio Markdown’un varsayılan arka planını şeffaf yapmak */
.question-box .gr-prose {
background-color: transparent !important;
box-shadow: none !important;
border: none !important;
}
.final-answer {
font-size: 2em;
font-weight: bold;
text-align: center;
margin: 20px;
}
.button-group {
display: flex;
justify-content: center;
gap: 20px;
}
/* Gradio footer kısmını gizle */
footer, .footer, .gradio-footer {
display: none !important;
}
"""
# Light mode zorunluluğu için ek CSS
custom_css = """
:root {
color-scheme: light;
}
""" + css
# Yerel ikonu Base64'e çevirme
with open("tubitech-su.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
icon_data_uri = f"data:image/png;base64,{encoded_string}"
icon_html = f"""
<div style="position: fixed; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 1000;">
<div style="width: 60px; height: 60px; background-color: white; border-radius: 50%;
display: flex; justify-content: center; align-items: center;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);">
<img src="{icon_data_uri}" alt="Icon" style="width: 50px; height: auto;">
</div>
</div>
"""
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
# Üstteki başlık
gr.Markdown("### TUBITECH # 9694 - Sea Creature Akinator", elem_classes="header-text")
# Arayüz elemanları
assistant_display = gr.Markdown(
value="",
label="",
show_label=False,
elem_classes="question-box"
)
final_answer_display = gr.Markdown(
value="",
label="",
show_label=False,
elem_classes="final-answer"
)
with gr.Row(elem_classes="button-group"):
btn_yes = gr.Button("Yes", elem_classes=["my-button"])
btn_no = gr.Button("No", elem_classes=["my-button"])
btn_dont_know = gr.Button("I don't know", elem_classes=["my-button"])
with gr.Row(elem_classes="button-group"):
btn_correct = gr.Button("Correct", visible=False, elem_classes=["my-button"])
btn_incorrect = gr.Button("Incorrect", visible=False, elem_classes=["my-button"])
btn_restart = gr.Button("Play Again", visible=False, elem_classes=["my-button"])
btn_continue = gr.Button("Continue", visible=False, elem_classes=["my-button"])
state = gr.State([])
final_state = gr.State("")
# Sayfa yüklendiğinde oyunu başlat
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
]
)
# Butonların click event'leri
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
]
)
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
]
)
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
]
)
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
]
)
gr.HTML(icon_html)
demo.launch(debug=True, show_error=True)
|