AI bot that could answer your questions 
Tags: python, machinelearning, tutorial, beginners
Published: 2021-11-17 11:44:54

Hi, I am Yash Makan and am back with another cool episode. Today as I was reading my school textbooks a thought came to my mind that wouldn't that be cool if python program can learn all the questions from my textbook. So in today's blog, we are developing a question answering bot. What this means is that you can feed this bot some data and ask questions from the textbook and the bot will give answers to each question pretty precisely.

![answer the questions](https://media0.giphy.com/media/3ogwG2KguRnvcg88KY/giphy.gif?cid=ecf05e4716o3383s63wehn2lu8bgo4z19ikmpml3ololdsuo&rid=giphy.gif&ct=g)

## Introduction

Here we will use a library known as [transformers](https://pypi.org/project/transformers/). This library is made by `huggingface` and has a lot of models available for different tasks. This library is my favourite and you should check other models out as well. Each model will have a tokenizer which will be in charge of preparing the inputs for a model. This means that whenever you are generating the output you must pass the model and tokenizer. There is also an easy way which is using `pipeline()`.  These pipelines are objects that abstract most of the complex code from the library. In this tutorial, we are using the pipeline. For example, in this blog, we are using the `question-answering` pipeline. This will clear once we code.

## Installation

`pip install transformers`

## Let's Start Small

![source code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53b0b5z4frqusv78dyxv.png)

The code is pretty straightforward. We are first importing pipeline from transformers. Then we are initializing the question_answering pipeline. After that, we input the question and get the result by passing the question and context in the question_answering pipeline. Here the context is the paragraph or the initial text from which the bot finds the answers. At last, the result is printed.

You see the more you provide the context, the better and widely the bot will return the result.

## what else? 🧑🏼

So you see the power with just a few lines of code we achieved a bot that can learn stuff very easily, more accurately and a lot faster than us. But you must be thinking about what can you do to make this better. And the answer is to provide more context. Here are some ideas for this project:

- Scrape [Wikipedia Articles](https://pypi.org/project/wikipedia/) and feed the data in the script. For example, scrape all the links related to history from Wikipedia(as much as you can) and then scrape the text from each page and put it together. This will create a bot that will know a lot about history I mean a lot...
- Are you a college student? If yes then you can feed your cs book [as pdf](https://pypi.org/project/pdftotext/) in the script and ask questions. The program will be able to answer your questions. 👨🏼‍🎓

## Conclusion

So you see it's that easy to make a question answering bot. I really hope that you liked this post. If yes then do hit the like button and don't forget to bookmark the post as well. I hope that this post is worth sharing with everyone. If you have any doubts about anything to add then you are welcome to add the message in the discussion below. Will meet with you guys next time till then b-bye!

## More Articles by Author

- [Generate your own favourite TV episode using python](https://dev.to/yash_makan/generate-your-own-favourite-tv-episode-using-python-1pb3)

## My Socials

- [Twitter](https://twitter.com/Yash_Makan)