AWS Marketplace

Building an Artificial Intelligence system for real-time text message classification and learning

by guest author Ievgen Sliusar, Assistant Professor, Ph.D.

In this blog post, I  demonstrate how to build an Artificial Intelligence (AI) system for real-time text message classification and learning using Dynamic AI56. This model package is available in AWS Marketplace.

With conventional machine learning (ML), you first train your model and then deploy it to get inferences on your data. Dynamic AI combines these two tasks in a single running image. Therefore, you can get inferences for text data and do training at the same time.

Prerequisites

  • An active AWS account
  • Basic Python skills
  • Basic understanding of ML technology

You will be using Jupyter Notebook technology in this tutorial. The notebook is the interactive, web-based front end for the Python backend, also called the kernel. The backend kernel processes all computations, and the results are visualized in real time at the notebook sections called cells. To run a cell, you can just put a cursor inside and press Shift+Enter. You can save notebooks to files and load into Jupyter when needed.

How to build an ML system for real-time text message classification and learning

Step 1: Prepare the notebook

1.1 Create a notebook instance

Open the Amazon SageMaker console. On the left menu pane, find the Notebook section and select Notebook instances. At the top right corner, select Create a new Notebook instance. The instance creation parameters page will open. Enter the desired name for the instance, such as dynamic-ai-demo. Leave the other settings as the defaults.

Scroll down the page to the Git repositories section. Select to expand it. I prepared a demo notebook of examples for Amazon SageMaker with a helper module for you to use. For your convenience, you can just clone this repository when creating a notebook. To do that, under the Default repository subsection of Git repositories, select the arrow on the right to change the Repository option from None to Clone a public Git repository to this notebook instance only. The Repository URL field will appear. Paste the following Git repository address into the field: https://github.com/Dynamic-AI/aws-sagemaker-examples.git. See the following screenshot.

git respository screenshot

At the bottom of the page, click Create notebook instance. The instance list will appear. Find your newly-created instance in the table. Wait a few minutes for your instance status to change from Pending to InService. Refresh the page until this instance shows as InService.

On the Notebook instances page, under the Actions column, select Open Jupyter. This launches a new browser tab. Wait a few seconds until you see a file tree in the Jupyter tab. Navigate to src folder and then to v1 folder. Select the DynamicAI Demo.ipynb file to open the notebook in a new browser tab.

1.2. Deploy Dynamic AI model on an Amazon SageMaker endpoint

In order to complete this tutorial, you have to subscribe to the Dynamic AI ML Model product at the AWS Marketplace. To do so, follow this link to the Dynamic AI Demo Model product page. The product has a 60-day free trial during which it will not incur any software costs. At the top right, select Continue to subscribe. Select Accept Offer.

Now return to the Jupyter Notebook tab you opened in step 1.1. Refer to the following screenshot.

Jupyter screenshot

This cell first calls get_model_package_arn() function from the helper module dynamic_ai.py, which is located in the same folder. It is used to determine the Model Package ARN of Dynamic AI Demo product for your current AWS Region. Then there is a call to deploy_model() function which does all the work to make the model up and running. The function takes two parameters:

  • model_arn – the product identifier you’ve just obtained
  • instance_type – this is the instance type to deploy the model to. Just keep default type of “ml.m5.4xlarge”.

Start this cell by putting cursor into it and pressing Shift+Enter. The operation may take a few minutes to complete. The following image shows the first cell with the previous parameters entered after completion.

first cell with previous parameters

1.3. Confirm that Dynamic AI is running

To confirm that the Dynamic AI system is up and running, start the next by pressing Shift+Enter in it. For more details about working with cells, see the Jupyter tutorial linked in the Prerequisites section. The following image shows that successful function and the positive result True under Out [2].

positive result True Under Out

If your result is not True, just wait a minute and re-run the cell until you receive a result of True.

Step 2: Add your messages

2.1. Feeding messages to the system

To add messages, use the add_message() function. This function takes a piece of text as an argument and returns a unique ID that can be used in later calls as a reference. In Jupyter, I added “Very simple test message” by entering that text into the first line of the next cell after the Adding a message heading. It returned a 22-character alphanumeric unique ID in the output field, as shown in the following screenshot.

unique message ID

You can also add multiple messages in a bunch by invoking add_message() function multiple times, passing different text. I did this in the next cell. I added “Another simple test message”, “And one more simple test message”, and “Last simple test message” in the Python code field of the cell. Refer to the following screenshot.

multiple messages

2.2. Checking your messages

To verify that your messages were added to the system, use the list_messages() function. It will give you back a dictionary with IDs and corresponding texts of added messages.

To do this, I entered dynai.list_message() into the next cell Python code field and then selected the start icon on the left. I received a list of the four messages I entered in the previous steps under Out[5], as shown in the following screenshot.

list messages screenshot

Please note that message text is stored only on the client side and will be erased if you restart the Jupyter kernel.

Congratulations! You have successfully added text data to the Dynamic AI system. Now, you can do something useful with it.

Step 3: Add and detect categories in your dataset

Organizing known messages into categories enables category predictions for new, unknown incoming texts previously unseen by the system. In this step, I show how to use the add_message() and set_category() functions to create a simple data set consisting of a single category with four messages. One of the distinctive features of the Dynamic AI system is ability to work even with such a small message set.

