AW Dev Rethought

🕵️ Debugging is like being the detective in a crime movie where you are also the murderer - Filipe Fortes

🧠 AI with Python – ❓ Question Answering with Hugging Face


Description:

One of the most impressive capabilities of modern Large Language Models is their ability to answer questions by understanding the context of a document. Instead of simply generating text, these models can read a passage, identify the relevant information, and extract the most appropriate answer.

This capability powers many AI applications we use today—from customer support bots and document assistants to enterprise knowledge bases and search engines.

In this project, we’ll build a simple Question Answering (QA) application using the Hugging Face Transformers library.


What Is Question Answering?

Question Answering (QA) is a Natural Language Processing (NLP) task where a model answers a user’s question based on a given piece of text.

Unlike a chatbot that generates responses from its own knowledge, a QA model searches the provided context and extracts the most relevant answer.

For example:

Context

Machine Learning is a subset of Artificial Intelligence that enables systems to learn from data.

Question

What is Machine Learning?

Answer

A subset of Artificial Intelligence that enables systems to learn from data.

Notice that the answer comes directly from the context.


Creating a Question Answering Pipeline

Hugging Face makes Question Answering extremely simple using the pipeline() API.

from transformers import pipeline

qa_pipeline = pipeline(
    "question-answering"
)

When executed for the first time, Hugging Face automatically downloads a pre-trained QA model and tokeniser.

After that, they’re cached locally for future use.


Providing Context

The model requires some information before it can answer questions.

This information is called the context.

context = """
Artificial Intelligence is the simulation of human intelligence by machines.
Machine Learning is a subset of AI.
Deep Learning is a subset of Machine Learning.
"""

Think of the context as the document the AI is allowed to read.

The model cannot answer questions outside this information.


Asking a Question

Once the context is available, asking questions is straightforward.

question = (
    "What is Machine Learning?"
)

The question and context are then passed to the pipeline.

result = qa_pipeline(
    question=question,
    context=context
)

The model analyses both inputs and returns the most relevant answer.


Understanding the Output

The result typically contains:

  • the extracted answer
  • a confidence score
  • the answer position within the context

For example:

print(result["answer"])
print(result["score"])

The confidence score indicates how certain the model is about its prediction.

Higher scores generally suggest greater confidence.


Trying Multiple Questions

The same context can answer several different questions.

questions = [
    "What is AI?",
    "What is Deep Learning?",
    "What does Hugging Face provide?"
]

This demonstrates how a single document can serve as the basis for multiple queries.


How Question Answering Works

Behind the scenes, the workflow looks like this:

Question
      │
      ▼

Context Document
      │
      ▼

Tokenizer
      │
      ▼

Transformer Model
      │
      ▼

Extract Best Answer
      │
      ▼

Confidence Score

Unlike text generation models, QA models don’t create entirely new responses.

Instead, they locate the most relevant span of text within the supplied context.


Question Answering vs Text Generation

Although both use transformer models, they solve different problems.

Question Answering

  • Reads supplied context
  • Extracts an answer from that context
  • Cannot answer questions beyond the provided information

Text Generation

  • Predicts the next tokens
  • Generates entirely new text
  • Can continue prompts creatively

Understanding this distinction is important when choosing the right model for an application.


Where Question Answering Is Used

Question Answering models are widely used in:

  • document search systems
  • customer support assistants
  • enterprise knowledge bases
  • legal document analysis
  • healthcare information retrieval
  • educational learning platforms

Many Retrieval-Augmented Generation (RAG) systems also rely on question answering techniques after retrieving relevant documents.


Key Takeaways

  1. Question Answering models extract answers from a supplied context.
  2. Hugging Face provides pre-trained QA models through the pipeline() API.
  3. The model returns both an answer and a confidence score.
  4. QA models retrieve information rather than generating it from scratch.
  5. Question Answering is a foundational capability for document assistants and knowledge-based AI systems.

Conclusion

Question Answering showcases how transformer models can understand both a user’s question and a supporting document to retrieve precise information. This ability forms the foundation of many intelligent AI applications, from customer support assistants to enterprise search systems. By combining pre-trained models with Hugging Face’s simple pipeline interface, developers can build powerful question-answering applications with surprisingly little code.

This continues the LLM Foundations track in the AI with Python series. In the upcoming projects, we’ll build on these concepts as we explore embeddings, semantic search, and Retrieval-Augmented Generation (RAG), bringing us closer to building real-world AI assistants.


Code Snippet:

# =========================================================
# 📦 Install Required Libraries
# =========================================================

# Run this in terminal if not installed:
# pip install transformers torch


# =========================================================
# 📦 Import Required Libraries
# =========================================================

from transformers import pipeline


# =========================================================
# 🤖 Create Question Answering Pipeline
# =========================================================

qa_pipeline = pipeline(
    task="question-answering"
)


# =========================================================
# 📖 Create Context
# =========================================================

context = """
Artificial Intelligence is the simulation of human intelligence by machines.

Machine Learning is a subset of Artificial Intelligence that allows systems
to learn patterns from data and improve performance without being explicitly programmed.

Deep Learning is a subset of Machine Learning that uses neural networks with multiple layers.

Hugging Face Transformers is a Python library that provides pretrained transformer models
for tasks such as text generation, question answering, summarization, translation,
sentiment analysis, and named entity recognition.
"""


# =========================================================
# ❓ Ask a Single Question
# =========================================================

question = "What is Machine Learning?"

result = qa_pipeline(
    question=question,
    context=context
)


# =========================================================
# 📊 Display Single Answer
# =========================================================

print("=== Single Question Answering ===\n")

print("Question:")
print(question)

print("\nAnswer:")
print(result["answer"])

print("\nConfidence:")
print(round(result["score"], 4))


# =========================================================
# 🔄 Ask Multiple Questions
# =========================================================

questions = [
    "What is Artificial Intelligence?",
    "What is Machine Learning?",
    "What is Deep Learning?",
    "What does Hugging Face Transformers provide?",
    "Which tasks are supported by Hugging Face Transformers?"
]


print("\n\n=== Multiple Questions ===")

for q in questions:

    result = qa_pipeline(
        question=q,
        context=context
    )

    print("\n" + "=" * 60)

    print("Question:")
    print(q)

    print("\nAnswer:")
    print(result["answer"])

    print("\nConfidence:")
    print(round(result["score"], 4))


# =========================================================
# 🧪 Try Custom Context
# =========================================================

custom_context = """
Python is a popular programming language used in web development,
automation, data science, artificial intelligence, and backend engineering.
It is known for its simple syntax, large ecosystem, and strong community support.
"""


custom_question = "Why is Python popular?"

custom_result = qa_pipeline(
    question=custom_question,
    context=custom_context
)


print("\n\n=== Custom Context Example ===\n")

print("Question:")
print(custom_question)

print("\nAnswer:")
print(custom_result["answer"])

print("\nConfidence:")
print(round(custom_result["score"], 4))


# =========================================================
# ✅ Final Note
# =========================================================

print("\nQuestion Answering demo completed successfully.")

Link copied!

Comments

Add Your Comment

Comment Added!