Skip to content

Commit

Permalink
more options and image placed in popup
Browse files Browse the repository at this point in the history
  • Loading branch information
BustosAndrew committed Apr 23, 2024
1 parent ba66e3d commit 5276e42
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
8 changes: 6 additions & 2 deletions lahacks/api/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@


def generate_recipe(req: dict):
print(req["cookware"])
prompt_parts = [
"Provide a healthier recipe consistent with the provided ingredients, along with the steps to make them. Provide nutritional info where possible. Add a name for the recipe and categorize the details as either healthy or unhealthy. Ignore ingredients with quantity 0.",
"Provide a healthier recipe consistent with the provided ingredients, along with the steps to make them. Adjust the entire recipe depending on the provided cookware. Provide nutritional info where possible. Add a name for the recipe and categorize the details as either healthy or unhealthy. Make sure to flag anything as unhealthy from the ingredients.",
"Ingredients: " + req["ingredients"],
"Use only these ingredients (True/False): " +
str(req["onlyIngredients"]),
"Cookware: " + req["cookware"],
]
response = model.generate_content(prompt_parts, stream=True)

Expand All @@ -48,7 +52,7 @@ def generate_recipe(req: dict):

def generate_prompt(req: str):
prompt_parts = [
"Generate only one prompt of an image that resembles very closely to the provided recipe. Be as specific and detailed as possible according to the recipe guideline. Ensure that the prompt also adheres closely to the given amount per ingredient. Only give the plain text of the prompt idea without any formatting or headers.",
"Generate only one prompt of an image that resembles very closely to the end result of the provided recipe. Be as specific and detailed as possible according to the recipe guideline. Ensure that the prompt also adheres closely to the given amount per ingredient. Only give the plain text of the prompt idea without any formatting or headers.",
"Recipe details: " + req,
]
response = model.generate_content(prompt_parts)
Expand Down
13 changes: 8 additions & 5 deletions lahacks/lahacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ def dynamic_form():
on_change=FieldState.set_quantity,
value=FieldState.quantity
),
rx.select(["grams", "oz", "fl oz", "gallon(s)", "piece(s)", "slice(s)", "can(s)", "jar(s)", "bottle(s)", "jug(s)", "bag(s)"], name="unit", placeholder="Units (optional)",
rx.select(["no unit", "grams", "oz", "fl oz", "gallon(s)", "piece(s)", "slice(s)", "can(s)", "jar(s)", "bottle(s)", "jug(s)", "bag(s)"], name="unit", placeholder="Units (optional)",
on_change=FieldState.set_unit, value=FieldState.unit),
rx.button("+", on_click=DynamicFormState.add_field(
FieldState.ingredient,
FieldState.quantity,
FieldState.unit
FieldState.unit,
), type="button", style=button_style),
rx.button("Clear", on_click=FieldState.reset_vals,
type="button", style=button_style),
),
rx.checkbox("Use only these ingredients?", size="3",
on_change=DynamicFormState.set_only_ingredients, checked=DynamicFormState.onlyIngredients, name="onlyIngredients"),
rx.input.root(rx.input(
placeholder="Enter your cookware (optional)",
name="cookware",
on_change=FieldState.set_cookware,
value=FieldState.cookware,
), width="100%"),
rx.text("Press the + button to add your ingredient."),
rx.spacer(),
rx.hstack(
Expand Down Expand Up @@ -72,10 +78,8 @@ def dynamic_form():
on_submit=DynamicFormState.handle_submit,
reset_on_submit=True,
height="100%",
max_width="100%",
),
height="100%",
max_width="90vw",
)


Expand All @@ -94,7 +98,6 @@ def index() -> rx.Component:
padding=20,
),
height="100%",

),
height="100vh",
paddingX=10,
Expand Down
33 changes: 26 additions & 7 deletions lahacks/pages/components/recipe_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
from lahacks.styles.styles import button_style


def notification():
return rx.dialog.root(
rx.dialog.trigger(
rx.button("View Recipe Image", style=button_style)),
rx.dialog.content(
rx.dialog.title("Generated Recipe Image"),
rx.image(
src=DynamicFormState.imageLink,
alt="Recipe Image",
width="100%",
height="100%",
border_radius=10,
),
rx.dialog.close(
rx.button("Close",
style=button_style),
margin_top=20,
),
),
)


@rx.page(route="/output/")
def output():
return rx.center(
Expand All @@ -14,13 +36,10 @@ def output():
rx.cond(
DynamicFormState.imageLink == "",
rx.box(rx.text("Cooking something up..."), rx.html(
'''<dotlottie-player src="https://lottie.host/f3bf595a-c177-4b15-bd92-7b77c9c1cb7f/5rP9GZJohZ.json" background="transparent" speed="1" style="width: 300px; height: 300px;" loop autoplay></dotlottie-player>''')),
rx.image(
src=DynamicFormState.imageLink,
alt="Recipe Image",
width="100%",
height="100%",
)),
'''<dotlottie-player src="https://lottie.host/f3bf595a-c177-4b15-bd92-7b77c9c1cb7f/5rP9GZJohZ.json" background="transparent" speed="1" style="width: 300px; height: 300px;" loop autoplay></dotlottie-player>'''),
),
notification()
),
rx.heading("Generated Recipe"),
rx.markdown(DynamicFormState.ai_response),
height="100%",
Expand Down
4 changes: 4 additions & 0 deletions lahacks/states/field_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FieldState(rx.State):
ingredient: str = ""
quantity: str = ""
unit: str = ""
cookware: str = ""

def set_ingredient(self, ingredient: str):
self.ingredient = ingredient
Expand All @@ -24,6 +25,9 @@ def set_quantity(self, quantity: str):
def set_unit(self, unit: str):
self.unit = unit

def set_cookware(self, cookware: str):
self.cookware = cookware

def reset_vals(self):
self.ingredient = ""
self.quantity = "0"
Expand Down
8 changes: 3 additions & 5 deletions lahacks/states/form_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from lahacks.states.field_state import FieldState
from lahacks.api.gemini import generate_recipe, generate_prompt
from lahacks.pages.text2img import text2img
import os

file_path = "assets/meal1.png"


class DynamicFormState(rx.State):
Expand Down Expand Up @@ -45,6 +42,8 @@ def handle_submit(self, form_data: dict):
f"{field[1]}{field[2]} {field[0]}"
for field in self.form_fields
]),
"onlyIngredients": self.onlyIngredients,
"cookware": form_data.get("cookware", "Decide based on the recipe."),
})

for chunk in chunks:
Expand All @@ -63,6 +62,7 @@ def handle_reset(self):
FieldState.ingredient = ""
FieldState.quantity = ""
FieldState.unit = ""
FieldState.cookware = ""
yield
self.ai_response = ""
self.form_data = {}
Expand All @@ -71,5 +71,3 @@ def handle_reset(self):
self.cantSubmit = True
self.onlyIngredients = False
yield
if os.path.exists(file_path):
os.remove(file_path)

0 comments on commit 5276e42

Please sign in to comment.