Complete the code to load the translation model using Hugging Face Transformers.
from transformers import MarianMTModel, MarianTokenizer model_name = 'Helsinki-NLP/opus-mt-en-de' tokenizer = MarianTokenizer.from_pretrained([1])
The variable model_name holds the model identifier string, so it should be passed to from_pretrained() to load the tokenizer.
Complete the code to tokenize the input text for translation.
text = 'Hello, how are you?' inputs = tokenizer([1], return_tensors='pt', padding=True)
The variable text contains the input string and should be passed directly to the tokenizer.
Fix the error in generating the translated tokens from the model output.
translated_tokens = model.generate([1])The generate method requires the input IDs tensor, which is accessed by inputs['input_ids'].
Fill both blanks to decode the translated tokens into a readable string.
translated_text = tokenizer.decode([1][0], skip_special_tokens=[2])
The decode method takes the first element of translated_tokens and skips special tokens by setting skip_special_tokens=True.
Fill all three blanks to create a function that translates English text to German.
def translate_en_to_de(text): tokenizer = MarianTokenizer.from_pretrained([1]) model = MarianMTModel.from_pretrained([2]) inputs = tokenizer(text, return_tensors=[3], padding=True) translated_tokens = model.generate(inputs['input_ids']) return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
The function loads the tokenizer and model for English to German translation using the same model name string. The tokenizer returns PyTorch tensors, so return_tensors='pt' is used.