AshenFdo commited on
Commit
8924789
Β·
verified Β·
1 Parent(s): 8fbc2b4

Uploading blood request emergency text classifier demo app.py

Browse files
Files changed (3) hide show
  1. README.md +43 -5
  2. app.py +75 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,14 +1,52 @@
 
1
  ---
2
- title: Blood Request Emergency Text Classifier
3
- emoji: 🌍
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.18.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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