Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load the translation pipeline from Hugging Face.
NLP
from transformers import pipeline translator = pipeline([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sentiment-analysis instead of a translation pipeline.
Choosing the wrong target language code.
✗ Incorrect
The correct pipeline name for English to German translation is "translation_en_to_de".
2fill in blank
mediumComplete the code to translate the English sentence to German using the pipeline.
NLP
result = translator([1]) print(result[0]['translation_text'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the sentence without quotes causing a syntax error.
Passing a list instead of a string.
✗ Incorrect
The input to the translator must be a string enclosed in quotes.
3fill in blank
hardFix the error in the code to correctly translate the sentence.
NLP
from transformers import pipeline translator = pipeline("translation_en_to_de") result = translator([1]) print(result[0]['translation_text'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing unquoted text causing NameError.
Passing a list instead of a string.
✗ Incorrect
The input to the translator must be a string with quotes, so "Good morning" or 'Good morning' is correct. Option A uses double quotes correctly.
4fill in blank
hardFill both blanks to create a translation pipeline from English to French and translate a sentence.
NLP
from transformers import pipeline translator = pipeline([1]) result = translator([2]) print(result[0]['translation_text'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong pipeline name.
Passing input without quotes.
✗ Incorrect
The pipeline must be set to English to French translation and the input sentence must be a string with quotes.
5fill in blank
hardFill all three blanks to translate a list of English sentences to Spanish and print each translation.
NLP
from transformers import pipeline translator = pipeline([1]) sentences = ["Hello", "Good night", "Thank you"] translations = [translator([2])[0]['translation_text'] for [3] in sentences] for t in translations: print(t)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string "sentence" instead of the variable sentence.
Using a different loop variable than the one passed to translator.
✗ Incorrect
The pipeline must be set for English to Spanish translation. The input to translator is the variable holding each sentence, which is 'sentence'. The loop variable is also 'sentence'.