Vishwas1 commited on
Commit
4963942
Β·
verified Β·
1 Parent(s): e439707

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+
5
+ # ---------- EXPLANATION TEXT ----------
6
+ motor_text = """
7
+ ## How an Electric Motor Runs
8
+
9
+ An electric motor converts electrical energy into mechanical energy using **electromagnetism**.
10
+
11
+ **Key parts:**
12
+ - **Rotor** – The rotating part.
13
+ - **Stator** – The stationary part with magnets or coils.
14
+ - **Commutator & Brushes** – Switch current direction to keep the rotor spinning.
15
+
16
+ **Basic principle:**
17
+ When current flows through a coil in a magnetic field, a force (Lorentz force) acts on it, making it rotate. Changing current direction at the right time keeps it spinning.
18
+ """
19
+
20
+ # ---------- DIAGRAM FUNCTION ----------
21
+ def draw_motor(angle):
22
+ fig, ax = plt.subplots(figsize=(4,4))
23
+ ax.set_xlim(-1.5, 1.5)
24
+ ax.set_ylim(-1.5, 1.5)
25
+ ax.set_aspect('equal')
26
+ ax.axis('off')
27
+
28
+ # Magnetic field lines
29
+ for y in np.linspace(-1, 1, 5):
30
+ ax.arrow(-1.4, y, 2.8, 0, head_width=0.05, color='blue')
31
+
32
+ # Rotor coil
33
+ coil_x = [0.5*np.cos(angle), -0.5*np.cos(angle)]
34
+ coil_y = [0.5*np.sin(angle), -0.5*np.sin(angle)]
35
+ ax.plot(coil_x, coil_y, color='red', linewidth=3)
36
+
37
+ # Rotor circle
38
+ circle = plt.Circle((0,0), 0.6, fill=False, color='black', linewidth=1.5)
39
+ ax.add_artist(circle)
40
+
41
+ # Force arrows
42
+ ax.arrow(coil_x[0], coil_y[0], 0.2*np.sin(angle), -0.2*np.cos(angle), color='green', width=0.01)
43
+ ax.arrow(coil_x[1], coil_y[1], -0.2*np.sin(angle), 0.2*np.cos(angle), color='green', width=0.01)
44
+
45
+ plt.close(fig)
46
+ return fig
47
+
48
+ # ---------- QUIZ ----------
49
+ quiz_questions = [
50
+ ("What part of a motor rotates?", ["Stator", "Rotor", "Magnet", "Brushes"], "Rotor"),
51
+ ("What converts electrical to mechanical energy?", ["Generator", "Motor", "Battery", "Switch"], "Motor"),
52
+ ("What keeps the motor spinning in the same direction?", ["Magnets", "Switch", "Commutator", "Resistor"], "Commutator")
53
+ ]
54
+
55
+ def quiz(q_idx, answer):
56
+ question, options, correct = quiz_questions[q_idx]
57
+ if answer == correct:
58
+ return "βœ… Correct!"
59
+ else:
60
+ return f"❌ Incorrect. Correct answer: {correct}"
61
+
62
+ # ---------- GRADIO UI ----------
63
+ with gr.Blocks() as demo:
64
+ gr.Markdown("# Spin It! β€” How an Electric Motor Runs")
65
+
66
+ with gr.Tab("Learn"):
67
+ gr.Markdown(motor_text)
68
+ angle_slider = gr.Slider(0, 2*np.pi, value=0, step=0.1, label="Rotor Angle")
69
+ image_output = gr.Plot()
70
+ angle_slider.change(lambda a: draw_motor(a), inputs=angle_slider, outputs=image_output)
71
+ draw_motor(0)
72
+
73
+ with gr.Tab("Test Yourself"):
74
+ q_idx = gr.Number(value=0, label="Question Number (0-2)", precision=0)
75
+ answer_choice = gr.Radio(["Stator", "Rotor", "Magnet", "Brushes"], label="Select your answer")
76
+ quiz_btn = gr.Button("Submit Answer")
77
+ quiz_output = gr.Textbox(label="Result")
78
+ quiz_btn.click(quiz, inputs=[q_idx, answer_choice], outputs=quiz_output)
79
+
80
+ demo.launch()