Spaces:
Build error
Build error
File size: 13,182 Bytes
5c884db d170141 5c884db 0bd95bb 5c884db fa6e511 5c884db fa6e511 5c884db c124a95 5c884db fa6e511 5c884db fa6e511 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
import gradio as gr
from transformers import pipeline
import numpy as np
from gradio_client import Client
import matplotlib.pyplot as plt
from lime.lime_text import LimeTextExplainer
from datetime import datetime
import sqlite3
import logging
DB_NAME = "mental_health_analysis.db"
# --- Database Setup ---
def create_database():
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS mental_health_analysis (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
sentiment TEXT,
sentiment_confidence REAL,
disorder TEXT,
disorder_confidence REAL,
risk_level TEXT,
recommendations TEXT,
date TEXT
)
""")
conn.commit()
conn.close()
# --- Insert Results into Database ---
def insert_into_database(text, sentiment, sentiment_confidence, disorder, disorder_confidence, risk_level, recommendations, date):
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Convert list to string for recommendations
recommendations_str = "; ".join(recommendations)
cursor.execute("""
INSERT INTO mental_health_analysis (
text, sentiment, sentiment_confidence, disorder, disorder_confidence, risk_level, recommendations, date
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (
text, sentiment, sentiment_confidence, disorder,
disorder_confidence, risk_level, recommendations_str, date
))
conn.commit()
conn.close()
create_database()
class MentalHealthChatbot:
def __init__(self):
self.sentiment_client = Client("hasanmustafa0503/sentiment")
self.disorder_client = Client("hasanmustafa0503/disoreder-api")
# Label mappings
self.label_mapping = {
"LABEL_0": "ADHD",
"LABEL_1": "BPD",
"LABEL_2": "OCD",
"LABEL_3": "PTSD",
"LABEL_4": "Anxiety",
"LABEL_5": "Autism",
"LABEL_6": "Bipolar",
"LABEL_7": "Depression",
"LABEL_8": "Eating Disorders",
"LABEL_9": "Health",
"LABEL_10": "Mental Illness",
"LABEL_11": "Schizophrenia",
"LABEL_12": "Suicide Watch"
}
self.sentiment_mapping = {
"POS": "Positive",
"NEG": "Negative",
"NEU": "Neutral"
}
self.exercise_recommendations = {
# Exercise recommendations data as defined in the original code
}
# Initialize the LIME explainer
self.explainer = LimeTextExplainer(class_names=list(self.sentiment_mapping.values()) + list(self.label_mapping.values()))
def get_sentiment(self, text):
results = self.sentiment_client.predict(text=text, api_name="/predict")
if results and isinstance(results, list):
label = results[0]["label"]
confidence_score = results[0]["score"]*100
sentiment = self.sentiment_mapping.get(label, "Unknown")
return sentiment, confidence_score
return "Unknown", 0
def get_disorder(self, text, threshold=35):
results = self.disorder_client.predict(text=text, api_name="/predict")
if results and isinstance(results, list):
best_result = results[0]
disorder_confidence = best_result["score"] * 100
if disorder_confidence > threshold:
disorder_label = self.label_mapping.get(best_result["label"], "Unknown Disorder")
if disorder_confidence < 50:
risk_level = "Low Risk"
elif 50 <= disorder_confidence <= 75:
risk_level = "Moderate Risk"
else:
risk_level = "High Risk"
return disorder_label, disorder_confidence, risk_level
return "No significant disorder detected", 0.0, "No Risk"
def predict_fn(self, texts):
sentiment_probs = []
disorder_probs = []
sentiment_labels = []
disorder_labels = []
for text in texts:
sentiment, sentiment_confidence = self.get_sentiment(text)
sentiment_probs.append([sentiment_confidence / 100])
sentiment_labels.append(sentiment)
disorder_label, disorder_confidence, risk_level = self.get_disorder(text)
disorder_probs.append([disorder_confidence / 100])
disorder_labels.append(disorder_label)
sentiment_probs = np.array(sentiment_probs)
disorder_probs = np.array(disorder_probs)
result = np.hstack([sentiment_probs, disorder_probs])
return result, sentiment_labels, disorder_labels
def lime_predict_fn(self, texts):
result, _, _ = self.predict_fn(texts)
return result
def explain_text(self, text):
explanation = self.explainer.explain_instance(text, self.lime_predict_fn, num_features=5, num_samples=25)
explanation.as_pyplot_figure() # Display the plot
plt.show()
explanation_str = "The model's prediction is influenced by the following factors: "
explanation_str += "; ".join([f'"{feature}" contributes with a weight of {weight:.4f}'
for feature, weight in explanation.as_list()]) + "."
return explanation_str
def get_recommendations(self, condition, risk_level):
exercise_recommendations = {
"Depression": {
"High Risk": ["Try 10 minutes of deep breathing.", "Go for a 15-minute walk in nature.", "Practice guided meditation."],
"Moderate Risk": ["Write down 3 things you’re grateful for.", "Do light stretching or yoga for 10 minutes.", "Listen to calming music."],
"Low Risk": ["Engage in a hobby you enjoy.", "Call a friend and have a short chat.", "Do a short 5-minute mindfulness exercise."]
},
"Anxiety": {
"High Risk": ["Try progressive muscle relaxation.", "Use the 4-7-8 breathing technique.", "Write down your thoughts to clear your mind."],
"Moderate Risk": ["Listen to nature sounds or white noise.", "Take a 15-minute break from screens.", "Try a short visualization exercise."],
"Low Risk": ["Practice slow, deep breathing for 5 minutes.", "Drink herbal tea and relax.", "Read a book for 10 minutes."]
},
"Bipolar": {
"High Risk": ["Engage in grounding techniques like 5-4-3-2-1.", "Try slow-paced walking in a quiet area.", "Listen to calm instrumental music."],
"Moderate Risk": ["Do a 10-minute gentle yoga session.", "Keep a mood journal for self-awareness.", "Practice self-affirmations."],
"Low Risk": ["Engage in light exercise like jogging.", "Practice mindful eating for a meal.", "Do deep breathing exercises."]
},
"OCD": {
"High Risk": ["Use exposure-response prevention techniques.", "Try 5 minutes of guided meditation.", "Write down intrusive thoughts and challenge them."],
"Moderate Risk": ["Take a short break from triggers.", "Practice progressive relaxation.", "Engage in a calming activity like drawing."],
"Low Risk": ["Practice deep breathing with slow exhales.", "Listen to soft music and relax.", "Try focusing on one simple task at a time."]
},
"PTSD": {
"High Risk": ["Try grounding techniques (hold an object, describe it).", "Do 4-7-8 breathing for relaxation.", "Write in a trauma journal."],
"Moderate Risk": ["Practice mindfulness for 5 minutes.", "Engage in slow, rhythmic movement (walking, stretching).", "Listen to soothing music."],
"Low Risk": ["Try positive visualization techniques.", "Engage in light exercise or stretching.", "Spend time in a quiet, safe space."]
},
"Suicide Watch": {
"High Risk": ["Immediately reach out to a mental health professional.", "Call a trusted friend or family member.", "Try a grounding exercise like cold water on hands."],
"Moderate Risk": ["Write a letter to your future self.", "Listen to uplifting music.", "Practice self-care (take a bath, make tea, etc.)."],
"Low Risk": ["Watch a motivational video.", "Write down your emotions in a journal.", "Spend time with loved ones."]
},
"ADHD": {
"High Risk": ["Try structured routines for the day.", "Use a timer for focus sessions.", "Engage in short bursts of physical activity."],
"Moderate Risk": ["Do a quick exercise routine (jumping jacks, stretches).", "Use fidget toys to channel energy.", "Try meditation with background music."],
"Low Risk": ["Practice deep breathing.", "Listen to classical or instrumental music.", "Organize your workspace."]
},
"BPD": {
"High Risk": ["Try dialectical behavior therapy (DBT) techniques.", "Practice mindfulness.", "Use a weighted blanket for comfort."],
"Moderate Risk": ["Write down emotions and analyze them.", "Engage in creative activities like painting.", "Listen to calming podcasts."],
"Low Risk": ["Watch a lighthearted movie.", "Do breathing exercises.", "Call a friend for a short chat."]
},
"Autism": {
"High Risk": ["Engage in deep-pressure therapy (weighted blanket).", "Use noise-canceling headphones.", "Try sensory-friendly relaxation techniques."],
"Moderate Risk": ["Do repetitive physical activities like rocking.", "Practice structured breathing exercises.", "Engage in puzzles or memory games."],
"Low Risk": ["Spend time in a quiet space.", "Listen to soft instrumental music.", "Follow a structured schedule."]
},
"Schizophrenia": {
"High Risk": ["Seek immediate support from a trusted person.", "Try simple grounding exercises.", "Use distraction techniques like puzzles."],
"Moderate Risk": ["Engage in light physical activity.", "Listen to calming sounds or music.", "Write thoughts in a journal."],
"Low Risk": ["Read a familiar book.", "Do a 5-minute breathing exercise.", "Try progressive muscle relaxation."]
},
"Eating Disorders": {
"High Risk": ["Seek professional help immediately.", "Try self-affirmations.", "Practice intuitive eating (listen to body cues)."],
"Moderate Risk": ["Engage in mindful eating.", "Write down your emotions before meals.", "Do light stretching after meals."],
"Low Risk": ["Try a gentle walk after eating.", "Listen to calming music.", "Write a gratitude journal about your body."]
},
"Mental Health": {
"High Risk": ["Reach out to a mental health professional.", "Engage in deep relaxation techniques.", "Talk to a support group."],
"Moderate Risk": ["Write in a daily journal.", "Practice guided meditation.", "Do light physical activities like walking."],
"Low Risk": ["Try deep breathing exercises.", "Watch an uplifting video.", "Call a friend for a chat."]
}
}
if condition in exercise_recommendations:
if risk_level in exercise_recommendations[condition]:
return exercise_recommendations[condition][risk_level]
return ["No specific recommendations available."]
bot = MentalHealthChatbot()
def analyze_text(text):
if not text.strip():
return "No input provided.", "", "", "", "", ""
disorder, disorder_conf, risk = bot.get_disorder(text)
sentiment, sentiment_conf = bot.get_sentiment(text)
if risk == "High Risk":
logging.info("🚨 High risk detected! Alert triggered.")
alert_msg = "✔ 🚨 Alert Notification Triggered: High risk detected!"
else:
alert_msg = "✔ Risk is not high. No alert triggered."
recs = bot.get_recommendations(disorder, risk)
lime_explanation = bot.explain_text(text)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Insert data into DB
insert_into_database(
text,
sentiment,
sentiment_conf,
disorder,
disorder_conf,
risk,
recs,
timestamp
)
return (
f"{sentiment} ({sentiment_conf:.2f}%)",
f"{disorder} ({disorder_conf:.2f}%)",
risk,
"; ".join(recs),
lime_explanation,
alert_msg
)
# Gradio interface
gr.Interface(
fn=analyze_text,
inputs=gr.Textbox(lines=6, label="Describe how you're feeling..."),
outputs=[
gr.Text(label="Sentiment"),
gr.Text(label="Disorder"),
gr.Text(label="Risk Level"),
gr.Text(label="Recommendations"),
gr.Text(label="LIME Explanation"),
gr.Text(label="Alert Message")
],
title="🧠 Mental Health Analysis Assistant",
description="This tool uses AI to detect mental health conditions based on your input and suggest possible actions."
).launch() |