Spaces:
Sleeping
Sleeping
Create ocr_engine.py
Browse files- ocr_engine.py +38 -0
ocr_engine.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Load model directly
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModel, AutoTokenizer
|
5 |
+
from PIL import Image
|
6 |
+
import uuid
|
7 |
+
|
8 |
+
UPLOAD_FOLDER = "./uploads"
|
9 |
+
RESULTS_FOLDER = "./results"
|
10 |
+
|
11 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
12 |
+
if not os.path.exists(folder):
|
13 |
+
os.makedirs(folder)
|
14 |
+
|
15 |
+
class OCRModel:
|
16 |
+
tokenizer: AutoTokenizer
|
17 |
+
model: AutoModel
|
18 |
+
|
19 |
+
def __init__(self):
|
20 |
+
self.tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
21 |
+
self.model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda' if torch.cuda.is_available() else "cpu", use_safetensors=True, pad_token_id=self.tokenizer.eos_token_id)
|
22 |
+
self.model = self.model.eval()
|
23 |
+
if torch.cuda.is_available():
|
24 |
+
self.model = self.model.cuda()
|
25 |
+
|
26 |
+
def chat(self, image: Image.Image) -> str:
|
27 |
+
unique_id = str(uuid.uuid4())
|
28 |
+
image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
|
29 |
+
result_path = os.path.join(RESULTS_FOLDER, f"{unique_id}.txt")
|
30 |
+
|
31 |
+
image.save(image_path)
|
32 |
+
|
33 |
+
res = self.model.chat(self.tokenizer, image_path, ocr_type='ocr')
|
34 |
+
with open(result_path, 'w') as f:
|
35 |
+
f.write(res)
|
36 |
+
return res
|
37 |
+
|
38 |
+
ocr_model = OCRModel()
|