Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,89 +7,65 @@ def grade(student_code):
|
|
| 7 |
grade = 10 # Start with full points
|
| 8 |
|
| 9 |
try:
|
| 10 |
-
# Parse the student's code into an AST (Abstract Syntax Tree)
|
| 11 |
tree = ast.parse(student_code)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
blink_pattern = False
|
| 18 |
-
sleep_times = []
|
| 19 |
|
| 20 |
-
# Analyze the AST
|
| 21 |
for node in ast.walk(tree):
|
| 22 |
-
# Check pin setup
|
| 23 |
if isinstance(node, ast.Assign):
|
| 24 |
if isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Attribute):
|
| 25 |
if node.value.func.attr == "Pin":
|
| 26 |
args = node.value.args
|
| 27 |
if len(args) >= 2 and isinstance(args[0], ast.Constant):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Validate pin setup
|
| 47 |
-
if not pin_setup:
|
| 48 |
-
feedback.append("⛔️ Pin 21 must be correctly set as an output (-2 pts).")
|
| 49 |
-
grade -= 2
|
| 50 |
else:
|
| 51 |
-
feedback.append("✅ Correct pin setup detected (+
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
feedback.append("✅ Initial delay of 3 seconds detected (+2 pts).")
|
| 57 |
else:
|
| 58 |
-
feedback.append("
|
| 59 |
-
grade -= 2
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
feedback.append("⛔️ No `while True:` loop found! The LED must blink continuously (-2 pts).")
|
| 64 |
grade -= 2
|
| 65 |
else:
|
| 66 |
-
feedback.append("✅
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
feedback.append("✅ Blinking delay of 1 second detected (+2 pts).")
|
| 71 |
-
else:
|
| 72 |
-
feedback.append("⛔️ The LED must blink every second after the initial delay (-2 pts).")
|
| 73 |
-
grade -= 2
|
| 74 |
-
|
| 75 |
-
# Validate LED alternation
|
| 76 |
-
if not blink_pattern:
|
| 77 |
-
feedback.append("⛔️ The LED should alternate states in the loop (-2 pts).")
|
| 78 |
grade -= 2
|
| 79 |
else:
|
| 80 |
-
feedback.append("✅ LED
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
feedback = f"Error parsing code: {e}"
|
|
|
|
| 84 |
|
| 85 |
-
# Ensure the grade is not negative
|
| 86 |
grade = max(0, grade)
|
| 87 |
-
|
| 88 |
grade = f"{grade}/10"
|
| 89 |
-
|
| 90 |
feedback = "\n".join(feedback)
|
| 91 |
-
|
| 92 |
-
# Return feedback
|
| 93 |
return grade, feedback
|
| 94 |
|
| 95 |
# Interface Gradio seulement si le script est exécuté directement
|
|
|
|
| 7 |
grade = 10 # Start with full points
|
| 8 |
|
| 9 |
try:
|
|
|
|
| 10 |
tree = ast.parse(student_code)
|
| 11 |
|
| 12 |
+
pin_setup = {"22": False, "15": False}
|
| 13 |
+
button_used = False
|
| 14 |
+
led_toggle_detected = False
|
| 15 |
+
bool_variable_used = False
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
for node in ast.walk(tree):
|
|
|
|
| 18 |
if isinstance(node, ast.Assign):
|
| 19 |
if isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Attribute):
|
| 20 |
if node.value.func.attr == "Pin":
|
| 21 |
args = node.value.args
|
| 22 |
if len(args) >= 2 and isinstance(args[0], ast.Constant):
|
| 23 |
+
pin_num = str(args[0].value)
|
| 24 |
+
if pin_num in pin_setup:
|
| 25 |
+
pin_setup[pin_num] = True
|
| 26 |
+
elif isinstance(node.value, ast.NameConstant) and isinstance(node.targets[0], ast.Name):
|
| 27 |
+
bool_variable_used = True
|
| 28 |
+
|
| 29 |
+
if isinstance(node, ast.If):
|
| 30 |
+
if isinstance(node.test, ast.Call) and isinstance(node.test.func, ast.Attribute):
|
| 31 |
+
if node.test.func.attr == "value":
|
| 32 |
+
button_used = True
|
| 33 |
+
for stmt in node.body:
|
| 34 |
+
if isinstance(stmt, ast.Assign):
|
| 35 |
+
if isinstance(stmt.value, ast.UnaryOp) and isinstance(stmt.value.op, ast.Not):
|
| 36 |
+
led_toggle_detected = True
|
| 37 |
+
|
| 38 |
+
if not all(pin_setup.values()):
|
| 39 |
+
feedback.append("⛔️ Pins 22 (LED) and 15 (button) must be correctly set up (-3 pts).")
|
| 40 |
+
grade -= 3
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
else:
|
| 42 |
+
feedback.append("✅ Correct pin setup detected (+3 pts).")
|
| 43 |
|
| 44 |
+
if not button_used:
|
| 45 |
+
feedback.append("⛔️ No button press detection found (-3 pts).")
|
| 46 |
+
grade -= 3
|
|
|
|
| 47 |
else:
|
| 48 |
+
feedback.append("✅ Button press detection found (+3 pts).")
|
|
|
|
| 49 |
|
| 50 |
+
if not led_toggle_detected:
|
| 51 |
+
feedback.append("⛔️ No LED toggle logic detected (-2 pts).")
|
|
|
|
| 52 |
grade -= 2
|
| 53 |
else:
|
| 54 |
+
feedback.append("✅ LED toggle logic detected (+2 pts).")
|
| 55 |
|
| 56 |
+
if not bool_variable_used:
|
| 57 |
+
feedback.append("⛔️ No boolean variable found for tracking LED state (-2 pts).")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
grade -= 2
|
| 59 |
else:
|
| 60 |
+
feedback.append("✅ Boolean variable found for LED state tracking (+2 pts).")
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
+
feedback = [f"⛔️ Error parsing code: {e}"]
|
| 64 |
+
grade = 0
|
| 65 |
|
|
|
|
| 66 |
grade = max(0, grade)
|
|
|
|
| 67 |
grade = f"{grade}/10"
|
|
|
|
| 68 |
feedback = "\n".join(feedback)
|
|
|
|
|
|
|
| 69 |
return grade, feedback
|
| 70 |
|
| 71 |
# Interface Gradio seulement si le script est exécuté directement
|