Spaces:
Runtime error
Runtime error
| # app.py | |
| import gradio as gr | |
| from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
| from PIL import Image | |
| from sympy import sympify, solve, Eq, symbols | |
| # Load the math OCR model and processor (publicly available) | |
| processor = TrOCRProcessor.from_pretrained("lambdalabs/smicr_ocr_exp1") | |
| model = VisionEncoderDecoderModel.from_pretrained("lambdalabs/smicr_ocr_exp1") | |
| def predict_math_problem(image): | |
| try: | |
| # Transcribe the handwritten math problem | |
| image = image.convert("RGB") | |
| pixel_values = processor(image, return_tensors="pt").pixel_values | |
| generated_ids = model.generate(pixel_values) | |
| transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| # Standardize mathematical symbols | |
| transcription = (transcription | |
| .replace("×", "*") | |
| .replace("÷", "/") | |
| .replace("−", "-") | |
| .replace("√", "sqrt") | |
| .replace("²", "**2") | |
| .replace("³", "**3") | |
| .replace("½", "1/2") | |
| .replace("¼", "1/4") | |
| .replace("…", "...") | |
| ) | |
| # Solve the mathematical expression | |
| solution = None | |
| try: | |
| if '=' in transcription: | |
| lhs, rhs = transcription.split('=', 1) | |
| equation = Eq(sympify(lhs.strip()), sympify(rhs.strip())) | |
| variables = equation.free_symbols | |
| if variables: | |
| variable = variables.pop() | |
| solution = solve(equation, variable) | |
| solution = f"{variable} = {solution}" | |
| else: | |
| solution = "Solution: No Variables Found" | |
| else: | |
| solution = f"Result: {sympify(transcription)}" | |
| except: | |
| solution = "Error: Unable to Solve Expression" | |
| return transcription, solution | |
| except Exception as e: | |
| return f"Error: {str(e)}", "Processing Failed" | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict_math_problem, | |
| inputs=gr.Image(type="pil", label="Handwritten Math Problem"), | |
| outputs=[ | |
| gr.Textbox(label="Transcribed Math Text"), | |
| gr.Textbox(label="Math Solution") | |
| ], | |
| title="Handwritten Math Solver", | |
| description="Upload a handwritten math image to get transcription and solution." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |