Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,11 +13,6 @@ SYSTEM_PROMPT = (
|
|
13 |
)
|
14 |
|
15 |
def start_game():
|
16 |
-
"""
|
17 |
-
Starts the game; retrieves the first question.
|
18 |
-
The question box is visible, the final guess area is cleared,
|
19 |
-
the answer buttons are visible, the evaluation and restart/continue buttons are hidden.
|
20 |
-
"""
|
21 |
history = []
|
22 |
result = client.predict(
|
23 |
query="",
|
@@ -44,19 +39,10 @@ def start_game():
|
|
44 |
eval_buttons_update, # btn_incorrect
|
45 |
"", # final_state
|
46 |
restart_update, # btn_restart
|
47 |
-
continue_update
|
48 |
)
|
49 |
|
50 |
def process_turn(user_answer, history):
|
51 |
-
"""
|
52 |
-
Sends the user's answer to the API.
|
53 |
-
If the API's response contains <answer> tags, the final guess has been made.
|
54 |
-
- In this case, the question box is hidden,
|
55 |
-
- The final guess is displayed in large and bold,
|
56 |
-
- The answer buttons are hidden,
|
57 |
-
- The evaluation buttons are visible.
|
58 |
-
If there is no final guess, the assistant's answer (question) is updated.
|
59 |
-
"""
|
60 |
result = client.predict(
|
61 |
query=user_answer,
|
62 |
history=history,
|
@@ -71,7 +57,7 @@ def process_turn(user_answer, history):
|
|
71 |
end_idx = assistant_message.index("</answer>")
|
72 |
final_answer = assistant_message[start_idx:end_idx].strip()
|
73 |
if final_answer:
|
74 |
-
#
|
75 |
assistant_update = gr.update(visible=False)
|
76 |
final_text = f"**My guess:** **{final_answer}**"
|
77 |
answer_update = gr.update(visible=False)
|
@@ -79,6 +65,7 @@ def process_turn(user_answer, history):
|
|
79 |
restart_update = gr.update(visible=False)
|
80 |
continue_update = gr.update(visible=False)
|
81 |
else:
|
|
|
82 |
assistant_update = gr.update(visible=True, value=assistant_message)
|
83 |
final_text = ""
|
84 |
answer_update = gr.update(visible=True)
|
@@ -86,17 +73,17 @@ def process_turn(user_answer, history):
|
|
86 |
restart_update = gr.update(visible=False)
|
87 |
continue_update = gr.update(visible=False)
|
88 |
return (
|
89 |
-
assistant_update,
|
90 |
-
final_text,
|
91 |
-
history,
|
92 |
-
answer_update,
|
93 |
-
answer_update,
|
94 |
-
answer_update,
|
95 |
-
eval_update,
|
96 |
-
eval_update,
|
97 |
-
final_answer,
|
98 |
-
restart_update,
|
99 |
-
continue_update
|
100 |
)
|
101 |
|
102 |
def process_yes(history):
|
@@ -109,43 +96,26 @@ def process_dont_know(history):
|
|
109 |
return process_turn("I don't know", history)
|
110 |
|
111 |
def evaluate_correct(final_state):
|
112 |
-
"""
|
113 |
-
If the final guess is correct:
|
114 |
-
- The final text is updated to "My guess is correct! Shall we play again?"
|
115 |
-
- The evaluation buttons are hidden.
|
116 |
-
- The "Play Again" button becomes visible.
|
117 |
-
- The "Continue" button remains hidden.
|
118 |
-
"""
|
119 |
new_text = "**My guess is correct! Shall we play again?**"
|
120 |
return (
|
121 |
new_text,
|
122 |
-
gr.update(visible=False),
|
123 |
-
gr.update(visible=False),
|
124 |
-
gr.update(visible=True, value="Play Again"),
|
125 |
-
gr.update(visible=False)
|
126 |
)
|
127 |
|
128 |
def evaluate_incorrect(final_state):
|
129 |
-
"""
|
130 |
-
If the final guess is incorrect:
|
131 |
-
- The final text is updated to "Let's continue, press the continue button."
|
132 |
-
- The evaluation buttons are hidden.
|
133 |
-
- The "Continue" button becomes visible.
|
134 |
-
"""
|
135 |
new_text = "**Let's continue, press the continue button.**"
|
136 |
return (
|
137 |
new_text,
|
138 |
-
gr.update(visible=False),
|
139 |
-
gr.update(visible=False),
|
140 |
-
gr.update(visible=False),
|
141 |
-
gr.update(visible=True, value="Continue")
|
142 |
)
|
143 |
|
144 |
def continue_game(history):
|
145 |
-
"""
|
146 |
-
When the "Continue" button is clicked, the assistant asks a new question with the current history.
|
147 |
-
The conversation history is preserved, the question box and answer buttons are reactivated.
|
148 |
-
"""
|
149 |
result = client.predict(
|
150 |
query="",
|
151 |
history=history,
|
@@ -155,29 +125,57 @@ def continue_game(history):
|
|
155 |
assistant_message = result[1][-1][1]
|
156 |
history.append(("", assistant_message))
|
157 |
return (
|
158 |
-
gr.update(visible=True, value=assistant_message),
|
159 |
-
gr.update(visible=True, value=""),
|
160 |
-
history,
|
161 |
-
gr.update(visible=True),
|
162 |
-
gr.update(visible=True),
|
163 |
-
gr.update(visible=True),
|
164 |
-
gr.update(visible=False),
|
165 |
-
gr.update(visible=False),
|
166 |
-
"",
|
167 |
-
gr.update(visible=False),
|
168 |
-
gr.update(visible=False)
|
169 |
)
|
170 |
|
171 |
def restart_game():
|
172 |
-
"""
|
173 |
-
When the "Play Again" button is clicked, the game starts over (history is reset).
|
174 |
-
"""
|
175 |
global client
|
176 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
177 |
return start_game()
|
178 |
|
179 |
-
# Custom CSS:
|
180 |
css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
.question-box {
|
182 |
border: 2px solid #ccc;
|
183 |
padding: 10px;
|
@@ -185,62 +183,84 @@ css = """
|
|
185 |
margin: 10px auto;
|
186 |
width: 80%;
|
187 |
text-align: center;
|
|
|
188 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
.final-answer {
|
190 |
font-size: 2em;
|
191 |
font-weight: bold;
|
192 |
text-align: center;
|
193 |
margin: 20px;
|
194 |
}
|
|
|
195 |
.button-group {
|
196 |
display: flex;
|
197 |
justify-content: center;
|
198 |
gap: 20px;
|
199 |
}
|
|
|
|
|
|
|
|
|
|
|
200 |
"""
|
201 |
|
202 |
-
#
|
203 |
with open("tubitech-su.png", "rb") as image_file:
|
204 |
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
|
205 |
icon_data_uri = f"data:image/png;base64,{encoded_string}"
|
206 |
|
207 |
-
# Create the HTML for the fixed icon.
|
208 |
icon_html = f"""
|
209 |
<div style="position: fixed; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 1000;">
|
210 |
-
<
|
|
|
|
|
|
|
|
|
211 |
</div>
|
212 |
"""
|
213 |
|
214 |
with gr.Blocks(css=css) as demo:
|
215 |
-
|
|
|
216 |
|
217 |
-
#
|
218 |
-
assistant_display = gr.Markdown(
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
-
# Answer buttons: Yes, No, I don't know.
|
223 |
with gr.Row(elem_classes="button-group"):
|
224 |
-
btn_yes = gr.Button("Yes")
|
225 |
-
btn_no = gr.Button("No")
|
226 |
-
btn_dont_know = gr.Button("I don't know")
|
227 |
|
228 |
-
# Evaluation buttons: Correct, Incorrect (hidden initially).
|
229 |
with gr.Row(elem_classes="button-group"):
|
230 |
-
btn_correct = gr.Button("Correct", visible=False)
|
231 |
-
btn_incorrect = gr.Button("Incorrect", visible=False)
|
232 |
|
233 |
-
|
234 |
-
|
235 |
-
btn_restart = gr.Button("Play Again", visible=False)
|
236 |
-
# - If incorrect, "Continue"
|
237 |
-
btn_continue = gr.Button("Continue", visible=False)
|
238 |
|
239 |
-
# Hidden states:
|
240 |
state = gr.State([])
|
241 |
final_state = gr.State("")
|
242 |
|
243 |
-
#
|
244 |
demo.load(
|
245 |
fn=start_game,
|
246 |
outputs=[
|
@@ -258,7 +278,7 @@ with gr.Blocks(css=css) as demo:
|
|
258 |
]
|
259 |
)
|
260 |
|
261 |
-
#
|
262 |
btn_yes.click(
|
263 |
fn=process_yes,
|
264 |
inputs=[state],
|
@@ -311,7 +331,6 @@ with gr.Blocks(css=css) as demo:
|
|
311 |
]
|
312 |
)
|
313 |
|
314 |
-
# Evaluation buttons:
|
315 |
btn_correct.click(
|
316 |
fn=evaluate_correct,
|
317 |
inputs=[final_state],
|
@@ -335,7 +354,6 @@ with gr.Blocks(css=css) as demo:
|
|
335 |
]
|
336 |
)
|
337 |
|
338 |
-
# When the "Play Again" button is clicked:
|
339 |
btn_restart.click(
|
340 |
fn=restart_game,
|
341 |
inputs=[],
|
@@ -354,7 +372,6 @@ with gr.Blocks(css=css) as demo:
|
|
354 |
]
|
355 |
)
|
356 |
|
357 |
-
# When the "Continue" button is clicked:
|
358 |
btn_continue.click(
|
359 |
fn=continue_game,
|
360 |
inputs=[state],
|
@@ -373,7 +390,6 @@ with gr.Blocks(css=css) as demo:
|
|
373 |
]
|
374 |
)
|
375 |
|
376 |
-
# Add the fixed icon with the embedded base64 image.
|
377 |
gr.HTML(icon_html)
|
378 |
|
379 |
demo.launch(debug=True, show_error=True)
|
|
|
13 |
)
|
14 |
|
15 |
def start_game():
|
|
|
|
|
|
|
|
|
|
|
16 |
history = []
|
17 |
result = client.predict(
|
18 |
query="",
|
|
|
39 |
eval_buttons_update, # btn_incorrect
|
40 |
"", # final_state
|
41 |
restart_update, # btn_restart
|
42 |
+
continue_update # btn_continue
|
43 |
)
|
44 |
|
45 |
def process_turn(user_answer, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
result = client.predict(
|
47 |
query=user_answer,
|
48 |
history=history,
|
|
|
57 |
end_idx = assistant_message.index("</answer>")
|
58 |
final_answer = assistant_message[start_idx:end_idx].strip()
|
59 |
if final_answer:
|
60 |
+
# Assistant kendini tahmin ettiyse, asistan metnini sakla, final cevabı göster
|
61 |
assistant_update = gr.update(visible=False)
|
62 |
final_text = f"**My guess:** **{final_answer}**"
|
63 |
answer_update = gr.update(visible=False)
|
|
|
65 |
restart_update = gr.update(visible=False)
|
66 |
continue_update = gr.update(visible=False)
|
67 |
else:
|
68 |
+
# Tahmin yoksa, asistan metnini göster, final cevabı temizle
|
69 |
assistant_update = gr.update(visible=True, value=assistant_message)
|
70 |
final_text = ""
|
71 |
answer_update = gr.update(visible=True)
|
|
|
73 |
restart_update = gr.update(visible=False)
|
74 |
continue_update = gr.update(visible=False)
|
75 |
return (
|
76 |
+
assistant_update,
|
77 |
+
final_text,
|
78 |
+
history,
|
79 |
+
answer_update,
|
80 |
+
answer_update,
|
81 |
+
answer_update,
|
82 |
+
eval_update,
|
83 |
+
eval_update,
|
84 |
+
final_answer,
|
85 |
+
restart_update,
|
86 |
+
continue_update
|
87 |
)
|
88 |
|
89 |
def process_yes(history):
|
|
|
96 |
return process_turn("I don't know", history)
|
97 |
|
98 |
def evaluate_correct(final_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
new_text = "**My guess is correct! Shall we play again?**"
|
100 |
return (
|
101 |
new_text,
|
102 |
+
gr.update(visible=False),
|
103 |
+
gr.update(visible=False),
|
104 |
+
gr.update(visible=True, value="Play Again"),
|
105 |
+
gr.update(visible=False)
|
106 |
)
|
107 |
|
108 |
def evaluate_incorrect(final_state):
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
new_text = "**Let's continue, press the continue button.**"
|
110 |
return (
|
111 |
new_text,
|
112 |
+
gr.update(visible=False),
|
113 |
+
gr.update(visible=False),
|
114 |
+
gr.update(visible=False),
|
115 |
+
gr.update(visible=True, value="Continue")
|
116 |
)
|
117 |
|
118 |
def continue_game(history):
|
|
|
|
|
|
|
|
|
119 |
result = client.predict(
|
120 |
query="",
|
121 |
history=history,
|
|
|
125 |
assistant_message = result[1][-1][1]
|
126 |
history.append(("", assistant_message))
|
127 |
return (
|
128 |
+
gr.update(visible=True, value=assistant_message),
|
129 |
+
gr.update(visible=True, value=""),
|
130 |
+
history,
|
131 |
+
gr.update(visible=True),
|
132 |
+
gr.update(visible=True),
|
133 |
+
gr.update(visible=True),
|
134 |
+
gr.update(visible=False),
|
135 |
+
gr.update(visible=False),
|
136 |
+
"",
|
137 |
+
gr.update(visible=False),
|
138 |
+
gr.update(visible=False)
|
139 |
)
|
140 |
|
141 |
def restart_game():
|
|
|
|
|
|
|
142 |
global client
|
143 |
client = Client("Qwen/Qwen2.5-72B-Instruct")
|
144 |
return start_game()
|
145 |
|
|
|
146 |
css = """
|
147 |
+
body, .gradio-container {
|
148 |
+
font-family: 'Roboto', sans-serif;
|
149 |
+
background-color: #f0f2f5;
|
150 |
+
background-image: url('https://cdn.glitch.global/e803869d-6fae-4322-93f8-f15353a38bf3/pexels-jeremy-bishop-1260133-2397651.jpg?v=1740128669007') !important;
|
151 |
+
background-size: cover !important;
|
152 |
+
background-position: center !important;
|
153 |
+
background-repeat: no-repeat !important;
|
154 |
+
background-attachment: fixed !important;
|
155 |
+
color: #333;
|
156 |
+
}
|
157 |
+
|
158 |
+
/* Üstteki başlık için stil */
|
159 |
+
.header-text {
|
160 |
+
text-align: center;
|
161 |
+
margin-top: 20px;
|
162 |
+
font-size: 2em;
|
163 |
+
color: #4261a8;
|
164 |
+
}
|
165 |
+
|
166 |
+
/* Özel buton sınıfımız: my-button */
|
167 |
+
.my-button {
|
168 |
+
background-color: #4261a8 !important; /* Arka plan rengi */
|
169 |
+
color: #fff !important; /* Yazı rengi */
|
170 |
+
border: none !important;
|
171 |
+
transition: transform 0.3s, background-color 0.3s !important;
|
172 |
+
}
|
173 |
+
.my-button:hover {
|
174 |
+
background-color: #1c4196 !important;
|
175 |
+
transform: scale(1.05);
|
176 |
+
}
|
177 |
+
|
178 |
+
/* Soru kutusu */
|
179 |
.question-box {
|
180 |
border: 2px solid #ccc;
|
181 |
padding: 10px;
|
|
|
183 |
margin: 10px auto;
|
184 |
width: 80%;
|
185 |
text-align: center;
|
186 |
+
background-color: rgba(173, 216, 230, 0); /* Yarı saydam soluk cyan */
|
187 |
}
|
188 |
+
|
189 |
+
/* Gradio Markdown’un varsayılan arka planını şeffaf yapmak */
|
190 |
+
.question-box .gr-prose {
|
191 |
+
background-color: transparent !important;
|
192 |
+
box-shadow: none !important;
|
193 |
+
border: none !important;
|
194 |
+
}
|
195 |
+
|
196 |
.final-answer {
|
197 |
font-size: 2em;
|
198 |
font-weight: bold;
|
199 |
text-align: center;
|
200 |
margin: 20px;
|
201 |
}
|
202 |
+
|
203 |
.button-group {
|
204 |
display: flex;
|
205 |
justify-content: center;
|
206 |
gap: 20px;
|
207 |
}
|
208 |
+
|
209 |
+
/* Gradio footer kısmını gizle */
|
210 |
+
footer, .footer, .gradio-footer {
|
211 |
+
display: none !important;
|
212 |
+
}
|
213 |
"""
|
214 |
|
215 |
+
# Yerel ikonu Base64'e çevirme
|
216 |
with open("tubitech-su.png", "rb") as image_file:
|
217 |
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
|
218 |
icon_data_uri = f"data:image/png;base64,{encoded_string}"
|
219 |
|
|
|
220 |
icon_html = f"""
|
221 |
<div style="position: fixed; bottom: 10px; left: 50%; transform: translateX(-50%); z-index: 1000;">
|
222 |
+
<div style="width: 60px; height: 60px; background-color: white; border-radius: 50%;
|
223 |
+
display: flex; justify-content: center; align-items: center;
|
224 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);">
|
225 |
+
<img src="{icon_data_uri}" alt="Icon" style="width: 50px; height: auto;">
|
226 |
+
</div>
|
227 |
</div>
|
228 |
"""
|
229 |
|
230 |
with gr.Blocks(css=css) as demo:
|
231 |
+
# Üstteki başlık
|
232 |
+
gr.Markdown("### TUBITECH # 9694 - Sea Creature Akinator", elem_classes="header-text")
|
233 |
|
234 |
+
# show_label=False ve label="" ile ekstra çerçeveyi kapatıyoruz
|
235 |
+
assistant_display = gr.Markdown(
|
236 |
+
value="",
|
237 |
+
label="",
|
238 |
+
show_label=False,
|
239 |
+
elem_classes="question-box"
|
240 |
+
)
|
241 |
+
final_answer_display = gr.Markdown(
|
242 |
+
value="",
|
243 |
+
label="",
|
244 |
+
show_label=False,
|
245 |
+
elem_classes="final-answer"
|
246 |
+
)
|
247 |
|
|
|
248 |
with gr.Row(elem_classes="button-group"):
|
249 |
+
btn_yes = gr.Button("Yes", elem_classes=["my-button"])
|
250 |
+
btn_no = gr.Button("No", elem_classes=["my-button"])
|
251 |
+
btn_dont_know = gr.Button("I don't know", elem_classes=["my-button"])
|
252 |
|
|
|
253 |
with gr.Row(elem_classes="button-group"):
|
254 |
+
btn_correct = gr.Button("Correct", visible=False, elem_classes=["my-button"])
|
255 |
+
btn_incorrect = gr.Button("Incorrect", visible=False, elem_classes=["my-button"])
|
256 |
|
257 |
+
btn_restart = gr.Button("Play Again", visible=False, elem_classes=["my-button"])
|
258 |
+
btn_continue = gr.Button("Continue", visible=False, elem_classes=["my-button"])
|
|
|
|
|
|
|
259 |
|
|
|
260 |
state = gr.State([])
|
261 |
final_state = gr.State("")
|
262 |
|
263 |
+
# Sayfa yüklendiğinde oyunu başlat
|
264 |
demo.load(
|
265 |
fn=start_game,
|
266 |
outputs=[
|
|
|
278 |
]
|
279 |
)
|
280 |
|
281 |
+
# Butonların click event'leri
|
282 |
btn_yes.click(
|
283 |
fn=process_yes,
|
284 |
inputs=[state],
|
|
|
331 |
]
|
332 |
)
|
333 |
|
|
|
334 |
btn_correct.click(
|
335 |
fn=evaluate_correct,
|
336 |
inputs=[final_state],
|
|
|
354 |
]
|
355 |
)
|
356 |
|
|
|
357 |
btn_restart.click(
|
358 |
fn=restart_game,
|
359 |
inputs=[],
|
|
|
372 |
]
|
373 |
)
|
374 |
|
|
|
375 |
btn_continue.click(
|
376 |
fn=continue_game,
|
377 |
inputs=[state],
|
|
|
390 |
]
|
391 |
)
|
392 |
|
|
|
393 |
gr.HTML(icon_html)
|
394 |
|
395 |
demo.launch(debug=True, show_error=True)
|