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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 4 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 5 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Load the model and tokenizer
|
| 10 |
+
model_path = 'model.h5'
|
| 11 |
+
tokenizer_path = 'tokenizer.json'
|
| 12 |
+
|
| 13 |
+
# Load the tokenizer
|
| 14 |
+
with open(tokenizer_path, 'r') as tokenizer_file:
|
| 15 |
+
word_index = json.load(tokenizer_file)
|
| 16 |
+
tokenizer = Tokenizer(num_words=100000)
|
| 17 |
+
tokenizer.word_index = word_index
|
| 18 |
+
|
| 19 |
+
classes = ['ADHD', 'OCD', 'aspergers', 'depression', 'ptsd']
|
| 20 |
+
|
| 21 |
+
# Disease definitions
|
| 22 |
+
disease_definitions = {
|
| 23 |
+
'ADHD': "Attention-Deficit/Hyperactivity Disorder (ADHD) is a neurodevelopmental disorder characterized by persistent patterns of inattention, hyperactivity, and impulsivity.",
|
| 24 |
+
'OCD': "Obsessive-Compulsive Disorder (OCD) is a mental health disorder characterized by recurring unwanted thoughts (obsessions) and repetitive behaviors or mental rituals (compulsions).",
|
| 25 |
+
'aspergers': "Asperger's Syndrome, also known as Autism Spectrum Disorder (ASD), is a developmental disorder characterized by difficulties in social interaction, repetitive patterns of behavior, and limited interests or activities.",
|
| 26 |
+
'depression': "Depression is a common mental health disorder characterized by persistent sadness, loss of interest or pleasure in activities, changes in appetite or sleep patterns, and feelings of worthlessness or guilt.",
|
| 27 |
+
'ptsd': "Post-Traumatic Stress Disorder (PTSD) is a mental health condition triggered by a traumatic event. Symptoms may include flashbacks, nightmares, severe anxiety, and uncontrollable thoughts about the event."
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# Coping strategies
|
| 31 |
+
coping_strategies = {
|
| 32 |
+
'ADHD': 'Try breaking tasks into smaller, manageable steps and using reminders or organizational tools.',
|
| 33 |
+
'OCD': 'Practice exposure and response prevention (ERP) techniques and consider therapy options like Cognitive Behavioral Therapy (CBT).',
|
| 34 |
+
'aspergers': 'Focus on building social skills through therapy and practice. Seek support from autism support groups.',
|
| 35 |
+
'depression': 'Reach out to a mental health professional for therapy or medication options. Engage in self-care activities and reach out to supportive friends or family.',
|
| 36 |
+
'ptsd': 'Consider therapy options such as Cognitive Processing Therapy (CPT) or Eye Movement Desensitization and Reprocessing (EMDR). Practice grounding techniques during moments of distress.'
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# ... (Rest of the code remains the same)
|
| 40 |
+
# Resources
|
| 41 |
+
resources = {
|
| 42 |
+
'ADHD': 'Vidyasagar Institute of Mental Health and Neurosciences: http://www.vimhanshospital.org/',
|
| 43 |
+
'OCD': 'Serenity Neuropsychiatry Clinic: http://serenityclinic.org/',
|
| 44 |
+
'aspergers': 'Autism Society of India: http://www.autismsocietyofindia.org/',
|
| 45 |
+
'depression': 'iCALL - Psychosocial Helpline: https://www.icallhelpline.org/',
|
| 46 |
+
'ptsd': 'Mpower - The Centre: https://www.mpowerminds.com/'
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Doctors
|
| 50 |
+
doctors = {
|
| 51 |
+
'ADHD': ['Dr. John Doe', 'Dr. Jane Smith'],
|
| 52 |
+
'OCD': ['Dr. David Johnson', 'Dr. Emily Davis'],
|
| 53 |
+
'aspergers': ['Dr. Michael Brown', 'Dr. Sarah Wilson'],
|
| 54 |
+
'depression': ['Dr. Christopher Lee', 'Dr. Samantha Taylor'],
|
| 55 |
+
'ptsd': ['Dr. Robert Martinez', 'Dr. Jessica Thompson']
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
# Podcasts
|
| 59 |
+
podcasts = {
|
| 60 |
+
'ADHD': ['ADHD Experts Podcast', 'The ADHD Manual Podcast'],
|
| 61 |
+
'OCD': ['The OCD Stories Podcast', 'The OCD & Anxiety Show'],
|
| 62 |
+
'aspergers': ['The Autism Helper Podcast', 'Autism Spectrum Podcast'],
|
| 63 |
+
'depression': ['The Depression Files Podcast', 'The Hilarious World of Depression'],
|
| 64 |
+
'ptsd': ['The Trauma Therapist Podcast', 'PTSD Poetry Podcast']
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Books
|
| 68 |
+
books = {
|
| 69 |
+
'ADHD': ['Driven to Distraction by Edward M. Hallowell', 'Taking Charge of Adult ADHD by Russell A. Barkley'],
|
| 70 |
+
'OCD': ['Brain Lock by Jeffrey M. Schwartz', 'The OCD Workbook by Bruce M. Hyman'],
|
| 71 |
+
'aspergers': ['The Complete Guide to Asperger\'s Syndrome by Tony Attwood', 'Uniquely Human by Barry M. Prizant'],
|
| 72 |
+
'depression': ['The Noonday Demon by Andrew Solomon', 'Lost Connections by Johann Hari'],
|
| 73 |
+
'ptsd': ['The Body Keeps the Score by Bessel van der Kolk', 'After the War Zone by Laurie B. Slone']
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
# Music
|
| 77 |
+
music = {
|
| 78 |
+
'ADHD': ['Focus Playlist on Spotify', 'Energetic Beats Playlist on Apple Music'],
|
| 79 |
+
'OCD': ['Relaxing Sounds Playlist on Spotify', 'Calm Vibes Playlist on Apple Music'],
|
| 80 |
+
'aspergers': ['Soothing Melodies Playlist on Spotify', 'Peaceful Piano Playlist on Apple Music'],
|
| 81 |
+
'depression': ['Mood Booster Playlist on Spotify', 'Feel-Good Hits Playlist on Apple Music'],
|
| 82 |
+
'ptsd': ['Healing Sounds Playlist on Spotify', 'Emotional Support Playlist on Apple Music']
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
def get_response(user_input):
|
| 86 |
+
if 'hello' in user_input.lower() or 'hi' in user_input.lower():
|
| 87 |
+
return "Hello! How can I assist you today?"
|
| 88 |
+
|
| 89 |
+
if 'thank you' in user_input.lower() or 'thanks' in user_input.lower() or 'thanx' in user_input.lower():
|
| 90 |
+
return "You are welcome!"
|
| 91 |
+
|
| 92 |
+
# Check if the user asks for the definition of a disease
|
| 93 |
+
disease_keywords = ['definition', 'what is', 'explain', "what's", 'whats']
|
| 94 |
+
for keyword in disease_keywords:
|
| 95 |
+
if keyword in user_input.lower():
|
| 96 |
+
for disease, definition in disease_definitions.items():
|
| 97 |
+
if disease.lower() in user_input.lower():
|
| 98 |
+
return f"The definition of {disease} is: {definition}"
|
| 99 |
+
|
| 100 |
+
sequence = tokenizer.texts_to_sequences([user_input])
|
| 101 |
+
sequence = pad_sequences(sequence, maxlen=100)
|
| 102 |
+
|
| 103 |
+
# Load the model
|
| 104 |
+
model = load_model(model_path)
|
| 105 |
+
|
| 106 |
+
prediction = model.predict(sequence)
|
| 107 |
+
predicted_class_index = np.argmax(prediction)
|
| 108 |
+
predicted_class = classes[predicted_class_index]
|
| 109 |
+
|
| 110 |
+
# Calculate sentiment score
|
| 111 |
+
sid = SentimentIntensityAnalyzer()
|
| 112 |
+
sentiment_score = sid.polarity_scores(user_input)['compound']
|
| 113 |
+
|
| 114 |
+
# Determine the topic based on the highest predicted probability
|
| 115 |
+
predicted_topic = classes[np.argmax(prediction)]
|
| 116 |
+
|
| 117 |
+
if predicted_topic in classes:
|
| 118 |
+
# Provide coping strategies, resources, doctors, podcasts, books, and music based on the predicted topic
|
| 119 |
+
if np.max(prediction) < 0.7:
|
| 120 |
+
return f"The model predicts your mental health issue to be {predicted_class}, but it's recommended to consult a doctor for a proper diagnosis."
|
| 121 |
+
|
| 122 |
+
response = f"Predicted Disease: {predicted_class}\n"
|
| 123 |
+
|
| 124 |
+
if predicted_topic in coping_strategies:
|
| 125 |
+
response += f"Coping Strategy: {coping_strategies[predicted_topic]}\n"
|
| 126 |
+
|
| 127 |
+
if predicted_topic in resources:
|
| 128 |
+
response += f"Resource: {resources[predicted_topic]}\n"
|
| 129 |
+
|
| 130 |
+
if predicted_topic in doctors:
|
| 131 |
+
response += f"Doctors: {', '.join(doctors[predicted_topic])}\n"
|
| 132 |
+
|
| 133 |
+
if predicted_topic in podcasts:
|
| 134 |
+
response += f"Podcasts: {', '.join(podcasts[predicted_topic])}\n"
|
| 135 |
+
|
| 136 |
+
if predicted_topic in books:
|
| 137 |
+
response += f"Books: {', '.join(books[predicted_topic])}\n"
|
| 138 |
+
|
| 139 |
+
if predicted_topic in music:
|
| 140 |
+
response += f"Music: {', '.join(music[predicted_topic])}\n"
|
| 141 |
+
|
| 142 |
+
return response
|
| 143 |
+
|
| 144 |
+
if sentiment_score >= 0.2:
|
| 145 |
+
return "I'm glad to hear that you're feeling happy! If you need any mental health support, feel free to share."
|
| 146 |
+
elif sentiment_score <= -0.2:
|
| 147 |
+
return "I'm sorry to hear that you're feeling down. Remember, you're not alone. Reach out to someone you trust or consider seeking professional help."
|
| 148 |
+
|
| 149 |
+
return "Thank you for sharing. How can I assist you today?"
|
| 150 |
+
|
| 151 |
+
def main():
|
| 152 |
+
st.title("Health Chatbot")
|
| 153 |
+
st.write("Enter your message and get a response from the chatbot:")
|
| 154 |
+
|
| 155 |
+
user_input = st.text_input("User Input:")
|
| 156 |
+
|
| 157 |
+
if st.button("Submit"):
|
| 158 |
+
response = get_response(user_input)
|
| 159 |
+
st.write("Bot Response:")
|
| 160 |
+
st.write(response)
|
| 161 |
+
|
| 162 |
+
if __name__ == '__main__':
|
| 163 |
+
main()
|