Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import torch
|
|
2 |
from transformers import AutoModel, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
# Load the OCR model and tokenizer with low memory usage in mind
|
7 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
@@ -15,13 +16,17 @@ model = model.to(device)
|
|
15 |
|
16 |
# Function to perform OCR on the image
|
17 |
def perform_ocr(image):
|
18 |
-
#
|
19 |
-
|
|
|
20 |
|
21 |
# Use torch.no_grad() to avoid unnecessary memory usage
|
22 |
with torch.no_grad():
|
23 |
-
# Perform OCR using the model (
|
24 |
-
result = model.chat(tokenizer,
|
|
|
|
|
|
|
25 |
|
26 |
# Return the extracted text
|
27 |
return result
|
|
|
2 |
from transformers import AutoModel, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
+
import os
|
6 |
|
7 |
# Load the OCR model and tokenizer with low memory usage in mind
|
8 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
|
|
16 |
|
17 |
# Function to perform OCR on the image
|
18 |
def perform_ocr(image):
|
19 |
+
# Save the uploaded image temporarily
|
20 |
+
temp_image_path = "temp_image.png"
|
21 |
+
image.save(temp_image_path)
|
22 |
|
23 |
# Use torch.no_grad() to avoid unnecessary memory usage
|
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
|