Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pre-trained extractive QA model from Hugging Face.
NLP
from transformers import pipeline qa_pipeline = pipeline('question-answering', model=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a language model not fine-tuned for QA like 'gpt2'.
Using a base model without QA fine-tuning.
✗ Incorrect
The 'distilbert-base-uncased-distilled-squad' model is a lightweight model fine-tuned for extractive question answering tasks.
2fill in blank
mediumComplete the code to provide context and question to the QA pipeline and get the answer.
NLP
result = qa_pipeline({'question': [1], 'context': context_text}) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the context as the question.
Using incorrect keys in the input dictionary.
✗ Incorrect
The 'question' key must have the question string to ask the model.
3fill in blank
hardFix the error in accessing the answer text from the QA pipeline output.
NLP
answer_text = result[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' or 'result' keys which do not exist in the output.
Trying to access the answer as an attribute instead of a dictionary key.
✗ Incorrect
The pipeline returns a dictionary with the key 'answer' containing the extracted answer string.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each question to its answer using the QA pipeline.
NLP
answers = {q: qa_pipeline({'question': q, 'context': [1])[2] for q in questions} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for context.
Accessing the answer with incorrect keys like ['text'].
✗ Incorrect
The context is passed as 'context_text', and the answer is accessed with ['answer'] from the pipeline output.
5fill in blank
hardFill all three blanks to define a function that takes a question and context, runs the QA pipeline, and returns the answer text.
NLP
def answer_question([1], [2]): result = qa_pipeline({'question': [1], 'context': [2]) return result[3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter names and dictionary keys.
Returning the whole result dictionary instead of just the answer.
✗ Incorrect
The function parameters are 'question' and 'context'. The pipeline is called with these, and the answer is accessed with ['answer'].