File size: 1,882 Bytes
7951c72
 
3dc1b9e
 
7951c72
 
3dc1b9e
 
 
7951c72
 
 
 
 
 
 
 
5093ea9
7951c72
 
3dc1b9e
7951c72
 
 
 
3dc1b9e
7951c72
 
 
 
 
3dc1b9e
7951c72
3dc1b9e
 
7951c72
3dc1b9e
7951c72
3dc1b9e
 
 
 
 
7951c72
3dc1b9e
7951c72
3dc1b9e
7951c72
3dc1b9e
11d8425
3dc1b9e
 
7951c72
 
 
3dc1b9e
 
 
5093ea9
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
# app.py

import gradio as gr
from PIL import Image
import pytesseract
import sympy

def solve_math_problem(image):
    try:
        # Convert image to grayscale for better OCR performance
        image = image.convert("L")
        
        # Preprocess the image (optional enhancements can be added here)
        # For example, image = image.point(lambda x: 0 if x < 140 else 255, '1')
        
        # Use pytesseract to extract text from the image
        problem_text = pytesseract.image_to_string(image, config='--psm 7')
        
        # Clean and prepare the extracted text
        problem_text = problem_text.strip().replace('\n', '').replace(' ', '')
        
        # Use sympy to parse and solve the equation
        # Handle simple arithmetic and algebraic equations
        expr = sympy.sympify(problem_text)
        solution = sympy.solve(expr)
        
        # Format the solution for display
        if isinstance(solution, list):
            solution = ', '.join([str(s) for s in solution])
        else:
            solution = str(solution)
        
        return f"**Problem:** {problem_text}\n\n**Solution:** {solution}"
        
    except Exception as e:
        return f"**Error processing image:** {str(e)}"

# Create the Gradio interface
demo = gr.Interface(
    fn=solve_math_problem,
    inputs=gr.Image(
        type="pil", 
        label="Upload Handwritten Math Problem",
        image_mode="L"  # Grayscale mode improves OCR accuracy
    ),
    outputs=gr.Markdown(),
    title="Handwritten Math Problem Solver",
    description="Upload an image of a handwritten math problem, and the app will attempt to solve it.",
    examples=[
        ["example_addition.png"],
        ["example_algebra.jpg"]
    ],
    allow_flagging="never",
    webpage_title="Handwritten Math Solver",
    theme="soft"
)

if __name__ == "__main__":
    demo.launch()