Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,17 @@ 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,
|
11 |
pad_token_id=tokenizer.eos_token_id).eval()
|
12 |
|
13 |
-
#
|
14 |
device = torch.device('cpu')
|
15 |
model = model.to(device)
|
16 |
|
17 |
-
# Function to perform OCR on
|
18 |
def perform_ocr(image_file_path):
|
19 |
# Open the image using PIL
|
20 |
image = Image.open(image_file_path)
|
@@ -25,7 +25,7 @@ def perform_ocr(image_file_path):
|
|
25 |
|
26 |
# Use torch.no_grad() to avoid unnecessary memory usage
|
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
|
@@ -34,7 +34,7 @@ def perform_ocr(image_file_path):
|
|
34 |
# Return the extracted text
|
35 |
return result
|
36 |
|
37 |
-
#
|
38 |
iface = gr.Interface(fn=perform_ocr, inputs="file", outputs="text",
|
39 |
title="OCR Application", description="Upload an image to extract text.")
|
40 |
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
|
7 |
+
# Load the OCR model and tokenizer, trust_remote_code=True allows custom model logic
|
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,
|
11 |
pad_token_id=tokenizer.eos_token_id).eval()
|
12 |
|
13 |
+
# Move model to CPU
|
14 |
device = torch.device('cpu')
|
15 |
model = model.to(device)
|
16 |
|
17 |
+
# Function to perform OCR on an image file
|
18 |
def perform_ocr(image_file_path):
|
19 |
# Open the image using PIL
|
20 |
image = Image.open(image_file_path)
|
|
|
25 |
|
26 |
# Use torch.no_grad() to avoid unnecessary memory usage
|
27 |
with torch.no_grad():
|
28 |
+
# Perform OCR using the model on CPU (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
|
|
|
34 |
# Return the extracted text
|
35 |
return result
|
36 |
|
37 |
+
# Gradio interface for file upload and OCR
|
38 |
iface = gr.Interface(fn=perform_ocr, inputs="file", outputs="text",
|
39 |
title="OCR Application", description="Upload an image to extract text.")
|
40 |
|