adrianpierce commited on
Commit
31b33e7
1 Parent(s): 8fec388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -31
app.py CHANGED
@@ -29,15 +29,12 @@ serving_size = st.number_input(
29
  difficulty_dictionary = {
30
  "Quick & Easy": {
31
  "description": "Easy recipes with straightforward instructions. Ideal for beginners or those seeking quick and simple cooking.",
32
- "gpt_instruction": "Easy: provide a quick and easy recipe with simple/straightfoward ingredients and instructions."
33
  },
34
  "Intermediate": {
35
  "description": "Recipes with some intricate steps that invite a little challenge. Perfect for regular cooks wanting to expand their repertoire with new ingredients and techniques.",
36
- "gpt_instruction": "Intermediate: provide an intermediate recipe with some intricate ingredients and techniques."
37
  },
38
  "Professional": {
39
  "description": "Complex recipes that demand a high level of skill and precision. Suited for seasoned cooks aspiring to professional-level sophistication and creativity.",
40
- "gpt_instruction": "Professional: provide a restaurant quality dish that is innovative/experimental and uses a wide variety of ingredients and techniques."
41
  }
42
  }
43
 
@@ -64,28 +61,78 @@ user_inputs = {
64
  "user_direction" : user_direction,
65
  "exclusions": exclusions,
66
  "serving_size": serving_size,
67
- "difficulty": difficulty_dictionary[selected_difficulty]['gpt_instruction']
68
  }
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def generate_recipe(user_inputs):
72
  with st.spinner('Building the perfect recipe for you...'):
73
- context = """You are an expert chef assistant that can make a wide variety of recipes based on user input.
74
- Output a recipe in a valid JSON object with the following properties:
75
- recipe_name (string): provide a name for the generated recipe
76
- recipe_serving_size (string): the serving size of the recipe (example: "4 people")
77
- recipe_time (string): the amount of time required to make the recipe (example: "60 minutes (Preparation: 20 minutes, Baking: 40 minutes)")
78
- recipe_ingredients (string): python list of ingredients required to make the recipe
79
- recipe_instructions (string): python list of instructions to make the recipe
80
- """
81
- messages = [
82
- {"role": "system", "content": context},
83
- {"role": "user", "content": f'user_input={str(user_inputs)}'}
84
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  st.session_state.gpt_response = openai.ChatCompletion.create(
86
- model="gpt-3.5-turbo",
87
- messages=messages,
88
- temperature=0.25
 
 
 
89
  )
90
 
91
 
@@ -93,17 +140,23 @@ st.button(label='Submit', on_click=generate_recipe, kwargs=dict(user_inputs=user
93
 
94
  if st.session_state.gpt_response is not None:
95
  st.divider()
96
- loaded_recipe = json.loads(st.session_state.gpt_response['choices'][0]['message']['content'])
97
- st.header(loaded_recipe['recipe_name'])
98
- st.write(f"**Serving Size: {loaded_recipe['recipe_serving_size']}**")
99
- st.write(f"**Time To Make: {loaded_recipe['recipe_time']}**")
100
  st.subheader("Ingredients:")
101
- md_ingredients = ''
102
- for ingredient in loaded_recipe['recipe_ingredients']:
103
- md_ingredients += "- " + ingredient + "\n"
104
- st.markdown(md_ingredients)
 
 
 
105
  st.subheader("Instructions:")
106
- md_instructions = ''
107
- for instruction in loaded_recipe['recipe_instructions']:
108
- md_instructions += "- " + instruction + "\n"
109
- st.markdown(md_instructions)
 
 
 
 
29
  difficulty_dictionary = {
30
  "Quick & Easy": {
31
  "description": "Easy recipes with straightforward instructions. Ideal for beginners or those seeking quick and simple cooking.",
 
32
  },
33
  "Intermediate": {
34
  "description": "Recipes with some intricate steps that invite a little challenge. Perfect for regular cooks wanting to expand their repertoire with new ingredients and techniques.",
 
35
  },
36
  "Professional": {
37
  "description": "Complex recipes that demand a high level of skill and precision. Suited for seasoned cooks aspiring to professional-level sophistication and creativity.",
 
38
  }
39
  }
40
 
 
61
  "user_direction" : user_direction,
62
  "exclusions": exclusions,
63
  "serving_size": serving_size,
64
+ "difficulty": selected_difficulty
65
  }
66
 
67
+ def create_detailed_prompt(user_direction, exclusions, serving_size, difficulty):
68
+ if difficulty == "Quick and Easy":
69
+ prompt = (
70
+ f"Please provide a 'Quick and Easy' recipe for {user_direction} with a serving size of {serving_size}. "
71
+ f"It should require as few ingredients as possible and should be ready as little time as possible. "
72
+ f"The steps should be simple, and the ingredients should be commonly found in a household pantry. "
73
+ f"Ensure to exclude {exclusions} from the recipe."
74
+ )
75
+ elif difficulty == "Intermediate":
76
+ prompt = (
77
+ f"I'm looking for a recipe for a classic {user_direction} with a serving size of {serving_size} that offers a bit of a cooking challenge "
78
+ f"but doesn't require professional skills.The recipe should feature traditional ingredients and techniques that are authentic to its cuisine. "
79
+ f"Please provide a step-by-step guide that explains the process in detail. "
80
+ f"Ensure to exclude {exclusions} from the recipe."
81
+ )
82
+ elif difficulty == "Professional":
83
+ prompt = (
84
+ f"Create an advanced recipe for {user_direction} with a serving size of {serving_size} that pushes the boundaries of culinary arts."
85
+ f"This recipe should integrate unique ingredients, advanced cooking techniques, and innovative presentations."
86
+ f"I'm aiming for a dish that could be served at a high-end restaurant or would impress at a gourmet food competition."
87
+ f"Please detail the preparation and cooking process, considering that complexity and creativity are more important than prep and cooking time. "
88
+ f"Ensure to exclude {exclusions} from the recipe."
89
+ )
90
+ return prompt
91
 
92
  def generate_recipe(user_inputs):
93
  with st.spinner('Building the perfect recipe for you...'):
94
+ functions = [
95
+ {
96
+ "name": "provide_recipe",
97
+ "description": "Provides a detailed recipe strictly adhering to the user input/specifications, especially ingredient exclusions and the recipe difficulty",
98
+ "parameters": {
99
+ "type": "object",
100
+ "properties": {
101
+ "name": {
102
+ "type": "string",
103
+ "description": "A creative name for the recipe"
104
+ },
105
+ "description": {
106
+ "type": "string",
107
+ "description": "a brief one sentence description of the provided recipe"
108
+ },
109
+ "ingredients": {
110
+ "type": "string",
111
+ "description": "clear list of ingredients in the provided recipe, delimited by a semi colon"
112
+ },
113
+ "instructions": {
114
+ "type": "string",
115
+ "description": "list of step-by-step instructions to prepare the provided recipe, delimited by a semi colon"
116
+ }
117
+ },
118
+ "required": [
119
+ "name",
120
+ "description",
121
+ "ingredients",
122
+ "instructions"
123
+ ],
124
+ },
125
+ }
126
+ ]
127
+ prompt = create_detailed_prompt(user_inputs['user_direction'], user_inputs['exclusions'], user_inputs['serving_size'], user_inputs['difficulty'])
128
+ messages = [{"role": "user", "content": prompt}]
129
  st.session_state.gpt_response = openai.ChatCompletion.create(
130
+ model="gpt-3.5-turbo",
131
+ messages=messages,
132
+ temperature=0.75,
133
+ top_p=0.75,
134
+ functions=functions,
135
+ function_call={"name":"provide_recipe"}, # auto is default, but we'll be explicit
136
  )
137
 
138
 
 
140
 
141
  if st.session_state.gpt_response is not None:
142
  st.divider()
143
+ loaded_recipe = json.loads(st.session_state.gpt_response['choices'][0]['message']["function_call"]["arguments"])
144
+ st.header(loaded_recipe['name'])
145
+ # st.write(f"**Serving Size: {loaded_recipe['recipe_serving_size']}**")
146
+ st.write(f"**Time To Make:** {loaded_recipe['description']}**")
147
  st.subheader("Ingredients:")
148
+ try:
149
+ md_ingredients = ''
150
+ for ingredient in loaded_recipe['ingredients'].split('; '):
151
+ md_ingredients += "- " + ingredient + "\n"
152
+ st.markdown(md_ingredients)
153
+ except:
154
+ st.write(loaded_recipe['ingredients'])
155
  st.subheader("Instructions:")
156
+ try:
157
+ md_instructions = ''
158
+ for instruction in loaded_recipe['instructions'].split('\n'):
159
+ md_instructions += "- " + instruction + "\n"
160
+ st.markdown(md_instructions)
161
+ except:
162
+ st.write(loaded_recipe['instructions'])