bgaultier commited on
Commit
aed9387
·
verified ·
1 Parent(s): 37e94dc

Instructions fixed

Browse files
Files changed (1) hide show
  1. app.py +43 -46
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import ast
3
 
4
-
5
  def grade(student_code):
6
  feedback = []
 
7
  grade = 10 # Start with full points
8
 
9
  try:
@@ -11,11 +11,10 @@ def grade(student_code):
11
  tree = ast.parse(student_code)
12
 
13
  # Variables to track correctness
14
- pin_setup = False
15
- initial_delay = False
16
- loop_found = False
17
- blink_pattern = False
18
  sleep_times = []
 
 
19
 
20
  # Analyze the AST
21
  for node in ast.walk(tree):
@@ -25,8 +24,9 @@ def grade(student_code):
25
  if node.value.func.attr == "Pin":
26
  args = node.value.args
27
  if len(args) >= 2 and isinstance(args[0], ast.Constant):
28
- if str(args[0].value) == "21":
29
- pin_setup = True
 
30
 
31
  # Check for time.sleep() calls
32
  if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
@@ -40,74 +40,71 @@ def grade(student_code):
40
  # Check LED alternation
41
  if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call):
42
  if isinstance(node.value.func, ast.Attribute):
43
- if node.value.func.attr == "value" or node.value.func.attr == "on" or node.value.func.attr == "off":
44
- blink_pattern = True
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 (+2 pts).")
52
-
53
- # Validate initial delay
54
- if 3 in sleep_times:
55
- initial_delay = True
56
- feedback.append("✅ Initial delay of 3 seconds detected (+2 pts).")
57
- else:
58
- feedback.append("⛔️ The LED must stay ON for 3 seconds initially (-2 pts).")
59
- grade -= 2
60
 
61
  # Validate while loop
62
  if not loop_found:
63
- feedback.append("⛔️ No `while True:` loop found! The LED must blink continuously (-2 pts).")
 
64
  grade -= 2
65
  else:
66
  feedback.append("✅ Infinite loop detected (+2 pts).")
67
 
68
- # Validate blinking pattern
69
- if 1 in sleep_times:
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 alternation detected (+2 pts).")
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
96
  if __name__ == "__main__":
97
- with gr.Blocks(gr.themes.Default(primary_hue="cyan")) as demo:
98
  with gr.Row():
99
  instructions = gr.Markdown("""
100
  ## Instructions
101
- * Based on this [example]() , write a MicroPython code that will light up a red LED connected to pin `21` for 3 seconds. Once the 3 seconds have elapsed, make this LED blink every second (1 second off, then 1 second on) indefinitely!
102
-
103
- <img src="gradio_api/file/assets/blink.gif" alt="Level crossing light" width="64" style="margin-top: 16px; margin-bottom: 16px; margin-left: 50%; margin-right: 50%" />
104
-
105
- * You can use the [Vittascience simulator](https://fr.vittascience.com/esp32/?mode=code&console=bottom&toolbox=scratch&simu=1&board=basic-esp32) to test your code.
106
- * Copy and paste your code into the code editor on the right.
107
- * Click the 'Grade' button to get a grade and feedback.
108
- * If you are not satisfied with your grade, you can improve your code and click the 'Grade' button again.
109
- * You can click the 'Grade' button as many times as you want.
110
- """)
111
  code = gr.Code(label="MicroPython code", language="python", value='import machine\n\n# Your code here', lines=24)
112
  with gr.Row():
113
  feedback_output = gr.Textbox(label="Feedback", value="Click on the 'Grade' button to get feedback", interactive=False, scale=4)
@@ -115,4 +112,4 @@ if __name__ == "__main__":
115
  grade_btn = gr.Button("Grade", variant="primary")
116
  grade_btn.click(fn=grade, inputs=code, outputs=[grade_output, feedback_output], api_name="grade")
117
 
118
- demo.launch(show_error=False, allowed_paths=["/assets", "assets"], show_api=False)
 
1
  import gradio as gr
2
  import ast
3
 
 
4
  def grade(student_code):
5
  feedback = []
6
+ passed = True
7
  grade = 10 # Start with full points
8
 
9
  try:
 
11
  tree = ast.parse(student_code)
12
 
