Uploading blood request emergency text classifier demo app.py
Browse files- README.md +43 -5
- app.py +75 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,14 +1,52 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title: Blood Request
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: apache-2.0
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
title: Emergency Blood Request Classifier
|
| 4 |
+
emoji: π©Έ
|
| 5 |
colorFrom: red
|
| 6 |
colorTo: pink
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 5.0.0
|
|
|
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: apache-2.0
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# π©Έ Emergency Blood Request Classifier
|
| 15 |
+
|
| 16 |
+
A simple text classification demo to detect whether a blood donation request is an **emergency** or **non-emergency**.
|
| 17 |
+
|
| 18 |
+
## π How It Works
|
| 19 |
+
|
| 20 |
+
Type or paste a blood request message into the text box and the model will classify it into one of two categories:
|
| 21 |
+
|
| 22 |
+
- π¨ **Emergency** β Requires immediate attention
|
| 23 |
+
- β
**Non-Emergency** β Can be handled in a routine manner
|
| 24 |
+
|
| 25 |
+
Along with the verdict, you'll also see the **probability scores** for both classes.
|
| 26 |
+
|
| 27 |
+
## π§ Model
|
| 28 |
+
|
| 29 |
+
| Detail | Info |
|
| 30 |
+
|---|---|
|
| 31 |
+
| Base Model | [distilbert-base-uncased](https://huggingface.co/distilbert/distilbert-base-uncased) |
|
| 32 |
+
| Fine-tuned Model | [AshenFdo/emergency_blood_request_classifier](https://huggingface.co/AshenFdo/emergency_blood_request_classifier) |
|
| 33 |
+
| Dataset | [AshenFdo/synthetic_blood_request_urgency_dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset) |
|
| 34 |
+
| Task | Text Classification |
|
| 35 |
+
| Accuracy | 100% on eval set |
|
| 36 |
+
|
| 37 |
+
## π Try It Out
|
| 38 |
+
|
| 39 |
+
Just enter a message like:
|
| 40 |
+
|
| 41 |
+
> *"Patient is in critical condition after surgery and urgently needs O- blood immediately."*
|
| 42 |
+
|
| 43 |
+
or
|
| 44 |
+
|
| 45 |
+
> *"Looking for a B+ donor for a planned surgery next month."*
|
| 46 |
+
|
| 47 |
+
## π Links
|
| 48 |
+
|
| 49 |
+
- π¦ [Fine-tuned Model](https://huggingface.co/AshenFdo/emergency_blood_request_classifier)
|
| 50 |
+
- π [Dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset)
|
| 51 |
+
- π» [GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model/blob/main/notebooks/Huggingface_blood_request_emegency_classification_model_fine_tuning_.ipynb)
|
| 52 |
+
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# 1. Import the required packages
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from typing import Dict, Tuple
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
# 2. Define function to use our model on given text
|
| 10 |
+
def blood_request_classifier(text: str) -> Tuple[str, Dict[str, float]]:
|
| 11 |
+
# Set up text classification pipeline
|
| 12 |
+
classifier = pipeline(
|
| 13 |
+
task="text-classification",
|
| 14 |
+
model="AshenFdo/emergency_blood_request_classifier",
|
| 15 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 16 |
+
top_k=None
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Get outputs from pipeline
|
| 20 |
+
outputs = classifier(text)[0]
|
| 21 |
+
|
| 22 |
+
# Build probability scores dict + find top prediction
|
| 23 |
+
prob_scores = {}
|
| 24 |
+
top_label = ""
|
| 25 |
+
top_score = 0.0
|
| 26 |
+
|
| 27 |
+
for item in outputs:
|
| 28 |
+
label = "π¨ Emergency" if item["label"] == "LABEL_1" else "β
Non-Emergency"
|
| 29 |
+
prob_scores[label] = round(item["score"], 4)
|
| 30 |
+
if item["score"] > top_score:
|
| 31 |
+
top_score = item["score"]
|
| 32 |
+
top_label = label
|
| 33 |
+
|
| 34 |
+
# Build a nice verdict string
|
| 35 |
+
verdict = f"{top_label} β Confidence: {round(top_score * 100, 2)}%"
|
| 36 |
+
|
| 37 |
+
return verdict, prob_scores
|
| 38 |
+
|
| 39 |
+
# 3. Create a Gradio interface
|
| 40 |
+
description = """
|
| 41 |
+
A text classifier to determine whether a blood donation request is an **emergency** or **non-emergency**.
|
| 42 |
+
|
| 43 |
+
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on a
|
| 44 |
+
[synthetic blood request urgency dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset).
|
| 45 |
+
|
| 46 |
+
See [source code on GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model).
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=blood_request_classifier,
|
| 51 |
+
inputs=gr.Textbox(
|
| 52 |
+
lines=4,
|
| 53 |
+
placeholder="Enter a blood request message here...",
|
| 54 |
+
label="Blood Request Text"
|
| 55 |
+
),
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Textbox(label="π·οΈ Verdict"), # Shows label + confidence %
|
| 58 |
+
gr.Label(num_top_classes=2, label="π Probability Scores") # Shows both class probs
|
| 59 |
+
],
|
| 60 |
+
title="π©Έ Emergency Blood Request Classifier",
|
| 61 |
+
description=description,
|
| 62 |
+
examples=[
|
| 63 |
+
["Patient is in critical condition after surgery and urgently needs O- blood immediately or they may not survive."],
|
| 64 |
+
["Hi, I am looking for a B+ blood donor for my father's scheduled knee replacement surgery next month."],
|
| 65 |
+
["URGENT: Accident victim in ER needs AB+ blood NOW. Lives at stake, please respond immediately!"],
|
| 66 |
+
["Our hospital is planning a blood donation camp next Saturday. All blood types welcome."],
|
| 67 |
+
["A newborn baby in the ICU critically needs O+ blood within the next hour. Please help!"],
|
| 68 |
+
],
|
| 69 |
+
theme=gr.themes.Soft(),
|
| 70 |
+
allow_flagging="never"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# 4. Launch the interface
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
transformers
|