Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,10 +89,14 @@ def _merge_ner_entities(entities, original_text):
|
|
| 89 |
merged = []
|
| 90 |
for ent in entities:
|
| 91 |
word = original_text[ent['start']:ent['end']]
|
| 92 |
-
if merged and ent['start'] <= merged[-1]['end'] + 2:
|
|
|
|
| 93 |
merged[-1]['word'] = original_text[merged[-1]['start']:ent['end']]
|
| 94 |
merged[-1]['end'] = ent['end']
|
| 95 |
merged[-1]['score'] = (merged[-1]['score'] + float(ent['score'])) / 2
|
|
|
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
merged.append({
|
| 98 |
'word': word,
|
|
@@ -415,14 +419,28 @@ CSS = """
|
|
| 415 |
font-weight: 700 !important;
|
| 416 |
letter-spacing: 1px !important;
|
| 417 |
}
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
}
|
| 427 |
"""
|
| 428 |
|
|
@@ -430,10 +448,9 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base()) as demo:
|
|
| 430 |
gr.HTML('<h1 id="title">⚕ ADR Severity Analyzer</h1>')
|
| 431 |
gr.HTML('<p id="subtitle">Adverse Drug Reaction · Severity Classification · SHAP Explainability · This is a demo, do not use for medical advice.</p>')
|
| 432 |
|
| 433 |
-
# ── Input + Verdict
|
| 434 |
with gr.Row():
|
| 435 |
-
with gr.Column(scale=1):
|
| 436 |
-
gr.HTML("<div style='background:white; border:1.5px solid #3a5fc8; border-radius:12px; box-shadow:0 4px 12px rgba(58,95,200,0.15); padding:24px;'>")
|
| 437 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>📝 Please Enter Your Drug Reaction</h3>")
|
| 438 |
text_input = gr.Textbox(
|
| 439 |
label="",
|
|
@@ -441,41 +458,33 @@ with gr.Blocks(css=CSS, theme=gr.themes.Base()) as demo:
|
|
| 441 |
lines=5,
|
| 442 |
)
|
| 443 |
analyze_btn = gr.Button("🔍 Analyze Reaction", variant="primary")
|
| 444 |
-
|
| 445 |
-
with gr.Column(scale=1):
|
| 446 |
-
gr.HTML("<div style='background:white; border:1.5px solid #3a5fc8; border-radius:12px; box-shadow:0 4px 12px rgba(58,95,200,0.15); padding:24px;'>")
|
| 447 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>⚕ Severity Classification</h3>")
|
| 448 |
verdict_out = gr.HTML()
|
| 449 |
-
gr.HTML("</div>")
|
| 450 |
|
| 451 |
-
# ── Examples
|
| 452 |
-
gr.HTML("<
|
| 453 |
-
gr.HTML("<h3 style='text-align:center; margin-top:0;'>💡 Try an Example</h3>")
|
| 454 |
with gr.Row():
|
| 455 |
ex_btns = []
|
| 456 |
for example in EXAMPLES:
|
| 457 |
btn = gr.Button(
|
| 458 |
-
example[0],
|
| 459 |
size="sm",
|
|
|
|
| 460 |
)
|
| 461 |
ex_btns.append((btn, example[0]))
|
| 462 |
-
gr.HTML("</div>")
|
| 463 |
|
| 464 |
for btn, text in ex_btns:
|
| 465 |
btn.click(fn=lambda t=text: t, inputs=[], outputs=text_input)
|
| 466 |
|
| 467 |
-
# ── SHAP + NER
|
| 468 |
with gr.Row():
|
| 469 |
-
with gr.Column(scale=1):
|
| 470 |
-
gr.HTML("<div style='background:white; border:1.5px solid #3a5fc8; border-radius:12px; box-shadow:0 4px 12px rgba(58,95,200,0.15); padding:24px;'>")
|
| 471 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>🎨 Word-Level SHAP Highlights</h3>")
|
| 472 |
highlight_out = gr.HTML()
|
| 473 |
-
|
| 474 |
-
with gr.Column(scale=1):
|
| 475 |
-
gr.HTML("<div style='background:white; border:1.5px solid #3a5fc8; border-radius:12px; box-shadow:0 4px 12px rgba(58,95,200,0.15); padding:24px;'>")
|
| 476 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>🏥 Medical Entity Recognition</h3>")
|
| 477 |
ner_out = gr.HTML()
|
| 478 |
-
gr.HTML("</div>")
|
| 479 |
|
| 480 |
analyze_btn.click(
|
| 481 |
fn=predict,
|
|
|
|
| 89 |
merged = []
|
| 90 |
for ent in entities:
|
| 91 |
word = original_text[ent['start']:ent['end']]
|
| 92 |
+
if merged and ent['start'] <= merged[-1]['end'] + 2:
|
| 93 |
+
# Merge regardless of entity group — same word split across tokens
|
| 94 |
merged[-1]['word'] = original_text[merged[-1]['start']:ent['end']]
|
| 95 |
merged[-1]['end'] = ent['end']
|
| 96 |
merged[-1]['score'] = (merged[-1]['score'] + float(ent['score'])) / 2
|
| 97 |
+
# Keep the entity group with higher score
|
| 98 |
+
if float(ent['score']) > merged[-1]['score']:
|
| 99 |
+
merged[-1]['entity_group'] = ent['entity_group']
|
| 100 |
else:
|
| 101 |
merged.append({
|
| 102 |
'word': word,
|
|
|
|
| 419 |
font-weight: 700 !important;
|
| 420 |
letter-spacing: 1px !important;
|
| 421 |
}
|
| 422 |
+
#input-card, #verdict-card, #shap-card, #ner-card {
|
| 423 |
+
border: 1.5px solid #3a5fc8 !important;
|
| 424 |
+
border-radius: 12px !important;
|
| 425 |
+
box-shadow: 0 4px 12px rgba(58,95,200,0.15) !important;
|
| 426 |
+
padding: 16px !important;
|
| 427 |
+
}
|
| 428 |
+
.example-btn {
|
| 429 |
+
border: 1.5px solid #3a5fc8 !important;
|
| 430 |
+
border-radius: 8px !important;
|
| 431 |
+
background: white !important;
|
| 432 |
+
font-size: 0.82rem !important;
|
| 433 |
+
font-weight: 600 !important;
|
| 434 |
+
white-space: normal !important;
|
| 435 |
+
height: auto !important;
|
| 436 |
+
text-align: center !important;
|
| 437 |
+
line-height: 1.4 !important;
|
| 438 |
+
padding: 12px !important;
|
| 439 |
+
color: #222 !important;
|
| 440 |
+
}
|
| 441 |
+
.example-btn:hover {
|
| 442 |
+
background: #eef2ff !important;
|
| 443 |
+
cursor: pointer !important;
|
| 444 |
}
|
| 445 |
"""
|
| 446 |
|
|
|
|
| 448 |
gr.HTML('<h1 id="title">⚕ ADR Severity Analyzer</h1>')
|
| 449 |
gr.HTML('<p id="subtitle">Adverse Drug Reaction · Severity Classification · SHAP Explainability · This is a demo, do not use for medical advice.</p>')
|
| 450 |
|
| 451 |
+
# ── Input + Verdict ──
|
| 452 |
with gr.Row():
|
| 453 |
+
with gr.Column(scale=1, elem_id="input-card"):
|
|
|
|
| 454 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>📝 Please Enter Your Drug Reaction</h3>")
|
| 455 |
text_input = gr.Textbox(
|
| 456 |
label="",
|
|
|
|
| 458 |
lines=5,
|
| 459 |
)
|
| 460 |
analyze_btn = gr.Button("🔍 Analyze Reaction", variant="primary")
|
| 461 |
+
with gr.Column(scale=1, elem_id="verdict-card"):
|
|
|
|
|
|
|
| 462 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>⚕ Severity Classification</h3>")
|
| 463 |
verdict_out = gr.HTML()
|
|
|
|
| 464 |
|
| 465 |
+
# ── Examples ──
|
| 466 |
+
gr.HTML("<h3 style='text-align:center;'>💡 Try an Example</h3>")
|
|
|
|
| 467 |
with gr.Row():
|
| 468 |
ex_btns = []
|
| 469 |
for example in EXAMPLES:
|
| 470 |
btn = gr.Button(
|
| 471 |
+
f'"{example[0][:80]}..."',
|
| 472 |
size="sm",
|
| 473 |
+
elem_classes="example-btn"
|
| 474 |
)
|
| 475 |
ex_btns.append((btn, example[0]))
|
|
|
|
| 476 |
|
| 477 |
for btn, text in ex_btns:
|
| 478 |
btn.click(fn=lambda t=text: t, inputs=[], outputs=text_input)
|
| 479 |
|
| 480 |
+
# ── SHAP + NER ──
|
| 481 |
with gr.Row():
|
| 482 |
+
with gr.Column(scale=1, elem_id="shap-card"):
|
|
|
|
| 483 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>🎨 Word-Level SHAP Highlights</h3>")
|
| 484 |
highlight_out = gr.HTML()
|
| 485 |
+
with gr.Column(scale=1, elem_id="ner-card"):
|
|
|
|
|
|
|
| 486 |
gr.HTML("<h3 style='text-align:center; margin-top:0;'>🏥 Medical Entity Recognition</h3>")
|
| 487 |
ner_out = gr.HTML()
|
|
|
|
| 488 |
|
| 489 |
analyze_btn.click(
|
| 490 |
fn=predict,
|