Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
from transformers import AutoModel, AutoTokenizer
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
-
#
|
7 |
-
if torch.cuda.is_available():
|
8 |
-
print("CUDA is available! GPU is present.")
|
9 |
-
print(f"Number of GPUs: {torch.cuda.device_count()}")
|
10 |
-
print(f"GPU Name: {torch.cuda.get_device_name(0)}")
|
11 |
-
else:
|
12 |
-
print("CUDA is not available. Running on CPU.")
|
13 |
-
|
14 |
-
# Load the tokenizer and model
|
15 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
model = AutoModel.from_pretrained(
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Define the OCR function
|
26 |
def perform_ocr(image):
|
27 |
-
|
28 |
-
|
29 |
-
image
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
# Define the Gradio interface
|
37 |
interface = gr.Interface(
|
@@ -43,4 +57,4 @@ interface = gr.Interface(
|
|
43 |
)
|
44 |
|
45 |
# Launch the Gradio app
|
46 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import AutoModel, AutoTokenizer
|
3 |
from PIL import Image
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
8 |
|
9 |
+
# Try loading the model with error handling
|
10 |
+
try:
|
11 |
+
model = AutoModel.from_pretrained(
|
12 |
+
'ucaslcl/GOT-OCR2_0',
|
13 |
+
trust_remote_code=True,
|
14 |
+
low_cpu_mem_usage=True,
|
15 |
+
device_map='auto', # Use 'auto' to decide whether to use CPU or GPU
|
16 |
+
use_safetensors=True,
|
17 |
+
pad_token_id=tokenizer.eos_token_id
|
18 |
+
)
|
19 |
+
|
20 |
+
# Check if CUDA (GPU) is available, else fall back to CPU
|
21 |
+
if torch.cuda.is_available():
|
22 |
+
model = model.eval().cuda()
|
23 |
+
print("Model loaded on GPU.")
|
24 |
+
else:
|
25 |
+
model = model.eval().cpu()
|
26 |
+
print("CUDA not available, model loaded on CPU.")
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Error loading model: {e}")
|
30 |
|
31 |
# Define the OCR function
|
32 |
def perform_ocr(image):
|
33 |
+
try:
|
34 |
+
# Convert PIL image to RGB format (if necessary)
|
35 |
+
if image.mode != "RGB":
|
36 |
+
image = image.convert("RGB")
|
37 |
+
|
38 |
+
# Save the image to a temporary path
|
39 |
+
image_file_path = 'temp_image.jpg'
|
40 |
+
image.save(image_file_path)
|
41 |
|
42 |
+
# Perform OCR using the model
|
43 |
+
res = model.chat(tokenizer, image_file_path, ocr_type='ocr')
|
44 |
|
45 |
+
return res
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
return str(e)
|
49 |
|
50 |
# Define the Gradio interface
|
51 |
interface = gr.Interface(
|
|
|
57 |
)
|
58 |
|
59 |
# Launch the Gradio app
|
60 |
+
interface.launch()
|