deeplotx 0.8.8__py3-none-any.whl → 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deeplotx/__init__.py +3 -0
- deeplotx/ner/__init__.py +3 -0
- deeplotx/ner/base_ner.py +7 -0
- deeplotx/ner/bert_ner.py +72 -0
- deeplotx/ner/named_entity.py +8 -0
- {deeplotx-0.8.8.dist-info → deeplotx-0.9.0.dist-info}/METADATA +3 -1
- {deeplotx-0.8.8.dist-info → deeplotx-0.9.0.dist-info}/RECORD +10 -6
- {deeplotx-0.8.8.dist-info → deeplotx-0.9.0.dist-info}/WHEEL +0 -0
- {deeplotx-0.8.8.dist-info → deeplotx-0.9.0.dist-info}/licenses/LICENSE +0 -0
- {deeplotx-0.8.8.dist-info → deeplotx-0.9.0.dist-info}/top_level.txt +0 -0
deeplotx/__init__.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4
4
|
__ROOT__ = os.path.dirname(os.path.abspath(__file__))
|
5
5
|
|
6
6
|
from .encoder import Encoder, LongTextEncoder, LongformerEncoder
|
7
|
+
from .ner import BertNER, NamedEntity
|
7
8
|
from .nn import (
|
8
9
|
FeedForward,
|
9
10
|
MultiHeadFeedForward,
|
@@ -40,3 +41,5 @@ logger = logging.getLogger('deeplotx.trainer')
|
|
40
41
|
logger.setLevel(logging.DEBUG)
|
41
42
|
logger = logging.getLogger('deeplotx.embedding')
|
42
43
|
logger.setLevel(logging.DEBUG)
|
44
|
+
logger = logging.getLogger('deeplotx.ner')
|
45
|
+
logger.setLevel(logging.DEBUG)
|
deeplotx/ner/__init__.py
ADDED
deeplotx/ner/base_ner.py
ADDED
deeplotx/ner/bert_ner.py
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import logging
|
2
|
+
import os
|
3
|
+
from requests.exceptions import ConnectTimeout, SSLError
|
4
|
+
|
5
|
+
import torch
|
6
|
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
7
|
+
|
8
|
+
from deeplotx import __ROOT__
|
9
|
+
from deeplotx.ner.base_ner import BaseNER
|
10
|
+
from deeplotx.ner.named_entity import NamedEntity
|
11
|
+
|
12
|
+
CACHE_PATH = os.path.join(__ROOT__, '.cache')
|
13
|
+
DEFAULT_BERT_NER = 'Davlan/xlm-roberta-base-ner-hrl'
|
14
|
+
logger = logging.getLogger('deeplotx.ner')
|
15
|
+
|
16
|
+
|
17
|
+
class BertNER(BaseNER):
|
18
|
+
def __init__(self, model_name_or_path: str = DEFAULT_BERT_NER, device: str | None = None):
|
19
|
+
super().__init__()
|
20
|
+
self.device = torch.device(device) if device is not None \
|
21
|
+
else torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
22
|
+
try:
|
23
|
+
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
24
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
25
|
+
trust_remote_code=True)
|
26
|
+
self.encoder = AutoModelForTokenClassification.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
27
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
28
|
+
trust_remote_code=True).to(self.device)
|
29
|
+
except ConnectTimeout:
|
30
|
+
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
31
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
32
|
+
trust_remote_code=True, local_files_only=True)
|
33
|
+
self.encoder = AutoModelForTokenClassification.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
34
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
35
|
+
trust_remote_code=True, local_files_only=True).to(self.device)
|
36
|
+
except SSLError:
|
37
|
+
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
38
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
39
|
+
trust_remote_code=True, local_files_only=True)
|
40
|
+
self.encoder = AutoModelForTokenClassification.from_pretrained(pretrained_model_name_or_path=model_name_or_path,
|
41
|
+
cache_dir=CACHE_PATH, _from_auto=True,
|
42
|
+
trust_remote_code=True, local_files_only=True).to(self.device)
|
43
|
+
self.embed_dim = self.encoder.config.max_position_embeddings
|
44
|
+
self._ner_pipeline = pipeline(task='ner', model=self.encoder, tokenizer=self.tokenizer, trust_remote_code=True)
|
45
|
+
logger.debug(f'{BaseNER.__name__} initialized on device: {self.device}.')
|
46
|
+
|
47
|
+
def extract_entities(self, s: str, prob_threshold: float = .0, *args, **kwargs) -> list[NamedEntity]:
|
48
|
+
assert prob_threshold <= 1., f'prob_threshold ({prob_threshold}) cannot be larger than 1.'
|
49
|
+
s = ' ' + s
|
50
|
+
raw_entities = self._ner_pipeline(s)
|
51
|
+
entities = []
|
52
|
+
for ent in raw_entities:
|
53
|
+
entities.append([s[ent['start']: ent['end']], ent['entity'], ent['score'].item()])
|
54
|
+
while True:
|
55
|
+
for i, ent in enumerate(entities):
|
56
|
+
if len(ent[0].strip()) < 1:
|
57
|
+
del entities[i]
|
58
|
+
if ent[1].upper().startswith('I') and entities[i - 1][1].upper().startswith('B'):
|
59
|
+
entities[i - 1][0] += ent[0]
|
60
|
+
entities[i - 1][2] *= ent[2]
|
61
|
+
del entities[i]
|
62
|
+
_continue = False
|
63
|
+
for ent in entities:
|
64
|
+
if ent[1].upper().startswith('I'):
|
65
|
+
_continue = True
|
66
|
+
if not _continue:
|
67
|
+
break
|
68
|
+
for ent in entities:
|
69
|
+
ent[0] = ent[0].strip()
|
70
|
+
if ent[1].upper().startswith('B'):
|
71
|
+
ent[1] = ent[1].upper()[1:].strip('-')
|
72
|
+
return [NamedEntity(*_) for _ in entities if _[2] >= prob_threshold]
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: deeplotx
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.9.0
|
4
4
|
Summary: Easy-2-use long text NLP toolkit.
|
5
5
|
Requires-Python: >=3.10
|
6
6
|
Description-Content-Type: text/markdown
|
@@ -10,11 +10,13 @@ Requires-Dist: jupyter
|
|
10
10
|
Requires-Dist: numpy
|
11
11
|
Requires-Dist: protobuf
|
12
12
|
Requires-Dist: python-dotenv
|
13
|
+
Requires-Dist: sentencepiece
|
13
14
|
Requires-Dist: tiktoken
|
14
15
|
Requires-Dist: torch
|
15
16
|
Requires-Dist: transformers
|
16
17
|
Requires-Dist: typing-extensions
|
17
18
|
Requires-Dist: vortezwohl>=0.0.8
|
19
|
+
Requires-Dist: name2gender>=0.0.4a0
|
18
20
|
Dynamic: license-file
|
19
21
|
|
20
22
|
[](https://deepwiki.com/vortezwohl/DeepLoTX)
|
@@ -1,8 +1,12 @@
|
|
1
|
-
deeplotx/__init__.py,sha256=
|
1
|
+
deeplotx/__init__.py,sha256=_CAKf1HvuHxUMoEYmYGaH8aSQVK99_LIXSzoF7vIviU,1301
|
2
2
|
deeplotx/encoder/__init__.py,sha256=BrsF5_4O-4pfihYF2wjExDOoAY-03kGJTH-Mhez4tsE,129
|
3
3
|
deeplotx/encoder/encoder.py,sha256=wVRl3p_7eg7qT_tJEit5qnmZx7dXkMVLxAtao5vImkk,4201
|
4
4
|
deeplotx/encoder/long_text_encoder.py,sha256=4oRa9FqfGNZ8-gq14UKuhDkZC0A1Xi-wKmbQsn-uZ58,3966
|
5
5
|
deeplotx/encoder/longformer_encoder.py,sha256=7Lm65AUD3qwbrzrhJ3dPZkyHeNRSapga3f-5QJCxV5A,3538
|
6
|
+
deeplotx/ner/__init__.py,sha256=NhLSZ6Urvv9jokdZOKC5vQd0STKmm2gMrdcMgvjQULc,101
|
7
|
+
deeplotx/ner/base_ner.py,sha256=OwD9fTuTu1xq2lC2NUnJ0MDJ3KGhUiBCXw9ySYg4eQQ,185
|
8
|
+
deeplotx/ner/bert_ner.py,sha256=179cqfodG-eyOOD8s8QgnFprDQRBukuFByqLoMH4d6c,4420
|
9
|
+
deeplotx/ner/named_entity.py,sha256=NfwdnLntkMrfPeZnIuWYbVnAvf2sdKQU1naodqRy0b0,125
|
6
10
|
deeplotx/nn/__init__.py,sha256=YILwbxb-NHdiJjfOwBKH8F7PuZSDZSrGpTznPDucTro,710
|
7
11
|
deeplotx/nn/attention.py,sha256=R-i-Rd7gnsh6hwXDeYfqLQOJvfSZIGfQbFzRlC91XLo,2879
|
8
12
|
deeplotx/nn/auto_regression.py,sha256=j_R7WGPq9REngjpLuX5c0AaNqOpgGm2Vfrolw-XjWXw,877
|
@@ -28,8 +32,8 @@ deeplotx/trainer/text_binary_classification_trainer.py,sha256=TFxOX8rWU_zKliI9zm
|
|
28
32
|
deeplotx/util/__init__.py,sha256=5CH4MTeSgsmCe3LPMfvKoSBpwh6jDSBuHVElJvzQzgs,90
|
29
33
|
deeplotx/util/hash.py,sha256=qbNU3RLBWGQYFVte9WZBAkZ1BkdjCXiKLDaKPN54KFk,662
|
30
34
|
deeplotx/util/read_file.py,sha256=ptzouvEQeeW8KU5BrWNJlXw-vFXVrpS9SkAUxsu6A8A,612
|
31
|
-
deeplotx-0.
|
32
|
-
deeplotx-0.
|
33
|
-
deeplotx-0.
|
34
|
-
deeplotx-0.
|
35
|
-
deeplotx-0.
|
35
|
+
deeplotx-0.9.0.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
|
36
|
+
deeplotx-0.9.0.dist-info/METADATA,sha256=9dx6Tz81e9YWTnsy-1VPw30ts9oJUcQD0Kohm36_HKA,13230
|
37
|
+
deeplotx-0.9.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
38
|
+
deeplotx-0.9.0.dist-info/top_level.txt,sha256=hKg4pVDXZ-WWxkRfJFczRIll1Sv7VyfKCmzHLXbuh1U,9
|
39
|
+
deeplotx-0.9.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|