Spaces:
Runtime error

File size: 2,780 Bytes
309aa26
 
443a531
309aa26
443a531
309aa26
443a531
309aa26
443a531
309aa26
 
 
443a531
 
 
 
 
309aa26
3d322c5
443a531
 
309aa26
 
443a531
 
 
 
 
309aa26
 
 
 
443a531
309aa26
3d322c5
309aa26
 
443a531
309aa26
 
 
 
443a531
309aa26
 
443a531
309aa26
443a531
3d322c5
443a531
309aa26
443a531
 
 
 
 
 
309aa26
3d322c5
443a531
 
309aa26
3d322c5
309aa26
443a531
 
 
309aa26
443a531
309aa26
 
 
 
3d322c5
309aa26
443a531
309aa26
443a531
 
 
d39a007
 
443a531
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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