Fix Chapter 11 quiz inference routing and failure handling

#10
by John6666 - opened
Files changed (2) hide show
  1. app.py +33 -4
  2. requirements.txt +1 -1
app.py CHANGED
@@ -10,6 +10,7 @@ import black # Add black import
10
  # Initialize the inference client
11
  client = InferenceClient(
12
  api_key=os.getenv("HF_TOKEN"), # Make sure to set this environment variable
 
13
  )
14
 
15
  # Load questions from Hugging Face dataset
@@ -98,11 +99,18 @@ def check_code(
98
 
99
  except Exception as e:
100
  gr.Warning(f"Error checking code: {str(e)}")
101
- # Fall back to simple string comparison if LLM fails
 
102
  is_correct = formatted_user_code.strip() == formatted_solution.strip()
103
- status = "✅ Correct!" if is_correct else "❌ Incorrect!"
104
- gr.Info(f"{status} (Fallback comparison)")
105
- return is_correct
 
 
 
 
 
 
106
 
107
 
108
  def on_user_logged_in(token: gr.OAuthToken | None):
@@ -226,6 +234,27 @@ def handle_quiz(question_idx, user_answers, submitted_code, is_start):
226
  current_q["challenge"],
227
  current_q["assessment_criteria"],
228
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  user_answers.append(
230
  {
231
  "challenge": current_q["challenge"],
 
10
  # Initialize the inference client
11
  client = InferenceClient(
12
  api_key=os.getenv("HF_TOKEN"), # Make sure to set this environment variable
13
+ provider="auto", # Use the current Inference Providers routing layer
14
  )
15
 
16
  # Load questions from Hugging Face dataset
 
99
 
100
  except Exception as e:
101
  gr.Warning(f"Error checking code: {str(e)}")
102
+ # Fall back to exact comparison if LLM fails. A mismatch is
103
+ # inconclusive while the automated grader is unavailable.
104
  is_correct = formatted_user_code.strip() == formatted_solution.strip()
105
+ if is_correct:
106
+ gr.Info(" Correct! (Exact-match fallback)")
107
+ return True
108
+
109
+ gr.Warning(
110
+ "The automated grader is temporarily unavailable. "
111
+ "Your answer was not marked incorrect. Please try again."
112
+ )
113
+ return None
114
 
115
 
116
  def on_user_logged_in(token: gr.OAuthToken | None):
 
234
  current_q["challenge"],
235
  current_q["assessment_criteria"],
236
  )
237
+ # Keep the current question and score unchanged if the grader is
238
+ # unavailable, so an infrastructure failure is not recorded as wrong.
239
+ if is_correct is None:
240
+ return (
241
+ f"## Question {question_idx + 1} \n### {current_q['challenge']}",
242
+ gr.update(value=formatted_code, visible=True),
243
+ (
244
+ "The automated grader is temporarily unavailable. "
245
+ "Your answer was not marked incorrect. Please try again."
246
+ ),
247
+ question_idx,
248
+ user_answers,
249
+ gr.update(visible=False),
250
+ gr.update(visible=True),
251
+ gr.update(visible=False),
252
+ gr.update(visible=False),
253
+ gr.update(
254
+ value=current_q["image"],
255
+ visible=True if current_q["image"] else False,
256
+ ),
257
+ )
258
  user_answers.append(
259
  {
260
  "challenge": current_q["challenge"],
requirements.txt CHANGED
@@ -34,7 +34,7 @@ gradio-client==1.7.0
34
  h11==0.14.0
35
  httpcore==1.0.7
36
  httpx==0.28.1
37
- huggingface-hub==0.28.1
38
  idna==3.10
39
  ipykernel==6.29.5
40
  ipython==8.32.0
 
34
  h11==0.14.0
35
  httpcore==1.0.7
36
  httpx==0.28.1
37
+ huggingface-hub==1.4.1
38
  idna==3.10
39
  ipykernel==6.29.5
40
  ipython==8.32.0