raonoorrajput/math_solver
0
1import streamlit as st2import sympy as sp3import pytesseract4from PIL import Image5 6def solve_equation(equation_str):7 try:8 x = sp.Symbol('x')9 equation = sp.sympify(equation_str)10 solution = sp.solve(equation, x)11 steps = sp.pretty(sp.solve(equation, x, dict=True))12 return solution, steps13 except Exception as e:14 return None, str(e)15 16def extract_text_from_image(image):17 return pytesseract.image_to_string(image)18 19st.title("Mathematics Solver")20 21st.write("Enter a mathematical equation or upload an image containing the equation.")22 23equation = st.text_input("Enter equation (use 'x' as the variable, e.g., x**2 - 4 = 0):")24uploaded_file = st.file_uploader("Upload an image with an equation", type=["png", "jpg", "jpeg"])25 26if uploaded_file:27 image = Image.open(uploaded_file)28 extracted_text = extract_text_from_image(image)29 st.write(f"Extracted Equation: {extracted_text}")30 equation = extracted_text31 32if st.button("Solve"):33 if equation:34 solution, steps = solve_equation(equation)35 if solution:36 st.write(f"### Solution: {solution}")37 st.write(f"### Steps:\n{steps}")38 else:39 st.error("Invalid equation. Please enter a valid mathematical expression.")