-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt4_healthy_recipe_generation_nutrition.py
49 lines (40 loc) · 1.25 KB
/
gpt4_healthy_recipe_generation_nutrition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Use case: Nutrition - Healthy Recipe Generation
import openai
from transformers import GPT2Tokenizer
# Set up OpenAI API credentials
openai.api_key = "YOUR_API_KEY"
# Define the GPT-4 model and tokenizer
model_engine = "gpt-4"
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# Define the prompt for generating healthy recipes
prompt = """
Generate a healthy recipe with the following criteria:
- Low in calories and fat
- High in protein and fiber
- Includes vegetables and lean protein
- Easy to prepare
Recipe:
"""
# Generate healthy recipes using GPT-4
def generate_recipe(prompt, max_tokens=200, num_recipes=1):
recipes = []
for _ in range(num_recipes):
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
n=1,
stop=None,
temperature=0.7,
)
recipe = response.choices[0].text.strip()
recipes.append(recipe)
return recipes
# Generate multiple healthy recipes
num_recipes = 3
generated_recipes = generate_recipe(prompt, max_tokens=300, num_recipes=num_recipes)
# Print the generated recipes
for i, recipe in enumerate(generated_recipes):
print(f"Recipe {i+1}:")
print(recipe)
print("---")