Views
No views yet
1from transformers import T5ForConditionalGeneration, T5Tokenizer
2from peft import PeftModel
3
4# Load base model and tokenizer
5base_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")
6tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
7
8# Load LoRA adapter
9model = PeftModel.from_pretrained(base_model, "Chama99/flan-t5-small-recipe-generator")
10
11# Generate recipe
12def generate_recipe(ingredients):
13 prompt = f"Create a detailed recipe using these ingredients: {ingredients}. Include step-by-step cooking instructions:"
14
15 inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
16
17 with torch.no_grad():
18 outputs = model.generate(
19 **inputs,
20 max_length=400,
21 num_beams=5,
22 temperature=0.8,
23 do_sample=True,
24 repetition_penalty=1.4,
25 no_repeat_ngram_size=3
26 )
27
28 recipe = tokenizer.decode(outputs[0], skip_special_tokens=True)
29 return recipe.replace(prompt, "").strip()
30
31# Example usage
32ingredients = "chicken, mushrooms, garlic, cream"
33recipe = generate_recipe(ingredients)
34print(recipe)