Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pretrained abstractive summarization model using Hugging Face Transformers.
NLP
from transformers import pipeline summarizer = pipeline('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text-classification' instead of 'summarization'.
Confusing summarization with translation.
✗ Incorrect
The pipeline task for abstractive summarization is 'summarization'.
2fill in blank
mediumComplete the code to generate a summary from the input text using the summarizer pipeline.
NLP
text = "Machine learning helps computers learn from data." summary = summarizer([1], max_length=30, min_length=5, do_sample=False)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list of words instead of a string.
Passing a modified version of the text that changes meaning.
✗ Incorrect
The summarizer expects a string input, so pass the variable 'text' directly.
3fill in blank
hardFix the error in the code to correctly decode the generated summary tokens.
NLP
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM model_name = 'facebook/bart-large-cnn' tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) inputs = tokenizer('Deep learning is a subset of machine learning.', return_tensors='pt') summary_ids = model.generate(inputs['input_ids'], max_length=20, num_beams=4, early_stopping=True) summary = tokenizer.[1](summary_ids[0], skip_special_tokens=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'encode' which converts text to tokens.
Using 'tokenize' which splits text into tokens but does not decode.
✗ Incorrect
To convert token IDs back to text, use the 'decode' method.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
NLP
words = ['data', 'AI', 'model', 'run'] lengths = { [1] : len([2]) for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' inside the comprehension.
Using a fixed string like 'data' instead of the loop variable.
✗ Incorrect
The key should be 'word' and the length is calculated on 'word'.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each word in 'texts' to its uppercase form only if the word length is less than 5.
NLP
texts = ['AI', 'learn', 'code', 'data'] result = { [1] : [2] for [3] in texts if len([3]) < 5 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not applying the upper() method to the value.
✗ Incorrect
The key is 'word', the value is 'word.upper()', and the loop variable is 'word'.