Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,42 @@
|
|
|
|
1 |
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
|
5 |
-
#
|
6 |
-
model_name = "google/pix2struct-
|
7 |
model = Pix2StructForConditionalGeneration.from_pretrained(model_name)
|
8 |
processor = Pix2StructProcessor.from_pretrained(model_name)
|
9 |
|
10 |
-
def
|
11 |
try:
|
12 |
-
#
|
13 |
-
image =
|
14 |
-
|
15 |
-
#
|
16 |
-
inputs = processor(
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
),
|
57 |
-
outputs=
|
58 |
-
title="
|
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 |
|
68 |
if __name__ == "__main__":
|
69 |
-
|
|
|
1 |
+
import torch
|
2 |
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Load model and processor
|
7 |
+
model_name = "google/pix2struct-docvqa-large"
|
8 |
model = Pix2StructForConditionalGeneration.from_pretrained(model_name)
|
9 |
processor = Pix2StructProcessor.from_pretrained(model_name)
|
10 |
|
11 |
+
def process_image(image_path):
|
12 |
try:
|
13 |
+
# Load the image
|
14 |
+
image = Image.open(image_path).convert("RGB")
|
15 |
+
|
16 |
+
# Prepare the input
|
17 |
+
inputs = processor(images=image, text="What does this image say?", return_tensors="pt")
|
18 |
+
|
19 |
+
# Generate prediction
|
20 |
+
output = model.generate(**inputs)
|
21 |
+
|
22 |
+
# Decode the output
|
23 |
+
solution = processor.decode(output[0], skip_special_tokens=True)
|
24 |
+
return solution
|
25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
return f"Error processing image: {str(e)}"
|
28 |
|
29 |
+
def predict(image):
|
30 |
+
"""Handles image input for Gradio."""
|
31 |
+
return process_image(image)
|
32 |
+
|
33 |
+
# Gradio app
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=predict,
|
36 |
+
inputs=gr.Image(type="filepath"),
|
37 |
+
outputs="text",
|
38 |
+
title="Image Text Solution"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
+
iface.launch()
|