[cad161]: / scripts / serve.py

Download this file

87 lines (66 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import List
import spacy
from fastapi import FastAPI
from pydantic import BaseModel
import edsnlp
app = FastAPI(title="EDS-NLP", version=edsnlp.__version__)
nlp = spacy.blank("eds")
nlp.add_pipe("eds.sentences")
config = dict(
regex=dict(
covid=[
"covid",
r"covid[-\s]?19",
r"sars[-\s]?cov[-\s]?2",
r"corona[-\s]?virus",
],
),
attr="LOWER",
)
nlp.add_pipe("eds.matcher", config=config)
nlp.add_pipe("eds.negation")
nlp.add_pipe("eds.family")
nlp.add_pipe("eds.hypothesis")
nlp.add_pipe("eds.reported_speech")
class Entity(BaseModel): # (2)
# OMOP-style attributes
start: int
end: int
label: str
lexical_variant: str
normalized_variant: str
# Qualifiers
negated: bool
hypothesis: bool
family: bool
reported_speech: bool
class Document(BaseModel): # (1)
text: str
ents: List[Entity]
@app.post("/process", response_model=List[Document]) # (1)
async def process(
notes: List[str], # (2)
):
documents = []
for doc in nlp.pipe(notes):
entities = []
for ent in doc.ents:
entity = Entity(
start=ent.start_char,
end=ent.end_char,
label=ent.label_,
lexical_variant=ent.text,
normalized_variant=ent._.normalized_variant,
negated=ent._.negation,
hypothesis=ent._.hypothesis,
family=ent._.family,
reported_speech=ent._.reported_speech,
)
entities.append(entity)
documents.append(
Document(
text=doc.text,
ents=entities,
)
)
return documents