Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
|
7 |
-
# Load the OCR model and tokenizer
|
8 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
9 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True,
|
10 |
low_cpu_mem_usage=True,
|
@@ -14,9 +14,12 @@ model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True,
|
|
14 |
device = torch.device('cpu')
|
15 |
model = model.to(device)
|
16 |
|
17 |
-
# Function to perform OCR on the image
|
18 |
-
def perform_ocr(
|
19 |
-
#
|
|
|
|
|
|
|
20 |
temp_image_path = "temp_image.png"
|
21 |
image.save(temp_image_path)
|
22 |
|
@@ -24,10 +27,10 @@ def perform_ocr(image):
|
|
24 |
with torch.no_grad():
|
25 |
# Perform OCR using the model (pass the file path of the saved image)
|
26 |
result = model.chat(tokenizer, temp_image_path, ocr_type='ocr')
|
27 |
-
|
28 |
# Clean up the temporary image file
|
29 |
os.remove(temp_image_path)
|
30 |
-
|
31 |
# Return the extracted text
|
32 |
return result
|
33 |
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
|
7 |
+
# Load the OCR model and tokenizer
|
8 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
9 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True,
|
10 |
low_cpu_mem_usage=True,
|
|
|
14 |
device = torch.device('cpu')
|
15 |
model = model.to(device)
|
16 |
|
17 |
+
# Function to perform OCR on the image file
|
18 |
+
def perform_ocr(image_file_path):
|
19 |
+
# Open the image using PIL
|
20 |
+
image = Image.open(image_file_path)
|
21 |
+
|
22 |
+
# Save the image temporarily
|
23 |
temp_image_path = "temp_image.png"
|
24 |
image.save(temp_image_path)
|
25 |
|
|
|
27 |
with torch.no_grad():
|
28 |
# Perform OCR using the model (pass the file path of the saved image)
|
29 |
result = model.chat(tokenizer, temp_image_path, ocr_type='ocr')
|
30 |
+
|
31 |
# Clean up the temporary image file
|
32 |
os.remove(temp_image_path)
|
33 |
+
|
34 |
# Return the extracted text
|
35 |
return result
|
36 |
|