BOOK DEMO
FREE TRIAL
Apr 15, 2020
24 min

Build your TensorFlow Custom Module

written by: Lindsay WIlliams

Creating a good conversational AI might make you realize how complex the idea of successful conversation is. You will probably have to deal with problems that have never crossed your mind and look for possible solutions to assure that the conversation seems natural.  


Le
t’s imagine that you have created a virtual agent which answers any product related question perfectly but can’t react naturally to other statements. Take insults for example: for some reason, people tend to test virtual agents with offensive comments, maybe with the intention of verifying if the response is “human” enough. Wouldn’t it be great if your AI could detect abusive language and respond appropriately? But as we know, for an AI to learn properly we need to feed it enough data and write the correct machine learning algorithms.  

How can you manage that with Cognigy.AI? 

In this situation, TensorFlow is your best friend. This well-known deep learning library has kindly arranged a strategy to use its models in JavaScript. At the TensorFlow.js.org website you can find many existing models, which are converted and ready to use in any JavaScript application after installation. Luckily, there is even a TensorFlow.js model which detects toxic language! Now we can use that in order to handle our insulting-language-problem. 

How to create the module 

First, install the model: 

  • npm install @tensorflow/tfjs @tensorflow-models/toxicity  

Just like that, you are ready to create your toxicity detection Custom Module! You can learn how to create your own Custom Module in the docs 

Let’s start with the code. The first thing you need to do in your code file is import the TensorFlow.js model that you installed earlier.  

 

 

Afterwards, create an async function, as described in docs. In your function, apply methods from your model, following its documentation. For example, to save the toxicity model to a constant and use it on the input text, you would write:  

 

 

Now you are free to apply any changes to improve the model before saving the results. In the example, the results of the toxicity analysis will be written in Cognigy Context or Input Object: 

 

 

When your Custom Module is ready, you can upload it and add it to your Flow: 

 

You will see the analysis’ results in the Interaction Panel: 

 

 

 

Then, you can detect toxic language using the Logic node. For example, to identify an insult you could use an If node:  

 

Now you just need to add the appropriate reaction and test your AI with a new skill!  

 

 

The practicality of TensorFlow.js doesn’t end here. It is possible to convert any TensorFlow SavedModelmost TensorFlow Hub modulesKeras HDF5 or tf.keras SavedModel into TensorFlow.js with provided tfjs-converter. Just follow the steps explained here 

Explore More Topics

Ready to brush up on something new? We've got more to read right this way.