import json import gradio as gr from transformers import pipeline URGENCY_MODEL = "tomasdanjo/sagip-urgency-classifier" INCIDENT_MODEL = "tomasdanjo/sagip-incident-multilabel" INCIDENT_LABELS = [ "medical_crisis", "trapped_family", "rising_floodwaters", "infrastructure_damage", ] urgency_clf = pipeline( "text-classification", model=URGENCY_MODEL, tokenizer=URGENCY_MODEL, top_k=None, ) incident_clf = pipeline( "text-classification", model=INCIDENT_MODEL, tokenizer=INCIDENT_MODEL, top_k=None, function_to_apply="sigmoid", ) def normalize_output(output): if isinstance(output, list) and len(output) > 0 and isinstance(output[0], list): return output[0] return output def infer(text, threshold): if not text or not text.strip(): return {}, {}, "Please enter a distress message." urgency_scores = normalize_output(urgency_clf(text)) urgency_scores = sorted(urgency_scores, key=lambda x: x["score"], reverse=True) top_urgency = urgency_scores[0] incident_scores_raw = normalize_output(incident_clf(text)) label_map = {} for item in incident_scores_raw: label = item["label"] if label.startswith("LABEL_"): idx = int(label.split("_")[-1]) if idx < len(INCIDENT_LABELS): label = INCIDENT_LABELS[idx] label_map[label] = float(item["score"]) predicted_incidents = [ label for label, score in label_map.items() if score >= threshold ] ticket = { "summary": text, "predicted_urgency": top_urgency["label"], "urgency_confidence": float(top_urgency["score"]), "predicted_incident_types": predicted_incidents, "incident_probabilities": label_map, } urgency_bar = {item["label"]: float(item["score"]) for item in urgency_scores} incident_bar = label_map return urgency_bar, incident_bar, json.dumps(ticket, indent=2, ensure_ascii=False) with gr.Blocks(title="SAGIP AI Triage Demo") as demo: gr.Markdown("# SAGIP Needs Ticketing & AI Triage Demo") with gr.Row(): with gr.Column(): text = gr.Textbox(lines=6, label="Incoming distress message") threshold = gr.Slider(0.1, 0.9, value=0.5, step=0.05, label="Incident threshold") btn = gr.Button("Analyze") with gr.Column(): urgency_plot = gr.Label(label="Urgency scores", num_top_classes=4) incident_plot = gr.Label(label="Incident probabilities", num_top_classes=4) output_json = gr.Code(label="Structured ticket preview", language="json") btn.click( fn=infer, inputs=[text, threshold], outputs=[urgency_plot, incident_plot, output_json], ) demo.launch()