Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,67 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
5 |
from PIL import Image
|
6 |
-
import sympy
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
model =
|
|
|
11 |
|
12 |
def solve_math_problem(image):
|
13 |
try:
|
14 |
-
# Ensure the image is in RGB format
|
15 |
image = image.convert("RGB")
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Clean and prepare the extracted text
|
25 |
-
problem_text = generated_text.strip().replace(' ', '')
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
return f"
|
39 |
|
40 |
except Exception as e:
|
41 |
-
return f"
|
42 |
|
43 |
-
#
|
44 |
demo = gr.Interface(
|
45 |
fn=solve_math_problem,
|
46 |
inputs=gr.Image(
|
47 |
type="pil",
|
48 |
label="Upload Handwritten Math Problem",
|
49 |
-
image_mode="RGB"
|
50 |
),
|
51 |
-
outputs=gr.
|
52 |
title="Handwritten Math Problem Solver",
|
53 |
-
description="Upload an image of a handwritten math problem,
|
54 |
examples=[
|
55 |
["example_addition.png"],
|
56 |
["example_algebra.jpg"]
|
57 |
],
|
|
|
58 |
allow_flagging="never"
|
59 |
)
|
60 |
|
|
|
1 |
+
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
|
|
|
2 |
import gradio as gr
|
|
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
+
# Use a public model identifier. If you need a private model, remember to authenticate.
|
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 |
+
# Ensure the image is in RGB format.
|
13 |
image = image.convert("RGB")
|
14 |
|
15 |
+
# Preprocess the image and text. Note that header_text is omitted as it's not used for non-VQA tasks.
|
16 |
+
inputs = processor(
|
17 |
+
images=[image],
|
18 |
+
text="Solve the following math problem:",
|
19 |
+
return_tensors="pt",
|
20 |
+
max_patches=2048
|
21 |
+
)
|
|
|
|
|
22 |
|
23 |
+
# Generate the solution with generation parameters.
|
24 |
+
predictions = model.generate(
|
25 |
+
**inputs,
|
26 |
+
max_new_tokens=200,
|
27 |
+
early_stopping=True,
|
28 |
+
num_beams=4,
|
29 |
+
temperature=0.2
|
30 |
+
)
|
31 |
|
32 |
+
# Decode the problem text and generated solution.
|
33 |
+
problem_text = processor.decode(
|
34 |
+
inputs["input_ids"][0],
|
35 |
+
skip_special_tokens=True,
|
36 |
+
clean_up_tokenization_spaces=True
|
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: {problem_text}\nSolution: {solution}"
|
45 |
|
46 |
except Exception as e:
|
47 |
+
return f"Error processing image: {str(e)}"
|
48 |
|
49 |
+
# Set up the Gradio interface.
|
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="RGB" # This forces the input to be RGB.
|
56 |
),
|
57 |
+
outputs=gr.Textbox(label="Solution", show_copy_button=True),
|
58 |
title="Handwritten Math Problem Solver",
|
59 |
+
description="Upload an image of a handwritten math problem (algebra, arithmetic, etc.) and get the solution",
|
60 |
examples=[
|
61 |
["example_addition.png"],
|
62 |
["example_algebra.jpg"]
|
63 |
],
|
64 |
+
theme="soft",
|
65 |
allow_flagging="never"
|
66 |
)
|
67 |
|