How to build and deploy a Question-Answering AI web app.

Christian Ajulor
3 min readMar 19, 2022

Building a Question-Answer bot with pre-trained model from Hugging face and deploying using Gradio.

Image credit: Google photos

In this article, I’m going to show how to build a simple question-answering bot in python using pre-trained model from hugging face and deploying it as a web app using Gradio.

Here is the Github Repository containing the codes.

What is a Question-Answering Model?

According to wikipedia, Question answering (QA) is a computer science discipline within the fields of information retrieval and natural language processing (NLP), which is concerned with building systems that automatically answer questions posed by humans in a natural language.

Question-Answering Models are machine or deep learning models that can answer questions given some context, and sometimes without any context (e.g. open-domain QA). They can extract answer phrases from paragraphs, paraphrase the answer generatively, or choose one option out of a list of given options, and so on.

Let’s get straight to building

Stack: Tensorflow, Transformers, Huggingface, Gradio, huggingface spaces.

Install and import dependencies

To begin, we are going to install the needed dependencies

!pip install tensorflow
!pip install transformers

importing

import tensorflow as tf
import transformers
from
transformers import pipeline

Import Model

Here, we are going to import and download the pre-trained Question-answering model from Hugging Face. First, we are going to import the model class and the tokenizer.

from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering

Now we are going to install and setup the model and the tokenizer.

model = TFAutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad",return_dict=False)tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)

Above we got the name of the model in Hugging face which is the “bert-large-uncased-whole-word-masking-finetuned-squad”

Next, we instantiated the tokenizer and the model itself on the model name.

Testing the model

Deploying The Model as a Web App using Gradio

Here we are going to deploy our model using Gradio.

What is Gradio?

Gradio is a GUI library that allows you to create customizable GUI components for your Machine Learning model. To know more about Gradio u can check the Gradio website

Installing Gradio

!pip install gradio

Import

import gradio as gr

Creating the function for Gradio

To understand why we have to write a function, you must first understand that gradio builds GUI components for our Machine Learning model based on the function. The function provides a way for gradio to get input from users and pass it on to the ML model which will then process it and then pass it back to gradio which then passes the result out.

# creating the function
def func(context, question):
result = nlp(question = question, context=context)
return result['answer']

Now we are going to create our Web App interface using Gradio

# creating the interface
app = gr.Interface(fn=func, inputs = ['textbox', 'text'], outputs = 'textbox', title = 'Question Answering bot', theme = 'dark-grass', description = 'Input context and question, then get answers!')

launching the app

# launching the app
app.launch(inline=False)
The app interface

This is the link to the app hosted on hugging face spaces, Q-A bot

Conclusion

In this article, we have been able to build a natural language processing question-answer model using a pre-trained model from huggingface and deployed the model as a web app using Gradio.

--

--