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 lines10111213141582 unmodified lines9899100101102103104105106107108117 unmodified lines2262272282292302319 unmodified lines# Initialize the inference clientclient = InferenceClient(api_key=os.getenv("HF_TOKEN"), # Make sure to set this environment variable)# Load questions from Hugging Face dataset82 unmodified linesexcept Exception as e:gr.Warning(f"Error checking code: {str(e)}")# Fall back to simple string comparison if LLM failsis_correct = formatted_user_code.strip() == formatted_solution.strip()status = "✅ Correct!" if is_correct else "❌ Incorrect!"gr.Info(f"{status} (Fallback comparison)")return is_correctdef on_user_logged_in(token: gr.OAuthToken | None):117 unmodified linescurrent_q["challenge"],current_q["assessment_criteria"],)user_answers.append({"challenge": current_q["challenge"],9 unmodified lines1011121314151682 unmodified lines99100101102103104105106107108109110111112113114115116117 unmodified lines2342352362372382392402412422432442452462472482492502512522532542552562572582592609 unmodified lines# Initialize the inference clientclient = InferenceClient(api_key=os.getenv("HF_TOKEN"), # Make sure to set this environment variableprovider="auto", # Use the current Inference Providers routing layer)# Load questions from Hugging Face dataset82 unmodified linesexcept Exception as e:gr.Warning(f"Error checking code: {str(e)}")# 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()if is_correct:gr.Info("✅ Correct! (Exact-match fallback)")return Truegr.Warning("The automated grader is temporarily unavailable. ""Your answer was not marked incorrect. Please try again.")return Nonedef on_user_logged_in(token: gr.OAuthToken | None):117 unmodified linescurrent_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 lines3435363738394033 unmodified linesh11==0.14.0httpcore==1.0.7httpx==0.28.1huggingface-hub==0.28.1idna==3.10ipykernel==6.29.5ipython==8.32.033 unmodified lines3435363738394033 unmodified linesh11==0.14.0httpcore==1.0.7httpx==0.28.1huggingface-hub==1.4.1idna==3.10ipykernel==6.29.5ipython==8.32.0
app.py+33-4
requirements.txt+1-1