Building a NLP solution

Akoios
6 min readJul 22, 2021

--

Titan Tutorial #13: A NLP Toolkit using HuggingFace, Titan and No-Code tool.

Introduction

NLP (Natural Language Processing) is making an incredible progress through the application of advanced deep learning techniques. In particular, the advent of the Transformer architecture is paving the way towards the generalized use of NLP in all type of environments.

In this tutorial, we will cover for the first time the end-to-end process of a ML initiative, going from the development of the model to its consumption.

To this end, we will combine three building blocks:

  • HuggingFace: an open-source provider of Natural Language Processing (NLP) technologies.
  • Titan: Our MLOps tool to easily deploy ML models.
  • Wix: A low-code tool to create websites.

The main idea is to use readily available technologies to get a functional NLP product working from scratch using Titan. Before we start, it is convenient to take a brief look at the tools we will be using during the tutorial.

HuggingFace

Founded in 2016 in New York, HuggingFace offers NLP solutions. HuggingFace started as a chatbot application but it has become really relevant after releasing an open source library for NLP applications.

This library has been a huge success and now it is massively used. Moreover, their -also open-source- framework for Transformers is now being used for tasks like text-classification, information extraction, summarization, text-generation and conversational AI.

Apart from this, maybe the most important aspect of HuggingFace is the great community they have built which is constantly experimenting and training new NLP models.

Summarizing, HuggingFace offers out-of-the-box sophisticated NLP capabilities which can be used to build complex conversational interfaces.

WiX

Founded 10 years before, in 2006, Wix is an israeli company which offers a web-based platform to create HTML5 websites without requiring technical knowledge.

WiX has been constantly improving its capabilities and, as of today, it offers a wide-range of features such as e-commerce, social plugins, contact forms or the ability to connect with external APIs which we will show today.

Titan

In this tutorial, we will our tool to create and consume API services based on several of the main features offered by HuggingFace.

Even though HuggingFace already allows the deployment of models through their CLI (Command Line Interface),we will see how Titan, with its general purpose deployment capabilities, can be also easily used for this purpose in a very flexible way.

As we have seen in past tutorials, Titan can be used to deploy not only NLP models but all types of AI/ML you can think of.

Getting to work

Creating the services

As it was mentioned before, we will use ready-to-use HuggingFace capabilities to create the NLP models for this example.

HuggingFace provides a very powerful tool called pipelines in their transformers repository. Pipelines are a great way to use NLP models without having to deal with all the complexity of its training and design.

In general, each HuggingFace pipeline is made of:

  • A tokenizer to map the text into tokens
  • A model instance
  • Optional post-processing tools for the output

Using these pipelines could be as easy as it shown below in a Jupyter Notebook:

As usual, using Titan we can provision the required environment and hardware for this model in a markdown cell. In addition, the required dependencies have to be added to the requirements.txt file:

transformers==3.5

After that, we just need to import the pipelines from the transformers library to start using them. For this example, we will be using these three pipelines:

  • sentiment-analysis: To run a sentiment analysis (positive or negative) on an input.
  • question-answering: Provided a context and a question the model returns an answer to the question.
  • fill-mask (text generation): Given an input, this pipeline completes the sentence with several suggested options.

Once we have all the pipelines in the model, we just need to specify the required endpoints as we usually do in Titan.

# POST /question_answering# POST /sentiment_analysis# POST /text_generation

As usual, it is possible to create mock request in the Notebook so it can be tested locally. Regarding the parameters for the requests, the following approach has been used:

For the endpoints /question_answering and /text_generation, the input is passed using a query param:

args = {
“param”: [‘Such a nice weather outside !’]
}
REQUEST = json.dumps({ ‘args’: args })

For the /sentiment_analysis endpoint, the data is expected to arrive inside the body of the request:

body = {
'context':'Akoios is a Spanish company based in Madrid.' ,
'question': 'Where is Akoios based ?'
}

Once the model has been prepared, it can be deployed running:

$titan deploy

Creating a basic website

Once we have the services running, it is time to create a basic website to consume them. We will be using WiX for several reasons:

  • Drag and drop interface to create layouts
  • Integrated forms
  • Availability of a Javascript Fetch API which can be used to interact with REST APIs through HTTPS.

To create this basic website, we just need to register for a free WiX account and start working from a blank page or any of the pre-defined available templates. For our needs, we will just require three different forms apart from the desired titles and headlines:

Building a basic website with WiX

Once the layout is ready, we need to implement the logic to call the deployed endpoints APIs and to handle the responses. WiX can be considered as a low-code tool since it allows to create some more complex functionalities by adding some programming to the websites.

For this examples, we will need to edit two different files at Wix, one at the backend and one at the frontend:

  • Predictions.jsw: The backend file which will making the actual API calls and will return the results.
  • Home.jsw: The frontend file which gets the input data, calls the backend functions and shows the response in the specified fields.

Both files are shown in the following code snippets:

With all the code included, we can now test how everything works together. By publishing the created website with WiX, a page like this one will be generated:

https://javier869.wixsite.com/nlp-toolkit.

In that URL you can check the final result and tinker with the amusing NLP services of HuggingFace.

You can find all the code in this GitHub repository.

Wrap-up

In this post we have seen how to build an end-to-end NLP solution using COTS components provided by HuggingFace and Wix.

In spite of being a simple example, this tutorial illustrated how Titan can speed up the process to deliver real business value by using commercial and readily available tools and models.

And, of course, this same approach can be used to take advantage of all types of libraries not only in NLP but also in other fields like image recognition, predictions of any kind or automated classifications.

If you want to know more about how to start using Titan or getting a free demo, please visit our website or drop us a line at info@akoios.com.

If you prefer, you can schedule a meeting with us here.

Akoios: Frictionless solutions for modern data science.

--

--

No responses yet