MathSolverAI / app.py
NihalGazi's picture
Update app.py
a8b70d3 verified
raw
history blame
1.73 kB
import gradio as gr
import genai # Assuming this is the correct library for Gemini API
import sympy as sp
# Set up the Gemini model
model = genai.GenerativeModel("gemini-1.5-flash")
# Function for algebraic simplification
def algebra_simplification(expression):
try:
expr = sp.sympify(expression)
simplified_expr = sp.simplify(expr)
return str(simplified_expr)
except Exception as e:
return f"Error: {str(e)}"
# Function for solving algebraic equations
def algebra_solve(equation):
try:
eq = sp.sympify(equation)
variables = list(eq.free_symbols)
solutions = sp.solve(eq, *variables)
return str(solutions)
except Exception as e:
return f"Error: {str(e)}"
# Function to handle the chat with Gemini
def process_math_problem(math_problem):
chat = model.start_chat(
history=[
{"role": "user", "parts": "Hello"},
{"role": "model", "parts": "Great to meet you. What would you like to know?"},
]
)
# Send the math problem to Gemini
response = chat.send_message(math_problem)
# Return Gemini's response
return response.text
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Gemini Algebra Solver")
# Single input field for math problems
input_problem = gr.Textbox(label="Enter a Math Problem", placeholder="e.g., Simplify (x^2 - 4)/(x - 2) or Solve x^2 - 4 = 0")
output_solution = gr.Textbox(label="Solution")
# Button to trigger the solution process
solve_button = gr.Button("Solve")
solve_button.click(fn=process_math_problem, inputs=input_problem, outputs=output_solution)
# Launch the Gradio interface
demo.launch()