VOOZH about

URL: https://www.geeksforgeeks.org/nlp/how-to-use-hugging-face-model-for-question-answering/

⇱ How to Use Hugging Face Model for Question Answering - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Use Hugging Face Model for Question Answering

Last Updated : 14 Apr, 2026

Using models from Hugging Face for question answering allows developers to build systems that can automatically extract answers from a given context. These pre-trained transformer models make it easy to implement NLP applications such as chatbots, document search and knowledge‑based QA systems..

Step 1: Set Up the Environment

  • First, install the required libraries. Run the following command in your command prompt.
  • This installs the Transformers library

pip install transformers torch

Step 2: Import Required Libraries

Import the pipeline from Transformers, as it provides a high-level interface that automatically manages tokenisation, model loading, inference and output formatting in a single streamlined workflow.

Step 3: Initialise the Question Answering Pipeline

Initialise the question answering pipeline by specifying the task and loading a pre-trained model (distilbert-base-cased-distilled-squad), which is already fine-tuned on the SQuAD dataset for answering questions from text.

Output:

👁 Hugging-face
Loading the pre trained model

Step 4: Define Context and Question

Create a context and a question. The model searches the context to find the answer. Before answering, it performs tokenization:

  • Splits text into small tokens
  • Analyzes relationships between them
  • This helps the model understand the text and extract the correct answer.

Step 5: Run the Model

Pass the question and context into the pipeline to generate the answer. Internally, the model:

  • Tokenizes the input text
  • Predicts the start and end positions of the answer
  • Extracts the exact answer span from the context

Step 6: Display the Output

  • score : Confidence level of the prediction
  • start : Starting position of the answer in the text
  • end : Ending position of the answer
  • answer : Extracted response from the context

Output:

{'score': 0.2661724090576172, 'start': 42, 'end': 114, 'answer': 'a wealth of resources for computer science enthusiasts and professionals'}

You can download the full code from here

Comment
Article Tags:

Explore