Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,46 @@
|
|
| 1 |
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
st.markdown("---")
|
| 21 |
-
|
| 22 |
-
# AI Decision Logic
|
| 23 |
-
st.subheader("π§ AI Robot Decision System")
|
| 24 |
-
|
| 25 |
-
action = ""
|
| 26 |
-
|
| 27 |
-
if distance > 25:
|
| 28 |
-
action = "Moving Forward π"
|
| 29 |
-
st.success(action)
|
| 30 |
-
|
| 31 |
-
elif 10 < distance <= 25:
|
| 32 |
-
action = "Slowing Down β οΈ"
|
| 33 |
-
st.warning(action)
|
| 34 |
-
|
| 35 |
-
else:
|
| 36 |
-
action = "STOP + Turning Right π"
|
| 37 |
-
st.error(action)
|
| 38 |
-
|
| 39 |
-
# Robot Animation
|
| 40 |
-
st.markdown("---")
|
| 41 |
-
st.subheader("π₯ Robot Movement Simulation")
|
| 42 |
-
|
| 43 |
-
robot = st.empty()
|
| 44 |
-
|
| 45 |
-
if "Forward" in action:
|
| 46 |
-
for i in range(3):
|
| 47 |
-
robot.markdown("π€ β‘οΈ β‘οΈ β‘οΈ")
|
| 48 |
-
time.sleep(0.3)
|
| 49 |
-
|
| 50 |
-
elif "Slowing" in action:
|
| 51 |
-
for i in range(2):
|
| 52 |
-
robot.markdown("π€ β‘οΈ β οΈ")
|
| 53 |
-
time.sleep(0.5)
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
-
for i in range(2):
|
| 57 |
-
robot.markdown("π€ β π")
|
| 58 |
-
time.sleep(0.6)
|
| 59 |
-
|
| 60 |
-
# Arduino Code Explanation
|
| 61 |
-
st.markdown("---")
|
| 62 |
-
st.subheader("π» Arduino AI Logic Used")
|
| 63 |
-
|
| 64 |
-
st.code("""
|
| 65 |
if (distance > 25) {
|
| 66 |
moveForward();
|
| 67 |
-
}
|
| 68 |
-
else if (distance > 10) {
|
| 69 |
slowDown();
|
| 70 |
-
}
|
| 71 |
-
else {
|
| 72 |
stopRobot();
|
| 73 |
turnRight();
|
| 74 |
}
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# AI decision function
|
| 5 |
+
def ai_robot_simulator(distance):
|
| 6 |
+
# AI decision logic
|
| 7 |
+
if distance > 25:
|
| 8 |
+
action = "Moving Forward π"
|
| 9 |
+
robot_movement = "π€ β‘οΈ β‘οΈ β‘οΈ Moving Forward..."
|
| 10 |
+
elif 10 < distance <= 25:
|
| 11 |
+
action = "Slowing Down β οΈ"
|
| 12 |
+
robot_movement = "π€ β‘οΈ β οΈ Slowing Down..."
|
| 13 |
+
else:
|
| 14 |
+
action = "STOP + Turning Right π"
|
| 15 |
+
robot_movement = "π€ β π Turning Right to Avoid Obstacle..."
|
| 16 |
+
|
| 17 |
+
# Arduino logic code to show
|
| 18 |
+
arduino_code = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if (distance > 25) {
|
| 20 |
moveForward();
|
| 21 |
+
} else if (distance > 10) {
|
|
|
|
| 22 |
slowDown();
|
| 23 |
+
} else {
|
|
|
|
| 24 |
stopRobot();
|
| 25 |
turnRight();
|
| 26 |
}
|
| 27 |
+
"""
|
| 28 |
+
# Return all outputs
|
| 29 |
+
return f"Distance: {distance} cm", action, robot_movement, arduino_code
|
| 30 |
+
|
| 31 |
+
# Gradio interface
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=ai_robot_simulator,
|
| 34 |
+
inputs=gr.Slider(minimum=0, maximum=100, step=1, label="Set Distance from Obstacle (cm)"),
|
| 35 |
+
outputs=[
|
| 36 |
+
gr.Textbox(label="Sensor Reading"),
|
| 37 |
+
gr.Textbox(label="AI Decision"),
|
| 38 |
+
gr.Textbox(label="Robot Movement Simulation"),
|
| 39 |
+
gr.Code(label="Arduino Logic")
|
| 40 |
+
],
|
| 41 |
+
title="π€ AI Obstacle Avoiding Robot Simulator",
|
| 42 |
+
description="Interactive simulation of an Arduino AI robot with ultrasonic sensor. Move the slider to simulate obstacle distance."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the app
|
| 46 |
+
iface.launch()
|