Fix Chapter 11 quiz inference routing and failure handling
Browse filesThis PR follows up on the following forum report:
https://huggingface.co/proxy/discuss.huggingface.co/t/chapter-11-code-quiz-fails-due-to-api-inference-huggingface-co-dns-resolution-error/177781
The grader currently fails while trying to reach `api-inference.huggingface.co`, after which valid alternative solutions may be marked as incorrect by the string-comparison fallback.
### Changes
* Update `huggingface-hub` from `0.28.1` to `1.4.1`.
* Use `provider="auto"` with `InferenceClient`.
* Keep the existing grader model and prompt unchanged.
* If inference fails:
* accept an exact formatted-code match;
* otherwise report that the grader is unavailable without changing the score or advancing the quiz.
The patch is intentionally limited to restoring current inference routing and preventing infrastructure failures from being recorded as incorrect answers.
The patched Space was also confirmed to start successfully.
- app.py +33 -4
- requirements.txt +1 -1
|
@@ -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
|
|
|
|
| 102 |
is_correct = formatted_user_code.strip() == formatted_solution.strip()
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"],
|
|
@@ -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==
|
| 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
|