13
  # Variables to track correctness
14
+ pin_setup = {"21": False, "22": False}
 
 
 
15
  sleep_times = []
16
+ loop_found = False
17
+ led_alternation = False
18
 
19
  # Analyze the AST
20
  for node in ast.walk(tree):
 
24
  if node.value.func.attr == "Pin":
25
  args = node.value.args
26
  if len(args) >= 2 and isinstance(args[0], ast.Constant):
27
+ pin_num = str(args[0].value)
28
+ if pin_num in pin_setup:
29
+ pin_setup[pin_num] = True
30
 
31
  # Check for time.sleep() calls
32
  if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
 
40
  # Check LED alternation
41
  if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call):
42
  if isinstance(node.value.func, ast.Attribute):
43
+ if node.value.func.attr == "value":
44
+ led_alternation = True
45
 
46
  # Validate pin setup
47
+ if not all(pin_setup.values()):
48
+ feedback.append("⛔️ Both pins 21 and 22 must be correctly set as outputs (-3 pts).")
49
+ passed = False
50
+ grade -= 3
51
  else:
52
+ feedback.append("✅ Correct pin setup detected (+3 pts).")
 
 
 
 
 
 
 
 
53
 
54
  # Validate while loop
55
  if not loop_found:
56
+ feedback.append("⛔️ No `while True:` loop found! The LEDs must blink continuously (-2 pts).")
57
+ passed = False
58
  grade -= 2
59
  else:
60
  feedback.append("✅ Infinite loop detected (+2 pts).")
61
 
62
+ # Validate time.sleep() usage
63
+ if not sleep_times:
64
+ feedback.append("⛔️ No `time.sleep()` found! The LEDs need a delay to blink (-2 pts).")
65
+ passed = False
66
+ grade -= 2
67
  else:
68
+ feedback.append(" Appropriate delay function detected (+2 pts).")
69
+
70
+ # Validate sleep time range
71
+ if any(t < 0.3 or t > 1 for t in sleep_times):
72
+ feedback.append("⛔️ Delay time should be between 0.3s and 1s for a realistic emergency light effect (-2 pts).")
73
  grade -= 2
74
+ else:
75
+ feedback.append("✅ Delay time is within a good range (+2 pts).")
76
 
77
  # Validate LED alternation
78
+ if not led_alternation:
79
+ feedback.append("⛔️ The LEDs should alternate states in the loop (-1 pt).")
80
+ grade -= 1
81
  else:
82
+ feedback.append("✅ LED alternation detected (+1 pt).")
83
 
84
  except Exception as e:
85
+ feedback = [f"Erreur d'analyse du code: {e}"]
86
 
87
  # Ensure the grade is not negative
88
  grade = max(0, grade)
 
89
  grade = f"{grade}/10"
90
 
91
  feedback = "\n".join(feedback)
92
+
 
93
  return grade, feedback
94
 
95
  # Interface Gradio seulement si le script est exécuté directement
96
  if __name__ == "__main__":
97
+ with gr.Blocks(gr.themes.Soft(primary_hue="cyan")) as demo:
98
  with gr.Row():
99
  instructions = gr.Markdown("""
100
  ## Instructions
101
+ 1. Based on the examples you've seen so far, write a MicroPython code that follows the instructions above.
102
+ 2. You can use the [Vittascience simulator](https://fr.vittascience.com/esp32/?mode=code&console=bottom&toolbox=scratch&simu=1&board=basic-esp32) to test your code.
103
+ 2. Click the "Grade" button to get a grade and feedback.
104
+ 3. Make sure your code is well-structured and efficient.
105
+ 4. When you're happy with your code, please upload your solution below to get graded.
106
+ 6. Good luck!
107
+ """, container=True)
 
 
 
108
  code = gr.Code(label="MicroPython code", language="python", value='import machine\n\n# Your code here', lines=24)
109
  with gr.Row():
110
  feedback_output = gr.Textbox(label="Feedback", value="Click on the 'Grade' button to get feedback", interactive=False, scale=4)
 
112
  grade_btn = gr.Button("Grade", variant="primary")
113
  grade_btn.click(fn=grade, inputs=code, outputs=[grade_output, feedback_output], api_name="grade")
114
 
115
+ demo.launch(show_error=False)