Spaces:
Sleeping
update stress-detection model
Browse files📝 Store Note / Documentation for Stress Detection Gradio Space
1️⃣ Overview
Ye Gradio Space ek Student Stress Detection Tool hai jo 3 alag models ka prediction combine karke user ke text input se stress impact detect karta hai.
Models:
Mental Health Model – Detects mental states like Depression, Anxiety, Stress, etc.
Physical Emotion Model – Detects emotions like Joy, Sadness, Anger, Fear, Surprise, Love.
Polarity Model – Detects sentiment polarity: POSITIVE, NEGATIVE, NORMAL.
2️⃣ Key Features
Single Space deployment: Sabhi models ek hi interface me integrate kiye gaye hain.
Parallel inference: Models simultaneously predict using ThreadPoolExecutor for time-efficient computation.
Stress Impact Mapping: Expert-defined rules (stress_mapping) ke basis pe Mental + Physical + Polarity se Stress Impact determine hota hai (Low, Medium, High, Critical).
Confidence Handling: Low confidence predictions flagged automatically (Low_Confidence_Flag).
JSON Output: Each prediction includes label, confidence score, stress impact, and low-confidence flag.
3️⃣ Workflow
User text input deta hai Gradio textbox me.
System simultaneously 3 models ko call karta hai (parallel execution).
Predictions combine hoke Stress Impact calculate hota hai.
Output JSON me user ko return hota hai:
{
"Mental": "Depression",
"Mental_conf": 0.87,
"Physical": "Sadness",
"Physical_conf": 0.78,
"Polarity": "NEGATIVE",
"Polarity_conf": 0.91,
"Stress_Impact": "Critical",
"Low_Confidence_Flag": false
}
User ka input step-by-step survey ke liye process hota hai.
4️⃣ Model Details
Model Base Description Notes
Mental Health Model ShubhamCoder01 Predicts mental conditions Fine-tuned on stress dataset
Physical Emotion Model ShubhamCoder01 Predicts emotions Fine-tuned on emotion dataset
Polarity Model ShubhamCoder01 Predicts sentiment polarity Fine-tuned on polarity dataset
All models Hugging Face Hub se load kiye gaye hain.
Quantization not applied here; inference is CPU-friendly.
5️⃣ Technical Notes
Parallel Processing: ThreadPoolExecutor se 3 models simultaneously inference karte hain.
Confidence Thresholds:
Mental: 0.25
Physical: 0.5
Polarity: 0.5
Default Stress Impact: 'Medium' agar tuple mapping me na mile.
Deployment: Hugging Face Space, Gradio interface, shareable public link.
|
@@ -1,33 +1,210 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with torch.no_grad():
|
| 14 |
-
outputs =
|
| 15 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
return {
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=predict,
|
| 27 |
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
|
| 28 |
outputs="json",
|
| 29 |
-
title="Stress Detection
|
| 30 |
-
description="
|
| 31 |
)
|
| 32 |
|
| 33 |
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
|
| 6 |
+
# ------------------------------
|
| 7 |
+
# 1) Load Models
|
| 8 |
+
# ------------------------------
|
|
|
|
| 9 |
|
| 10 |
+
# Mental Model
|
| 11 |
+
mental_model_name = "ShubhamCoder01/mental-stress-model"
|
| 12 |
+
mental_tokenizer = AutoTokenizer.from_pretrained(mental_model_name)
|
| 13 |
+
mental_model = AutoModelForSequenceClassification.from_pretrained(mental_model_name)
|
| 14 |
+
|
| 15 |
+
# Physical / Emotion Model
|
| 16 |
+
physical_model_name = "ShubhamCoder01/emotion-detection-model"
|
| 17 |
+
physical_tokenizer = AutoTokenizer.from_pretrained(physical_model_name)
|
| 18 |
+
physical_model = AutoModelForSequenceClassification.from_pretrained(physical_model_name)
|
| 19 |
+
physical_id2label = {0: 'Joy', 1: 'Sadness', 2: 'Anger', 3: 'Love', 4: 'Surprise', 5: 'Fear'}
|
| 20 |
+
|
| 21 |
+
# Polarity Model
|
| 22 |
+
polarity_model_name = "ShubhamCoder01/polarity-model"
|
| 23 |
+
polarity_tokenizer = AutoTokenizer.from_pretrained(polarity_model_name)
|
| 24 |
+
polarity_model = AutoModelForSequenceClassification.from_pretrained(polarity_model_name)
|
| 25 |
+
polarity_id2label = {0: 'NEGATIVE', 1: 'POSITIVE', 2:"NORMAL"}
|
| 26 |
+
|
| 27 |
+
# ------------------------------
|
| 28 |
+
# 2) Expert Stress Mapping
|
| 29 |
+
# ------------------------------
|
| 30 |
+
stress_mapping = {
|
| 31 |
+
('Depression', 'Sadness', 'NEGATIVE'): 'Critical',
|
| 32 |
+
('Depression', 'Sadness', 'NORMAL'): 'High',
|
| 33 |
+
('Depression', 'Sadness', 'POSITIVE'): 'Medium',
|
| 34 |
+
('Depression', 'Fear', 'NEGATIVE'): 'Critical',
|
| 35 |
+
('Depression', 'Fear', 'NORMAL'): 'High',
|
| 36 |
+
('Depression', 'Fear', 'POSITIVE'): 'Medium',
|
| 37 |
+
('Depression', 'Anger', 'NEGATIVE'): 'High',
|
| 38 |
+
('Depression', 'Anger', 'NORMAL'): 'Medium',
|
| 39 |
+
('Depression', 'Anger', 'POSITIVE'): 'Low',
|
| 40 |
+
('Depression', 'Joy', 'NEGATIVE'): 'Medium',
|
| 41 |
+
('Depression', 'Joy', 'NORMAL'): 'Low',
|
| 42 |
+
('Depression', 'Joy', 'POSITIVE'): 'Low',
|
| 43 |
+
('Depression', 'Surprise', 'NEGATIVE'): 'High',
|
| 44 |
+
('Depression', 'Surprise', 'NORMAL'): 'Medium',
|
| 45 |
+
('Depression', 'Surprise', 'POSITIVE'): 'Low',
|
| 46 |
+
('Suicidal', 'Sadness', 'NEGATIVE'): 'Critical',
|
| 47 |
+
('Suicidal', 'Sadness', 'NORMAL'): 'High',
|
| 48 |
+
('Suicidal', 'Sadness', 'POSITIVE'): 'Medium',
|
| 49 |
+
('Suicidal', 'Fear', 'NEGATIVE'): 'Critical',
|
| 50 |
+
('Suicidal', 'Fear', 'NORMAL'): 'High',
|
| 51 |
+
('Suicidal', 'Fear', 'POSITIVE'): 'Medium',
|
| 52 |
+
('Suicidal', 'Anger', 'NEGATIVE'): 'High',
|
| 53 |
+
('Suicidal', 'Anger', 'NORMAL'): 'Medium',
|
| 54 |
+
('Suicidal', 'Anger', 'POSITIVE'): 'Low',
|
| 55 |
+
('Suicidal', 'Joy', 'NEGATIVE'): 'Medium',
|
| 56 |
+
('Suicidal', 'Joy', 'NORMAL'): 'Low',
|
| 57 |
+
('Suicidal', 'Joy', 'POSITIVE'): 'Low',
|
| 58 |
+
('Suicidal', 'Surprise', 'NEGATIVE'): 'High',
|
| 59 |
+
('Suicidal', 'Surprise', 'NORMAL'): 'Medium',
|
| 60 |
+
('Suicidal', 'Surprise', 'POSITIVE'): 'Low',
|
| 61 |
+
('Anxiety', 'Sadness', 'NEGATIVE'): 'High',
|
| 62 |
+
('Anxiety', 'Sadness', 'NORMAL'): 'Medium',
|
| 63 |
+
('Anxiety', 'Sadness', 'POSITIVE'): 'Low',
|
| 64 |
+
('Anxiety', 'Fear', 'NEGATIVE'): 'High',
|
| 65 |
+
('Anxiety', 'Fear', 'NORMAL'): 'Medium',
|
| 66 |
+
('Anxiety', 'Fear', 'POSITIVE'): 'Low',
|
| 67 |
+
('Anxiety', 'Anger', 'NEGATIVE'): 'Medium',
|
| 68 |
+
('Anxiety', 'Anger', 'NORMAL'): 'Low',
|
| 69 |
+
('Anxiety', 'Anger', 'POSITIVE'): 'Low',
|
| 70 |
+
('Anxiety', 'Joy', 'NEGATIVE'): 'Low',
|
| 71 |
+
('Anxiety', 'Joy', 'NORMAL'): 'Low',
|
| 72 |
+
('Anxiety', 'Joy', 'POSITIVE'): 'Low',
|
| 73 |
+
('Anxiety', 'Surprise', 'NEGATIVE'): 'Medium',
|
| 74 |
+
('Anxiety', 'Surprise', 'NORMAL'): 'Low',
|
| 75 |
+
('Anxiety', 'Surprise', 'POSITIVE'): 'Low',
|
| 76 |
+
('Stress', 'Sadness', 'NEGATIVE'): 'High',
|
| 77 |
+
('Stress', 'Sadness', 'NORMAL'): 'Medium',
|
| 78 |
+
('Stress', 'Sadness', 'POSITIVE'): 'Low',
|
| 79 |
+
('Stress', 'Fear', 'NEGATIVE'): 'High',
|
| 80 |
+
('Stress', 'Fear', 'NORMAL'): 'Medium',
|
| 81 |
+
('Stress', 'Fear', 'POSITIVE'): 'Low',
|
| 82 |
+
('Stress', 'Anger', 'NEGATIVE'): 'Medium',
|
| 83 |
+
('Stress', 'Anger', 'NORMAL'): 'Low',
|
| 84 |
+
('Stress', 'Anger', 'POSITIVE'): 'Low',
|
| 85 |
+
('Stress', 'Joy', 'NEGATIVE'): 'Low',
|
| 86 |
+
('Stress', 'Joy', 'NORMAL'): 'Low',
|
| 87 |
+
('Stress', 'Joy', 'POSITIVE'): 'Low',
|
| 88 |
+
('Stress', 'Surprise', 'NEGATIVE'): 'Medium',
|
| 89 |
+
('Stress', 'Surprise', 'NORMAL'): 'Low',
|
| 90 |
+
('Stress', 'Surprise', 'POSITIVE'): 'Low',
|
| 91 |
+
('Normal', 'Sadness', 'NEGATIVE'): 'Medium',
|
| 92 |
+
('Normal', 'Sadness', 'NORMAL'): 'Low',
|
| 93 |
+
('Normal', 'Sadness', 'POSITIVE'): 'Low',
|
| 94 |
+
('Normal', 'Fear', 'NEGATIVE'): 'Medium',
|
| 95 |
+
('Normal', 'Fear', 'NORMAL'): 'Low',
|
| 96 |
+
('Normal', 'Fear', 'POSITIVE'): 'Low',
|
| 97 |
+
('Normal', 'Anger', 'NEGATIVE'): 'Low',
|
| 98 |
+
('Normal', 'Anger', 'NORMAL'): 'Low',
|
| 99 |
+
('Normal', 'Anger', 'POSITIVE'): 'Low',
|
| 100 |
+
('Normal', 'Joy', 'NEGATIVE'): 'Low',
|
| 101 |
+
('Normal', 'Joy', 'NORMAL'): 'Low',
|
| 102 |
+
('Normal', 'Joy', 'POSITIVE'): 'Low',
|
| 103 |
+
('Normal', 'Surprise', 'NEGATIVE'): 'Low',
|
| 104 |
+
('Normal', 'Surprise', 'NORMAL'): 'Low',
|
| 105 |
+
('Normal', 'Surprise', 'POSITIVE'): 'Low',
|
| 106 |
+
('Bipolar', 'Sadness', 'NEGATIVE'): 'High',
|
| 107 |
+
('Bipolar', 'Sadness', 'NORMAL'): 'Medium',
|
| 108 |
+
('Bipolar', 'Sadness', 'POSITIVE'): 'Low',
|
| 109 |
+
('Bipolar', 'Fear', 'NEGATIVE'): 'High',
|
| 110 |
+
('Bipolar', 'Fear', 'NORMAL'): 'Medium',
|
| 111 |
+
('Bipolar', 'Fear', 'POSITIVE'): 'Low',
|
| 112 |
+
('Bipolar', 'Anger', 'NEGATIVE'): 'Medium',
|
| 113 |
+
('Bipolar', 'Anger', 'NORMAL'): 'Low',
|
| 114 |
+
('Bipolar', 'Anger', 'POSITIVE'): 'Low',
|
| 115 |
+
('Bipolar', 'Joy', 'NEGATIVE'): 'Low',
|
| 116 |
+
('Bipolar', 'Joy', 'NORMAL'): 'Low',
|
| 117 |
+
('Bipolar', 'Joy', 'POSITIVE'): 'Low',
|
| 118 |
+
('Bipolar', 'Surprise', 'NEGATIVE'): 'Medium',
|
| 119 |
+
('Bipolar', 'Surprise', 'NORMAL'): 'Low',
|
| 120 |
+
('Bipolar', 'Surprise', 'POSITIVE'): 'Low',
|
| 121 |
+
('Personality disorder', 'Sadness', 'NEGATIVE'): 'High',
|
| 122 |
+
('Personality disorder', 'Sadness', 'NORMAL'): 'Medium',
|
| 123 |
+
('Personality disorder', 'Sadness', 'POSITIVE'): 'Low',
|
| 124 |
+
('Personality disorder', 'Fear', 'NEGATIVE'): 'High',
|
| 125 |
+
('Personality disorder', 'Fear', 'NORMAL'): 'Medium',
|
| 126 |
+
('Personality disorder', 'Fear', 'POSITIVE'): 'Low',
|
| 127 |
+
('Personality disorder', 'Anger', 'NEGATIVE'): 'Medium',
|
| 128 |
+
('Personality disorder', 'Anger', 'NORMAL'): 'Low',
|
| 129 |
+
('Personality disorder', 'Anger', 'POSITIVE'): 'Low',
|
| 130 |
+
('Personality disorder', 'Joy', 'NEGATIVE'): 'Low',
|
| 131 |
+
('Personality disorder', 'Joy', 'NORMAL'): 'Low',
|
| 132 |
+
('Personality disorder', 'Joy', 'POSITIVE'): 'Low',
|
| 133 |
+
('Personality disorder', 'Surprise', 'NEGATIVE'): 'Medium',
|
| 134 |
+
('Personality disorder', 'Surprise', 'NORMAL'): 'Low',
|
| 135 |
+
('Personality disorder', 'Surprise', 'POSITIVE'): 'Low'
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
# ------------------------------
|
| 139 |
+
# 3) Prediction Functions for Each Model
|
| 140 |
+
# ------------------------------
|
| 141 |
+
def predict_mental(text):
|
| 142 |
+
inputs = mental_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 143 |
+
with torch.no_grad():
|
| 144 |
+
outputs = mental_model(**inputs)
|
| 145 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 146 |
+
idx = torch.argmax(probs).item()
|
| 147 |
+
return mental_model.config.id2label[idx], probs[0][idx].item()
|
| 148 |
+
|
| 149 |
+
def predict_physical(text):
|
| 150 |
+
inputs = physical_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 151 |
+
with torch.no_grad():
|
| 152 |
+
outputs = physical_model(**inputs)
|
| 153 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 154 |
+
idx = torch.argmax(probs).item()
|
| 155 |
+
return physical_id2label[idx], probs[0][idx].item()
|
| 156 |
+
|
| 157 |
+
def predict_polarity(text):
|
| 158 |
+
inputs = polarity_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 159 |
with torch.no_grad():
|
| 160 |
+
outputs = polarity_model(**inputs)
|
| 161 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 162 |
+
idx = torch.argmax(probs).item()
|
| 163 |
+
return polarity_id2label[idx], probs[0][idx].item()
|
| 164 |
+
|
| 165 |
+
# ------------------------------
|
| 166 |
+
# 4) Combined Prediction Function with Parallel Execution
|
| 167 |
+
# ------------------------------
|
| 168 |
+
def predict(text):
|
| 169 |
+
with ThreadPoolExecutor() as executor:
|
| 170 |
+
future_mental = executor.submit(predict_mental, text)
|
| 171 |
+
future_physical = executor.submit(predict_physical, text)
|
| 172 |
+
future_polarity = executor.submit(predict_polarity, text)
|
| 173 |
+
|
| 174 |
+
mental_label, mental_score = future_mental.result()
|
| 175 |
+
physical_label, physical_score = future_physical.result()
|
| 176 |
+
polarity_label, polarity_score = future_polarity.result()
|
| 177 |
+
|
| 178 |
+
# Expert-defined Stress Impact
|
| 179 |
+
stress_impact = stress_mapping.get((mental_label, physical_label, polarity_label), 'Medium')
|
| 180 |
+
|
| 181 |
+
# Low confidence flag
|
| 182 |
+
low_confidence = False
|
| 183 |
+
if mental_score < 0.25 or physical_score < 0.5 or polarity_score < 0.5:
|
| 184 |
+
low_confidence = True
|
| 185 |
|
| 186 |
return {
|
| 187 |
+
"Mental": mental_label,
|
| 188 |
+
"Mental_conf": round(mental_score,4),
|
| 189 |
+
"Physical": physical_label,
|
| 190 |
+
"Physical_conf": round(physical_score,4),
|
| 191 |
+
"Polarity": polarity_label,
|
| 192 |
+
"Polarity_conf": round(polarity_score,4),
|
| 193 |
+
"Stress_Impact": stress_impact,
|
| 194 |
+
"Low_Confidence_Flag": low_confidence
|
| 195 |
}
|
| 196 |
|
| 197 |
+
# ------------------------------
|
| 198 |
+
# 5) Gradio Interface
|
| 199 |
+
# ------------------------------
|
| 200 |
iface = gr.Interface(
|
| 201 |
fn=predict,
|
| 202 |
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
|
| 203 |
outputs="json",
|
| 204 |
+
title="Student Stress Detection (Parallel)",
|
| 205 |
+
description="Predict Mental, Emotion, Polarity and Stress Impact using parallel model inference."
|
| 206 |
)
|
| 207 |
|
| 208 |
iface.launch(share=True)
|
| 209 |
+
|
| 210 |
+
|