Skip to content

Commit

Permalink
Merge pull request #26 from kat9897/dev
Browse files Browse the repository at this point in the history
BIG UPDATE
  • Loading branch information
kat9897 authored Jan 28, 2024
2 parents d4c156e + c279109 commit 3937a7a
Show file tree
Hide file tree
Showing 23 changed files with 225 additions and 44 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion app/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# UofTHacks
# SnapToSavor
Our ingredients to recipe app for UofTHacks 11!

## Instructions
Expand Down
Binary file added app/__pycache__/food_api.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file modified app/backend/sql_bcknd/RecipePublic.db
Binary file not shown.
121 changes: 102 additions & 19 deletions app/backend/sql_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,130 @@ def add_user(db, cursor, FirstName, LastName):
cursor.execute('INSERT INTO UserPublic (UserID, FirstName, LastName, DateJoined)VALUES ("' + UserID +'","'+ FirstName +'","'+ LastName +'",'+ str(dt_now) + ')')
db.commit()

def add_ingredient(db, cursor, IngredientName, Description = None, Sugar = None, Sodium = None, Fats = None,
Protien = None, A = None, B = None, C = None, D = None, Fiber = None, Calories = None):
##def add_ingredient(db, cursor, IngredientName, IngredientID, Description = None, Sugar = None, Sodium = None, Fats = None,
##Protien = None, A = None, B = None, C = None, D = None, Fiber = None, Calories = None):
def add_ingredient(db, cursor, Ingredient):

db.commit()
ing_ids = get_ing_ids(db, cursor)
#print(ing_ids)
cursor.row_factory = None

IngredientID = 'MGJ83KT'
while IngredientID in ing_ids:
IngredientID = ''.join(random.choice('0123456789ABCDEF') for i in range(7))

args = locals()
args.pop("db")
args.pop("cursor")
args["IngredientID"] = IngredientID
if Ingredient["IngredientID"] not in ing_ids:
insert_query = '''INSERT INTO Ingredients (IngredientID, IngredientName)
VALUES ("{IngredientID}", "{IngredientName}")
'''.format(**Ingredient)

insert_query = '''INSERT INTO Ingredients (IngredientID, IngredientName, Description, Sugar_g, Sodium_mg, Fats_g, Protien_g, Vitamin_A_mcg, Vitamin_B_mcg, Vitamin_C_mcg, Vitamin_D_mcg, Fiber_g, Calories)
VALUES ("{IngredientID}", "{IngredientName}", "{Description}", "{Sugar}", "{Sodium}", "{Fats}", "{Protien}", "{A}", "{B}", "{C}", "{D}", "{Fiber}", "{Calories}")
'''.format(**args)
cursor.execute(insert_query)
db.commit()

cursor.execute(insert_query)
db.commit()

"""
def add_recipe(db, cursor, RecipeName, ):
def add_recipe(db, cursor, Recipe):

recipe_ids = get_rec_ids(db, cursor)
cursor.row_factory = None

RecipeID = 'ZKD3VY'
while RecipeID in recipe_ids:
RecipeID = ''.join(random.choice('0123456789ABCDEF') for i in range(6))
"""

user_ids = get_user_ids(db, cursor)
cursor.row_factory = None
UserID = random.choice(user_ids) ## DEV

dt_now = datetime.datetime.now()
dt_now = int(dt_now.strftime('%Y%m%d'))

Recipe["RecipeID"] = RecipeID
Recipe["UserID"] = UserID
Recipe["DatePosted"] = dt_now
#Recipe["RecipeThumbnailLink"] = Recipe["RecipeThumbnailLink"].replace('/','?')
#Recipe["RecipeThumbnailLink"] = Recipe["RecipeThumbnailLink"].replace(':','-')
Recipe["RecipeThumbnailLink"] = Recipe["RecipeThumbnailLink"].replace(' ','')
Recipe["Description"] = ""

insert_recipe = '''INSERT INTO Recipes (RecipeID, RecipeName, UserID, Description, RecipeThumbnailLink, DatePosted)
VALUES (
"{RecipeID}",
"{RecipeName}",
"{UserID}",
"{Description}",
"{RecipeThumbnailLink}",
"{DatePosted}"
)
'''.format(**Recipe)

cursor.execute(insert_recipe)

for RecipeIngredient in Recipe["RecipeIngredients"]:
RecipeIngredient["RecipeID"] = RecipeID

insert_recipe_ingredient = '''INSERT INTO RecipeIngredients_STG (RecipeID, IngredientID, Quantity, QuantityUnit)
VALUES (
"{RecipeID}",
"{IngredientID}",
{Quantity},
"{QuantityUnit}"
)
'''.format(**RecipeIngredient)

#print(insert_recipe_ingredient)

cursor.execute(insert_recipe_ingredient)
db.commit()

add_ingredient(db,cursor,RecipeIngredient)



def get_recipe(db, cursor, RecipeID):
select_query = '''SELECT * FROM (
SELECT
Recipes.*,
group_concat(Quantity || " " || QuantityUnit || " of " || A.IngredientName, ", ") AS "IngredientsList"
FROM Recipes
JOIN RecipeIngredients_STG ON RecipeIngredients_STG.RecipeID = Recipes.RecipeID
JOIN (
SELECT DISTINCT IngredientID, IngredientName FROM Ingredients
) AS A ON RecipeIngredients_STG.IngredientID = A.IngredientID
GROUP BY Recipes.RecipeID
) AS A
WHERE A.RecipeID = "{}"
'''.format(RecipeID)

cursor.execute(select_query)
return cursor.fetchall()

def get_recipe_by_ingredient(IngredientList):
IngredientsString = ', '.join(f'"{i}"' for i in IngredientList)

select_query = '''SELECT DISTINCT RecipeID
FROM (
SELECT RecipeIngredients_STG.RecipeID, RecipeIngredients_STG.IngredientID, Ingredients.IngredientName FROM RecipeIngredients_STG
JOIN Ingredients ON Ingredients.IngredientID = RecipeIngredients_STG.IngredientID
) AS A
WHERE A.IngredientName IN ({})
'''.format(IngredientsString)

cursor.row_factory = lambda cursor, row: row[0]
cursor.execute(select_query)
return cursor.fetchall()

## ------------- Testing Workspace ----------------

##db, cursor = sql_connect('RecipePublic.db')
#db, cursor = sql_connect('RecipePublic.db')
#print(get_recipe(db, cursor, "015284"))
#print(get_recipe_by_ingredient(["butter","cream cheese"]))
##add_user(db, cursor, "Jane", "Doe")
##add_ingredient(db, cursor, IngredientName = "Banana", Description = "An elongated, edible fruit, botanically a berry")

##recipes = get_recipes()
##for recipe in recipes:
##add_recipe(db, cursor, recipe)

##with open('sql_bcknd/test.sql', 'r') as sql_file:
##sql_script = sql_file.read()

Expand Down
2 changes: 1 addition & 1 deletion app/extra/ex.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"RecipeID": 640266,
"RecipeName": "Crab and Shrimp Burgers With Garlic Grits Fries",
"Description": "",
"RecipeThumbnailLink": "https: //spoonacular.com/recipeImages/640266-556x370.jpg",
"RecipeThumbnailLink": "https://spoonacular.com/recipeImages/640266-556x370.jpg",
"DatePosted": "",
"RecipeIngredients": [
{
Expand Down
Binary file not shown.
Binary file removed app/extra/src/img/MBTI.png
Binary file not shown.
22 changes: 19 additions & 3 deletions app/food_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import requests
from PIL import Image
import os
from pages.landing_page import *

def compress_img(input_img_path, output_img_path, quality=50):
try:
with Image.open(input_img_path) as img:
img.save(output_img_path, quality=quality)
except Exception as e:
print(f"Error: {e}")

#saves a list of the different items!
# img format should be the file path of the image. Ex: /User
def get_ingredients(img):
api_user_token = 'fb5a4c7df78e749b63d71a34cfd375c689a8e441'
api_user_token = '016da25e7e5a4e2f2b8bff3b0664c6612665ef09'
headers = {'Authorization': 'Bearer ' + api_user_token}

# Compression
input_image_path = img
output_image_path = "./img/compressed_image.jpg"
compress_img(input_image_path, output_image_path)
img = output_image_path

# Single/Several Dishes Detection
url = 'https://api.logmeal.es/v2/image/segmentation/complete'
resp = requests.post(url,files={'image': open(img, 'rb')},headers=headers)
Expand All @@ -20,5 +36,5 @@ def get_ingredients(img):


# Test of the call
#food_name_test = get_ingredients('/Users/marilynzhang/Desktop/food_test_2.jpg')
#print(food_name_test)
# food_name_test = get_ingredients(selected_files)
# print(food_name_test)
Binary file added app/img/compressed_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pages.found_recipes_page import *
from pages.root_page import *
from pages.confirm_ingredients_page import *
from food_api import *

pages = {
"/": root_page,
Expand All @@ -18,6 +19,31 @@
}

if __name__ == "__main__":

Gui(pages=pages).run(debug=True, use_reloader=True, port=8080, stylekit=stylekit, dark_mode=False)



from taipy import Gui

from pages.landing_page import *
from pages.found_recipes_page import *
from pages.root_page import *
from pages.confirm_ingredients_page import *

pages = {
"/": root_page,
"ingredients": landing_page,
"confirm": confirm_ingredients_page,
"recipes": found_recipes_page
}

stylekit = {
"color_primary": "#F87F7F",
"color_secondary": "#C0FFE",
}

if __name__ == "__main__":
Gui(pages=pages).run(debug=True, use_reloader=True, port=8081, stylekit=stylekit, dark_mode=False)


32 changes: 21 additions & 11 deletions app/pages/confirm_ingredients_page.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
from taipy.gui import navigate
import os
from .landing_page import *
from food_api import *

# Bindings
here = os.path.dirname(os.path.abspath(__file__))
image = os.path.join(here, "./img/image-missing.svg") # default image without any user input
num_ingred = 0
# here = os.path.dirname(os.path.abspath(__file__))
# image = os.path.join(here, "./img/image-missing.svg") # default image without any user input
selected_files = None
ingredients = []
num_ingred = 1

confirm_ingredients_page="""
<|layout|columns=2 2|
<|layout|columns=2 3|
<|card card-bg|
<|{image}|image|label="Uploaded Fridge Image"|>
<center>
<|{selected_files}|image|>
</center>
|>
<|card card-bg|
<|Confirm {num_ingred} ingredient(s):|>\n
<|Confirmed {num_ingred} ingredient(s):|>\n
<|{ingredients[0]}|>
|>
<|Confirm|button|on_action=confirmed_ingred|>
<|Confirm|button|class_name=success|on_action=confirmed_ingred|>
<|Delete|button|class_name=error|on_action=delete_ingred|>
|>
"""

def confirmed_ingred(state):
print(image)
# navigate(state, "recipes")
def update_num_ingred(state):
num_ingred = num_ingred + 1

def confirmed_ingred(state):
navigate(state, "recipes")

def delete_ingred(state):
navigate(state, to="ingredients")
33 changes: 32 additions & 1 deletion app/pages/found_recipes_page.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
found_recipes_page = """
### Hello. You found recipes
<|layout|columns=1 3|
<|card card-bg|
## Ingredients
<|card card-bg| {.red}
BANANA\n
BANANA BABY\n
BANANA
|>
|>
<|card card-bg|
## Recipes
<|layout|columns=1 1 1|
<|card card-bg|
<|app/pages/img/banana.jpg|image|>
|>
<|card card-bg|
<|app/pages/img/banana-baby-producto-caribbean-exotics.png|image|>
|>
<|card card-bg|
<|app/pages/img/banana.jpg|image|>
|>
|>
|>
|>
"""


# recipe = call database
# recipes = len[recipe]

# def nonempty():
# return recipes != 0
Binary file removed app/pages/img/MBTI.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/pages/img/banana.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions app/pages/img/edit-button.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions app/pages/img/image-missing.svg

This file was deleted.

Binary file added app/pages/img/missing_img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/pages/img/trash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions app/pages/landing_page.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from taipy.gui import navigate
from food_api import get_ingredients

# Bindings
selected_files = None

value = "Search for recipe..."

landing_page="""
<|{selected_files}|file_selector|label=Upload File|on_action=uploaded_files|extensions=.jpg,.jpeg,.png|drop_message=Drop Message|>
#Search for recipe:
## Search for recipe
<|{value}|input|><|find|button|on_action=uploaded_files|>
"""

def uploaded_files(state):
state.ingredients = get_ingredients(state.selected_files)
state.num_ingred = len(state.ingredients)
navigate(state, to="confirm")

2 changes: 1 addition & 1 deletion app/pages/root_page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
root_page = """
<|navbar|>
# Ingredient to Recipe App
# SnaptoSavor
"""

0 comments on commit 3937a7a

Please sign in to comment.