Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sympy import symbols, Eq, solve, sympify
|
| 3 |
+
|
| 4 |
+
x, y = symbols('x y')
|
| 5 |
+
|
| 6 |
+
def parse_equation(eq_str):
|
| 7 |
+
# Replace implicit multiplication (e.g., 2x → 2*x)
|
| 8 |
+
eq_str = eq_str.replace(" ", "")
|
| 9 |
+
eq_str = eq_str.replace("x", "*x").replace("y", "*y")
|
| 10 |
+
eq_str = eq_str.replace("*x*x", "x*x").replace("*y*y", "y*y") # fix double stars
|
| 11 |
+
eq_str = eq_str.replace("=*x", "=x").replace("=*y", "=y") # fix edge cases
|
| 12 |
+
|
| 13 |
+
# Split into LHS and RHS
|
| 14 |
+
if "=" not in eq_str:
|
| 15 |
+
raise ValueError("Equation must contain '='")
|
| 16 |
+
lhs, rhs = eq_str.split("=")
|
| 17 |
+
return Eq(sympify(lhs), sympify(rhs))
|
| 18 |
+
|
| 19 |
+
def solve_system(eq1_str, eq2_str):
|
| 20 |
+
try:
|
| 21 |
+
eq1 = parse_equation(eq1_str)
|
| 22 |
+
eq2 = parse_equation(eq2_str)
|
| 23 |
+
solution = solve((eq1, eq2), (x, y))
|
| 24 |
+
if not solution:
|
| 25 |
+
return "❌ No solution found. Check your equations."
|
| 26 |
+
return f"✅ Solution:\nx = {solution[x]}\ny = {solution[y]}"
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"⚠️ Error: {str(e)}"
|
| 29 |
+
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=solve_system,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Textbox(label="Equation 1 (e.g., 2x + 3y = 50)"),
|
| 34 |
+
gr.Textbox(label="Equation 2 (e.g., 2x - 3y = 10)")
|
| 35 |
+
],
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="🧮 Two-Variable Algebra Solver",
|
| 38 |
+
description="Enter equations like 2x + 3y = 50. We'll auto-parse and solve them!"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|