Nitin00043 commited on
Commit
3dc1b9e
·
verified ·
1 Parent(s): 5a87995

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
2
+ import gradio as gr
3
+ from PIL import Image
4
+
5
+ # Load the pre-trained Pix2Struct model and processor
6
+ model_name = "google/pix2struct-mathqa-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
+ # Preprocess the image
13
+ image = image.convert("RGB") # Ensure RGB format
14
+ inputs = processor(
15
+ images=[image], # Wrap in list
16
+ text="Solve the following math problem:", # More specific prompt
17
+ return_tensors="pt",
18
+ max_patches=2048, # Increased from default 1024 for better math handling
19
+ header_text="Math Problem" # Add header text
20
+ )
21
+
22
+ # Generate the solution
23
+ predictions = model.generate(
24
+ **inputs,
25
+ max_new_tokens=200,
26
+ early_stopping=True,
27
+ num_beams=4,
28
+ temperature=0.2
29
+ )
30
+
31
+ # Decode the output
32
+ solution = processor.decode(
33
+ predictions[0],
34
+ skip_special_tokens=True,
35
+ clean_up_tokenization_spaces=True
36
+ )
37
+
38
+ # Format the solution
39
+ return f"Problem: {processor.decode(inputs.input_ids[0])}\nSolution: {solution}"
40
+
41
+ except Exception as e:
42
+ return f"Error processing image: {str(e)}"
43
+
44
+ # Gradio interface with explicit image handling
45
+ demo = gr.Interface(
46
+ fn=solve_math_problem,
47
+ inputs=gr.Image(
48
+ type="pil",
49
+ label="Upload Handwritten Math Problem",
50
+ image_mode="RGB", # Force RGB format
51
+ source="upload"
52
+ ),
53
+ outputs=gr.Textbox(label="Solution", show_copy_button=True),
54
+ title="Handwritten Math Problem Solver",
55
+ description="Upload an image of a handwritten math problem (algebra, arithmetic, etc.) and get the solution",
56
+ examples=[
57
+ ["example_addition.png"], # Make sure to upload these files
58
+ ["example_algebra.jpg"]
59
+ ],
60
+ theme="soft",
61
+ allow_flagging="never"
62
+ )
63
+
64
+ if __name__ == "__main__":
65
+ demo.launch()