Uploading blood request emergency text classifier demo app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ 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) ->
|
| 11 |
# Set up text classification pipeline
|
| 12 |
classifier = pipeline(
|
| 13 |
task="text-classification",
|
|
@@ -15,26 +15,12 @@ def blood_request_classifier(text: str) -> Tuple[str, Dict[str, float]]:
|
|
| 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 |
-
|
| 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 |
-
|
| 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 = """
|
|
@@ -46,28 +32,23 @@ Fine-tuned from [DistilBERT](https://huggingface.co/distilbert/distilbert-base-u
|
|
| 46 |
See [source code on GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model).
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
demo = gr.Interface(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 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 |
-
|
| 70 |
-
)
|
| 71 |
|
| 72 |
# 4. Launch the interface
|
| 73 |
if __name__ == "__main__":
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
# 2. Define function to use our model on given text
|
| 10 |
+
def blood_request_classifier(text: str) -> Dict[str, float]:
|
| 11 |
# Set up text classification pipeline
|
| 12 |
classifier = pipeline(
|
| 13 |
task="text-classification",
|
|
|
|
| 15 |
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 16 |
top_k=None
|
| 17 |
)
|
| 18 |
+
output_dict = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
for item in outputs:
|
| 20 |
+
output_dict[item["label"]] = item["score"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
return output_dict
|
|
|
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
# 3. Create a Gradio interface
|
| 26 |
description = """
|
|
|
|
| 32 |
See [source code on GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model).
|
| 33 |
"""
|
| 34 |
|
| 35 |
+
demo = gr.Interface(fn=food_not_food_classifier,
|
| 36 |
+
inputs=gr.Textbox(
|
| 37 |
+
lines=4,
|
| 38 |
+
placeholder="Enter a blood request message here...",
|
| 39 |
+
label="Blood Request Text",
|
| 40 |
+
max= 200
|
| 41 |
+
),
|
| 42 |
+
outputs=gr.Label(num_top_classes=2), # show top 2 classes (that's all we have)
|
| 43 |
+
title="🩸 Emergency Blood Request Classifier",
|
| 44 |
+
description=description,
|
| 45 |
+
examples=[
|
| 46 |
+
["Patient is in critical condition after surgery and urgently needs O- blood immediately or they may not survive."],
|
| 47 |
+
["Hi, I am looking for a B+ blood donor for my father's scheduled knee replacement surgery next month."],
|
| 48 |
+
["URGENT: Accident victim in ER needs AB+ blood NOW. Lives at stake, please respond immediately!"],
|
| 49 |
+
["Our hospital is planning a blood donation camp next Saturday. All blood types welcome."],
|
| 50 |
+
["A newborn baby in the ICU critically needs O+ blood within the next hour. Please help!"],
|
| 51 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# 4. Launch the interface
|
| 54 |
if __name__ == "__main__":
|