Jenny Pereira
commited on
Commit
·
abd01f0
1
Parent(s):
abd29c7
add custom handler
Browse files- __pycache__/handler.cpython-38.pyc +0 -0
- handler.py +30 -0
__pycache__/handler.cpython-38.pyc
ADDED
Binary file (1.28 kB). View file
|
|
handler.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from flair.data import Sentence
|
3 |
+
from flair.models import SequenceTagger
|
4 |
+
|
5 |
+
class EndpointHandler():
|
6 |
+
def __init__(self, path=""):
|
7 |
+
#code
|
8 |
+
self.tagger = SequenceTagger.load(path)
|
9 |
+
|
10 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
11 |
+
#code
|
12 |
+
inputs = data.pop("inputs", data)
|
13 |
+
sentence: Sentence = Sentence(inputs)
|
14 |
+
|
15 |
+
self.tagger.predict(sentence, label_name="predicted")
|
16 |
+
entities = []
|
17 |
+
for span in sentence.get_spans("predicted"):
|
18 |
+
if len(span.tokens) == 0:
|
19 |
+
continue
|
20 |
+
current_entity = {
|
21 |
+
"entity_group": span.tag,
|
22 |
+
"word": span.text,
|
23 |
+
"start": span.tokens[0].start_position,
|
24 |
+
"end": span.tokens[-1].end_position,
|
25 |
+
"score": span.score,
|
26 |
+
}
|
27 |
+
|
28 |
+
entities.append(current_entity)
|
29 |
+
|
30 |
+
return entities
|