Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -128,7 +128,8 @@ def check_answer(state, user_answer):
|
|
| 128 |
return result_text + "\n" + difficulty_update, is_correct
|
| 129 |
|
| 130 |
# -----------------------------
|
| 131 |
-
# 7. Build the Gradio Interface with a Hip, Rainbow Theme
|
|
|
|
| 132 |
# -----------------------------
|
| 133 |
custom_css = """
|
| 134 |
body {
|
|
@@ -175,8 +176,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 175 |
user_answer = gr.Textbox(label="Your Answer")
|
| 176 |
submit_button = gr.Button("Submit Answer")
|
| 177 |
result_output = gr.Textbox(label="Result", interactive=False)
|
| 178 |
-
|
| 179 |
-
fireworks_output = gr.HTML(label="Celebration", value="")
|
| 180 |
|
| 181 |
def update_difficulty_label(state):
|
| 182 |
return f"**Current Level**: {state['difficulty']} (Score: {state['score']})"
|
|
@@ -187,33 +187,38 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 187 |
def on_generate_question(state):
|
| 188 |
question = generate_question(state)
|
| 189 |
difficulty_text = update_difficulty_label(state)
|
| 190 |
-
# Clear
|
| 191 |
return question, difficulty_text, ""
|
| 192 |
|
| 193 |
generate_button.click(
|
| 194 |
fn=on_generate_question,
|
| 195 |
inputs=state,
|
| 196 |
-
outputs=[question_output, difficulty_label,
|
| 197 |
)
|
| 198 |
|
| 199 |
def on_submit_answer(user_answer, state):
|
| 200 |
feedback, is_correct = check_answer(state, user_answer)
|
| 201 |
difficulty_text = update_difficulty_label(state)
|
| 202 |
if is_correct:
|
| 203 |
-
#
|
| 204 |
-
|
| 205 |
<div style="text-align: center;">
|
| 206 |
-
<img src="https://media.giphy.com/media/
|
| 207 |
</div>
|
| 208 |
"""
|
| 209 |
else:
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
submit_button.click(
|
| 214 |
fn=on_submit_answer,
|
| 215 |
inputs=[user_answer, state],
|
| 216 |
-
outputs=[result_output, difficulty_label,
|
| 217 |
)
|
| 218 |
|
| 219 |
demo.launch()
|
|
|
|
| 128 |
return result_text + "\n" + difficulty_update, is_correct
|
| 129 |
|
| 130 |
# -----------------------------
|
| 131 |
+
# 7. Build the Gradio Interface with a Hip, Rainbow Theme
|
| 132 |
+
# Display a confetti GIF for correct answers and a red X GIF for wrong answers.
|
| 133 |
# -----------------------------
|
| 134 |
custom_css = """
|
| 135 |
body {
|
|
|
|
| 176 |
user_answer = gr.Textbox(label="Your Answer")
|
| 177 |
submit_button = gr.Button("Submit Answer")
|
| 178 |
result_output = gr.Textbox(label="Result", interactive=False)
|
| 179 |
+
feedback_gif_output = gr.HTML(label="Celebration / Error", value="")
|
|
|
|
| 180 |
|
| 181 |
def update_difficulty_label(state):
|
| 182 |
return f"**Current Level**: {state['difficulty']} (Score: {state['score']})"
|
|
|
|
| 187 |
def on_generate_question(state):
|
| 188 |
question = generate_question(state)
|
| 189 |
difficulty_text = update_difficulty_label(state)
|
| 190 |
+
# Clear any previous GIF when a new challenge starts.
|
| 191 |
return question, difficulty_text, ""
|
| 192 |
|
| 193 |
generate_button.click(
|
| 194 |
fn=on_generate_question,
|
| 195 |
inputs=state,
|
| 196 |
+
outputs=[question_output, difficulty_label, feedback_gif_output]
|
| 197 |
)
|
| 198 |
|
| 199 |
def on_submit_answer(user_answer, state):
|
| 200 |
feedback, is_correct = check_answer(state, user_answer)
|
| 201 |
difficulty_text = update_difficulty_label(state)
|
| 202 |
if is_correct:
|
| 203 |
+
# Confetti GIF for correct answers
|
| 204 |
+
gif_html = """
|
| 205 |
<div style="text-align: center;">
|
| 206 |
+
<img src="https://media.giphy.com/media/l4KhQo2MESJkc6QbS/giphy.gif" alt="Confetti!" style="width: 300px;"/>
|
| 207 |
</div>
|
| 208 |
"""
|
| 209 |
else:
|
| 210 |
+
# Red X or "Nope" GIF for incorrect answers
|
| 211 |
+
gif_html = """
|
| 212 |
+
<div style="text-align: center;">
|
| 213 |
+
<img src="https://media.giphy.com/media/hPPx8yk3Bmqys/giphy.gif" alt="Incorrect!" style="width: 200px;"/>
|
| 214 |
+
</div>
|
| 215 |
+
"""
|
| 216 |
+
return feedback, difficulty_text, gif_html
|
| 217 |
|
| 218 |
submit_button.click(
|
| 219 |
fn=on_submit_answer,
|
| 220 |
inputs=[user_answer, state],
|
| 221 |
+
outputs=[result_output, difficulty_label, feedback_gif_output]
|
| 222 |
)
|
| 223 |
|
| 224 |
demo.launch()
|