3.1. Using the set_category() function

To mark a message as a member of particular user-defined category, use the set_category() function. This function takes two parameters. The first is message ID, and the second is an arbitrary category name. These will serve as inputs for the Dynamic AI classifier. To use the set_category() function, first set a category for all the text messages you previously added in Step 2. Add your messages as shown in the next cell. The function should return True, indicating success.

To do the same with the other messages, in the next cell I did this in a batch.

The following screenshot shows that successful function and repeating it with the other three sample messages.

successful set category function and repeat

3.2. Using the predict_category() function

To use the Dynamic AI system to predict category for a previously-unknown message, you use two functions. First, use the add_message() function to add a new message. Then use the predict_category() function to predict its category based on the training you did in the previous section, Using the set_category() function.

When you invoke predict_category() function, it returns the following values:

  • category – the predicted category name
  • accuracy – the estimated accuracy of the prediction
  • is_approved – a boolean flag; True means that the system’s prediction is verified and reliable (up to estimated accuracy). Following is a brief explanation .

In the next cell, you can see the code which adds a new text and making prediction of a category instead of setting it. Refer to the following screenshot.

category prediction

This last flag (is_approved) is a unique feature of the Dynamic AI, which shows how confident the system is in the accuracy score it has given. It is based on semantic similarity analysis provided by Dynamic AI’s patented technology and depends on previous input and classifications made by the system. If you try the same method on completely different message, you get a different result.

For example, in the next cell, I entered dynai.add_message(‘Hey, here I am – a completely different message’) and got the result (‘category’: None, ‘accuracy’:0.0, ‘is_approved’: False). The category result of None indicates that this message does not belong to any known category, which is correct. See the following screenshot.

unrelated message category result

Step 4: Working with large datasets

If you have large datasets, a best practice is to automate the process to save time and resources. Here is how to do that using Dynamic AI and a sample dataset.

For this tutorial, I’m using my sample dataset in GitHub. This is a complex usage example involving rent-a-car-corpus-small corpus. It is plain text file that contains one message per line, together with corresponding categories. It has three categories and contains about 100 messages. You can also run your own trial with my full rent-a-car-corpus with seven categories and more messages.

4.1. Importing a sample dataset

Following the Complex classification example heading, there is an example code that demonstrates loading the dataset. This cell first reads the input corpus file and then adds messages labeled with corresponding categories to the system. Corpus file is read line by line and then fed to the system. This operation will take up to one hour, which is a limitation of the demo model. See the following screenshot showing the outcome of running the cell.

dataset outcome

4.2. Testing the classification system

The sample dataset has three categories:

  • Accident – everything about accidents
  • Cancel – cancellation requests and questions about company’s cancellation policy
  • Invoice – invoice-related questions and requests

To make a test, I just add new messages to the system and analyze the categories the classification function returns. To do this, in the next cell, follow the instructions from the Using the predict_category() function from step 3.2. A value of True for is_approved response field indicates an accurate categorization.

I tested it by entering three messages. In the next few cells, I did that for two phrases – ‘Dear Rent-A-Car company, I have lost my invoice, can you send it to me one more time?’, ‘Hello! My plans have changed, so can you cancel my reservation please?’ and got the correct results, as shown on the next screenshot.

result of test message categorization

These two phrases were successfully classified by the system as belonging to Invoice and Cancel categories, which is correct.

To see the result of adding a message unrelated to the three previously defined categories, you do the following. In next cell, use the same construct but for a sentence completely unrelated to the categories known by the system. I entered the phrase ‘Hello, can you please sell me a cow?’. You should receive the result stating that this message does not belong to any of the available categories, as shown in the following screenshot.

unrelated message category result

In this step, I showed how to use a pre-defined categorized corpus to kickoff the Dynamic AI message classification and to test results for new incoming messages. Your next step is to try this using your own corpus and categories to see how it works for your business case.

Cleanup

To clean up after this demo, you have to delete the model endpoint and notebook instance in the Amazon SageMaker. Go to the last cell of the notebook and run it by pressing Shift+Enter. It will call the shutdown() helper function, which takes care of terminating the model endpoint. Refer to the following screenshot.

shutdown function

Then, close all Jupyter tabs you have opened, one for the notebook and other for the file navigator. Now navigate back to the Amazon SageMaker console and click Notebook instances at the left menu panel. Find your notebook instance in the table and select Stop from the Actions drop-down at the top right corner. After the notebook instance has been stopped, select Delete from that same Actions drop-down.

Also, it’s a good idea to manually check models and endpoints in the Amazon SageMaker console and delete ones that are not used to ensure that your account won’t be charged at the end of the trial period.

Next Steps

More details and links to probable use cases are available on the product page in AWS marketplace. A video version of this tutorial is available on YouTube, and further information on integration options for SAP, Salesforce, and Microsoft Dynamics can be found here.

The code and the dataset we used in this tutorial are available on GitHub. Feel free to play with it and use it to start building an application for your use case.

Conclusion

In this blog post, I showed how to use the Dynamic AI56 product from Amazon SageMaker Marketplace to build an AI system for real-time text message classification and learning. I showed how you can mix adding and classifying messages within the same session. This capability enables you to extend your categories in real time and re-map messages to different categories on the fly.

The content and opinions in this post are those of the third-party author, and AWS is not responsible for the content or accuracy of this post.