katesiplon commited on
Commit
44a44ec
·
verified ·
1 Parent(s): 2193742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -61
app.py CHANGED
@@ -1,62 +1,62 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
-
4
- MODEL_NAME = "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest"
5
-
6
- classifier = pipeline("text-classification", model=MODEL_NAME, top_k=None)
7
-
8
- DISCLAIMER = """
9
- This demo detects patterns in self-provided text that may indicate support-seeking or distress-related language.
10
- It is for educational demonstration only and is NOT a diagnostic, disciplinary, or clinical tool.
11
- If someone may be in immediate danger, contact emergency services or a qualified crisis resource.
12
- """
13
-
14
- DISTRESS_KEYWORDS = {
15
- "sadness", "fear", "anger", "disgust", "negative"
16
- }
17
-
18
- def analyze_text(text):
19
- if not text or not text.strip():
20
- return "Please enter some text.", {}
21
-
22
- results = classifier(text[:512])[0]
23
-
24
- label_scores = {item["label"]: float(item["score"]) for item in results}
25
-
26
- distress_score = 0.0
27
- for label, score in label_scores.items():
28
- if label.lower() in DISTRESS_KEYWORDS:
29
- distress_score += score
30
-
31
- if distress_score >= 0.60:
32
- assessment = "Potential support-seeking / distress-related language detected"
33
- elif distress_score >= 0.30:
34
- assessment = "Some distress-related language detected"
35
- else:
36
- assessment = "No strong distress-related language detected"
37
-
38
- return assessment, label_scores
39
-
40
- demo = gr.Interface(
41
- fn=analyze_text,
42
- inputs=gr.Textbox(
43
- lines=6,
44
- label="Student text input",
45
- placeholder="Enter a message, reflection, or post for analysis..."
46
- ),
47
- outputs=[
48
- gr.Textbox(label="Assessment"),
49
- gr.Label(label="Model scores")
50
- ],
51
- title="Student Support-Seeking Language Demo",
52
- description=DISCLAIMER,
53
- examples=[
54
- ["I feel overwhelmed and I do not know who to talk to anymore."],
55
- ["I am stressed about finals but I think I can manage."],
56
- ["I had a good day and finished my homework."],
57
- ],
58
- flagging_mode="never"
59
- )
60
-
61
- if __name__ == "__main__":
62
  demo.launch()
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ MODEL_NAME = "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest"
5
+
6
+ classifier = pipeline("text-classification", model=MODEL_NAME, top_k=None)
7
+
8
+ DISCLAIMER = """
9
+ This demo detects patterns in self-provided text that may indicate support-seeking or distress-related language.
10
+ It is for educational demonstration only and is NOT a diagnostic, disciplinary, or clinical tool.
11
+ If someone may be in immediate danger, contact emergency services or a qualified crisis resource.
12
+ """
13
+
14
+ DISTRESS_KEYWORDS = {
15
+ "sadness", "fear", "anger", "disgust", "negative"
16
+ }
17
+
18
+ def analyze_text(text):
19
+ if not text or not text.strip():
20
+ return "Please enter some text.", {}
21
+
22
+ results = classifier(text[:512])[0]
23
+
24
+ label_scores = {item["label"]: float(item["score"]) for item in results}
25
+
26
+ distress_score = 0.0
27
+ for label, score in label_scores.items():
28
+ if label.lower() in DISTRESS_KEYWORDS:
29
+ distress_score += score
30
+
31
+ if distress_score >= 0.60:
32
+ assessment = "Potential support-seeking / distress-related language detected"
33
+ elif distress_score >= 0.30:
34
+ assessment = "Some distress-related language detected"
35
+ else:
36
+ assessment = "No strong distress-related language detected"
37
+
38
+ return assessment, label_scores
39
+
40
+ demo = gr.Interface(
41
+ fn=analyze_text,
42
+ inputs=gr.Textbox(
43
+ lines=6,
44
+ label="Student text input",
45
+ placeholder="Enter a message, reflection, or post for analysis..."
46
+ ),
47
+ outputs=[
48
+ gr.Textbox(label="Assessment"),
49
+ gr.Label(label="Model scores")
50
+ ],
51
+ title="Student Support-Seeking Language Demo",
52
+ description=DISCLAIMER,
53
+ examples=[
54
+ ["I feel overwhelmed and I do not know who to talk to anymore."],
55
+ ["I am stressed about finals but I think I can manage."],
56
+ ["I had a good day and finished my homework."],
57
+ ],
58
+ allow_flagging="never"
59
+ )
60
+
61
+ if __name__ == "__main__":
62
  demo.launch()