[cad161]: / demo / app.py

Download this file

353 lines (289 with data), 8.6 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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from typing import Any
import pandas as pd
import streamlit as st
from spacy import displacy
import edsnlp
import edsnlp.pipes as eds
from edsnlp.utils.filter import filter_spans
DEFAULT_TEXT = """\
Motif :
Le patient est admis le 29 août pour des difficultés respiratoires.
Antécédents familiaux :
Le père du patient n'est pas asthmatique.
HISTOIRE DE LA MALADIE
Le patient dit avoir de la toux depuis trois jours. \
Elle a empiré jusqu'à nécessiter un passage aux urgences.
A noter deux petits kystes bénins de 1 et 2cm biopsiés en 2005.
Priorité: 2 (établie par l'IAO à l'entrée)
adicaps ABCD0A12 et ABCD0A13
Conclusion
Possible infection au coronavirus. Prescription de paracétomol pour la fièvre.\
"""
REGEX = """
# RegEx and terms matcher
nlp.add_pipe(
eds.matcher(
regex=dict(custom=r"{custom_regex}"),
attr="NORM",
),
)
"""
CODE = """
import edsnlp, edsnlp.pipes as eds
# Declare the pipeline
nlp = edsnlp.blank("eds")
# General-purpose components
nlp.add_pipe(eds.normalizer())
nlp.add_pipe(eds.sentences())
{pipes}
# Qualifier pipes
nlp.add_pipe(eds.negation())
nlp.add_pipe(eds.family())
nlp.add_pipe(eds.hypothesis())
nlp.add_pipe(eds.rspeech())
# Define the note text
text = {text}
# Apply the pipeline
doc = nlp(text)
# Explore matched elements
doc.ents
"""
PIPES = {
"Drugs": "drugs",
"CIM10": "cim10",
"Dates": "dates",
"Quantities": "quantities",
"Charlson": "charlson",
"SOFA": "sofa",
"Elston & Ellis": "elston_ellis",
"TNM": "tnm",
"Priority": "emergency_priority",
"CCMU": "emergency_ccmu",
"GEMSA": "emergency_gemsa",
"Covid": "covid",
"Adicap": "adicap",
"Diabetes": "diabetes",
"Tobacco": "tobacco",
"AIDS": "aids",
"Lymphoma": "lymphoma",
"Leukemia": "leukemia",
"Solid Tumor": "solid_tumor",
"CKD": "ckd",
"Hemiplegia": "hemiplegia",
"Liver Disease": "liver_disease",
"Peptic Ulcer Disease": "peptic_ulcer_disease",
"Connective Tissue Disease": "connective_tissue_disease",
"COPD": "copd",
"Dementia": "dementia",
"Cerebrovascular Accident": "cerebrovascular_accident",
"Peripheral Vascular Disease": "peripheral_vascular_disease",
"Congestive Heart Failure": "congestive_heart_failure",
"Myocardial Infarction": "myocardial_infarction",
"Alcohol": "alcohol",
}
@st.cache_resource()
def load_model(custom_regex: str, **enabled):
pipes = []
# Declare the pipeline
nlp = edsnlp.blank("eds")
nlp.add_pipe(eds.normalizer())
nlp.add_pipe(eds.sentences())
for title, name in PIPES.items():
if name == "drugs":
if enabled["drugs"]:
if enabled["fuzzy_drugs"]:
nlp.add_pipe(eds.drugs(term_matcher="simstring"))
pipes.append('nlp.add_pipe(eds.drugs(term_matcher="simstring"))')
else:
nlp.add_pipe(eds.drugs())
pipes.append("nlp.add_pipe(eds.drugs())")
else:
if enabled[name]:
nlp.add_pipe(f"eds.{name}")
pipes.append(f"nlp.add_pipe(eds.{name}())")
if pipes:
pipes.insert(0, "# Entity extraction pipes")
if custom_regex:
nlp.add_pipe(
eds.matcher(
regex=dict(custom=custom_regex),
attr="NORM",
),
)
regex = REGEX.format(custom_regex=custom_regex)
else:
regex = ""
nlp.add_pipe(eds.negation())
nlp.add_pipe(eds.family())
nlp.add_pipe(eds.hypothesis())
nlp.add_pipe(eds.rspeech())
return nlp, pipes, regex
st.set_page_config(
page_title="EDS-NLP Demo",
page_icon="📄",
)
st.title("EDS-NLP")
st.warning(
"You should **not** put sensitive data in the example, as this application "
"**is not secure**."
)
st.sidebar.header("About")
st.sidebar.markdown(
"EDS-NLP is a contributive effort maintained by AP-HP's Data Science team. "
"Have a look at the "
"[documentation](https://aphp.github.io/edsnlp/) for "
"more information on the available components."
)
st.sidebar.header("Pipeline")
st.sidebar.markdown(
"This example runs a simplistic pipeline detecting a few synonyms for "
"COVID-related entities.\n\n"
"You can add or remove pre-defined pipeline components, and see how "
"the pipeline reacts. You can also search for your own custom RegEx."
)
st.sidebar.header("Custom RegEx")
st_custom_regex = st.sidebar.text_input(
"Regular Expression:",
r"asthmatique|difficult[ée]s?\srespiratoires?",
)
st.sidebar.markdown("The RegEx you defined above is detected under the `custom` label.")
st.sidebar.subheader("Pipeline Components")
st_pipes = {}
st_pipes["cim10"] = st.sidebar.checkbox("CIM10 (loading can be slow)", value=False)
st_drugs_container = st.sidebar.columns([1, 2])
st_pipes["drugs"] = st_drugs_container[0].checkbox("Drugs", value=True)
st_fuzzy_drugs = st_drugs_container[1].checkbox(
"Fuzzy drugs search", value=True, disabled=not st_pipes["drugs"]
)
for title, name in PIPES.items():
if name == "drugs" or name == "cim10":
continue
st_pipes[name] = st.sidebar.checkbox(title, value=True)
st.sidebar.markdown(
"These are just a few of the components provided out-of-the-box by EDS-NLP. "
"See the [documentation](https://aphp.github.io/edsnlp/latest/pipes/) "
"for detail."
)
model_load_state = st.info("Loading model...")
nlp, pipes, regex = load_model(
fuzzy_drugs=st_fuzzy_drugs,
custom_regex=st_custom_regex,
**st_pipes,
)
model_load_state.empty()
st.header("Enter a text to analyse:")
text = st.text_area(
"Modify the following text and see the pipeline react :",
DEFAULT_TEXT,
height=375,
)
doc = nlp(text)
doc.ents = filter_spans(
(*doc.ents, *doc.spans.get("dates", []), *doc.spans.get("quantities", []))
)
st.header("Visualisation")
st.markdown(
"The pipeline extracts simple entities using a dictionnary of RegEx (see the "
"[Export the pipeline section](#export-the-pipeline) for more information)."
)
category20 = [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#ffbb78",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5",
"#8c564b",
"#c49c94",
"#e377c2",
"#f7b6d2",
"#7f7f7f",
"#c7c7c7",
"#bcbd22",
"#dbdb8d",
"#17becf",
"#9edae5",
]
labels = [
"date",
"covid",
"drug",
"cim10",
"emergency_priority",
"sofa",
"charlson",
"size",
"weight",
"adicap",
]
colors = {label: cat for label, cat in zip(labels, category20)}
colors["custom"] = "linear-gradient(90deg, #aa9cfc, #fc9ce7)"
options = {
"colors": colors,
}
html = displacy.render(doc, style="ent", options=options)
html = html.replace("line-height: 2.5;", "line-height: 2.25;")
html = (
'<div style="padding: 10px; border: solid 2px; border-radius: 10px; '
f'border-color: #afc6e0;">{html}</div>'
)
st.write(html, unsafe_allow_html=True)
data = []
for ent in doc.ents:
d = dict(
start=ent.start_char,
end=ent.end_char,
text=ent.text,
label=ent.label_,
normalized_value=str(ent._.value or ""),
negation="YES" if ent._.negation else "NO",
family="YES" if ent._.family else "NO",
hypothesis="YES" if ent._.hypothesis else "NO",
reported_speech="YES" if ent._.reported_speech else "NO",
)
data.append(d)
st.header("Entity qualification")
def color_qualifiers(val: Any) -> str:
"""
Add color to qualifiers.
Parameters
----------
val : Any
DataFrame value
Returns
-------
str
style
"""
if val == "NO":
return "color: #dc3545;"
elif val == "YES":
return "color: #198754;"
return ""
if data:
df = pd.DataFrame.from_records(data)
df.normalized_value = df.normalized_value.replace({"None": ""})
df = df.style.applymap(color_qualifiers)
st.dataframe(df)
else:
st.markdown("You pipeline did not match any entity...")
pipes_text = ""
if pipes:
pipes_text += "\n" + "\n".join(pipes) + "\n"
if regex:
pipes_text += regex
code = CODE.format(
pipes=pipes_text,
text=f'"""\n{text}\n"""',
)
st.header("Export the pipeline")
st.markdown(
"The code below recreates the pipeline. Copy and paste it "
"in a Jupyter Notebook to interact with it."
)
with st.expander("Show the runnable code"):
st.markdown(f"```python\n{code}\n```\n\nThis code runs as is.")