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.
Here we will use a library known as 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.
pip install transformers
Let's Start Small
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 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 in the script and ask questions. The program will be able to answer your questions. 👨🏼🎓
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!