Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sympy as sp
|
| 3 |
+
|
| 4 |
+
# Function to solve algebraic equations
|
| 5 |
+
def solve_equation(equation):
|
| 6 |
+
lhs, rhs = equation.split("=")
|
| 7 |
+
lhs = sp.sympify(lhs)
|
| 8 |
+
rhs = sp.sympify(rhs) # Removed extra text
|
| 9 |
+
solution = sp.solve(lhs - rhs)
|
| 10 |
+
return f"Solution: {solution}"
|
| 11 |
+
|
| 12 |
+
# Function to simplify expressions
|
| 13 |
+
def simplify_expression(expression):
|
| 14 |
+
expr = sp.sympify(expression)
|
| 15 |
+
simplified_expr = sp.simplify(expr)
|
| 16 |
+
return f"Simplified Expression: {simplified_expr}"
|
| 17 |
+
|
| 18 |
+
# Function to factor expressions
|
| 19 |
+
def factor_expression(expression):
|
| 20 |
+
expr = sp.sympify(expression)
|
| 21 |
+
factored_expr = sp.factor(expr)
|
| 22 |
+
return f"Factored Expression: {factored_expr}"
|
| 23 |
+
|
| 24 |
+
# Function to solve basic arithmetic word problems
|
| 25 |
+
def solve_word_problem(operation, num1, num2):
|
| 26 |
+
if operation == "addition":
|
| 27 |
+
result = num1 + num2
|
| 28 |
+
elif operation == "subtraction":
|
| 29 |
+
result = num1 - num2
|
| 30 |
+
elif operation == "multiplication":
|
| 31 |
+
result = num1 * num2
|
| 32 |
+
elif operation == "division":
|
| 33 |
+
if num2 == 0:
|
| 34 |
+
result = "Error! Division by zero."
|
| 35 |
+
else:
|
| 36 |
+
result = num1 / num2
|
| 37 |
+
return f"Result: {result}"
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
def create_interface():
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# Math Problem-Solution App")
|
| 43 |
+
|
| 44 |
+
with gr.Tab("Solve Algebraic Equation"):
|
| 45 |
+
equation_input = gr.Textbox(label="Enter an equation (e.g., 2*x + 3 = 5)", lines=1)
|
| 46 |
+
solve_button = gr.Button("Solve Equation")
|
| 47 |
+
solution_output = gr.Textbox(label="Solution", interactive=False)
|
| 48 |
+
solve_button.click(solve_equation, inputs=equation_input, outputs=solution_output)
|
| 49 |
+
|
| 50 |
+
with gr.Tab("Simplify Expression"):
|
| 51 |
+
expression_input = gr.Textbox(label="Enter an expression (e.g., 2*x + 3*x - 4)", lines=1)
|
| 52 |
+
simplify_button = gr.Button("Simplify")
|
| 53 |
+
simplified_output = gr.Textbox(label="Simplified Expression", interactive=False)
|
| 54 |
+
simplify_button.click(simplify_expression, inputs=expression_input, outputs=simplified_output)
|
| 55 |
+
|
| 56 |
+
with gr.Tab("Factor Expression"):
|
| 57 |
+
factor_input = gr.Textbox(label="Enter an expression to factor (e.g., x^2 - 5*x + 6)", lines=1)
|
| 58 |
+
factor_button = gr.Button("Factor Expression")
|
| 59 |
+
factored_output = gr.Textbox(label="Factored Expression", interactive=False)
|
| 60 |
+
factor_button.click(factor_expression, inputs=factor_input, outputs=factored_output)
|
| 61 |
+
|
| 62 |
+
with gr.Tab("Solve Word Problem"):
|
| 63 |
+
operation_dropdown = gr.Dropdown(choices=["addition", "subtraction", "multiplication", "division"], label="Operation")
|
| 64 |
+
num1_input = gr.Number(label="Enter the first number")
|
| 65 |
+
num2_input = gr.Number(label="Enter the second number")
|
| 66 |
+
word_button = gr.Button("Solve Word Problem")
|
| 67 |
+
word_output = gr.Textbox(label="Result", interactive=False)
|
| 68 |
+
word_button.click(solve_word_problem, inputs=[operation_dropdown, num1_input, num2_input], outputs=word_output)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|
| 71 |
+
|
| 72 |
+
# Run the interface
|
| 73 |
+
create_interface()
|