import gradio as gr import ast def grade(student_code): feedback = [] grade = 10 # Start with full points try: tree = ast.parse(student_code) ema_detected = False i2s_used = False rms_calculation = False db_conversion = False for node in ast.walk(tree): # Check for I2S microphone usage if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call): if isinstance(node.value.func, ast.Name) and node.value.func.id == "I2S": i2s_used = True # Check for RMS calculation if isinstance(node, ast.FunctionDef) and node.name == "calculate_rms": rms_calculation = True # Check for dB conversion if isinstance(node, ast.FunctionDef) and node.name == "rms_to_db": db_conversion = True # Check for EMA implementation if isinstance(node, ast.Assign) and isinstance(node.value, ast.BinOp): if isinstance(node.value.op, ast.Add) and isinstance(node.value.left, ast.BinOp): if isinstance(node.value.left.op, ast.Mult): ema_detected = True if not i2s_used: feedback.append("⛔️ No I2S microphone setup detected (-3 pts).") grade -= 3 else: feedback.append("✅ I2S microphone setup detected (+3 pts).") if not rms_calculation: feedback.append("⛔️ No RMS calculation function detected (-2 pts).") grade -= 2 else: feedback.append("✅ RMS calculation function detected (+2 pts).") if not db_conversion: feedback.append("⛔️ No RMS to dB conversion function detected (-2 pts).") grade -= 2 else: feedback.append("✅ RMS to dB conversion function detected (+2 pts).") if not ema_detected: feedback.append("⛔️ No exponential moving average (EMA) detected (-3 pts).") grade -= 3 else: feedback.append("✅ Exponential moving average (EMA) detected (+3 pts).") except Exception as e: feedback = [f"⛔️ Error parsing code: {e}"] grade = 0 grade = max(0, grade) grade = f"{grade}/10" feedback = "\n".join(feedback) return grade, feedback # Interface Gradio seulement si le script est exécuté directement if __name__ == "__main__": with gr.Blocks(gr.themes.Default(primary_hue="cyan")) as demo: with gr.Row(): instructions = gr.Markdown(""" ## Instructions 1. Based on the examples you've seen so far, write a MicroPython code that follows the instructions above. 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. 2. Click the "Grade" button to get a grade and feedback. 3. Make sure your code is well-structured and efficient. 4. When you're happy with your code, please upload your solution below to get graded. 6. Good luck! """, container=False) code = gr.Code(label="MicroPython code", language="python", value='import machine\nimport time\nimport math\nfrom machine import I2S, Pin\n\n# Define your I2S pins\nCK_PIN = 32 # Clock pin\nWS_PIN = 31 # Word Select (LRCLK)\nSD_PIN = 26 # Serial Data\n\n# Your code here', lines=24) with gr.Row(): feedback_output = gr.Textbox(label="Feedback", value="Click on the 'Grade' button to get feedback", interactive=False, scale=4) grade_output = gr.Textbox(label="Grade", interactive=False, value="0/10", scale=1) grade_btn = gr.Button("Grade", variant="primary") grade_btn.click(fn=grade, inputs=code, outputs=[grade_output, feedback_output], api_name="grade") demo.launch(show_error=False, allowed_paths=["/assets", "assets"], show_api=False)