Final Modified Document PG
Final Modified Document PG
PYTHON
A project report submitted in partial fulfilment of the requirement for the
award of Degree of B. Tech
Submitted by,
MEENA LOCHARLA - 20NT5A0555
M. ARAVIND - 20NT1A0557
D.SAGAR SUMANTH - 21NT5A0504
M.MANISH KUMAR - 21NT5A0549
CERTIFICATE
This is to certify that this project report is a bonafide work of, Meena Locharla
(20NT5A0555),M.Aravind (20NT1A0557), M.Manish Kumar (21NT5A0549),
D.Sagar sumanth (21NT5A0504) who have done the project which entitled
“AI HEALTH CARE BOT SYSTEM USING PYTHON ”
EXTERNAL EXAMINAR
DECLARATION
I also thank our guide K.VIJAY sir, for her guidance, in completion of
this project successfully.
With Gratitude,
MEENA LOCHARLA - (20NT5A0555)
M.ARAVIND - (20NT1A0557)
D.SAGAR SUMANTH - (21NT5A0504)
M.MANISH KUMAR -
(21NT1A0549)
ABSTRACT
Today in the present era, the major challenges that India as a country is facing
are to cater good quality and affordable healthcare services to its growing
population and at the same time, they are not cost-efficient. This inaccessibility
of healthcare facilities especially in rural areas and the difficulty in accessing
means of transport causes patients to postpone their treatment, or option for
medical facilities which is closer but at the same time are not cost-efficient and
well-matched to their medical needs.
Our python-based project comprises 2 modules: User and Admin. The user will
need to register first to access the system. They can log in using their
credentials after registering successfully. They can manage their profile and
change the password if they want. The users can chat with the bot and resolve
their health-related queries. They can also view doctors and their clinics near
their location using Google places API. The users can also view hospitals
nearby their location.
The admin can log in directly using their credentials to access the system. They
can manage the question and answers and will require to train the model. They
can also view the details of registered users.
We have created our own dataset to implement this system. Also, the CNN
algorithm is used to develop this system. The accuracy of general disease risk
prediction of CNN is higher as compared to other algorithms.
INDEX
CHAPTER NO TITLE PAGE.NO
1 INTRODUCTION
2 LITERATURE REVIEW
2.4PACKAGE INSTALLER
2.5TENSORFLOW
2.6KERAS
2.7NLP
2.8NLTK
2.9PICKLE
2.10NUMPY
3 METHODOLOGY
3.1.1 JSON
3.1.2INTENTS
3.2 REQUIREMENTS
3.4 MODELS
3.4.4LSTM
3.6.2 EPOCHS
3.6.3 VERBOSE
4 TESTING
REFERENCE
APPENDIX
A.SOURCE CODE
B.SCREEN SHOTS
LIST OF FIGURES
4. SYSTEM ARCHITECTURE
6. ACCURACY
7.
CHAPTER 1
INTRODUCTION:
1.1 Outlet of the Project:
This is an automated chat robot design to answer users frequently asked questions,
earlier natural language processing techniques were using to design this robot but
its accuracy of giving correct answer was less and now due to Deep Learning
algorithms accuracy of giving correct answer increase, so here using python deep
learning project we are building CHATBOT application to answer users questions.
To implement this technique first we train deep learning models with the train data
(all possible question’s answers) and whenever users give any question then
application will apply this test question on train model to predict exact answer for
given question.
Earlier companies were hiring humans to answer user’s queries but by using this
application we can answer user’s question without using any manpower.
Chabot can be described as software that can chat with people using artificial
intelligence. Chabot’s are generally used to respond quickly to users. Chabot’s, a
common name for automated conversational interfaces, present a new way for
individuals to interact with computer systems. Traditionally, to get a question
answered by a software program involves using a search engine or filling out a
form. A Chabot allows a user to simply ask questions in the same manner that they
would address a human. There are many well-known voice-based catboats
currently available in the market: Google Assistant, Alexa and Siri. Chabot’s are
currently being adopted at a high rate on computer chat platforms.
To implement this project, we are using python deep learning neural networks and
NLTK (natural Language Processing API) to process train and test text data.
Python
● Tensor flow
● Keras
● NLTK
● Pickle
Processor- minimum
intel i5 core/Above
Windows- 7/Above
CHAPTER 2
LITERATURE SURVEY
In today’s world, health is the major key in the development of each sector. Health
also needs modern technology and their implementations in order to boost the
development in this area. As know that it is the most important field in each country,
need to provide sufficient technological development. A lot of researches have been
done in this field in order to modernize the methods of health status. Old methods of
diagnosing are still a major hindrance in the advancement of medical facilities. The
best way to overcome this problem is using a medical chatbot with self-diagnosis
using Artificial Intelligence.
The proposed method can use deep learning algorithms to increase the capabilities
of computers and by enhancing it can understand what humans can do, which
includes speech and text recognition. In this will use text-text in mediots to contact
the patients and teach the chatbots to process natural language text It discusses
about processing natural language using Recurrent Neural Network (RNN). The
sequence to sequence long short-term memory cell neural network (LSTM) is used
to train the model. In addition, it also talks about the challenges of implementing a
Recurrent Neural Network based chatbot. Disease diagnosis system using several
machine learning algorithms is proposed. A detailed comparison of four Machine
Learning algorithms to predict disease based on symptoms provided, is also
presented in this paper.
The purpose was to find the best Machine Learning algorithm to diagnose diseases
early to help doctors and patients, as early prediction of disease can improve
treatment efficiency. The chatbot will start questioning the patients regarding their
symptoms and problems they are suffering and suggest an effective method to
reduce the disease or give the medication according to the disease user suffering.
Deep learning is a subset of machine learning in artificial intelligence that has
networks capable of learning unsupervised from data that is unstructured or
unlabeled. Also known as deep neural learning or deep neural
network. Recurrent Neural Network (RNN) are a type of Neural Network where the
output from the previous step is fed as input to the current step. ... It uses the same
parameters for each input as it performs the same task on all the inputs or hidden
layers to produce the output [8]. In this can build algorithms to make the computer
automatically analyze the given data and also make it understand human language.
In this, mainly use text recognition using deep learning. Deep learning algorithms
are used to enhance the capability of a chatbot. The main focus of the review is to
implement medibot using both deep learning and neural networks.
The process of learning begins with observations or data, such as examples, direct
experience, or instruction, in order to look for patterns in data and make better
decisions in the future based on the examples that we provide. The primary aim is
to allow the computers learn automatically without human intervention or assistance
and adjust actions accordingly.
Can apply what has been learned in the past to new data using labeled examples to
predict future events. Starting from the analysis of a known training dataset, the
learning algorithm produces an inferred function to make predictions about the
output values. The system can provide targets for any new input after sufficient
training. The learning algorithm can also compare its output with the correct,
intended output and find errors in order to modify the model accordingly.
Are used when the information used to train is neither classified nor labeled.
Unsupervised learning studies how systems can infer a function to describe a
hidden structure from unlabeled data. The system does not figure out the right
output, but it explores the data and can draw inferences from datasets to describe
hidden structures from unlabeled data.
Fall somewhere in between supervised and unsupervised learning, since they use
both labeled and unlabeled data for training – typically a small amount of labeled
data and a large amount of unlabeled data. The systems that use this method can
considerably improve learning accuracy. Usually, semi-supervised learning is
chosen when the acquired labeled data requires skilled and relevant resources in
order to train it learn from it. Otherwise, acquiring unlabeled data generally does not
require additional resources.
Is a learning method that interacts with its environment by producing actions and
discovers errors or rewards. Trial and error search and delayed reward are the most
relevant characteristics of reinforcement learning. This method allows machines and
software agents to automatically determine the ideal behavior within a specific
context in order to maximize its performance. Simple reward feedback is required
for the agent to learn which action is best; this is known as the reinforcement signal.
Machine learning enables analysis of massive quantities of data. While it generally
delivers faster, more accurate results in order to identify profitable opportunities or
dangerous risks, it may also require additional time and resources to train it
properly. Combining machine learning with AI and cognitive technologies can make
it even more effective in processing large volumes of information.
Deep learning is an AI function that imitates the workings of the human brain in
processing data and creating patterns for use in decision making. Deep learning is a
subset of ML in artificial intelligence that has networks capable of learning
unsupervised from data that is unstructured or unlabelled. Also known as deep
neural learning or deep neural network.
● Deep learning is an AI function that mimics the workings of the human brain
in processing data for use in detecting objects, recognizing speech, translating
languages, and making decisions.
● Deep learning AI can learn without human supervision, drawing from data
that is both unstructured and unlabelled.
● Deep learning, a form of machine learning, can be used to help detect fraud
or money laundering, among other functions.
Deep learning has evolved together with the digital era, which has brought about an
explosion of data in all forms and from every region of the world. This data, known
simply as big data, is drawn from sources like social media, internet search engines,
e-commerce platforms, and online cinemas, among others. This enormous amount
of data is readily accessible and can be shared
However, the data, which normally is unstructured, is so vast that it could take
decades for humans to comprehend it and extract relevant information.
Companies realize the incredible potential that can result from unravelling this
wealth of information and are increasingly adapting to AI systems for automated
support.
2.3 Python
The Python interpreter is easily extended with new functions and data types
implemented in C or C++ (or other languages callable from C). Python is also
suitable as an extension language for customizable applications.
2.5 TensorFlow
If you need more flexibility, eager execution allows for immediate iteration and
intuitive debugging. For large ML training tasks, use the Distribution Strategy API for
distributed training on different hardware configurations without changing the model
definition. TensorFlow has always provided a direct path to production. Whether it’s
on servers, edge devices, or the web, TensorFlow lets you train and deploy your
model easily, no matter what language or platform you use.
TensorFlow gives you the flexibility and control with features like the Keras
Functional API and Model Subclassing API for creation of complex topologies. For
easy prototyping and fast debugging, use eager execution.
2.6 Keras
Keras is a deep learning API written in Python, running on top of the machine
learning platform TensorFlow. It was developed with a focus on enabling fast
experimentation. Being able to go from idea to result as fast as possible is key to
doing good research.
Keras is a open-source software library that provides a Python interface for artificial
neural networks. Keras acts as an interface for the TensorFlow library.
Keras is the most used deep learning framework among top-5 winning teams
Natural language processing strives to build machines that understand and respond
to text or voice data—and respond with text or speech of their own—in much the
same way humans do.
NLP drives computer programs that translate text from one language to another,
respond to spoken commands, and summarize large volumes of text rapidly—even
in real time. There is a good chance you have interacted with NLP
NLP Use Cases - Natural language processing is the driving force behind machine
intelligence in many modern real-world applications. Here are a few examples:
2. Spam detection
3. Text summarization
5. Machine translation
2.8 NLTK
NLTK is a leading platform for building Python programs to work with human
language data. NLTK includes libraries for many of the NLP tasks, plus libraries for
subtasks, such as sentence parsing, word segmentation, stemming and
lemmatization (methods of trimming words down to their roots), and tokenization
(for breaking phrases, sentences, paragraphs and passages into tokens that help
the computer better understand the text). It also includes libraries for implementing
capabilities such as semantic reasoning, the ability to reach logical conclusions
based on facts extracted from text.
NLTK has been called “a wonderful tool for teaching, and working in, computational
linguistics using Python,” and “an amazing library to play with natural language.”
The Python programming language provides a wide range of tools and libraries for
attacking specific NLP tasks. Many of these are found in the Natural Language
Toolkit, or NLTK, an open-source collection of libraries, programs, and education
resources for building NLP programs.
2.9 Pickle
The Pickle module implements binary protocols for serializing and de-serializing a
Python object structure. “Pickling” is the process whereby a Python object hierarchy
is converted into a byte stream, and “unpickling” is the inverse operation, whereby a
byte stream (from a binary file or bytes-like object) is converted back into an object
hierarchy. Pickling (and unpickling) is alternatively known as “serialization”.
The data format used by pickle is Python-specific. This has the advantage that there
are no restrictions imposed by external standards such as JSON or XDR (which
can’t represent pointer sharing); however, it means that non-Python programs may
not be able to reconstruct pickled Python objects.
By default, the pickle data format uses a relatively compact binary representation. If
you need optimal size characteristics, you can efficiently compress pickled data.
The module pickle tools contain tools for analysing data stream generated by
Pickle.
2.10 Numpy
CHAPTER -3
METHODOLOGY:
3.1 DataSets:
A data set is a collection of data. In the case of tabular data, a data set corresponds
to one or more database tables, where every column of a table represents a
particular variable, and each row corresponds to a given record of the data set in
question. The data set lists values for each of the variables, such as the height and
weight of an object, for each member of the data set. Each value is known as a
datum. Data sets can also consist of a collection of documents or files.
3.1.1 JSON:
JSON is a text format for storing and transporting data. JSON is self-describing and
easy to understand.
"address": {
},
Why JSON?
The JSON format is syntactically similar to the code for creating JavaScript objects.
Because of this, a JavaScript program can easily convert JSON data into JavaScript
objects.
Since the format is text only, JSON data can easily be sent between computers,
and used by any programming language.
3.1.2 Intents:
Local Data Storage (No External Data Set required) File Format: JSON
The dataset we will be using is ‘intents.json’. This is a JSON file that contains the
patterns we need to find and the responses we want to return to the user.
3.2 Requirements:
This chapter provides the details of the project’s need-based survey, system
requirements, Hardware Requirements, Software Requirements, and System
Requirements.
• To implement this technique first we train deep learning models with the train
data (all possible questions’ answers) and whenever users give any question then
application will apply this test question on train model to predict exact answer for
given question.
Before exploring new technology, one should examine prior work and learn from
past ideas, both succeed and failed attempts. This section presents a selection of
events from the last century, which introduced the ideas that formed the present
definition of a Chabot. It is not an attempt to give an all-encompassing overview
about the history of computing, instead the aim is to explain where the concept of
chat bots and the interest of creating them originated.
Draw backs of Existing System:
• No proper support
• Communication overhead
• High risk
• More Manpower
Chatbot can be described as software that can chat with people using artificial
intelligence. Chatbots are generally used to respond quickly to users. Chatbots, a
common name for automated conversational interfaces, present a new way for
individuals to interact with computer systems. Traditionally, to get a question
answered by a software program involves using a search engine or filling out a
form. A chatbot allows a user to simply ask questions in the same manner that they
would address a human. There are many well-known voice-based chatbots
currently available in the market: Google Assistant, Alexa and Siri. Chatbots are
currently being adopted at a high rate on computer chat platforms. A chatbot can be
used anywhere a human is interacting with a computer system. These are the areas
where the fastest adoption is occurring.
Advantages: -
• Cost Efficient
When working with text data, we need to perform various pre-processing on the
data before we make a machine learning or a deep learning model. Based on the
requirements we need to apply various operations to pre-process the
data.Tokenizing is the most basic and first thing you can do on text data. Tokenizing
is the process of breaking the whole text into small parts like words.
Here we iterate through the patterns and tokenize the sentence using
nltk.word_tokenize() function and append each word in the words list. We also
create a list of classes for our tags.
w = nltk.word_tokenize(pattern) words.extend(w)
# sort classes
classes = sorted(list(set(classes)))
# classes = intents
pickle.dump(classes,open('classes.pkl','wb'))
3.4 Models:
The core idea of Sequential model is simply arranging the Keras layers in a
sequential order and so, it is called Sequential model. Most of the ANN also has
layers in sequential order and the data flows from one layer to another layer in the
given order until the data finally reaches the output layer.
A Sequential model is appropriate for a plain stack of layers where each layer has
exactly one input tensor and one output tensor.
In this project there are 3 layers, first layer 128 neurons, second layer 64 neurons
and 3rd output layer contain number of neurons equal to number of intents to
predict output intent with SoftMax.
Dense layer is the regular deeply connected neural network layer. It is most
common and frequently used layer. Dense layer does the below operation on the
input and return the output.
● dot represent NumPy dot product of all input and its corresponding weights
In the first layer we have used Relu function (ReLu is a non-linear activation
function that is used in multi-layer neural networks or deep neural networks), and in
the third layer we have used Softmax function (function that converts a vector of
numbers into a vector of probabilities, where the probabilities of each value are
proportional to the relative scale of each value in the vector.).
3.4.3 RNN:
Advantages:
3.4.4 LSTM:
The major highlights include the interior design of a basic LSTM cell, the variations
brought into the LSTM architecture and few applications of LSTMs that are highly in
demand.
RNN is a network that works on the present input by taking into consideration the
previous output (feedback) and storing in its memory for a short period of time
(short-term memory). Nevertheless, there are drawbacks to RNNs.
First, it fails to store information for a longer period of time. At times, a reference to
certain information stored quite a long time ago is required to predict the current
output. But RNNs are absolutely incapable of handling such “long-term
dependencies”.
Second, there is no finer control over which part of the context needs to be carried
forward and how much of the past needs to be ‘forgotten’.
Other issues with RNNs are exploding and vanishing gradients which occur during
the training process of a network through backtracking. Thus, LSTM was brought
into the picture. It has been designed that the vanishing gradient problem is almost
completely removed, while the training model is left unaltered.
During the training process of a network, the main goal is to minimize loss (in terms
of error or cost). We calculate the gradient i.e., loss with respect to a particular set
of weights. Adjust the weights accordingly and repeat this process until the optimal
set of weights for which the loss is minimum. This is the concept of backtracking.
When this gradient is multiplied with the learning rate, which is itself a small value
ranging between 0.1-0.001, it results in a smaller value. As a consequence, the
alteration in weights is quite small, producing almost the same output as before.
Similarly, if the gradients are quite large in value due to the large values of
components, the weights get updated to a value beyond the optimal value. This is
known as the problem of exploding gradients.
To avoid this scaling effect, the neural network unit was re-built in such a way that
the scaling factor was fixed to one. The cell was then enriched by several gating
units and termed as LSTM.
LSTMs comprises of three logistic sigmoid gates and one tanh layer. Gates have
been introduced in order to limit the information that is passed through the cell.
They determine which part of the information will be needed by the next cell and
which part is to be discarded. The output is usually in the range of 0-1 where ‘0’
means ‘reject all’ and ‘1’ means ‘include all’.
Fig-1
✔ Each LSTM cell has three inputs ht-1, Ct-1 and xt and two outputs ht and Ct. For
a given time t, ht is the hidden state, Ct is the cell state or memory, xt is the current
data point or input.
✔ The first sigmoid layer has two inputs ht-1 and xt where ht-1 is the hidden state of
the previous cell.
✔ First sigmoid layer is also known as the forget gate as its output selects the
amount of information of the previous cell to be included. The output is a number in
[0,1] which is multiplied (pointwise) with the previous cell state Ct-1.
✔ Second sigmoid layer is the input gate that decides what new information is to be
added to the cell. It takes two inputs ht-1 and xt.
✔ The tanh layer creates a vector Ct of the new candidate values. Together, these
two layers determine the information to be stored in the cell state. Their point-wise
multiplication (it ? Ct) tells us the amount of information to be added to the cell state.
✔ The result is then added with the result of the forget gate multiplied with previous
cell state (ft * Ct-1) to produce the current cell state Ct.
✔ Next, the output of the cell is calculated using a sigmoid and a tanh layer. The
sigmoid layer decides which part of the cell state will be present in the output
whereas tanh layer shifts the output in the range of [-1,1].
✔ The results of the two layers undergo point-wise multiplication to produce the
output ht of the cell.
3.5 Experimental Setup:
The sample of data used to provide unbiased evaluation of a model to fit on the
train data set. The evaluation becomes more biased as skill on the validation
dataset is incorporated into the model configuration.
The sample of data used to provide unbiased evaluation of a final model. It used to
test if the model is making accurate predictions. The test set is a set of observations
used to evaluate the performance of the model using some performance metric. It is
important that no observations from the training set are included in the test set. If
the test set does contain examples from the training set, it will be difficult to assess
whether the algorithm has learned to generalize from the training set or has simply
memorized it.
3.6.2 Epochs:
Integer. Number of epochs to train the model. An epoch is an iteration over the
entire x and y data provided. Note that in conjunction with initial epoch, epochs is to
be understood as "final epoch". The model is not trained for a number of iterations
given by epochs, but merely until the epoch of index epochs is reached.
3.6.3 Verbose:
Float between 0 and 1. Fraction of the training data to be used as validation data.
The model will set apart this fraction of the training data, will not train on it, and will
evaluate the loss and any model metrics on this data at the end of each epoch. 20%
of the train dataset is used as validation dataset for validating the model which is
trained by 80% of the train dataset.
FIG-2
FIG-3
Design Flow Diagram
In this Design Flow Diagram first, it has to start and the user had to send input to
intent classification. From intent files, data will be sent to intent classification. Then
after it goes to search pattern where it searches for the correct pattern in pattern
check if it is yes it goes to return response if not data base. In the next section going
to explain the literature review in which know more about the medibot.
FIG-4
System Architecture
System Architecture is important for the development of a system. This medibot
provides proper guidance to patients who have no awareness of their physical
condition. Many people hesitate to ask doctors about symptoms with chatbots so
they can solve their problems. The below fig 3 gives us working of the system.
FIG-5
According to the above diagram User, messages will be classified into Intent and
Entities by Intent and Entities classification. After it will go to the response generator
where it will send a response to candidates from context and responses. Response
selector will select a response from the above responses shows in fig.4
5. Testing
The purpose of testing is to discover errors. Testing is the process of trying to discover every
conceivable fault or weakness in a work product. It provides a way to check the functionality of
components, sub-assemblies, assemblies and/or a finished product. It is a process of exercising
software with the intent of ensuring that theSoftware system meets its requirements and user
expectations and does not fail in an unacceptable manner. There are various types of test. Each test
type addresses a specific testing requirement.
Unit Testing:
Unit testing involves the design of test cases that validate, that the internal program logic is
functioning properly, and that program inputs produce valid outputs. All decision branches and
internal code flow should be validated. It is the testing of individual software units of the
application .it is done after the completion of an individual unit before integration. This is a
structural testing, that relies on knowledge of its construction and is invasive. Unit tests perform
basic tests at component level and test a specific business process, application, and/or system
configuration. Unit tests ensure that each unique path of a business process performs accurately to
the documented specifications and contains clearly defined inputs and expected results.
Integration Testing:
Integration tests are designed to test integrated software components to determine if they actually
run as one program. Testing is event driven and is more concerned with the basic outcome of
screens or fields. Integration tests demonstrate that although the components were individually
satisfaction, as shown by successfully unit testing, the combination of components is correct and
consistent. Integration testing is specifically aimed at exposing the problems that arise from the
combination of components.
Functional Tests:
Functional tests provide systematic demonstrations that functions tested are available as specified
by the business and technical requirements, system documentation, and user manuals.
System Test:
System testing ensures that the entire integrated software system meets requirements. It tests a
configuration to ensure known and predictable results. An example of system testing is the
configuration oriented system integration test. System testing is based on process descriptions and
flows, emphasizing pre-driven process links and integration points.
White Box Testing is a testing in which in which the software tester has knowledge of the inner
workings, structure and language of the software, or at least its purpose. It is purpose. It is used to
test areas that cannot be reached from a black box level.
Black Box Testing is testing the software without any knowledge of the inner workings, structure or
language of the module being tested. Black box tests, as most other kinds of tests, must be written
from a definitive source document, such as specification or requirements document, such as
specification or requirements document. It is a testing in which the software under test is treated,
as a black box .you cannot “see” into it. The test provides inputs and responds to outputs without
considering how the software works.
Unit Testing:
Unit testing is usually conducted as part of a combined code and unit test phase of the software
lifecycle, although it is not uncommon for coding and unit testing to be conducted as two distinct
phases.
Integration Testing:
The simulation tested with different commands like wish and xgraph and god attributes.
Acceptance Testing:
User Acceptance Testing is a critical phase of any project and requires significant participation by
the end user. It also ensures that the system meets the functional requirements. The input from the
user like source and destination node ids are accepting as per the simulation criteria.
Test Results: All the test cases mentioned above passed successfully. No defects encountered.
5.2 Test Strategy and Approach:
Field testing will be performed manually and functional tests will be written in detail.
Test objectives
Features to be tested
CASE-1
1) Accuracy
2) Loss
model.evaluate(np.array(train_x), np.array(train_y))
Fig-6
We will load the trained model and then use a graphical user interface
that will predict the response from the bot. The model will only tell us
the class it belongs to, so we will implement some functions which will
identify the class and then retrieve us a random response from the list
of responses.
Again we import the necessary packages and load the ‘words.pkl’ and
‘classes.pkl’ pickle files which we have created when we trained our
model:
import nltk
from nltk.stem import WordNetLemmatizer lemmatizer =
WordNetLemmatizer()
import pickle
import numpy as np
from keras.models import load_model model =
load_model('chatbot_model.h5') import json
import random
intents = json.loads(open('intents.json').read()) words =
pickle.load(open('words.pkl','rb')) classes =
pickle.load(open('classes.pkl','rb'))
To predict the class, we will need to provide input in the same way as
we did while training. So we will create some functions that will perform
text pre-processing and then predict the class.
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
sentence_words]
return sentence_words
# 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
After predicting the class, we will get a random response from the list of
intents.
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
def chatbot_response(text):
ints = predict_class(text, model) res = getResponse(ints, intents) return
res
Now we will develop a graphical user interface. Let’s use Tkinter library
which is shipped with tons of useful libraries for GUI. We will take the
input message from the user and then use the helper functions we have
created to get the response from the bot and display it on the GUI. Here
is the full source code for the GUI.
5.1 CONCLUSION:
The Intent of this paper is to increase the awareness of health among the people. In
current days, many people show their lazy behavior and don’t consult a doctor during
a time of illness so the implementation of a chatbot will help the people to diagnose
the disease without consulting a doctor. The chatbot will act as a virtual doctor. The
user will prescribe their symptoms of their illness and the chatbot will analyze the
disease and suggest the necessary healthcare steps that need to be taken. In the
datasets it includes information regarding diseases and health care steps.
import random
from flask import jsonify
import secrets
from flask import Flask, render_template, flash, redirect, url_for,
session, logging, request, session
from flask_sqlalchemy import SQLAlchemy
from collections.abc import Mapping
def make_token():
"""
Creates a cryptographically-secure, URL-safe string
"""
return secrets.token_urlsafe(16)
class user(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
email = db.Column(db.String(120))
password = db.Column(db.String(80))
@app.route("/")
def index():
return render_template("index.html")
userSession = {}
@app.route("/user")
def index_auth():
my_id = make_token()
userSession[my_id] = -1
return render_template("index_auth.html",sessionId=my_id)
@app.route("/instruct")
def instruct():
return render_template("instructions.html")
@app.route("/upload")
def bmi():
return render_template("bmi.html")
@app.route("/diseases")
def diseases():
return render_template("diseases.html")
@app.route('/pred_page')
def pred_page():
pred = session.get('pred_label', None)
f_name = session.get('filename', None)
return render_template('pred.html', pred=pred, f_name=f_name)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
uname = request.form["uname"]
passw = request.form["passw"]
login = user.query.filter_by(username=uname,
password=passw).first()
if login is not None:
return redirect(url_for("index_auth"))
return render_template("login.html")
return redirect(url_for("login"))
return render_template("register.html")
all_result = {
'name':'',
'age':0,
'gender':'',
'symptoms':[]
}
# Import Dependencies
# import gradio as gr
import pandas as pd
import numpy as np
from joblib import load
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def predict_disease_from_symptom(symptom_list):
disease_details = getDiseaseInfo(result[0])
print(df_test)
# Cleanup
del df_test
#return f"<b>{result[0]}</b><br>{disease_details}",result[0]
#print(df_test)
return f"<b>{result[0]}</b><br>{disease_details}",result[0]
user_symptoms = symptom_list
# Vectorize symptoms using CountVectorizer
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['Symptoms'])
user_X = vectorizer.transform([', '.join(user_symptoms)])