-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
357 lines (283 loc) · 12.2 KB
/
Main.py
File metadata and controls
357 lines (283 loc) · 12.2 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Justice Pankey-Thompson - CSC 492-01
# The Main Class is the center of this program
# It is here where the Model is loaded and used
# It is here where the class objects are created and managed
# It is here where the User will interact with the program as a whole
import os
import tkinter as tk
import requests
from PIL import ImageTk, Image
from transformers import AutoTokenizer, AutoModelForCausalLM
from Pantry import Pantry
from Profile import Profile
from SpoonacularAPIHandler import Spoonacular
#Create global references for the class objects
global pantry
global profile
global spoonacular
#Method that will load the model, and use it to get recommendations and instructions
@staticmethod
def generateRecipes():
query = ""
#Use the Profile class to get the Health and Tastes info on the User
cuisine = profile.getPrefCuisine()
#If there is no cuisine specified, then don't one to the query
if cuisine != "none":
query += f"&cuisine={cuisine}"
diet = profile.getDiet()
#If there is no diet specified, don't add one to the query
if diet != "none":
query += f"&diet={diet}"
#Use ingredients from the Users pantry
onhand = ','.join(pantry.getIng())
if onhand != "":
query += f"&includeIngredients={onhand}"
#Form the query to include instructions, and be healthy
query += "&instructionsRequired=true"
query += "&sort=healthiness"
query += "&sortDirection=desc"
#Get Spoonacular's response into 5 recipes
results = spoonacular.generateRecipes(query)
dishes = results.get('results')
dishNames : list[str]
dishNames = []
dishID = list[int]
dishID = []
#Format the dishes from Spoonacular for the model
for dish in dishes:
dishNames.append(f"Name: {dish.get('title')} Recipe")
dishID.append(int(dish.get('id')))
#Load the Model, and use it to get its recommendations from the list of recipes
tokenizer = AutoTokenizer.from_pretrained('./Models/Gemma2b;2024-05-09;16-56-17/')
model = AutoModelForCausalLM.from_pretrained('./Models/Gemma2b;2024-05-09;16-56-17/')
#Cuda:0 means the GPU
device = "cuda:0"
#Put the model and inputs on the same device (GPU)
model = model.to(device)
#Generate the Recommendations
recommendations = {}
for i in range(len(dishNames)):
inputs = tokenizer(dishNames[i], return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=20)
current = tokenizer.decode(outputs[0], skip_special_tokens=True)
if " Indian " in current:
recommendations.update({dishNames[i][6:-7]:dishID[i]})
#Show the User the Recommendations and ask for response
print("Do any of these sound good?")
i = 1
for recommendation in recommendations.keys():
print(f"{recommendation} : {i}")
i += 1
#The User needs to input a proper choice on the list
while True:
try:
print("\nPlease type the number associated with name of the dish you would like to make: ", end='')
choice = int(input())
if choice < 1 or choice > len(recommendations.values()):
print("Not a valid response. Try again.")
continue
name = list(recommendations.keys())[choice - 1]
break
except:
print("Not a valid response. Try again.")
continue
#Once the User makes a selection, get the instructions for them
print(f"Your selection is {name}. Fetching the recipe instructions.")
info = spoonacular.getInstructions(recommendations.get(name))
used = []
recipe = info.get('analyzedInstructions')
#Print out the instructions to the User
for item in recipe:
for instruction in item.get('steps'):
print(f"Step {instruction.get('number')}: {instruction.get('step')}\n")
for ingredient in instruction.get('ingredients'):
used.append([ingredient.get('name'), 1])
#Enjoy!
print("Enjoy your meal!")
#Update the Pantry to use up what was in the recipe
results = spoonacular.getNutrition(recommendations.get(name))
pantry.useUp(used)
#Update the Profile's Daily Values so the user can monitor their nutritional intake
info = results.get('nutrition')
nutrients = info.get('nutrients')
dv = []
for nutrient in nutrients:
dv.append(float(nutrient.get('amount')))
profile.updateDV(dv)
#Print out a picture of the recipe the user is going to make
#Format: https://img.spoonacular.com/recipes/id-556x370.jpg
#Make sure to replace the id with the id of the recipe you want to view
image_url = f"https://img.spoonacular.com/recipes/{recommendations.get(name)}-556x370.jpg"
#Save image to local folder to view
img_data = requests.get(image_url).content
#Open image file and read the bytes
with open('curr.jpg', 'wb') as handler:
handler.write(img_data)
#Create tkinter window for viewing the image
root = tk.Tk()
root.title(f"{name}")
img = ImageTk.PhotoImage(Image.open("curr.jpg"))
label = tk.Label(image=img)
label.pack()
#Display the window
root.mainloop()
#Update Health Data through Profile's survey
@staticmethod
def updateHealth(healthData):
profile.inputData(healthData)
#Update Daily Values from the list of Daily Values consumed
@staticmethod
def updateDailyValues(dailyValues):
profile.updateDV(dailyValues)
#Used to update the User's die
def updateDiet(self):
print("Please input the diet you wish to change to. Type none for no diet:", end=" ")
diet = input().lower()
if profile.updateDiet(diet) == 1:
print("Error: Invalid selection. Try again.")
self.updateDiet()
return
#Calls the Profile class to update the user's preferred cuisine
@staticmethod
def updateCuisine(cuisine):
profile.updateCuisine(cuisine)
#Allow the user to add to their pantry (like if they went grocery shopping)
@staticmethod
def addIngredients():
print("Please enter the name of the ingredient you wish to add (singular). Leave empty to stop:", end=" ")
name = input()
#Let the user continue adding until they've added everything
while name != "":
print(f"How many {name}s do you want to add?:", end=" ")
quantity = int(input())
pantry.addIngredients((name, quantity))
#Start next loop
print("Please enter the name of the ingredient you wish to add (singular). Leave empty to stop:", end=" ")
name = input()
print("Finished adding ingredients.")
#Allow the user to remove from their pantry (like if they cooked themselves, or food spoiled)
@staticmethod
def manualRemoveIng():
print("Please enter the name of the ingredient you wish to use (singular). Leave empty to stop:", end=" ")
name = input()
ingList : list
ingList = []
#Let the user continue removing all they need to remove
while name != "":
print(f"How many {name}s did you use?:", end=" ")
quantity = int(input())
ingList.append([name, quantity])
#Start next loop
print("Please enter the name of the ingredient you wish to use (singular). Leave empty to stop:", end=" ")
name = input()
print("Finished removing ingredients.")
#Update Pantry
pantry.useUp(ingList)
#Similar to manualRemoveIng, but made for one ingredient at a time from recipes
@staticmethod
def useIng(ingredients):
pantry.useUp(ingredients)
#Retrieve all of the user's pantry and profile information
@staticmethod
def getUserInfo():
#Create a string and fill it with the all of the user's information
info : str
info = f"Health: {profile.getIntolerances()}\n"
info += f"Preferred Cuisine: {profile.getPrefCuisine()}\n"
info += f"Diet: {profile.getDiet()}\n"
info += f"Daily Values:\n"
#Daily Values is a list of different nutrients
for dv in profile.getDV():
info += "\t" + dv + "\n"
info += f"\nIngredients in Pantry:\n"
#Pantry has a list of ingredients and quantities
ings = pantry.getIngFull()
for i in range(len(ings[0])):
info += f"\t{ings[0][i]} : {ings[1][i]}\n"
#Return complete info
return info
#Main handles the main menu the user interacts with
#Main also creates and deletes the different class objects
def main():
#Greet user and create objects
print("Welcome to the Recipe Recommender!\n")
global profile, pantry, spoonacular
profile = Profile()
pantry = Pantry()
spoonacular = Spoonacular()
#Until the user selects Quit (7), keep the program running
while True:
print("""\nWhat would you like to do?
Recommend me a Recipe : 1
View all of my Info : 2
View my Pantry Items : 3
View my Health/Taste : 4
Update Pantry Items : 5
Update My Profile : 6
Quit : 7""")
#The user needs to input a valid response. Keep asking them to retry until they put one in
while True:
try:
print("\nPlease type the number associated with the action you wish to take: ", end="")
choice = int(input())
if choice < 1 or choice > 7:
print("Not a valid response. Try again.")
continue
break
except:
print("Not a vailid response. Try again.")
continue
#Depending what the user selected, run their selection
match choice:
case 1:
print("Recommending recipes...\n")
generateRecipes()
case 2:
#All user info
print("Fetching Information...\n")
print(getUserInfo())
case 3:
#Just pantry
print("Fetching Pantry Information...\n")
for ingredients in pantry.getIng():
print(ingredients)
case 4:
#Just profile
print("Fetching User Profile...\n")
print(f"Diet: {profile.getDiet()}")
print(f"Intolerances: {profile.getIntolerances()}")
print(f"Preferred Cuisine: {profile.getPrefCuisine()}")
print(f"Daily Values:")
for dv in profile.getDV():
print(f"\t{dv}")
case 5:
#Update the pantry
#Need to determine if user is adding or deleting first
print("Okay. Let's update the pantry.\n")
#Make sure the user gives an appropriate answer
while True:
print("Type 1 to add ingredients. Type 2 to remove ingredients: ", end="")
try:
response = int(input())
if response == 1:
addIngredients()
elif response == 2:
manualRemoveIng()
else:
print("Invalid response. Try again.")
continue
break
except:
print("Invalid response. Try again.")
continue
case 6:
#Have the user take the quiz to update their health and preferences
print("Okay. Let's update your profile.\n")
profile.quiz()
case 7:
#Delete class objects, and say goodbye. End the program
del(profile, pantry, spoonacular)
print("Thank you for using this program. Goodbye.")
break
#Start the program
main()