0
0
NLPml~5 mins

QA with Hugging Face pipeline in NLP

Choose your learning style9 modes available
Introduction

QA with Hugging Face pipeline helps you quickly find answers to questions from a given text. It makes understanding text easy without writing complex code.

You want to find answers from a document or article quickly.
You need a simple way to build a question-answering feature in an app.
You want to test how well a model understands a paragraph.
You want to extract specific information from customer reviews or feedback.
Syntax
NLP
from transformers import pipeline

qa_pipeline = pipeline('question-answering')

result = qa_pipeline(question='Your question here', context='Your text here')

The pipeline function loads a ready-to-use model for question answering.

You provide a question and a context (text) where the answer is searched.

Examples
Simple example asking what AI means.
NLP
from transformers import pipeline

qa = pipeline('question-answering')

result = qa(question='What is AI?', context='AI means artificial intelligence.')
print(result)
Prints only the answer string from the result dictionary.
NLP
from transformers import pipeline

qa = pipeline('question-answering')

result = qa(question='Where is Paris?', context='Paris is the capital of France.')
print(result['answer'])
Sample Model

This program loads a QA model, asks when the Eiffel Tower was built, and prints the answer.

NLP
from transformers import pipeline

# Load the question-answering pipeline
qa_pipeline = pipeline('question-answering')

# Define context and question
context = "The Eiffel Tower is a famous landmark in Paris, France. It was built in 1889."
question = "When was the Eiffel Tower built?"

# Get answer
result = qa_pipeline(question=question, context=context)

# Print the full result
print(result)

# Print only the answer
print(f"Answer: {result['answer']}")
OutputSuccess
Important Notes

The score shows how confident the model is about the answer.

Context should be clear and contain the answer for best results.

You can use different models by passing the model parameter to pipeline.

Summary

QA pipeline finds answers from text easily.

Just give a question and context to get an answer.

Great for building simple question-answering tools quickly.