diff --git a/backend/Recipio.py b/backend/Recipio.py new file mode 100644 index 0000000..f98a86b --- /dev/null +++ b/backend/Recipio.py @@ -0,0 +1,100 @@ +#this is gonna be the start of the file for wednesday +import pathlib +import textwrap +import google.generativeai as genai +#import allrecipesdatabase as ardb +import sys; +#"set the environment variable, Replace /path/to/your/credentials.json with the actual path to your Google Cloud credentials file." +import os +#os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '%APPDATA%\gcloud\application_default_credentials.json' +#"authenticate with google cloud" +''' +from google.oauth2 import service_account +credentials = service_account.Credentials.from_service_account_file( + 'test' +) +''' +#"create a storage client" +#from google.cloud import storage +#client = storage.Client(credentials=credentials) +import configparser +#from google.colab import userdate +from IPython.display import display +from IPython.display import Markdown + +#api key +GOOGLE_API_KEY="YOUR_API_KEY_HERE" +genai.configure(api_key=GOOGLE_API_KEY) + +from PIL import Image +import numbers +#i just copied this off of the link, it's a helper method used to clean up response objects and return them +def to_markdown(text): + text = text.replace('•', ' *') + return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True)) +#this is the function that we're gonna use to run the image recognition thing +def imager(image): + model = genai.GenerativeModel('gemini-1.5-flash') + response = model.generate_content(["Generate a list of the ingredients provided in the following image using * as bulletpoints and be specific about what the ingredients are and do not include duplicates", image], stream=False) + to_markdown(response.text) + print(response.text) + return userinput(response) +#this is the function that represents response generation +def userinput(contents): + userResponse=input("Is this what you are looking for? [Yes or No]") + if(userResponse=="No"): + #if sensitive topic or first candidate not sufficient + print("Here are alternatives or why you might not see something: ") + print(contents.candidates) + contents.prompt_feedback + elif(userResponse=="Yes"): + #the output should be an array of all the ingredients + hold=[] + ret=[] + for token in contents.text: + if((token.isalpha or token.isspace()) and not token=="*"): + hold.append(token) + #if the current token is a newline or the last element + if(token=="\n"): + ret.append(''.join(hold[:-1])) + hold=[] + #if(token is contents.text[-1]): + #ret.append(''.join(hold)) + print(ret) + for i,x in enumerate(ret): + if(x=='' or len(x)==1 or len(x)==2): + ret.pop(i) + return ret + else: + #potential solution: gen_recipe(image)? + print("Please enter either Yes or No") + userinput(contents) +#This is the function where you interact with the backend (aka the allrecipesdatabase) +def store(image): + #hold holds the resulting array with parsed ingredients + hold=imager(image) + sys.stdin="PICTURE\n"+hold + #ardb.main() +def process_image(image): + image.show() # Display the processed image +''' +client = storage.Client() +bucket = client.get_bucket('Downloads') +blob = bucket.get_blob('spices.jpeg') +blob.download_to_filename('spices.jpeg') +''' +def preload(): + image_path = "spices.jpeg" + input_image = Image.open(image_path) + #input_image=Image.open(sys.stdin) + process_image(input_image) + arr=imager(input_image) + stream="" + hold=[] + for x in arr: + hold.append(x) + hold.append(input("Enter measurement for "+x+" (include units): ")) + return hold + #store(input_image) +#testing the function +preload() \ No newline at end of file diff --git a/backend/allrecipesdatabase.py b/backend/allrecipesdatabase.py index 91e8ac5..6bc812d 100644 --- a/backend/allrecipesdatabase.py +++ b/backend/allrecipesdatabase.py @@ -1,44 +1,55 @@ from allrecipes import AllRecipes import pandas as pd - -def getIngredientList(prompt, include_measurement=True): +import Recipio as rec +import sys +def getIngredientList(prompt, image,include_measurement=True): """Ask user for ingredients repeatedly to construct a list. param: prompt String that will ask user if they want to include or exclude ingredients. param: include_measurement Boolean to determine if measurements are needed. """ print(prompt) user_dict = {} - - while True: - ingredient = input("Enter ingredient (or press Enter to stop): ") - if not ingredient: # Stop if ingredient is empty - print("Ending list...") - break + #if image recognition was not used + if(not image): + while True: + ingredient = input("Enter ingredient (or press Enter to stop): ") + if not ingredient: # Stop if ingredient is empty + print("Ending list...") + break # Ask for measurement if including ingredients - if include_measurement: - while True: - measurement = input("Enter measurement for {ingredient} (include units): ") - if measurement: # Only accept non-empty measurement - break - print("Measurement cannot be empty. Please provide a value.") - else: - measurement = None # No measurement for excluded ingredients + if include_measurement: + while True: + measurement = input("Enter measurement for {ingredient} (include units): ") + if measurement: # Only accept non-empty measurement + break + print("Measurement cannot be empty. Please provide a value.") + else: + measurement = None # No measurement for excluded ingredients # Add to the dictionary - user_dict[ingredient] = measurement - + user_dict[ingredient] = measurement + #if image recognition was used + else: + i=0 + #while loop that loops through every other index starting with 0 + while(i