Spaces:
Sleeping
Sleeping
Baptiste Gaultier
commited on
Commit
·
37e94dc
1
Parent(s):
88200eb
First commit
Browse files- README.md +3 -5
- app.py +114 -60
- assets/blink.gif +0 -0
- requirements.txt +0 -1
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title: Level
|
3 |
-
emoji:
|
4 |
colorFrom: yellow
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.0.1
|
8 |
app_file: app.py
|
@@ -10,5 +10,3 @@ pinned: false
|
|
10 |
license: agpl-3.0
|
11 |
short_description: 'Digital manufacturing course activity: Level-crossing light'
|
12 |
---
|
13 |
-
|
14 |
-
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
1 |
---
|
2 |
+
title: Level crossing light
|
3 |
+
emoji: 🚦
|
4 |
colorFrom: yellow
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.0.1
|
8 |
app_file: app.py
|
|
|
10 |
license: agpl-3.0
|
11 |
short_description: 'Digital manufacturing course activity: Level-crossing light'
|
12 |
---
|
|
|
|
app.py
CHANGED
@@ -1,64 +1,118 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
10 |
+
# Parse the student's code into an AST (Abstract Syntax Tree)
|
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):
|
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 |
+
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):
|
33 |
+
if node.func.attr == "sleep" and isinstance(node.args[0], ast.Constant):
|
34 |
+
sleep_times.append(node.args[0].value)
|
35 |
+
|
36 |
+
# Check for while loop
|
37 |
+
if isinstance(node, ast.While):
|
38 |
+
loop_found = True
|
39 |
+
|
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)
|
114 |
+
grade_output = gr.Textbox(label="Grade", interactive=False, value="0/10", scale=1)
|
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)
|
assets/blink.gif
ADDED
![]() |
requirements.txt
CHANGED
@@ -1 +0,0 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|