How To — Questions Generation using Transformers 🤗

Nicola Gheza
2 min readJan 12, 2022

--

Introduction

Question generation is the task of automatically generating questions from a text paragraph.

In this tutorial, you will learn how to use Huggingface library and Suraj Patil’s transformers model to implement your own question generating applications.

Install Dependencies

pip install transformers==3.0.0
pip install nltk
python -m nltk.downloader punkt

Then, clone the repository from Suraj Patil and cd into it.

git clone https://github.com/patil-suraj/question_generation.git
cd question_generation

Let’s code

Create a new python file in the question_generation folder and copy the following code

from pipelines import pipelinenlp = pipeline("e2e-qg")text = "Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum \
and first released in 1991, Python's design philosophy emphasizes code \
readability with its notable use of significant whitespace."
response = nlp(text)print("Input Text ==>", text)
print("Generated Questions ==>", response)

These few lines of code will be enough to start generating questions. Here are the generated questions you should get using the same input text:

Generated Questions ➡️ [‘Who created Python?’, ‘When was Python first released?’, “What is Python’s design philosophy?”]

Now using gradio we will build a friendly web interface to demo this machine learning model.

Make sure to install the gradio package: pip install gradio

Then with the following code we will build a small webapp

from pipelines import pipeline
import gradio as gr
nlp = pipeline("e2e-qg")def generate(input):
questions = nlp(input)
return questions
iface = gr.Interface(
fn=generate,
inputs=gr.inputs.Textbox(lines=2, placeholder="Input Text Here..."),
outputs="text")
iface.launch()

After launching this program the web app will be running on local URL: http://127.0.0.1:7860/ Open it up and test it with any text paragraph

Conclusion

In this tutorial we have seen how to use Huggingface library and Suraj Patil’s transformer model to build a tiny web app capable of generating questions from a text paragraph. Let me know in the comments if you enjoyed the tutorial and if you have any questions or requests.

--

--

Nicola Gheza
Nicola Gheza

No responses yet