import gradio as gr from sympy import symbols, Eq, solve, sympify x, y = symbols('x y') def parse_equation(eq_str): # Replace implicit multiplication (e.g., 2x → 2*x) eq_str = eq_str.replace(" ", "") eq_str = eq_str.replace("x", "*x").replace("y", "*y") eq_str = eq_str.replace("*x*x", "x*x").replace("*y*y", "y*y") # fix double stars eq_str = eq_str.replace("=*x", "=x").replace("=*y", "=y") # fix edge cases # Split into LHS and RHS if "=" not in eq_str: raise ValueError("Equation must contain '='") lhs, rhs = eq_str.split("=") return Eq(sympify(lhs), sympify(rhs)) def solve_system(eq1_str, eq2_str): try: eq1 = parse_equation(eq1_str) eq2 = parse_equation(eq2_str) solution = solve((eq1, eq2), (x, y)) if not solution: return "❌ No solution found. Check your equations." return f"✅ Solution:\nx = {solution[x]}\ny = {solution[y]}" except Exception as e: return f"⚠️ Error: {str(e)}" demo = gr.Interface( fn=solve_system, inputs=[ gr.Textbox(label="Equation 1 (e.g., 2x + 3y = 50)"), gr.Textbox(label="Equation 2 (e.g., 2x - 3y = 10)") ], outputs="text", title="🧮 Two-Variable Algebra Solver", description="Enter equations like 2x + 3y = 50. We'll auto-parse and solve them!" ) demo.launch()