Update app/utils.py
Browse files- app/utils.py +12 -15
app/utils.py
CHANGED
@@ -1,36 +1,33 @@
|
|
1 |
-
from transformers import AutoModel, AutoTokenizer
|
2 |
import os
|
3 |
-
import
|
4 |
-
from
|
5 |
|
6 |
class OCRModel:
|
7 |
_instance = None
|
8 |
-
|
9 |
def __new__(cls):
|
10 |
if cls._instance is None:
|
11 |
cls._instance = super(OCRModel, cls).__new__(cls)
|
12 |
cls._instance.initialize()
|
13 |
return cls._instance
|
14 |
-
|
15 |
def initialize(self):
|
16 |
model_path = os.getenv('MODEL_PATH', 'RufusRubin777/GOT-OCR2_0_CPU')
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
local_files_only=False
|
22 |
)
|
23 |
|
24 |
-
self.
|
25 |
model_path,
|
26 |
-
|
27 |
-
low_cpu_mem_usage=True,
|
28 |
-
device_map='cpu',
|
29 |
-
use_safetensors=True,
|
30 |
-
pad_token_id=self.tokenizer.eos_token_id
|
31 |
)
|
32 |
|
33 |
-
self.model
|
34 |
|
35 |
def process_image(self, image_stream):
|
36 |
try:
|
|
|
|
|
1 |
import os
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
from transformers_modules.RufusRubin777.GOT_OCR2_0_CPU.modeling_GOT import GOTModel, GOTConfig
|
4 |
|
5 |
class OCRModel:
|
6 |
_instance = None
|
7 |
+
|
8 |
def __new__(cls):
|
9 |
if cls._instance is None:
|
10 |
cls._instance = super(OCRModel, cls).__new__(cls)
|
11 |
cls._instance.initialize()
|
12 |
return cls._instance
|
13 |
+
|
14 |
def initialize(self):
|
15 |
model_path = os.getenv('MODEL_PATH', 'RufusRubin777/GOT-OCR2_0_CPU')
|
16 |
|
17 |
+
# تحميل النموذج بالطريقة الصحيحة
|
18 |
+
config = GOTConfig.from_pretrained(model_path)
|
19 |
+
self.model = GOTModel.from_pretrained(
|
20 |
+
model_path,
|
21 |
+
config=config,
|
22 |
local_files_only=False
|
23 |
)
|
24 |
|
25 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
26 |
model_path,
|
27 |
+
local_files_only=False
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
+
self.model.eval()
|
31 |
|
32 |
def process_image(self, image_stream):
|
33 |
try:
|