- Test out my chatbot at my personal portfolio: https://ongaunjie.com
- IMPORTANT NOTE: If the chatbot is not responding, it is because it takes about 2-5 minutes for it to spin up. This is due to the limitations of free deployments on Render. The service will go into hibernation mode after 15 minutes of inactivity.
- Development of an intent-based chatbot using NLTK and Deep Learning models.
- Integrating the Chatbot with APIs (Weather API, Movie API) and also parsing data from wikipedia using the wikipedia library in python.
- Establishment of an API endpoint using the flask framework.
- Creation of a user interface for the chatbot using JavaScript, HTML, CSS, and ReactJS, ensuring a seamless and interactive user experience.
- Intent Recognition: Utilizes Natural Language Processing to recognize user intents.
- NLTK Library: Leverages the NLTK library for NLP tasks such as tokenization, stemming and Bag of Words.
- Deep Learning Model: Implements a Feedforward Neutral-Network for predicting user intents.
- Customer Support: Implementing chatbots for various industries, such as e-commerce, healthcare, finance, and technology, providing efficient and responsive customer support services.
- Preparing a dataset that includes examples of user inputs and corresponding intents. Each input is associated with a specific intent.
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Hi",
"Hey",
"How are you",
"Hello",
"Good day",
"Yo"
],
"responses": [
"Hey :-)",
"Hello, thanks for visiting",
"Hi there, what can I do for you?",
"Hi there, how can I help?",
"Greetings!",
"Good to see you!",
"Hello, how can I assist you today?",
"Hi! I'm here to help.",
"Welcome! How can I be of service?",
"Hello, what can I answer for you?"
]
},
Before model training, it is necessary to perform tokenization, stemming, and create a Bag of Words representation for the intents.
- Tokenization is used to break down user inputs and intents into individual words.
- Stemming is applied to reduce words to their base form, helping the model handle variations.
- Bag of Words is a representation of the intents, creating a numerical format for training the machine learning model.
- The prepared dataset, including tokenized and preprocessed intents, is used to train a Feedforward Neural Network (FNN).
- The FNN is implemented using a deep learning framework, such as PyTorch or TensorFlow. (For this repo, I am using PyTorch)
- The neural network architecture comprises input, hidden, and output layers, with appropriate activation functions like ReLU for non-linearity.
- During training, the model learns to map tokenized input sequences to corresponding intents.
- The loss function is employed to measure the difference between predicted and actual intents, and optimization techniques, like stochastic gradient descent, are utilized to minimize this loss.
- The training process involves multiple epochs, refining the model's parameters to enhance its predictive accuracy.
- The trained model is saved for later use in the chatbot application.
- ntlk_utils.py: contains custom functions
- model.py: contains a class created for the FNN
- train.py: Used to preprocess the intents.json and train the model
- chat.py: This is the file where it utilizes the model generated from training and predicts on user inputs
git clone https://github.com/ongaunjie1/AI-Chatbot-DL-NLP.git
cd AI-Chatbot-DL-NLP
python -m venv venv
venv\Scripts\activate
(venv) pip install Flask torch torchvision nltk
or
(venv) pip install -r requirements.txt
(venv) python
>>> import nltk
>>> nltk.download('punkt')
- This part is where you can customize your own data
(venv) python train.py
(venv) python chat.py
- Weather API: fetch real-time weather information based on user queries
- Movie API: fetch movie title's data based on user queries
- For the weather API, use openweathermap
- For the movie API, use themoviedb
- This requires knowledge on extracting data from APIs, read the documentations for more details
Refer below for a snippet of how to integrate APIs. NOTE: Add-on the integration within the chat.py file
pip install requests
def weather_details(city):
api_key = 'INSERT YOUR API KEY HERE'
country_code = 'MY'
city_name = city
search_url = f'http://api.openweathermap.org/data/2.5/weather?q={city_name},{country_code}&appid={api_key}'
# Make the API call to search for the movie
response2 = requests.get(search_url)
if response2.status_code == 200:
data = response2.json()
if len(data) > 0:
city = data['name']
temp_celsius = kelvin_to_celsius(data['main']['temp'])
formatted_celsius = f'{temp_celsius}°C'
description = data['weather'][0]['description']
humidity = data['main']['humidity']
formatted_humidity = f'{humidity}%'
wind_speed = data['wind']['speed']
formatted_wind = f'{wind_speed}m/s'
city_details = {
'city': city,
'celsius': formatted_celsius,
'description': description,
'humidity': formatted_humidity,
'Wind Speed': formatted_wind }
return city_details
else:
print(f'No results found for {city}')
else:
print(f'Failed to retrieve data for {city}')
return None
- Refer to the chat.py for the movie API implementation and also using the wikipedia library to parse data.
- Upon adding APIs, you will need to create conditional statements (if, elif, else) to help the chatbot differentiate between different types of user queries and trigger specific actions or responses.
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from chat import get_response
import download_nltk_data
app = Flask(__name__)
CORS(app)
@app.post("/predict")
def predict():
text = request.get_json().get("message")
# TODO check if text is valid
response = get_response(text)
message = {"answer": response}
return jsonify(message)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)
python app.py
- http://127.0.0.1:8000/predict
- To deploy your chatbot's backend into the cloud. A free option is by using Render.
- If you have experience working with front-end frameworks such as CSS, HTML and JavaScript
- You can refer to an chatbot UI made by me within the chatbotui folder.
- Feel free to modify the design to your own liking.
- To test out a live version of the chatbot, check out my portfolio. Portfolio