Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from gliner import GLiNER
|
| 4 |
+
|
| 5 |
+
model = GLiNER.from_pretrained("Ihor/gliner-biomed-base-v1.0")
|
| 6 |
+
|
| 7 |
+
MAX_LABELS = 12
|
| 8 |
+
|
| 9 |
+
PALETTE = [
|
| 10 |
+
"#FEF0C3", "#E8D5B7", "#F5E6CC", "#FFDDBE", "#D2E3FC", "#C8E6C9",
|
| 11 |
+
"#EADDFF", "#F9DEDC", "#B2DFDB", "#FFE0B2", "#F0F4C3", "#BBDEFB",
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
DEFAULT_LABELS = ["patient_name", "age", "sex", "symptom", "diagnosis", "medication", "vital_sign", "procedure"]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def extract(text, labels_list, threshold):
|
| 18 |
+
labels = [l for l in labels_list if l][:MAX_LABELS]
|
| 19 |
+
if not labels or not text.strip():
|
| 20 |
+
return None, [], ""
|
| 21 |
+
|
| 22 |
+
color_map = {l: PALETTE[i % len(PALETTE)] for i, l in enumerate(labels)}
|
| 23 |
+
|
| 24 |
+
start = time.perf_counter()
|
| 25 |
+
entities = model.predict_entities(text, labels, threshold=threshold)
|
| 26 |
+
latency_ms = (time.perf_counter() - start) * 1000
|
| 27 |
+
|
| 28 |
+
entities = sorted(entities, key=lambda e: e["start"])
|
| 29 |
+
|
| 30 |
+
hl_entities = [{"entity": e["label"], "start": e["start"], "end": e["end"]} for e in entities]
|
| 31 |
+
table = [[e["label"], e["text"], f"{e['score']:.2f}"] for e in entities]
|
| 32 |
+
|
| 33 |
+
return (
|
| 34 |
+
gr.HighlightedText(value={"text": text, "entities": hl_entities}, color_map=color_map),
|
| 35 |
+
table,
|
| 36 |
+
f"{latency_ms:.1f} ms | {len(entities)} entities",
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
EXAMPLES = [
|
| 41 |
+
[
|
| 42 |
+
"""Patient: Jane Doe, 58-year-old female.
|
| 43 |
+
Chief Complaint: Chest pain and shortness of breath for 2 days.
|
| 44 |
+
|
| 45 |
+
History of Present Illness:
|
| 46 |
+
Patient presents with substernal chest pain radiating to the left arm,
|
| 47 |
+
rated 7/10, worsening with exertion. She reports associated dyspnea and
|
| 48 |
+
diaphoresis. She has a history of Type 2 Diabetes Mellitus diagnosed in
|
| 49 |
+
2015 and Hypertension diagnosed in 2018.
|
| 50 |
+
|
| 51 |
+
Current Medications:
|
| 52 |
+
- Metformin 1000mg PO BID for diabetes
|
| 53 |
+
- Lisinopril 20mg PO daily for hypertension
|
| 54 |
+
- Aspirin 81mg PO daily for cardiac prophylaxis
|
| 55 |
+
|
| 56 |
+
Vitals: BP 158/92, HR 96, SpO2 94%, Temp 98.6F
|
| 57 |
+
|
| 58 |
+
Assessment:
|
| 59 |
+
1. Acute coronary syndrome - rule out myocardial infarction
|
| 60 |
+
2. Uncontrolled hypertension
|
| 61 |
+
3. Type 2 Diabetes Mellitus - stable on current regimen
|
| 62 |
+
|
| 63 |
+
Plan:
|
| 64 |
+
- Stat ECG and troponin levels
|
| 65 |
+
- Start Heparin drip 18 units/kg/hr IV
|
| 66 |
+
- Nitroglycerin 0.4mg sublingual PRN chest pain
|
| 67 |
+
- Cardiology consult
|
| 68 |
+
- Continue home medications""",
|
| 69 |
+
DEFAULT_LABELS,
|
| 70 |
+
0.4,
|
| 71 |
+
],
|
| 72 |
+
[
|
| 73 |
+
"""DISCHARGE SUMMARY
|
| 74 |
+
Patient: Robert Chen, 72-year-old male.
|
| 75 |
+
Admission Date: 2024-01-15. Discharge Date: 2024-01-19.
|
| 76 |
+
|
| 77 |
+
Principal Diagnosis: Community-acquired pneumonia, right lower lobe.
|
| 78 |
+
Secondary Diagnoses: COPD, Atrial fibrillation.
|
| 79 |
+
|
| 80 |
+
Hospital Course:
|
| 81 |
+
Patient admitted with fever 101.8F, productive cough with purulent sputum,
|
| 82 |
+
and oxygen saturation of 88% on room air. Chest X-ray confirmed right lower
|
| 83 |
+
lobe consolidation. Started on Ceftriaxone 1g IV daily and Azithromycin
|
| 84 |
+
500mg PO daily. Supplemental O2 via nasal cannula at 3L/min.
|
| 85 |
+
|
| 86 |
+
Discharge Medications:
|
| 87 |
+
- Amoxicillin-Clavulanate 875mg PO BID x 5 days
|
| 88 |
+
- Albuterol inhaler 2 puffs q4-6h PRN
|
| 89 |
+
- Warfarin 5mg PO daily
|
| 90 |
+
- Metoprolol 50mg PO BID
|
| 91 |
+
|
| 92 |
+
Follow-up: Pulmonology clinic in 2 weeks. Repeat chest X-ray in 6 weeks.""",
|
| 93 |
+
DEFAULT_LABELS,
|
| 94 |
+
0.4,
|
| 95 |
+
],
|
| 96 |
+
[
|
| 97 |
+
"""ED Note - 03/10/2024 22:45
|
| 98 |
+
Chief Complaint: Laceration to right hand.
|
| 99 |
+
|
| 100 |
+
HPI: 34-year-old male presents after cutting his right palm on broken glass
|
| 101 |
+
approximately 1 hour ago. Reports moderate bleeding controlled with direct
|
| 102 |
+
pressure. Denies numbness or weakness in fingers. No foreign body sensation.
|
| 103 |
+
Tetanus up to date.
|
| 104 |
+
|
| 105 |
+
Exam: 3cm linear laceration to right thenar eminence, clean edges, no tendon
|
| 106 |
+
involvement, neurovascular intact distally.
|
| 107 |
+
|
| 108 |
+
Procedure: Wound irrigated with normal saline. Repaired with 4-0 nylon,
|
| 109 |
+
5 interrupted sutures. Sterile dressing applied.
|
| 110 |
+
|
| 111 |
+
Disposition: Home with wound care instructions. Suture removal in 10 days.""",
|
| 112 |
+
DEFAULT_LABELS + ["body_part", "wound"],
|
| 113 |
+
0.4,
|
| 114 |
+
],
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
with gr.Blocks(title="GLiNER Biomedical NER") as demo:
|
| 118 |
+
gr.Markdown("# GLiNER Biomedical NER\nZero-shot named entity recognition with `gliner-biomed-base-v1.0`")
|
| 119 |
+
|
| 120 |
+
with gr.Row():
|
| 121 |
+
with gr.Column(scale=2):
|
| 122 |
+
text_input = gr.Textbox(label="Clinical Text", lines=12)
|
| 123 |
+
labels_input = gr.Dropdown(
|
| 124 |
+
label="Entity Labels",
|
| 125 |
+
choices=DEFAULT_LABELS,
|
| 126 |
+
value=DEFAULT_LABELS,
|
| 127 |
+
multiselect=True,
|
| 128 |
+
allow_custom_value=True,
|
| 129 |
+
max_choices=MAX_LABELS,
|
| 130 |
+
)
|
| 131 |
+
threshold = gr.Slider(0.0, 1.0, value=0.4, step=0.05, label="Confidence Threshold")
|
| 132 |
+
run_btn = gr.Button("Extract", variant="primary")
|
| 133 |
+
|
| 134 |
+
with gr.Column(scale=3):
|
| 135 |
+
latency_output = gr.Textbox(label="Latency")
|
| 136 |
+
highlight_output = gr.HighlightedText(label="Entities", combine_adjacent=False, show_legend=True)
|
| 137 |
+
table_output = gr.Dataframe(headers=["Label", "Text", "Score"], label="Extracted Entities")
|
| 138 |
+
|
| 139 |
+
run_btn.click(extract, inputs=[text_input, labels_input, threshold], outputs=[highlight_output, table_output, latency_output])
|
| 140 |
+
gr.Examples(EXAMPLES, inputs=[text_input, labels_input, threshold])
|
| 141 |
+
|
| 142 |
+
demo.launch()
|