Views
No views yet
| Input | Output |
|---|---|
| My favorite color is yellow. | My favourite colour is yellow. |
| I saw a guy in yellow sneakers at the subway station. | I saw a bloke in yellow trainers at the underground station. |
| You could have gotten hurt! | You could have got hurt! |
1import torch
2from transformers import T5ForConditionalGeneration,T5Tokenizer
3
4device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
6model = T5ForConditionalGeneration.from_pretrained("EnglishVoice/t5-base-us-to-uk-english")
7tokenizer = T5Tokenizer.from_pretrained("EnglishVoice/t5-base-us-to-uk-english")
8model = model.to(device)
9
10input = "My favorite color is yellow."
11
12text = "US to UK: " + input
13encoding = tokenizer.encode_plus(text, return_tensors = "pt")
14input_ids = encoding["input_ids"].to(device)
15attention_masks = encoding["attention_mask"].to(device)
16beam_outputs = model.generate(
17 input_ids = input_ids,
18 attention_mask = attention_masks,
19 early_stopping = True,
20)
21
22result = tokenizer.decode(beam_outputs[0], skip_special_tokens=True)
23print(result)
24My favourite colour is yellow.