48 75 Dsa Report
48 75 Dsa Report
48 75 Dsa Report
Submitted by
ASHWIN A (2020506016)
1|Page
MENTAL HEALTH CHATBOT
AIM:
To create a Chatbot that deals and gives solutions to mental health of
human beings.
PROJECT DESCRIPTION:
2|Page
Motivation:
The motivation behind our Mental Health Chatbot project stems from
the critical need for accessible and immediate mental health support. Mental
health challenges are widespread, and many individuals hesitate to seek help
due to stigma, lack of resources, or concerns about privacy. This chatbot
serves as a proactive solution, providing a discreet and non-judgmental
platform for users to express their feelings and receive timely assistance.
By integrating natural language processing and deep learning, our chatbot can
understand and respond empathetically to a variety of mental health concerns.
The goal is to bridge the gap between individuals in need and professional
support by offering informative content, coping strategies, and resources. The
user-friendly web interface ensures easy accessibility, catering to a diverse
audience.
SYSTEM REQUIREMENTS:
To deploy and run the Mental Health Chatbot project, you need to ensure that
your system meets the following requirements:
1. Hardware:
A computer with sufficient processing power to handle the chatbot's
inference tasks.
Recommended: A machine with a GPU (Graphics Processing Unit) for
faster deep learning model inference.
2. Software:
Python: Ensure that you have Python installed (version 3.x).
3|Page
Dependencies: Install the required Python libraries by running:
“pip install nltk keras flask”
3. Project Files:
Obtain the pre-trained model file (`model.h5`), word data (`texts.pkl`),
label data (`labels.pkl`), and intents data (`intents.json`).
Ensure the availability of the HTML template (`index.html`) for the
web interface.
4. Web Server:
Install Flask for the web server:
“pip install Flask”
5. Internet Connection:
A stable internet connection is required to download NLTK data
packages and for the Flask application to run.
7. Operating System:
The code is platform-independent, and it should work on major
operating systems such as Windows, macOS, and Linux.
Ensure that you have fulfilled these system requirements before attempting to
run the Mental Health Chatbot project. Additionally, if you plan to deploy the
chatbot as a web application, make sure that the required ports are accessible.
ALGORITHMS USED:
4|Page
The chatbot is integrated into a Flask web application to provide a user
interface. Here's a breakdown of the code:
1. Libraries:
SOURCE CODE:
import nltk
nltk.download('popular')
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import pickle
import numpy as np
from keras.models import load_model
model = load_model('model.h5')
import json
import random
intents = json.loads(open('intents.json').read())
words = pickle.load(open('texts.pkl','rb'))
classes = pickle.load(open('labels.pkl','rb'))
def clean_up_sentence(sentence):
# tokenize the pattern - split words into array
sentence_words = nltk.word_tokenize(sentence)
# stem each word - create short form for word
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in
sentence_words]
return sentence_words
6|Page
# return bag of words array: 0 or 1 for each word in the bag that exists in the
sentence
def bow(sentence, words, show_details=True):
# tokenize the pattern
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0]*len(words)
for s in sentence_words:
for i,w in enumerate(words):
if w == s:
# assign 1 if current word is in the vocabulary position
bag[i] = 1
if show_details:
print ("found in bag: %s" % w)
return(np.array(bag))
def predict_class(sentence, model):
# filter out predictions below a threshold
p = bow(sentence, words,show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
def getResponse(ints, intents_json):
tag = ints[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if(i['tag']== tag):
result = random.choice(i['responses'])
break
return result
7|Page
def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res
from flask import Flask, render_template, request
app = Flask(__name__)
app.static_folder = 'static'
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
return chatbot_response(userText)
if __name__ == "__main__":
app.run()
OUTPUTS(Screenshots):
8|Page
FUTURE WORKS:
The Mental Health Chatbot project lays the foundation for future
enhancements and expansions to further improve its impact and capabilities.
Some potential future works include:
9|Page
4. Multi-modal Interaction:
- Expand the chatbot's capabilities to process and respond to multi-modal
inputs, including images and voice, to enhance user engagement and
understanding.
8. Language Support:
- Extend language support to make the chatbot accessible to a more diverse
user base by incorporating additional languages.
9. Research Collaboration:
- Collaborate with mental health researchers to validate and enhance the
effectiveness of the chatbot in supporting mental health and well-being.
By focusing on these future works, the Mental Health Chatbot can evolve into
a more advanced and impactful tool, contributing to the well-being of users
and the advancement of technology-driven mental health support.
10 | P a g e
CONCLUSION:
11 | P a g e