Uploading blood request emergency text classifier demo app.py
Browse filesFiles changed (3)
- README.md+43-5
- app.pyAdded+75
- requirements.txtAdded+3
1234567891011121314---title: Blood Request Emergency Text Classifieremoji: πcolorFrom: redcolorTo: pinksdk: gradiosdk_version: 6.18.0python_version: '3.13'app_file: app.pypinned: falselicense: apache-2.0---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152---title: Emergency Blood Request Classifieremoji: π©ΈcolorFrom: redcolorTo: pinksdk: gradiosdk_version: 5.0.0app_file: app.pypinned: falselicense: apache-2.0---# π©Έ Emergency Blood Request ClassifierA simple text classification demo to detect whether a blood donation request is an **emergency** or **non-emergency**.## π How It WorksType or paste a blood request message into the text box and the model will classify it into one of two categories:- π¨ **Emergency** β Requires immediate attention- β **Non-Emergency** β Can be handled in a routine mannerAlong with the verdict, you'll also see the **probability scores** for both classes.## π§ Model| Detail | Info ||---|---|| Base Model | [distilbert-base-uncased](https://huggingface.co/distilbert/distilbert-base-uncased) || Fine-tuned Model | [AshenFdo/emergency_blood_request_classifier](https://huggingface.co/AshenFdo/emergency_blood_request_classifier) || Dataset | [AshenFdo/synthetic_blood_request_urgency_dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset) || Task | Text Classification || Accuracy | 100% on eval set |## π Try It OutJust enter a message like:> *"Patient is in critical condition after surgery and urgently needs O- blood immediately."*or> *"Looking for a B+ donor for a planned surgery next month."*## π Links- π¦ [Fine-tuned Model](https://huggingface.co/AshenFdo/emergency_blood_request_classifier)- π [Dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset)- π» [GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model/blob/main/notebooks/Huggingface_blood_request_emegency_classification_model_fine_tuning_.ipynb)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
# 1. Import the required packagesimport torchimport gradio as gr
from typing import Dict, Tuplefrom transformers import pipeline
# 2. Define function to use our model on given textdef blood_request_classifier(text: str) -> Tuple[str, Dict[str, float]]: # Set up text classification pipeline classifier = pipeline( task="text-classification", model="AshenFdo/emergency_blood_request_classifier", device="cuda" if torch.cuda.is_available() else "cpu", top_k=None )
# Get outputs from pipeline outputs = classifier(text)[0]
# Build probability scores dict + find top prediction prob_scores = {} top_label = "" top_score = 0.0
for item in outputs: label = "π¨ Emergency" if item["label"] == "LABEL_1" else "β
Non-Emergency" prob_scores[label] = round(item["score"], 4) if item["score"] > top_score: top_score = item["score"] top_label = label
# Build a nice verdict string verdict = f"{top_label} β Confidence: {round(top_score * 100, 2)}%"
return verdict, prob_scores
# 3. Create a Gradio interfacedescription = """A text classifier to determine whether a blood donation request is an **emergency** or **non-emergency**.
Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-uncased) on a [synthetic blood request urgency dataset](https://huggingface.co/datasets/AshenFdo/synthetic_blood_request_urgency_dataset).
See [source code on GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model)."""
demo = gr.Interface( fn=blood_request_classifier, inputs=gr.Textbox( lines=4, placeholder="Enter a blood request message here...", label="Blood Request Text" ), outputs=[ gr.Textbox(label="π·οΈ Verdict"), # Shows label + confidence % gr.Label(num_top_classes=2, label="π Probability Scores") # Shows both class probs ], title="π©Έ Emergency Blood Request Classifier", description=description, examples=[ ["Patient is in critical condition after surgery and urgently needs O- blood immediately or they may not survive."], ["Hi, I am looking for a B+ blood donor for my father's scheduled knee replacement surgery next month."], ["URGENT: Accident victim in ER needs AB+ blood NOW. Lives at stake, please respond immediately!"], ["Our hospital is planning a blood donation camp next Saturday. All blood types welcome."], ["A newborn baby in the ICU critically needs O+ blood within the next hour. Please help!"], ], theme=gr.themes.Soft(), allow_flagging="never")
# 4. Launch the interfaceif __name__ == "__main__": demo.launch()123gradiotorchtransformersREADME.md+43-5
app.pyAdded+75
requirements.txtAdded+3