Views
No views yet
pip install transformers torch datasets1from transformers import T5Tokenizer, T5ForConditionalGeneration
2import torch
3
4model_path = "./t5_recipe_finetuned_fp16"
5tokenizer = T5Tokenizer.from_pretrained(model_path)
6model = T5ForConditionalGeneration.from_pretrained(model_path).to("cuda").half()
7
8ingredients = ["1 lb chicken breast", "2 cups rice", "1 onion", "2 tbsp soy sauce"]
9input_text = "generate recipe directions from ingredients: " + " ".join(ingredients)
10input_ids = tokenizer(input_text, return_tensors="pt", max_length=128, truncation=True).input_ids.to("cuda")
11
12model.eval()
13with torch.no_grad():
14 output_ids = model.generate(input_ids, max_length=256, num_beams=4, early_stopping=True, no_repeat_ngram_size=2)
15directions = tokenizer.decode(output_ids[0], skip_special_tokens=True)
16print(directions)