Update app.py
Browse files
app.py
CHANGED
@@ -5,22 +5,21 @@ from PIL import Image
|
|
5 |
# Load the tokenizer and model
|
6 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
7 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
8 |
-
model = model.eval() #
|
9 |
|
10 |
-
# Define the OCR function
|
11 |
def perform_ocr(image):
|
12 |
-
|
13 |
-
|
14 |
-
image
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
image.save(image_file_path)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
return res
|
24 |
|
25 |
# Define the Gradio interface
|
26 |
interface = gr.Interface(
|
|
|
5 |
# Load the tokenizer and model
|
6 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
7 |
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
8 |
+
model = model.eval() # Ensure model is set to evaluation mode
|
9 |
|
10 |
+
# Define the OCR function with error handling
|
11 |
def perform_ocr(image):
|
12 |
+
try:
|
13 |
+
# Convert PIL image to RGB format (if necessary)
|
14 |
+
if image.mode != "RGB":
|
15 |
+
image = image.convert("RGB")
|
16 |
|
17 |
+
# Perform OCR using the model
|
18 |
+
res = model.chat(tokenizer, image, ocr_type='ocr')
|
|
|
19 |
|
20 |
+
return res
|
21 |
+
except Exception as e:
|
22 |
+
return f"An error occurred: {str(e)}"
|
|
|
23 |
|
24 |
# Define the Gradio interface
|
25 |
interface = gr.Interface(
|