Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,58 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
model_name = "google/pix2struct-textcaps-base"
|
| 7 |
-
model = Pix2StructForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
-
processor = Pix2StructProcessor.from_pretrained(model_name)
|
| 9 |
|
| 10 |
def solve_math_problem(image):
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
image = image.convert("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
images=[image],
|
| 18 |
-
text="Solve the following math problem:",
|
| 19 |
-
return_tensors="pt",
|
| 20 |
-
max_patches=2048
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
early_stopping=True,
|
| 28 |
-
num_beams=4,
|
| 29 |
-
temperature=0.2
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
-
solution = processor.decode(
|
| 39 |
-
predictions[0],
|
| 40 |
-
skip_special_tokens=True,
|
| 41 |
-
clean_up_tokenization_spaces=True
|
| 42 |
-
)
|
| 43 |
|
| 44 |
-
return f"Problem
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"Error processing image
|
| 48 |
|
| 49 |
-
#
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=solve_math_problem,
|
| 52 |
inputs=gr.Image(
|
| 53 |
type="pil",
|
| 54 |
label="Upload Handwritten Math Problem",
|
| 55 |
-
image_mode="
|
| 56 |
),
|
| 57 |
-
outputs=gr.
|
| 58 |
title="Handwritten Math Problem Solver",
|
| 59 |
-
description="Upload an image of a handwritten math problem
|
| 60 |
examples=[
|
| 61 |
["example_addition.png"],
|
| 62 |
["example_algebra.jpg"]
|
| 63 |
],
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
+
import pytesseract
|
| 6 |
+
import sympy
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def solve_math_problem(image):
|
| 9 |
try:
|
| 10 |
+
# Convert image to grayscale for better OCR performance
|
| 11 |
+
image = image.convert("L")
|
| 12 |
+
|
| 13 |
+
# Preprocess the image (optional enhancements can be added here)
|
| 14 |
+
# For example, image = image.point(lambda x: 0 if x < 140 else 255, '1')
|
| 15 |
+
|
| 16 |
+
# Use pytesseract to extract text from the image
|
| 17 |
+
problem_text = pytesseract.image_to_string(image, config='--psm 7')
|
| 18 |
|
| 19 |
+
# Clean and prepare the extracted text
|
| 20 |
+
problem_text = problem_text.strip().replace('\n', '').replace(' ', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Use sympy to parse and solve the equation
|
| 23 |
+
# Handle simple arithmetic and algebraic equations
|
| 24 |
+
expr = sympy.sympify(problem_text)
|
| 25 |
+
solution = sympy.solve(expr)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Format the solution for display
|
| 28 |
+
if isinstance(solution, list):
|
| 29 |
+
solution = ', '.join([str(s) for s in solution])
|
| 30 |
+
else:
|
| 31 |
+
solution = str(solution)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return f"**Problem:** {problem_text}\n\n**Solution:** {solution}"
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
+
return f"**Error processing image:** {str(e)}"
|
| 37 |
|
| 38 |
+
# Create the Gradio interface
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=solve_math_problem,
|
| 41 |
inputs=gr.Image(
|
| 42 |
type="pil",
|
| 43 |
label="Upload Handwritten Math Problem",
|
| 44 |
+
image_mode="L" # Grayscale mode improves OCR accuracy
|
| 45 |
),
|
| 46 |
+
outputs=gr.Markdown(),
|
| 47 |
title="Handwritten Math Problem Solver",
|
| 48 |
+
description="Upload an image of a handwritten math problem, and the app will attempt to solve it.",
|
| 49 |
examples=[
|
| 50 |
["example_addition.png"],
|
| 51 |
["example_algebra.jpg"]
|
| 52 |
],
|
| 53 |
+
allow_flagging="never",
|
| 54 |
+
webpage_title="Handwritten Math Solver",
|
| 55 |
+
theme="soft"
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|