Build an NLP extractive Question-Answer web application

Use Hugging Face transformers, Gradio to build an extractive question-answer web app and deploy it on Hugging Face spaces

Uppu Rajesh Kumar
5 min readApr 18, 2022
Photo by Andy Kelly on Unsplash

Introduction

A question-answer system is a type of technology that delivers the correct short answer to a question given the context. Customer support, addressing user inquiries on a website and retrieving material from a big corpus of knowledge all benefit from these systems.

Recent advances in Natural Language Processing (NLP) made these systems available to us for industry use. In this article, we’ll look at how to develop a question-answering system with hugging face transformers, gradio, and deploy it on hugging face spaces in this article.

Overview

  1. Gradio
  2. Hugging face Spaces
  3. Building Application
  4. Deployment
  5. Conclusion

Gradio

Gradio is an open-source Python framework that allows us to quickly prototype Machine Learning models using web interfaces. Gradio has been popular in recent years, and machine learning practitioners use it. Gradio was recently acquired by the hugging face company. Gradio allows us to quickly produce a demo of machine learning models in action. It is incredibly simple to code, as an interface can be created with only a few lines of code.

Gradio is a low code python framework to convert machine learning models into web apps
Image source: Gradio

Hugging face Spaces

Hugging face spaces is a free platform where we can host our machine learning web apps built with streamlit and gradio and demonstrate how our model performs in practice. It’s simple to set up a platform that’s also incredibly stable. We can design and deploy as many applications as we desire. We can also write code on the same platform rather than on a separate machine. We will write code on this platform and host our application on the same platform in this project.

Image source: Spaces — Hugging Face

Building the Application

To build this application we will use the hugging face transformers library and gradio library. We will use the ‘pipeline’ module of the transformers library. This ‘pipeline’ module has several functions for sentiment analysis, text summarization, etc. You can find more about the hugging face transformers library in their official transformers documentation.

We will use the ‘question-answering’ function of the pipeline module in our application. This one takes a context text and a corresponding question as an input and outputs the answer to that question along with a confidence score, start and end values which are the positions of the extracted answer in the text. So, let’s code our application by installing the necessary libraries using the pip command.

pip install transformers
pip install gradio

Now open your code editor and create an ‘app.py’ file. Let's import the libraries as shown below

import gradio as gr
from transformers import pipeline

Now that we imported the libraries let’s create an instance of a question-answering pipeline and also let’s give a title for our app, an example question, and context as shown below...

question_answerer = pipeline("question-answering")title = 'Question Answering demo with transformers and gradio'context = "The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname, and French Guiana. States or departments in four nations contain 'Amazonas' in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."question = "Which name is also used to describe the Amazon rainforest in English?"

Now, all we need to create is an interface for our app using gradio. We do that as follows…

interface = gr.Interface.from_pipeline(question_answerer,
title = title,
theme = "peach",
examples = [[context, question]]).launch()

Gradio creates an interface using an ‘Interface’ object. Since we are using a transformers pipeline we use ‘from_pipeline’ to load the pipeline instance we created. We give the title and set a theme as shown above. We also set the examples using the context and question we created as shown above. The entire code of the above steps can be seen below…

Save the file and let’s deploy it on hugging face spaces.

Deployment

We created our app file. It’s time to put it to work with hugging face spaces. Create a user account on the hugging face spaces website. Click ‘create space’ after you’ve created an account. Following that, you’ll see screens asking for names for your app and tech stack. Give the project a name, choose a license, select ‘Gradio’ from the SDK drop-down menu, and then click ‘Create’.

After that, you’ll see a page with instructions for cloning and pushing your GitHub repo to spaces. You can also create a repository within a space. Create a file called ’requirements.txt’ in this folder. In the requirements text file, paste the following material...

tensorflow
transformers

Also, upload the ‘app.py’ file we created in the previous step. Now hugging face spaces will take care of the rest and deploys our application. Our application looks like the below while working…

Image by author

We can see that our application not only gives the answer to our question but also the confidence score. Please find the application I built and deployed on hugging face spaces in the below link…

Question Answering Gradio — a Hugging Face Space by rajesh1729

Conclusion

We created an extractive question-answering application for hugging face spaces and launched it. The Gradio library, which is a low-code tool that allows us to create a demo of our machine learning models in action with a user-friendly web interface, made this simple. As a result, we may use hugging face transformers and gradio to create NLP web apps in the manner described in this article.

--

--

Uppu Rajesh Kumar

I am a data science technical writer. I explore new AI tools and I write about them.