File size: 1,373 Bytes
c807e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()