File size: 12,266 Bytes
6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb ac8a8fb 3a3fcba 13ab1cf 6f46aeb 13ab1cf 9951d91 6f46aeb f49bcc1 13ab1cf f49bcc1 9951d91 f49bcc1 13ab1cf f49bcc1 6f46aeb 0063041 13ab1cf f49bcc1 13ab1cf 6f46aeb 13ab1cf 6f46aeb f49bcc1 13ab1cf f49bcc1 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb f49bcc1 13ab1cf f49bcc1 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb 13ab1cf 6f46aeb f49bcc1 13ab1cf f49bcc1 6f46aeb 13ab1cf 6f46aeb f49bcc1 13ab1cf 6f46aeb e36ad24 13ab1cf 6f46aeb 13ab1cf 6f46aeb 13ab1cf 6f46aeb f49bcc1 13ab1cf f49bcc1 13ab1cf 6f46aeb 13ab1cf 9951d91 13ab1cf 9951d91 6f46aeb f49bcc1 13ab1cf f49bcc1 13ab1cf 6f46aeb 2035f66 81f6bfd 6f46aeb f49bcc1 9951d91 f49bcc1 9951d91 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb f49bcc1 6f46aeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
import os
from typing import List, Optional
from pydantic import BaseModel, Field
import gradio as gr
from datasets import load_dataset
from huggingface_hub import InferenceClient
import black
# Initialize the inference client
HF_TOKEN = os.getenv("HF_TOKEN")
HF_API_URL = os.getenv("HF_API_URL", "Qwen/Qwen2.5-Coder-32B-Instruct")
client = InferenceClient(model=HF_API_URL, token=HF_TOKEN)
# Load questions from Hugging Face dataset
EXAM_MAX_QUESTIONS = int(os.getenv("EXAM_MAX_QUESTIONS", 1))
EXAM_DATASET_ID = "agents-course/smolagents-quiz-data"
# prep the dataset for the quiz
ds = load_dataset(EXAM_DATASET_ID, split="train", download_mode="force_redownload")
quiz_data = list(ds)
if EXAM_MAX_QUESTIONS:
quiz_data = quiz_data[:EXAM_MAX_QUESTIONS]
# Check if dataset has image feature
HAS_IMAGE_FEATURE = "image" in ds.features
class CriterionFeedback(BaseModel):
"""Feedback for a single assessment criterion"""
criterion: str = Field(..., description="The assessment criterion being evaluated")
met: bool = Field(..., description="Whether the criterion was met")
explanation: str = Field(
..., description="Detailed explanation of how well the criterion was met"
)
improvement_tips: Optional[str] = Field(
None, description="Specific tips for improvement if needed"
)
class CodeFeedback(BaseModel):
"""Structured feedback for code submission"""
overall_feedback: str = Field(
..., description="Overall assessment of the code solution"
)
criteria_feedback: List[CriterionFeedback] = Field(
..., description="Detailed feedback for each assessment criterion"
)
def format_python_code(code: str) -> str:
"""Format Python code using black."""
try:
return black.format_str(code, mode=black.Mode())
except Exception as e:
gr.Warning(f"Code formatting failed: {str(e)}")
return code
EVALUATION_TEMPLATE = """Evaluate this Python code solution:
Challenge:
{challenge}
Reference Solution:
```python
{solution}
```
Student's Solution:
```python
{student_code}
```
Assessment Criteria:
{criteria}
Approach:
Be highly tollerent of differences in approach, as long as they meet Assessment Criteria.
Provide detailed feedback on how well each criterion was met."""
def check_code(
user_code: str, solution: str, challenge: str, assessment_criteria: List[str]
) -> dict:
"""
Use LLM to evaluate the user's code solution and provide structured feedback.
"""
# Format both user code and solution
formatted_user_code = format_python_code(user_code)
formatted_solution = format_python_code(solution)
# Format criteria as bullet points
criteria_text = "\n".join(f"- {c}" for c in assessment_criteria)
# Fill the template
prompt = EVALUATION_TEMPLATE.format(
challenge=challenge,
solution=formatted_solution,
student_code=formatted_user_code,
criteria=criteria_text,
)
try:
# Get structured feedback using response_format with schema from Pydantic model
response = client.text_generation(
prompt=prompt,
grammar={
"type": "json_object",
"value": CodeFeedback.model_json_schema(),
},
)
# Parse response into Pydantic model
feedback = CodeFeedback.model_validate_json(response)
# Format the feedback for display
formatted_feedback = [
f"### Overall Assessment\n{feedback.overall_feedback}\n\n"
]
for cf in feedback.criteria_feedback:
tip = cf.improvement_tips or ""
tip_text = f"\n💡 Tip: {tip}" if tip else ""
formatted_feedback.append(
f"### {cf.criterion}\n"
f"{'✅' if cf.met else '❌'} {cf.explanation}"
f"{tip_text}\n"
)
return {"feedback": "\n".join(formatted_feedback)}
except Exception as e:
gr.Warning(f"Error generating feedback: {str(e)}")
return {"feedback": "Unable to generate detailed feedback due to an error."}
def on_user_logged_in(token: gr.OAuthToken | None):
"""
Handle user login state.
On a valid token, hide the login button and reveal the Start button while keeping Next hidden.
Also, clear the question text, code input, status, and image.
"""
if token is not None:
return (
gr.update(visible=False), # login_btn hidden
gr.update(visible=True), # start_btn shown
gr.update(visible=False), # next_btn hidden
"", # Clear question_text
gr.update(value="", visible=False), # Clear code_input
"", # Clear status_text
gr.update(value="", visible=False), # Clear question_image
)
else:
return (
gr.update(visible=True), # login_btn visible
gr.update(visible=False), # start_btn hidden
gr.update(visible=False), # next_btn hidden
"",
gr.update(value="", visible=False),
"",
gr.update(value="", visible=False),
)
def handle_quiz(question_idx, user_answers, submitted_code, is_start):
"""Handle quiz state and progression"""
if is_start:
question_idx = 0
else:
# If not the first question and there's a submission, store it
if question_idx < len(quiz_data) and submitted_code.strip():
current_q = quiz_data[question_idx]
# Format the submitted code before checking
formatted_code = format_python_code(submitted_code)
feedback_dict = check_code(
formatted_code,
current_q["solution"],
current_q["challenge"],
current_q["assessment_criteria"],
)
user_answers.append(
{
"challenge": current_q["challenge"],
"submitted_code": formatted_code,
"correct_solution": current_q["solution"],
"assessment_criteria": current_q["assessment_criteria"],
"feedback": feedback_dict["feedback"],
}
)
question_idx += 1
# If we've reached the end, show final results
if question_idx >= len(quiz_data):
results_text = """## Code Review Complete! 📚
This feedback should help you improve your skills.
⛔️ The feedback uses Qwen/Qwen2.5-Coder-32B-Instruct to compare your response to a gold
standard solution. As we know, LLMs are not perfect. You should compare your work against
the assessment criteria if you doubt the feedback.
Here's your detailed feedback:"""
for idx, answer in enumerate(user_answers):
# Format assessment criteria as bullet points
criteria_bullets = "\n".join(
f"- {c}" for c in answer["assessment_criteria"]
)
# Build the results text piece by piece
results_text += (
f"### Question {idx + 1}: {answer['challenge']}\n\n"
"#### Your Solution:\n```python\n"
f"{answer['submitted_code']}\n```\n\n"
"#### Reference Solution:\n```python\n"
f"{answer['correct_solution']}\n```\n\n"
"#### Assessment Criteria:\n"
f"{criteria_bullets}\n\n"
"#### Feedback:\n"
f"{answer['feedback']}\n\n"
"---\n\n"
)
return (
"", # question_text cleared
gr.update(value="", visible=False), # hide code_input
"Review your feedback below to improve your coding skills!",
question_idx, # updated question index
user_answers, # accumulated answers
gr.update(visible=False), # start_btn hidden
gr.update(visible=False), # next_btn hidden
gr.update(value=results_text, visible=True), # final_markdown
gr.update(visible=False), # question_image hidden
)
else:
# Show the next question
q = quiz_data[question_idx]
# Format assessment criteria as bullet points
criteria_bullets = "\n".join(f"- {c}" for c in q["assessment_criteria"])
challenge_text = (
f"## Question {question_idx + 1}\n\n"
f"### Challenge:\n{q['challenge']}\n\n"
"### Assessment Criteria:\n"
f"{criteria_bullets}"
)
# Only show image if the feature exists and question has an image
show_image = HAS_IMAGE_FEATURE and q.get("image") is not None
image_update = gr.update(
value=q.get("image") if show_image else None, visible=show_image
)
return (
challenge_text, # question_text
gr.update(value=q["placeholder"], visible=True), # code_input
"Submit your solution and click 'Next' to continue.",
question_idx, # updated question_idx
user_answers, # user_answers
gr.update(visible=False), # start_btn hidden
gr.update(visible=True), # next_btn visible
gr.update(visible=False), # final_markdown hidden
image_update, # question_image
)
with gr.Blocks() as demo:
demo.title = f"Coding Quiz: {EXAM_DATASET_ID}"
# State variables
question_idx = gr.State(value=0)
user_answers = gr.State(value=[])
with gr.Row(variant="compact"):
intro_text = """
## Welcome to the smolagents code reviewer
This application will review your smolagents code, and provide feedback on your solutions. This exercise is not reviewed or certified! It's about trying out smolagents for the first time.
ℹ️ Log in first, then click 'Start' to begin. Complete each coding challenge and click 'Next' to proceed. You'll get feedback on your solutions at the end."""
intro_text = gr.Markdown(intro_text)
with gr.Row(variant="panel"):
with gr.Column():
question_text = gr.Markdown("")
question_image = gr.Image(
label="Question Image",
visible=True if HAS_IMAGE_FEATURE else False,
type="pil",
) # Add image component
with gr.Column():
code_input = gr.Code(
language="python", label="Your Solution", visible=False
)
with gr.Row(variant="compact"):
status_text = gr.Markdown("")
with gr.Row(variant="compact"):
login_btn = gr.LoginButton()
start_btn = gr.Button("Start")
next_btn = gr.Button("Next ⏭️", visible=False)
with gr.Row(variant="compact"):
final_markdown = gr.Markdown("", visible=False)
login_btn.click(
fn=on_user_logged_in,
inputs=None,
outputs=[
login_btn,
start_btn,
next_btn,
question_text,
code_input,
status_text,
question_image,
],
)
start_btn.click(
fn=handle_quiz,
inputs=[question_idx, user_answers, code_input, gr.State(True)],
outputs=[
question_text, # Markdown with question text
code_input, # Code input field
status_text, # Status text (instructions/status messages)
question_idx, # Updated question index (state)
user_answers, # Updated user answers (state)
start_btn, # Update for start button (will be hidden)
next_btn, # Update for next button (shown for in-progress quiz)
final_markdown, # Final results markdown (hidden until quiz ends)
question_image, # Image update for the quiz question
],
)
next_btn.click(
fn=handle_quiz,
inputs=[question_idx, user_answers, code_input, gr.State(False)],
outputs=[
question_text,
code_input,
status_text,
question_idx,
user_answers,
start_btn,
next_btn,
final_markdown,
question_image,
],
)
if __name__ == "__main__":
demo.launch()
|