Spaces:
Sleeping
Sleeping
OnlyBiggg
commited on
Commit
·
5816e57
1
Parent(s):
fb78001
fix load model
Browse files- app/ner/models/base_model.py +0 -16
- app/ner/services/ner.py +6 -4
- core/conf.py +4 -2
app/ner/models/base_model.py
DELETED
|
@@ -1,16 +0,0 @@
|
|
| 1 |
-
from datetime import datetime
|
| 2 |
-
from transformers import AutoTokenizer
|
| 3 |
-
from optimum.onnxruntime import ORTModelForTokenClassification
|
| 4 |
-
from optimum.pipelines import pipeline
|
| 5 |
-
start = datetime.now()
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained("model_ner")
|
| 10 |
-
model = ORTModelForTokenClassification.from_pretrained("model_ner", provider="CPUExecutionProvider")
|
| 11 |
-
pipe = pipeline("token-classification", model=model, tokenizer=tokenizer, accelerator="ort", device=-1)
|
| 12 |
-
|
| 13 |
-
result = pipe("Tôi tên là Trần Văn Đại, địa chỉ 12 Phan đình phùng")
|
| 14 |
-
end = datetime.now()
|
| 15 |
-
print(result)
|
| 16 |
-
print("Time taken: ", (end - start).total_seconds())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/ner/services/ner.py
CHANGED
|
@@ -8,11 +8,13 @@ class NER:
|
|
| 8 |
self.pipeline = None
|
| 9 |
|
| 10 |
def load_model(self):
|
| 11 |
-
from transformers import AutoTokenizer
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
self.tokenizer = AutoTokenizer.from_pretrained(self.model_dir
|
| 14 |
-
self.model =
|
| 15 |
-
self.pipeline = pipeline(settings.TASK_NAME, model=self.model, tokenizer=self.tokenizer)
|
| 16 |
|
| 17 |
def predict(self, text: str, entity_tag: str = None):
|
| 18 |
if self.pipeline is None:
|
|
|
|
| 8 |
self.pipeline = None
|
| 9 |
|
| 10 |
def load_model(self):
|
| 11 |
+
from transformers import AutoTokenizer
|
| 12 |
+
from optimum.onnxruntime import ORTModelForTokenClassification
|
| 13 |
+
from optimum.pipelines import pipeline
|
| 14 |
|
| 15 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_dir)
|
| 16 |
+
self.model = ORTModelForTokenClassification.from_pretrained(self.model_dir)
|
| 17 |
+
self.pipeline = pipeline(task=settings.TASK_NAME, model=self.model, tokenizer=self.tokenizer, accelerator=settings.ACCELERATOR, device=settings.DEVICE)
|
| 18 |
|
| 19 |
def predict(self, text: str, entity_tag: str = None):
|
| 20 |
if self.pipeline is None:
|
core/conf.py
CHANGED
|
@@ -32,8 +32,10 @@ class Settings(BaseSettings):
|
|
| 32 |
# DATABASE_PASSWORD: str
|
| 33 |
|
| 34 |
# MODEl NER
|
| 35 |
-
NER_MODEL_DIR: str =
|
| 36 |
-
TASK_NAME: str = '
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# FastAPI
|
| 39 |
FASTAPI_API_V1_PATH: str = '/api/v1'
|
|
|
|
| 32 |
# DATABASE_PASSWORD: str
|
| 33 |
|
| 34 |
# MODEl NER
|
| 35 |
+
NER_MODEL_DIR: str = 'app/ner/models/ner'
|
| 36 |
+
TASK_NAME: str = 'token-classification'
|
| 37 |
+
ACCELERATOR: str = 'ort'
|
| 38 |
+
DEVICE: str = 'cpu'
|
| 39 |
|
| 40 |
# FastAPI
|
| 41 |
FASTAPI_API_V1_PATH: str = '/api/v1'
|