CazC commited on
Commit
cd34e38
·
1 Parent(s): cbb9b75
Files changed (2) hide show
  1. app.py +33 -14
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,39 +1,58 @@
1
  import gradio as gr
2
  import json
3
  from pydantic import BaseModel
 
 
4
  from typing import List
 
 
 
 
 
 
 
5
 
6
  # Define the Pydantic models
7
  class Ingredient(BaseModel):
 
8
  quantity: str
9
  unit: str
10
  name: str
11
 
12
  class Steps(BaseModel):
 
13
  stepNumber: int
14
  instruction: str
15
 
 
16
  class ProduceRecipe(BaseModel):
 
17
  mealName: str
18
- ingredients: List[Ingredient]
19
- steps: List[Steps]
 
20
 
21
- # This is a placeholder for your actual function
22
  def generate_recipe(meal_name: str, calories: int, meal_time: str) -> ProduceRecipe:
23
  # This is where you'll implement your recipe generation logic
24
  # For now, we'll return a dummy recipe
25
- dummy_recipe = {
26
- "mealName": meal_name,
27
- "ingredients": [
28
- {"quantity": "2", "unit": "large", "name": "eggs"},
29
- {"quantity": "1/4", "unit": "cup", "name": "onion, diced"}
 
 
 
 
 
 
30
  ],
31
- "steps": [
32
- {"stepNumber": 1, "instruction": "Crack the eggs into a bowl."},
33
- {"stepNumber": 2, "instruction": "Whisk the eggs and add diced onions."}
34
- ]
35
- }
36
- return ProduceRecipe(**dummy_recipe)
37
 
38
  def format_recipe(recipe: ProduceRecipe) -> str:
39
  formatted = f"<h2>{recipe.mealName}</h2>\n\n"
 
1
  import gradio as gr
2
  import json
3
  from pydantic import BaseModel
4
+ from pydantic import BaseModel
5
+ from openai import OpenAI
6
  from typing import List
7
+ import os
8
+ API_KEY = os.getenv('api_key')
9
+
10
+ client = OpenAI(
11
+ api_key=API_KEY
12
+ )
13
+
14
 
15
  # Define the Pydantic models
16
  class Ingredient(BaseModel):
17
+ """An ingredient for a recipe"""
18
  quantity: str
19
  unit: str
20
  name: str
21
 
22
  class Steps(BaseModel):
23
+ """Steps to make a recipe"""
24
  stepNumber: int
25
  instruction: str
26
 
27
+
28
  class ProduceRecipe(BaseModel):
29
+ """Makes a recipe for a meal"""
30
  mealName: str
31
+ ingredients: list[Ingredient]
32
+ steps : list[Steps]
33
+
34
 
 
35
  def generate_recipe(meal_name: str, calories: int, meal_time: str) -> ProduceRecipe:
36
  # This is where you'll implement your recipe generation logic
37
  # For now, we'll return a dummy recipe
38
+
39
+ meal_template = f'''
40
+ "role": "user", "content": "Create a recipe for a {meal_time} of {meal_name} with the following ingredients that is roughly {calories} calories."
41
+ '''
42
+
43
+
44
+ completion = client.beta.chat.completions.parse(
45
+ model="gpt-4o-2024-08-06",
46
+ messages=[
47
+ {"role": "system", "content": "You are an expert chef and nutritionalist. You will be given a meal request and should convert it into a structured Recipe at the correct calories."},
48
+ {"role": "user", "content": meal_template}
49
  ],
50
+ response_format=ProduceRecipe,
51
+ )
52
+
53
+
54
+ recipe = completion.choices[0].message.parsed
55
+ return recipe
56
 
57
  def format_recipe(recipe: ProduceRecipe) -> str:
58
  formatted = f"<h2>{recipe.mealName}</h2>\n\n"
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ pydantic