Spaces:
Running
on
Zero
Running
on
Zero
do it normally
Browse files
app.py
CHANGED
@@ -49,8 +49,6 @@ model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True,
|
|
49 |
model = model.eval().cuda()
|
50 |
model.config.pad_token_id = tokenizer.eos_token_id
|
51 |
|
52 |
-
|
53 |
-
|
54 |
def save_image_to_temp_file(image):
|
55 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
56 |
image.save(temp_file, format="PNG")
|
@@ -62,32 +60,27 @@ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
|
62 |
if image is None:
|
63 |
return "No image provided", None
|
64 |
|
65 |
-
# Save the PIL Image to a temporary file
|
66 |
temp_image_path = save_image_to_temp_file(image)
|
67 |
|
68 |
-
with io.BytesIO() as buffer:
|
69 |
-
pil_image.save(buffer, format="PNG")
|
70 |
-
image_path = "/tmp/temp_image.png"
|
71 |
-
with open(image_path, "wb") as f:
|
72 |
-
f.write(buffer.getvalue())
|
73 |
-
|
74 |
if task == "Plain Text OCR":
|
75 |
-
res = model.chat(tokenizer,
|
76 |
elif task == "Format Text OCR":
|
77 |
-
res = model.chat(tokenizer,
|
78 |
elif task == "Fine-grained OCR (Box)":
|
79 |
-
res = model.chat(tokenizer,
|
80 |
elif task == "Fine-grained OCR (Color)":
|
81 |
-
res = model.chat(tokenizer,
|
82 |
elif task == "Multi-crop OCR":
|
83 |
-
res = model.chat_crop(tokenizer, image_file=
|
84 |
elif task == "Render Formatted OCR":
|
85 |
-
res = model.chat(tokenizer,
|
86 |
with open('./results/demo.html', 'r') as f:
|
87 |
html_content = f.read()
|
|
|
88 |
return res, html_content
|
89 |
|
90 |
-
|
|
|
91 |
|
92 |
return res, None
|
93 |
except Exception as e:
|
|
|
49 |
model = model.eval().cuda()
|
50 |
model.config.pad_token_id = tokenizer.eos_token_id
|
51 |
|
|
|
|
|
52 |
def save_image_to_temp_file(image):
|
53 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
54 |
image.save(temp_file, format="PNG")
|
|
|
60 |
if image is None:
|
61 |
return "No image provided", None
|
62 |
|
|
|
63 |
temp_image_path = save_image_to_temp_file(image)
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
if task == "Plain Text OCR":
|
66 |
+
res = model.chat(tokenizer, temp_image_path, ocr_type='ocr')
|
67 |
elif task == "Format Text OCR":
|
68 |
+
res = model.chat(tokenizer, temp_image_path, ocr_type='format')
|
69 |
elif task == "Fine-grained OCR (Box)":
|
70 |
+
res = model.chat(tokenizer, temp_image_path, ocr_type=ocr_type, ocr_box=ocr_box)
|
71 |
elif task == "Fine-grained OCR (Color)":
|
72 |
+
res = model.chat(tokenizer, temp_image_path, ocr_type=ocr_type, ocr_color=ocr_color)
|
73 |
elif task == "Multi-crop OCR":
|
74 |
+
res = model.chat_crop(tokenizer, image_file=temp_image_path)
|
75 |
elif task == "Render Formatted OCR":
|
76 |
+
res = model.chat(tokenizer, temp_image_path, ocr_type='format', render=True, save_render_file='./results/demo.html')
|
77 |
with open('./results/demo.html', 'r') as f:
|
78 |
html_content = f.read()
|
79 |
+
os.remove(temp_image_path)
|
80 |
return res, html_content
|
81 |
|
82 |
+
# Clean up
|
83 |
+
os.remove(temp_image_path)
|
84 |
|
85 |
return res, None
|
86 |
except Exception as e:
|