Integrate Trained Machine Learning Model to a DialogFlow Chatbot

Learn how to build, train, and store a Machine Learning model. Use Google’s Dialogflow to build a chatbot that uses the trained custom ML model to answer user queries.

Swati Rajwal
Towards Data Science

--

Image by Mohamed Hassan from Pixabay

Problem Statement

We will first create a basic Machine Learning (ML) model which will be trained on a dataset. The trained model will be saved using the pickle module. Thereafter, a flask application will utilize the trained model and answer queries like what is the per capita income in a year based on past data.

You can also see the recorded session here: Link

Installations:

This app is Python based on a Windows 10 machine. I have used chrome as the default browser, and VS Code for editing the code. You also need to install these python packages: flask, pickle, json, numpy, pandas, and sklearn.

Dataset

For the purpose of demonstration, the Canada Per Capita Income Single variable data set available on Kaggle is used. The dataset has only two attributes “year” and “per capita income (US$)”.

Machine Learning Model

Please note that at the moment the focus is not on building an accurate model. This blog shows how to utilize a trained model to answer user queries via a chatbot (Dialogflow).

Download the above-mentioned dataset and use the following code to create a model:

Image by Author

The next step is to save the above-trained model. Again, we are not concerned about prediction accuracy at all. Here end-to-end project integration is demonstrated.

Using the below lines of code you can store the trained model (linearmodel.pkl in our example) and it is now ready to be consumed in any application independently.

Image by Author

After the above code executes, a ‘linearmodel.pkl’ file will be created in the project directory.

Flask Application

  1. Let’s create a directory (‘demo’). Move the linearmodel.pkl in this directory
  2. In the same directory create a file, let’s say ‘app.py’.
  3. You can use your favorite IDE for editing the code. I am using VS Code.
  4. The app.py will contain the code as below. It is alright if you do not understand the code straight away. Just follow along for more explaination.
Image by Author

Execute the Application- Locally

  1. Inside the project directory, run the following command: “python app.py”
  2. If no errors, you should be able to see the application on localhost
  3. But you can only see “Hello World. I am Swati”.
  4. In the next section, we are going to use ngrok to create a public HTTPS URL for our application running locally.

Execute the Application- Publically

  1. Download ngrok
  2. Extract and open the folder to find the ‘ngrok.exe’ file
  3. Run this exe file and type “ngrok http 5000”
  4. Copy the following marked URL and paste it on your browser and Voilà! Your app is publically available (but for 2 hours only).
Figure 1: ngrok cmd (Image by Author)

DialogFlow Chatbot

  1. Go to Dialogflow and create an account.
  2. Follow the steps below to set up an agent and a very simple follow up based intent:
Gif 1: Creating Dialogflow Chatbot intents (Image by Author)

Once you are done, your bot is able to greet the user and ask for a query. At this point, it will not be able to make any predictions. Let’s see the next sections!

DialogFlow Chatbot with fulfillment URL

Let us now take a step back and understand how Dialogflow works behind the scene. The diagram below shows how the various elements are linked with each other to serve user queries.

Figure 2: Typical Architecture for the current project (Image by Author)

A user makes a request like “Tell me per capita income in the year 1972”. The query is internally converted into a request body as shown below:

{
"responseId": "xyz-unique-id",
"queryResult": {
"queryText": "Tell me per capita income in the year 1972",
"action": "DefaultWelcomeIntent.DefaultWelcomeIntent-yes",
"parameters": {
"yearinput": 1972
},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
..........
..........
..........
}

The above request body is sent to the fulfillment URL via an HTTP POST request if specified. The app.py is the application that handles the incoming requests and uses the trained ML model to make predictions or computations. The response is then sent back in the following form:

Figure 3: Response sent by the app i.e., the response of fulfillment URL (Image by Author)

Fulfillment URL

  1. Go back to the DialogFlow console and open the action we had created
  2. In the DataYear follow-up intent, under ‘fulfillment’ enable ‘webhook call for this intent’, as shown in the Gif below.
  3. Click on ‘Fulfillment’ on the left side navigation pane
  4. Enable the webhook option
  5. Paste the ngrok URL and append ‘/webhook’ at the end and click save:
Gif 2: Adding ngrok URL in the application and testing in real-time (Image by Author)

Testing the chatbot:

The easiest way is the testing pane on the right side of the Dialogflow console. I personally love this feature as it allows you to instantly test your chatbot!

Dialogflow chatbots can be integrated with many platforms like slack, Telegram, Messenger, line, and others. Below I have shown the web-demo integration.

Gif 3: Web demo of Chatbot (Image by Author)

Conclusion

That was a long article! Nevertheless, I sincerely hope that you find it helpful. The integration shown in this article is one of the many possibilities. As a next step, try to create more complex chatbots and deploy the app on some cloud application platform like Heroku or Azure web app and then integrate with Dialogflow.

Also, you can ask me a question on Twitter and LinkedIn!

References

[1] Dialogflow Documentation |. (n.d.). Google Cloud. Retrieved September 16, 2021, from https://cloud.google.com/dialogflow/docs

[2] Getting Started with Dialogflow. (2019, February 5). YouTube. https://www.youtube.com/watch?v=Ov3CDTxZRQc&ab_channel=GoogleCloudTech

--

--