⚡️ Saturday AI Sparks 🤖 - 📄🤖 Question Answering from Text


Description:

Introduction

One of the most exciting capabilities of modern NLP is Question Answering (QA).

Instead of training a model from scratch, we can use pre-trained models to answer questions directly from a text passage or document.

In this post, we’ll use Hugging Face’s transformers library to set up a question-answering pipeline, feed it a passage of text, and ask natural questions.


Why Question Answering?

  • Practical use cases → document search, FAQ systems, educational tools, personal assistants.
  • Pre-trained models → already fine-tuned on QA datasets (like SQuAD).
  • No training needed → just load a pipeline and run.
  • Fast to implement → works in just a few lines of Python.

Loading the Text Document

We need some text to work with. This could be loaded from a .txt file, a PDF, or even pasted directly into your script.

For example:

context = """
Python is a powerful programming language widely used for AI and machine learning. 
It is popular because of its simple syntax, strong community support, and 
rich ecosystem of libraries like TensorFlow, PyTorch, and scikit-learn. 
Python’s versatility makes it suitable for web development, data science, automation, and more.
"""

Building the QA Pipeline

Hugging Face provides a question-answering pipeline that can be initialized with a pre-trained model such as distilbert-base-cased-distilled-squad.

from transformers import pipeline

qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")

Asking Questions

Once the pipeline is ready, you can provide a question and the context text, and the model will extract the answer.

questions = [
    "Why is Python popular?",
    "Which libraries are mentioned for machine learning?",
    "What are some applications of Python?"
]

for q in questions:
    result = qa_pipeline(question=q, context=context)
    print(f"Q: {q}")
    print(f"A: {result['answer']}\n")

Sample Output

Q: Why is Python popular?  
A: simple syntax, strong community support, and rich ecosystem of libraries  

Q: Which libraries are mentioned for machine learning?  
A: TensorFlow, PyTorch, and scikit-learn  

Q: What are some applications of Python?  
A: web development, data science, automation, and more

Key Takeaways

  • Hugging Face’s QA pipeline allows you to extract answers from text with minimal setup.
  • Works best for fact-based questions where the answer exists in the passage.
  • For large documents, you may need to split text into chunks before running QA.
  • A great starting point for chatbots, knowledge assistants, and educational tools.

Code Snippet:

from transformers import pipeline


context = """
Python is a powerful programming language widely used for AI and machine learning.
It is popular because of its simple syntax, strong community support, and 
rich ecosystem of libraries like TensorFlow, PyTorch, and scikit-learn.
Python’s versatility makes it suitable for web development, data science, automation, and more.
"""


qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")


questions = [
    "Why is Python popular?",
    "Which libraries are mentioned for machine learning?",
    "What are some applications of Python?"
]

for q in questions:
    result = qa_pipeline(question=q, context=context)
    print(f"Q: {q}")
    print(f"A: {result['answer']}\n")

Link copied!

Comments

Add Your Comment

Comment Added!