Download app.py from malaleeli/steamm26: direct link, hf CLI and curl.
- Browser
- Download file 5.72 kB
-
https://huggingface.co/spaces/malaleeli/steamm26/resolve/main/app.py
- Command line
-
hf download hf://spaces/malaleeli/steamm26/app.py
-
curl -L -o app.py https://huggingface.co/spaces/malaleeli/steamm26/resolve/main/app.py
5.72 kB
| # -*- coding: utf-8 -*- | |
| """STEAM | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/13Yt-6kIJnuI4b2dgt2T7QTnwE65vE-MI | |
| """ | |
| import gradio as gr | |
| import re | |
| def physics_hardcoded_chatbot(question): | |
| q_clean = question.lower().replace("-", " ").replace("–", " ").replace("?", "").strip() | |
| # --------- v-t graph questions --------- | |
| if "slope" in q_clean and "v t graph" in q_clean: | |
| return "The slope of a velocity-time graph represents the acceleration." | |
| if "area" in q_clean and "v t graph" in q_clean: | |
| return "The area under a velocity-time graph represents the displacement." | |
| if "horizontal" in q_clean and "v t graph" in q_clean: | |
| return "A horizontal line in a v-t graph indicates constant velocity." | |
| if "downward" in q_clean and "v t graph" in q_clean: | |
| return "A downward sloping line in a v-t graph indicates negative acceleration (slowing down)." | |
| # --------- mass, weight, gravity --------- | |
| if "mass" in q_clean and "unit" in q_clean: | |
| return "The SI unit of mass is the kilogram (kg)." | |
| if "weight" in q_clean and "unit" in q_clean: | |
| return "The SI unit of weight (force) is the newton (N)." | |
| if "gravity" in q_clean and "acceleration" in q_clean: | |
| return "The acceleration due to gravity on Earth is approximately 9.8 m/s²." | |
| if "weight" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Weight (W) = mass (m) × gravity (g)." | |
| if "mass" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Mass (m) = Weight (W) / gravity (g)." | |
| if "gravity" in q_clean and "force" in q_clean: | |
| return "The force of gravity (weight) on an object is its mass multiplied by the acceleration due to gravity." | |
| # --------- force and motion --------- | |
| if "force" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Force (F) = mass (m) × acceleration (a)." | |
| if "acceleration" in q_clean and "formula" in q_clean: | |
| return "Acceleration (a) = change in velocity / time = Δv / t." | |
| if "momentum" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Momentum (p) = mass (m) × velocity (v)." | |
| # --------- work, energy, power --------- | |
| if "work" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Work (W) = Force (F) × displacement (d) × cosθ." | |
| if "energy" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Kinetic Energy (KE) = 0.5 × mass × velocity². Potential Energy (PE) = mass × gravity × height." | |
| if "power" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Power (P) = Work (W) / time (t)." | |
| # --------- pressure, density --------- | |
| if "pressure" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Pressure (P) = Force (F) / Area (A)." | |
| if "density" in q_clean and ("formula" in q_clean or "calculate" in q_clean): | |
| return "Density (ρ) = mass / volume." | |
| # --------- numeric calculations --------- | |
| numbers = re.findall(r"[-+]?\d*\.\d+|\d+", q_clean) | |
| # Velocity | |
| if "velocity" in q_clean and len(numbers) >= 2: | |
| v = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated velocity: {v} m/s" | |
| # Acceleration | |
| if "acceleration" in q_clean and len(numbers) >= 2: | |
| a = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated acceleration: {a} m/s²" | |
| # Displacement (s = ut + 1/2 at^2) | |
| if ("distance" in q_clean or "displacement" in q_clean) and len(numbers) >= 3: | |
| u, a_val, t = map(float, numbers[:3]) | |
| s = u * t + 0.5 * a_val * t**2 | |
| return f"Calculated displacement: {s} m" | |
| # Weight calculation: W = m * g | |
| if "weight" in q_clean and len(numbers) >= 2: | |
| W = float(numbers[0]) * float(numbers[1]) | |
| return f"Calculated weight: {W} N" | |
| # Mass calculation: m = W / g | |
| if "mass" in q_clean and len(numbers) >= 2: | |
| m = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated mass: {m} kg" | |
| # Force: F = m * a | |
| if "force" in q_clean and len(numbers) >= 2: | |
| F = float(numbers[0]) * float(numbers[1]) | |
| return f"Calculated force: {F} N" | |
| # Work: W = F * d | |
| if "work" in q_clean and len(numbers) >= 2: | |
| W = float(numbers[0]) * float(numbers[1]) | |
| return f"Calculated work: {W} J" | |
| # Pressure: P = F / A | |
| if "pressure" in q_clean and len(numbers) >= 2: | |
| P = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated pressure: {P} Pa" | |
| # Density: ρ = m / V | |
| if "density" in q_clean and len(numbers) >= 2: | |
| rho = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated density: {rho} kg/m³" | |
| # Power: P = W / t | |
| if "power" in q_clean and len(numbers) >= 2: | |
| P = float(numbers[0]) / float(numbers[1]) | |
| return f"Calculated power: {P} W" | |
| # Momentum: p = m * v | |
| if "momentum" in q_clean and len(numbers) >= 2: | |
| p = float(numbers[0]) * float(numbers[1]) | |
| return f"Calculated momentum: {p} kg·m/s" | |
| # --------- fallback --------- | |
| return ("I can help with physics concepts like v-t graphs, velocity, acceleration, displacement, " | |
| "mass, weight, gravity, force, work, energy, power, pressure, density, and momentum.") | |
| # --------- Gradio Interface --------- | |
| gr.Interface( | |
| fn=physics_hardcoded_chatbot, | |
| inputs="text", | |
| outputs="text", | |
| title="Extensive Physics Study Buddy", | |
| description="Answers questions on all major physics topics", | |
| theme="default" | |
| ).launch(show_error=True) | |