Download app.py from lavaro/maths: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/lavaro/maths/resolve/main/app.py
- Command line
-
hf download hf://spaces/lavaro/maths/app.py
-
curl -L -o app.py https://huggingface.co/spaces/lavaro/maths/resolve/main/app.py
2.78 kB
| import gradio as gr | |
| import sympy as sp | |
| from sympy import symbols, Eq, solve, diff, integrate, simplify | |
| x, y = symbols('x y') | |
| def resolver(tipo, expr1, expr2, var_name): | |
| try: | |
| var = symbols(var_name) | |
| if tipo == "Ecuación": | |
| expr = sp.sympify(expr1) | |
| sols = solve(Eq(expr, 0), var) | |
| if not sols: | |
| return "⚠️ Sin solución real", "" | |
| res = "\n".join([f"{var_name} = {s}" for s in sols]) | |
| return res, f"Ecuación: {expr} = 0" | |
| elif tipo == "Sistema": | |
| if not expr2.strip(): | |
| return "⚠️ Necesitas 2 ecuaciones", "" | |
| eq1 = sp.sympify(expr1) | |
| eq2 = sp.sympify(expr2) | |
| sols = solve((Eq(eq1, 0), Eq(eq2, 0)), (x, y), dict=True) | |
| if not sols: | |
| return "⚠️ Sistema sin solución", "" | |
| res = "\n".join([f"{k} = {v}" for k, v in sols[0].items()]) | |
| return res, "Sistema resuelto" | |
| elif tipo == "Derivada": | |
| expr = sp.sympify(expr1) | |
| deriv = diff(expr, var) | |
| return f"d/d{var_name} = {deriv}", f"de: {expr}" | |
| elif tipo == "Integral": | |
| expr = sp.sympify(expr1) | |
| integ = integrate(expr, var) | |
| return f"∫ = {integ} + C", f"de: {expr}" | |
| elif tipo == "Simplificar": | |
| expr = sp.sympify(expr1) | |
| simp = simplify(expr) | |
| return f"= {simp}", f"de: {expr}" | |
| except Exception as e: | |
| return f"❌ {str(e)}", "💡 Usa: 2*x, x**2, sin(x)" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🧮 Solver Matemático") | |
| gr.Markdown("Resuelve ecuaciones, sistemas, derivadas e integrales") | |
| with gr.Row(): | |
| tipo = gr.Radio( | |
| ["Ecuación", "Sistema", "Derivada", "Integral", "Simplificar"], | |
| label="Problema", | |
| value="Ecuación" | |
| ) | |
| expr1 = gr.Textbox(label="Expresión 1", placeholder="x**2 - 4") | |
| expr2 = gr.Textbox(label="Expresión 2 (solo sistemas)", placeholder="x + y - 5") | |
| var = gr.Textbox(label="Variable", value="x", max_lines=1) | |
| btn = gr.Button("🚀 Resolver") | |
| with gr.Row(): | |
| out1 = gr.Textbox(label="Resultado", lines=6) | |
| out2 = gr.Textbox(label="Detalles", lines=3) | |
| btn.click(resolver, [tipo, expr1, expr2, var], [out1, out2]) | |
| gr.Examples( | |
| examples=[ | |
| ["Ecuación", "x**2 - 4", "", "x"], | |
| ["Sistema", "x + y - 5", "2*x - y - 1", "x"], | |
| ["Derivada", "x**3 + 2*x", "", "x"], | |
| ["Integral", "2*x + 3", "", "x"], | |
| ], | |
| inputs=[tipo, expr1, expr2, var], | |
| outputs=[out1, out2], | |
| fn=resolver, | |
| ) | |
| demo |