Get startedGet started for free

Language translation with pretrained PyTorch model

Your team at PyBooks is working on an AI project that involves translation from one language to another. They want to leverage pre-trained models for this task, which can save a lot of training time and resources. The task for this exercise is to set up a translation model from HuggingFace's Transformers library, specifically the T5 (Text-To-Text Transfer Transformer) model, and use it to translate an English phrase to French.

T5Tokenizer, T5ForConditionalGeneration have been loaded for you.

This exercise is part of the course

Deep Learning for Text with PyTorch

View Course

Exercise instructions

  • Initialize the tokenizer and model from the pretrained "t5-small" model.
  • Encode the input prompt using the tokenizer, making sure to return PyTorch tensors.
  • Translate the input prompt using model and generate the translated output.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initalize tokenizer and model
tokenizer = ____.from_pretrained("t5-small")
model = ____.from_pretrained("t5-small")

input_prompt = "translate English to French: 'Hello, how are you?'"

# Encode the input prompt using the tokenizer
input_ids = ____.____(input_prompt, return_tensors="____")

# Generate the translated ouput
output = model.____(input_ids, max_length=50)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("Generated text:",generated_text)
Edit and Run Code