Fix Chapter 11 quiz inference routing and failure handling
#10
by John6666 - opened
Files changed (2)
- app.py+33-4
- requirements.txt+1-1
9 unmodified lines1011121314151682 unmodified lines99100101101102103104103104105105106107108109110111112113114115116117 unmodified lines2342352362372382392402412422432442452462472482492502512522532542552562572582592609 unmodified lines# Initialize the inference clientclient = InferenceClient( api_key=os.getenv("HF_TOKEN"), # Make sure to set this environment variable provider="auto", # Use the current Inference Providers routing layer)
# Load questions from Hugging Face dataset82 unmodified lines
except Exception as e: gr.Warning(f"Error checking code: {str(e)}") # Fall back to simple string comparison if LLM fails # Fall back to exact comparison if LLM fails. A mismatch is # inconclusive while the automated grader is unavailable. is_correct = formatted_user_code.strip() == formatted_solution.strip() status = "✅ Correct!" if is_correct else "❌ Incorrect!" gr.Info(f"{status} (Fallback comparison)") return is_correct if is_correct: gr.Info("✅ Correct! (Exact-match fallback)") return True
gr.Warning( "The automated grader is temporarily unavailable. " "Your answer was not marked incorrect. Please try again." ) return None
def on_user_logged_in(token: gr.OAuthToken | None):117 unmodified lines current_q["challenge"], current_q["assessment_criteria"], ) # Keep the current question and score unchanged if the grader is # unavailable, so an infrastructure failure is not recorded as wrong. if is_correct is None: return ( f"## Question {question_idx + 1} \n### {current_q['challenge']}", gr.update(value=formatted_code, visible=True), ( "The automated grader is temporarily unavailable. " "Your answer was not marked incorrect. Please try again." ), question_idx, user_answers, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update( value=current_q["image"], visible=True if current_q["image"] else False, ), ) user_answers.append( { "challenge": current_q["challenge"],33 unmodified lines343536373738394033 unmodified linesh11==0.14.0httpcore==1.0.7httpx==0.28.1huggingface-hub==0.28.1huggingface-hub==1.4.1idna==3.10ipykernel==6.29.5ipython==8.32.0app.py+33-4
requirements.txt+1-1