Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -111,6 +111,7 @@ def generate_question(state):
|
|
111 |
|
112 |
# -----------------------------
|
113 |
# 6. Check the User's Answer
|
|
|
114 |
# -----------------------------
|
115 |
def check_answer(state, user_answer):
|
116 |
correct_answer = state["answer"].lower().strip()
|
@@ -118,14 +119,16 @@ def check_answer(state, user_answer):
|
|
118 |
if user_answer_clean == correct_answer:
|
119 |
state["score"] += 1
|
120 |
result_text = "Correct! Nice work!"
|
|
|
121 |
else:
|
122 |
state["score"] -= 1
|
123 |
result_text = f"Oops! The correct answer was: {state['answer']}"
|
|
|
124 |
difficulty_update = adjust_difficulty(state)
|
125 |
-
return result_text + "\n" + difficulty_update
|
126 |
|
127 |
# -----------------------------
|
128 |
-
# 7. Build the Gradio Interface with a Hip, Rainbow Theme
|
129 |
# -----------------------------
|
130 |
custom_css = """
|
131 |
body {
|
@@ -153,6 +156,7 @@ body {
|
|
153 |
"""
|
154 |
|
155 |
with gr.Blocks(css=custom_css) as demo:
|
|
|
156 |
state = gr.State(init_state())
|
157 |
|
158 |
gr.Markdown("# Lingo Quest: The Ultimate Word Adventure")
|
@@ -171,24 +175,45 @@ with gr.Blocks(css=custom_css) as demo:
|
|
171 |
user_answer = gr.Textbox(label="Your Answer")
|
172 |
submit_button = gr.Button("Submit Answer")
|
173 |
result_output = gr.Textbox(label="Result", interactive=False)
|
|
|
|
|
174 |
|
175 |
def update_difficulty_label(state):
|
176 |
return f"**Current Level**: {state['difficulty']} (Score: {state['score']})"
|
177 |
|
|
|
178 |
demo.load(fn=update_difficulty_label, inputs=state, outputs=difficulty_label)
|
179 |
|
180 |
def on_generate_question(state):
|
181 |
question = generate_question(state)
|
182 |
difficulty_text = update_difficulty_label(state)
|
183 |
-
|
|
|
184 |
|
185 |
-
generate_button.click(
|
|
|
|
|
|
|
|
|
186 |
|
187 |
def on_submit_answer(user_answer, state):
|
188 |
-
feedback = check_answer(state, user_answer)
|
189 |
difficulty_text = update_difficulty_label(state)
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
-
submit_button.click(
|
|
|
|
|
|
|
|
|
193 |
|
194 |
demo.launch()
|
|
|
111 |
|
112 |
# -----------------------------
|
113 |
# 6. Check the User's Answer
|
114 |
+
# Returns both the feedback message and a flag indicating correctness.
|
115 |
# -----------------------------
|
116 |
def check_answer(state, user_answer):
|
117 |
correct_answer = state["answer"].lower().strip()
|
|
|
119 |
if user_answer_clean == correct_answer:
|
120 |
state["score"] += 1
|
121 |
result_text = "Correct! Nice work!"
|
122 |
+
is_correct = True
|
123 |
else:
|
124 |
state["score"] -= 1
|
125 |
result_text = f"Oops! The correct answer was: {state['answer']}"
|
126 |
+
is_correct = False
|
127 |
difficulty_update = adjust_difficulty(state)
|
128 |
+
return result_text + "\n" + difficulty_update, is_correct
|
129 |
|
130 |
# -----------------------------
|
131 |
+
# 7. Build the Gradio Interface with a Hip, Rainbow Theme and Fireworks Effects
|
132 |
# -----------------------------
|
133 |
custom_css = """
|
134 |
body {
|
|
|
156 |
"""
|
157 |
|
158 |
with gr.Blocks(css=custom_css) as demo:
|
159 |
+
# Create persistent session state.
|
160 |
state = gr.State(init_state())
|
161 |
|
162 |
gr.Markdown("# Lingo Quest: The Ultimate Word Adventure")
|
|
|
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 |
+
# New HTML output to display fireworks or celebration visuals.
|
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']})"
|
183 |
|
184 |
+
# Update the difficulty label when the interface loads.
|
185 |
demo.load(fn=update_difficulty_label, inputs=state, outputs=difficulty_label)
|
186 |
|
187 |
def on_generate_question(state):
|
188 |
question = generate_question(state)
|
189 |
difficulty_text = update_difficulty_label(state)
|
190 |
+
# Clear fireworks 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, fireworks_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 |
+
# Fireworks effect: using an embedded GIF.
|
204 |
+
fireworks_html = """
|
205 |
+
<div style="text-align: center;">
|
206 |
+
<img src="https://media.giphy.com/media/3o6Zt481isNVuQI1l6/giphy.gif" alt="Fireworks!" style="width: 300px;"/>
|
207 |
+
</div>
|
208 |
+
"""
|
209 |
+
else:
|
210 |
+
fireworks_html = ""
|
211 |
+
return feedback, difficulty_text, fireworks_html
|
212 |
|
213 |
+
submit_button.click(
|
214 |
+
fn=on_submit_answer,
|
215 |
+
inputs=[user_answer, state],
|
216 |
+
outputs=[result_output, difficulty_label, fireworks_output]
|
217 |
+
)
|
218 |
|
219 |
demo.launch()
|