Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -141,35 +141,74 @@ def adr_predict(x):
|
|
| 141 |
# ββ NER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 142 |
try:
|
| 143 |
res = ner_pipe(text_input)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
"
|
|
|
|
|
|
|
| 152 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
prev_end = 0
|
| 156 |
-
|
| 157 |
-
for entity in
|
| 158 |
start, end = entity["start"], entity["end"]
|
| 159 |
-
word
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
f"<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
prev_end = end
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
htext =
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
label_output = {
|
| 175 |
"Severe Reaction": float(scores[1]),
|
|
@@ -217,10 +256,14 @@ with gr.Blocks(title="ADR Detector", css=custom_css, theme=gr.themes.Soft()) as
|
|
| 217 |
gr.Markdown("### Examples")
|
| 218 |
gr.Examples(
|
| 219 |
examples=[
|
| 220 |
-
["A
|
| 221 |
-
"
|
| 222 |
-
["A
|
| 223 |
-
"taking Acetaminophen."],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
],
|
| 225 |
inputs=[prob1],
|
| 226 |
)
|
|
@@ -248,4 +291,4 @@ with gr.Blocks(title="ADR Detector", css=custom_css, theme=gr.themes.Soft()) as
|
|
| 248 |
outputs=[label, shap_out, htext_out],
|
| 249 |
)
|
| 250 |
|
| 251 |
-
demo.launch()
|
|
|
|
| 141 |
# ββ NER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 142 |
try:
|
| 143 |
res = ner_pipe(text_input)
|
| 144 |
+
|
| 145 |
+
# Richer config: background, border accent, and human-readable display name
|
| 146 |
+
entity_config = {
|
| 147 |
+
"Severity": {"bg": "#ffe0de", "border": "#e07070", "label": "Severity"},
|
| 148 |
+
"Sign_symptom": {"bg": "#d4f5d4", "border": "#5aaa5a", "label": "Symptom"},
|
| 149 |
+
"Medication": {"bg": "#d0e8ff", "border": "#4a90d9", "label": "Medication"},
|
| 150 |
+
"Age": {"bg": "#fff3cc", "border": "#d4a800", "label": "Age"},
|
| 151 |
+
"Sex": {"bg": "#ffe8fb", "border": "#c070b0", "label": "Sex"},
|
| 152 |
+
"Diagnostic_procedure": {"bg": "#e8e8e8", "border": "#888888", "label": "Diagnostic"},
|
| 153 |
+
"Biological_structure": {"bg": "#ddeeff", "border": "#6699cc", "label": "Body Part"},
|
| 154 |
}
|
| 155 |
+
default_cfg = {"bg": "#f0f0f0", "border": "#aaaaaa", "label": "Other"}
|
| 156 |
+
|
| 157 |
+
# Legend β only show entity types actually found in this result
|
| 158 |
+
seen_groups = list(dict.fromkeys(e["entity_group"] for e in sorted(res, key=lambda e: e["start"])))
|
| 159 |
+
legend_html = (
|
| 160 |
+
"<div style='display:flex; flex-wrap:wrap; gap:10px; "
|
| 161 |
+
"margin-bottom:16px; padding-bottom:12px; "
|
| 162 |
+
"border-bottom:1px solid #e0e0e0;'>"
|
| 163 |
+
)
|
| 164 |
+
for grp in seen_groups:
|
| 165 |
+
cfg = entity_config.get(grp, default_cfg)
|
| 166 |
+
legend_html += (
|
| 167 |
+
f"<span style='display:inline-flex; align-items:center; gap:6px; "
|
| 168 |
+
f"font-size:0.8em; font-weight:600; color:#444; "
|
| 169 |
+
f"font-family:system-ui, sans-serif;'>"
|
| 170 |
+
f"<span style='display:inline-block; width:13px; height:13px; "
|
| 171 |
+
f"border-radius:3px; background:{cfg[\"bg\"]}; "
|
| 172 |
+
f"border:2px solid {cfg[\"border\"]};'></span>"
|
| 173 |
+
f"{cfg['label']}</span>"
|
| 174 |
+
)
|
| 175 |
+
legend_html += "</div>"
|
| 176 |
|
| 177 |
+
# Annotated text β each entity as a pill with a small label badge above it
|
| 178 |
+
text_html = (
|
| 179 |
+
"<div style='line-height:3.4; font-size:1.05em; color:#111; "
|
| 180 |
+
"font-family:Georgia, serif; letter-spacing:0.01em;'>"
|
| 181 |
+
)
|
| 182 |
prev_end = 0
|
| 183 |
+
res_sorted = sorted(res, key=lambda e: e["start"])
|
| 184 |
+
for entity in res_sorted:
|
| 185 |
start, end = entity["start"], entity["end"]
|
| 186 |
+
word = text_input[start:end]
|
| 187 |
+
cfg = entity_config.get(entity["entity_group"], default_cfg)
|
| 188 |
+
|
| 189 |
+
# Plain text before this entity
|
| 190 |
+
text_html += f"<span style='color:#111;'>{text_input[prev_end:start]}</span>"
|
| 191 |
+
|
| 192 |
+
# Entity pill: small uppercase label above, coloured word below
|
| 193 |
+
text_html += (
|
| 194 |
+
f"<span style='display:inline-block; position:relative; "
|
| 195 |
+
f"vertical-align:middle; margin:0 2px; text-align:center;'>"
|
| 196 |
+
f"<span style='display:block; font-size:0.6em; font-weight:800; "
|
| 197 |
+
f"letter-spacing:0.08em; text-transform:uppercase; "
|
| 198 |
+
f"color:{cfg[\"border\"]}; font-family:system-ui, sans-serif; "
|
| 199 |
+
f"line-height:1.1; margin-bottom:2px;'>{cfg['label']}</span>"
|
| 200 |
+
f"<span style='background:{cfg[\"bg\"]}; border:1.5px solid {cfg[\"border\"]}; "
|
| 201 |
+
f"color:#111; padding:3px 8px; border-radius:6px; "
|
| 202 |
+
f"font-weight:600; white-space:nowrap;'>{word}</span>"
|
| 203 |
+
f"</span>"
|
| 204 |
)
|
| 205 |
prev_end = end
|
| 206 |
+
|
| 207 |
+
text_html += f"<span style='color:#111;'>{text_input[prev_end:]}</span></div>"
|
| 208 |
+
htext = legend_html + text_html
|
| 209 |
+
|
| 210 |
+
except Exception as ex:
|
| 211 |
+
htext = f"<p style='color:#c00;'>NER processing error: {ex}</p>"
|
| 212 |
|
| 213 |
label_output = {
|
| 214 |
"Severe Reaction": float(scores[1]),
|
|
|
|
| 256 |
gr.Markdown("### Examples")
|
| 257 |
gr.Examples(
|
| 258 |
examples=[
|
| 259 |
+
["A 42 year-old male developed a severe migraine and elevated blood pressure "
|
| 260 |
+
"shortly after taking Aspirin. He was admitted for observation."],
|
| 261 |
+
["A 28 year-old female reported mild nausea and minor discomfort in the upper "
|
| 262 |
+
"abdomen after taking Acetaminophen 500mg. Symptoms resolved within two hours."],
|
| 263 |
+
["A 67 year-old male with a history of renal impairment experienced acute kidney "
|
| 264 |
+
"injury and oliguria following treatment with Ibuprofen for three days."],
|
| 265 |
+
["A 54 year-old female noted slight dizziness and dry mouth after her first dose "
|
| 266 |
+
"of Metformin. No further symptoms were reported at the follow-up visit."],
|
| 267 |
],
|
| 268 |
inputs=[prob1],
|
| 269 |
)
|
|
|
|
| 291 |
outputs=[label, shap_out, htext_out],
|
| 292 |
)
|
| 293 |
|
| 294 |
+
demo.launch()
|