Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| MODEL_NAME = "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest" | |
| classifier = pipeline("text-classification", model=MODEL_NAME, top_k=None) | |
| DISCLAIMER = """ | |
| This demo detects patterns in self-provided text that may indicate support-seeking or distress-related language. | |
| It is for educational demonstration only and is NOT a diagnostic, disciplinary, or clinical tool. | |
| If someone may be in immediate danger, contact emergency services or a qualified crisis resource. | |
| """ | |
| DISTRESS_KEYWORDS = { | |
| "sadness", "fear", "anger", "disgust", "negative" | |
| } | |
| def analyze_text(text): | |
| if not text or not text.strip(): | |
| return "Please enter some text.", {} | |
| results = classifier(text[:512])[0] | |
| label_scores = {item["label"]: float(item["score"]) for item in results} | |
| distress_score = 0.0 | |
| for label, score in label_scores.items(): | |
| if label.lower() in DISTRESS_KEYWORDS: | |
| distress_score += score | |
| if distress_score >= 0.60: | |
| assessment = "Potential support-seeking / distress-related language detected" | |
| elif distress_score >= 0.30: | |
| assessment = "Some distress-related language detected" | |
| else: | |
| assessment = "No strong distress-related language detected" | |
| return assessment, label_scores | |
| demo = gr.Interface( | |
| fn=analyze_text, | |
| inputs=gr.Textbox( | |
| lines=6, | |
| label="Student text input", | |
| placeholder="Enter a message, reflection, or post for analysis..." | |
| ), | |
| outputs=[ | |
| gr.Textbox(label="Assessment"), | |
| gr.Label(label="Model scores") | |
| ], | |
| title="Student Support-Seeking Language Demo", | |
| description=DISCLAIMER, | |
| examples=[ | |
| ["I feel overwhelmed and I do not know who to talk to anymore."], | |
| ["I am stressed about finals but I think I can manage."], | |
| ["I had a good day and finished my homework."], | |
| ], | |
| allow_flagging="never" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |