Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions backend/Recipio.py
Original file line number Diff line number Diff line change
@@ -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()
55 changes: 33 additions & 22 deletions backend/allrecipesdatabase.py
Original file line number Diff line number Diff line change
@@ -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<len(image)-1):
user_dict[image[i]]=image[i+1]
i+=2
return user_dict

def main():
print("Welcome to Recip.io!")

'''image=True
if(image):
arr=rec.imager(THE_IMAGE_HERE)'''
#run the below code if there's an image passed
image=rec.preload()
# Get ingredients to include
ingredients_incl = getIngredientList("Please start typing your ingredients and measurements below (hit enter twice to end your list)", include_measurement=True)
ingredients_incl = getIngredientList("Please start typing your ingredients and measurements below (hit enter twice to end your list)",image, include_measurement=True)
print("Ingredients entered:", ingredients_incl)

# Get ingredients to exclude
ingredients_excl = getIngredientList("Please start typing the ingredients you want to exclude (hit enter twice to end your list)", include_measurement=False)
ingredients_excl = getIngredientList("Please start typing the ingredients you want to exclude (hit enter twice to end your list)",False, include_measurement=False)
print("Ingredients excluded:", ingredients_excl)

# If the ingredient list is empty, ask again
Expand Down
Binary file added backend/spices.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.