Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -195,3 +195,70 @@ def score_drawing(image_path, expected_sides):
|
|
195 |
except Exception as e:
|
196 |
print(f"[OpenCV ERROR] Failed to process image: {e}")
|
197 |
return 0, 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
except Exception as e:
|
196 |
print(f"[OpenCV ERROR] Failed to process image: {e}")
|
197 |
return 0, 0
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
# --- NEW: Gradio UI Helper Functions ---
|
202 |
+
|
203 |
+
def save_final_answer(current_index, current_answer, all_answers):
|
204 |
+
"""A dedicated function to save the last answer before submitting."""
|
205 |
+
all_answers[current_index] = current_answer
|
206 |
+
return all_answers
|
207 |
+
|
208 |
+
def update_view(new_index, all_answers, module):
|
209 |
+
"""Updates the UI elements when navigating to a new question."""
|
210 |
+
q_data = module.STRUCTURED_QUESTIONS[new_index]
|
211 |
+
progress = f"## {q_data['main_cat']} - Q{q_data['main_num']}{q_data['sub_letter']} ({new_index + 1} of {module.TOTAL_QUESTIONS})"
|
212 |
+
|
213 |
+
is_drawing_q = "draw a copy" in q_data["question"]
|
214 |
+
|
215 |
+
return (
|
216 |
+
f"Say 🔊 {q_data['question']}",
|
217 |
+
all_answers[new_index],
|
218 |
+
new_index,
|
219 |
+
progress,
|
220 |
+
q_data["instruction"],
|
221 |
+
module.QUESTION_CHOICES[new_index],
|
222 |
+
gr.update(visible=is_drawing_q),
|
223 |
+
None # Clear the audio_input component
|
224 |
+
)
|
225 |
+
|
226 |
+
def save_and_navigate(direction, current_index, current_answer, all_answers, module):
|
227 |
+
"""Saves the current answer and moves to the next/previous question."""
|
228 |
+
all_answers[current_index] = current_answer
|
229 |
+
if direction == "next":
|
230 |
+
new_index = min(current_index + 1, module.TOTAL_QUESTIONS - 1)
|
231 |
+
else: # prev
|
232 |
+
new_index = max(current_index - 1, 0)
|
233 |
+
return update_view(new_index, all_answers, module) + (all_answers,)
|
234 |
+
|
235 |
+
def jump_to_question(selected_choice, current_index, current_answer, all_answers, module):
|
236 |
+
"""Saves the current answer and jumps to the selected question."""
|
237 |
+
if not selected_choice:
|
238 |
+
return update_view(current_index, all_answers, module) + (all_answers,)
|
239 |
+
all_answers[current_index] = current_answer
|
240 |
+
new_index = module.QUESTION_CHOICES.index(selected_choice)
|
241 |
+
return update_view(new_index, all_answers, module) + (all_answers,)
|
242 |
+
|
243 |
+
def reset_app(module):
|
244 |
+
"""Resets the state of an assessment tab to its initial view."""
|
245 |
+
initial_q = module.STRUCTURED_QUESTIONS[0]
|
246 |
+
is_drawing_q = "draw a copy" in initial_q["question"]
|
247 |
+
return (
|
248 |
+
0, # question_index
|
249 |
+
[""] * module.TOTAL_QUESTIONS, # answers
|
250 |
+
"", # score_lines
|
251 |
+
"", # total
|
252 |
+
f"Say 🔊 {initial_q['question']}", # question_button
|
253 |
+
f"## {initial_q['main_cat']} - Q{initial_q['main_num']}{initial_q['sub_letter']} (1 of {module.TOTAL_QUESTIONS})",
|
254 |
+
initial_q["instruction"],
|
255 |
+
"", # answer_text
|
256 |
+
module.QUESTION_CHOICES[0], # jump_nav
|
257 |
+
None, # audio_input
|
258 |
+
None, # image_upload
|
259 |
+
gr.update(visible=False), # start_over_btn
|
260 |
+
gr.update(visible=True), # submit_btn
|
261 |
+
None, # tts_audio
|
262 |
+
"" # score_state
|
263 |
+
)
|
264 |
+
|