Uploading blood request emergency text classifier demo app.py
Browse files6 unmodified lines789101112131 unmodified line15161718192021222324252627282930313233343536373839405 unmodified lines464748495051525354555657585960616263646566676869707172736 unmodified linesfrom 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 pipelineclassifier = pipeline(task="text-classification",1 unmodified linedevice="cuda" if torch.cuda.is_available() else "cpu",top_k=None)# Get outputs from pipelineoutputs = classifier(text)[0]# Build probability scores dict + find top predictionprob_scores = {}top_label = ""top_score = 0.0for 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 stringverdict = f"{top_label} — Confidence: {round(top_score * 100, 2)}%"return verdict, prob_scores# 3. Create a Gradio interfacedescription = """5 unmodified linesSee [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!"],])# 4. Launch the interfaceif __name__ == "__main__":6 unmodified lines789101112131 unmodified line1516171819202122232425265 unmodified lines32333435363738394041424344454647484950515253546 unmodified linesfrom transformers import pipeline# 2. Define function to use our model on given textdef blood_request_classifier(text: str) -> Dict[str, float]:# Set up text classification pipelineclassifier = pipeline(task="text-classification",1 unmodified linedevice="cuda" if torch.cuda.is_available() else "cpu",top_k=None)output_dict = {}for item in outputs:output_dict[item["label"]] = item["score"]return output_dict# 3. Create a Gradio interfacedescription = """5 unmodified linesSee [source code on GitHub](https://github.com/AshenFdo/Blood-Request-Emergency-Classification-Model)."""demo = gr.Interface(fn=food_not_food_classifier,inputs=gr.Textbox(lines=4,placeholder="Enter a blood request message here...",label="Blood Request Text",max= 200),outputs=gr.Label(num_top_classes=2), # show top 2 classes (that's all we have)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!"],])# 4. Launch the interfaceif __name__ == "__main__":
app.py+21-40