Simple and fast Question Answering system using HuggingFace DistilBERT — single & batch inference examples provided.
Question Answering systems have many use cases like automatically responding to a customer’s query by reading through the company’s documents and finding a perfect answer.
In this blog post, we will see how we can implement a state-of-the-art, super-fast, and lightweight question answering system using DistilBERT from Huggingface transformers library.
Input
The input will be a short paragraph what we call context, and a question –
Context :
The US has passed the peak on new coronavirus cases, President Donald Trump said and predicted that some states would reopen this month.The US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths, the highest for any country in the world.Question:
What was President Donald Trump's prediction?
Output
The output from our question-answering system will be an answer from the context paragraph as shown below —
Answer:
some states would reopen this month.
Let’s get started :
First, let’s see how we can answer a single question as shown above. Then we will see how we can leverage batch processing to answer multiple questions at once from the context.
Single Inference :
Here is the code to do single inference with DistilBERT :
The output will be :
Question What was President Donald Trump's prediction?Answer Tokens:
['some', 'states', 'would', 're', '##open', 'this', 'month']Answer : some states would reopen this month
Batch Inference :
Now let’s try to do the same with batch inference where we try to pass three questions and get answers for them as a batch –
Three questions are –
1. What was President Donald Trump's prediction?
2. How many deaths have been reported from the virus?
3. How many cases have been reported in the United States?
Here is the code to do batch inference with DistilBERT :
The output will be —
Context : The US has passed the peak on new coronavirus cases, President Donald Trump said and predicted that some states would reopen this month.The US has over 637,000 confirmed Covid-19 cases and over 30,826 deaths, the highest for any country in the world.Question: What was President Donald Trump's prediction?
Answer: some states would reopen this monthQuestion: How many deaths have been reported from the virus?
Answer: 30 , 826Question: How many cases have been reported in the United States?
Answer: over 637 , 000
If you are interested in automatic question generation from context rather than question answering , you can check out my various algorithms and open-sourced code in the links below –
- True or False Question Generation
- Multiple Choice Question Generation
- Generate pronoun questions for English language learning
- Grammar MCQ generation
Happy coding! If you have any questions or just wanted to say hi, you can reach out to me on ramsrigouthamg@gmail.com
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
移动交互设计精髓
班格 (Cameron Banga)、温霍尔德 (Josh Weinhold) / 傅小贞、张颖鋆 / 电子工业出版社 / 2015-4-1 / CNY 89.00
越来越多的人正涌入移动应用领域,而设计和体验将是移动应用成败的关键。作者通过上百款应用的设计实践,系统化地梳理了移动应用的设计方法论,在理解用户、跨平台和适配设计、移动组件应用、界面视觉感染力、简约设计等方面都进行了深入阐述。此外,作者还介绍了一些非常实用的移动设计工具,分享了设计师该如何与开发工程师协同工作,以及如何收集用户反馈、甄别版本迭代的更新需求等。 《移动交互设计精髓——设计完美的......一起来看看 《移动交互设计精髓》 这本书的介绍吧!