Bird
Raised Fist0
NlpHow-ToBeginner · 3 min read

How to Do Question Answering in Python for NLP

You can do question answering in Python using the transformers library from Hugging Face, which provides pre-trained models like distilbert-base-uncased-distilled-squad. Use the pipeline API with task set to question-answering to input a question and context, and get the answer directly.
📐

Syntax

Use the pipeline function from the transformers library with the task set to question-answering. Provide a question string and a context string to get the answer.

  • pipeline('question-answering'): Creates a question answering model pipeline.
  • question: The question you want to ask.
  • context: The text containing the answer.
python
from transformers import pipeline

qa_pipeline = pipeline('question-answering')

result = qa_pipeline({
    'question': 'Your question here?',
    'context': 'The context text where the answer is found.'
})

print(result)
💻

Example

This example shows how to ask a question about a given context using a pre-trained model. The model finds the answer span in the context and returns it with a confidence score.

python
from transformers import pipeline

# Create a question answering pipeline
qa_pipeline = pipeline('question-answering')

# Define question and context
question = 'What is the capital of France?'
context = 'France is a country in Europe. Its capital is Paris, known for the Eiffel Tower.'

# Get answer
result = qa_pipeline({
    'question': question,
    'context': context
})

print(f"Answer: {result['answer']}")
print(f"Score: {result['score']:.4f}")
Output
Answer: Paris Score: 0.9785
⚠️

Common Pitfalls

  • Not installing the transformers library or missing dependencies like torch.
  • Passing empty or very short context that does not contain the answer.
  • Using a question that is too vague or unrelated to the context.
  • Ignoring the model's confidence score which indicates answer reliability.
python
from transformers import pipeline

# Wrong: Empty context
qa_pipeline = pipeline('question-answering')
result_wrong = qa_pipeline({'question': 'Where is the Eiffel Tower?', 'context': ''})
print(f"Wrong answer: {result_wrong['answer']}")

# Right: Provide proper context
context = 'The Eiffel Tower is located in Paris, France.'
result_right = qa_pipeline({'question': 'Where is the Eiffel Tower?', 'context': context})
print(f"Right answer: {result_right['answer']}")
Output
Wrong answer: Right answer: Paris
📊

Quick Reference

StepDescription
Install transformerspip install transformers torch
Import pipelinefrom transformers import pipeline
Create QA pipelineqa_pipeline = pipeline('question-answering')
Prepare inputsquestion (str), context (str)
Get answerresult = qa_pipeline({'question': question, 'context': context})
Read outputresult['answer'] and result['score']

Key Takeaways

Use Hugging Face transformers pipeline with task 'question-answering' for easy QA in Python.
Provide both a clear question and relevant context text for accurate answers.
Check the confidence score to understand how reliable the answer is.
Always install required libraries like transformers and torch before running code.
Avoid empty or unrelated context to prevent incorrect or empty answers.