Skip to content

Commit

Permalink
qol update
Browse files Browse the repository at this point in the history
  • Loading branch information
BustosAndrew committed Apr 23, 2024
1 parent e333533 commit 0a6b8b6
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lahacks/api/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def generate_recipe(req: dict):

def generate_prompt(req: str):
prompt_parts = [
"Generate only one prompt of what an image should look like from the provided recipe details, including all the ingredients. Only give the plain text of the prompt idea without any formatting or headers, and be as accurate as possible.",
"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.",
"Recipe details: " + req,
]
response = model.generate_content(prompt_parts)
Expand Down
49 changes: 29 additions & 20 deletions lahacks/lahacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from lahacks.pages.components.recipe_image import output
from lahacks.states.form_state import DynamicFormState
from lahacks.states.field_state import FieldState
from lahacks.styles.styles import button_style


def dynamic_form():
Expand All @@ -10,12 +11,12 @@ def dynamic_form():
rx.vstack(
rx.foreach(
DynamicFormState.form_fields,
lambda field: rx.hstack(
lambda field: rx.box(rx.vstack(
rx.vstack(rx.heading("Ingredient", size="4"),
rx.text(field[0])),
rx.vstack(rx.heading("Weight", size="4"),
rx.text(field[1] + field[2]),),
),
rx.text(field[0], style={"overflow-wrap": "break-word", "word-wrap": "break-word"})),
rx.heading("Amount", size="4"),
rx.text(field[1] + " " + field[2]),
), border_width=1, border_color="gray-300", border_radius=10, padding=10, width="100%"),
),
rx.hstack(
rx.input(
Expand All @@ -25,32 +26,37 @@ def dynamic_form():
on_change=FieldState.set_ingredient
),
rx.input(
placeholder="Quantity",
name="quantity",
# value=f"{FieldState.quantity}",
on_change=FieldState.set_quantity
placeholder="Amount",
name="amount",
on_change=FieldState.set_quantity,
value=FieldState.quantity
),
rx.select(["g", "oz"], name="unit", value=FieldState.unit,
on_change=FieldState.set_unit),
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)",
on_change=FieldState.set_unit, value=FieldState.unit),
rx.button("+", on_click=DynamicFormState.add_field(
FieldState.ingredient,
FieldState.quantity,
FieldState.unit
), type="button"),
), type="button", style=button_style),
rx.button("Clear", on_click=FieldState.reset_vals,
type="button"),
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.text("Press the + button to add your ingredient."),
rx.spacer(),
rx.hstack(
rx.button(DynamicFormState.buttonText, type="submit",
disabled=DynamicFormState.cantSubmit),
disabled=DynamicFormState.cantSubmit, style=button_style),
rx.button(
"Reset", on_click=DynamicFormState.handle_reset, type="button"),
"Reset", on_click=DynamicFormState.handle_reset, type="button", style=button_style),
rx.cond(
DynamicFormState.submitted,
rx.text("Recipe generating!"),
rx.html('''<script src="https://unpkg.com/@dotlottie/player-component@latest/dist/dotlottie-player.mjs" type="module"></script>
<dotlottie-player src="https://lottie.host/d395e1a4-28dc-4e60-bffd-8a8ed8844318/qvoNVQ79Bc.json" background="transparent" speed="1" style="width: 50px; height: 40px;" loop autoplay></dotlottie-player>'''),
),
align="center",
),
rx.cond(
DynamicFormState.ai_response != "",
Expand All @@ -62,14 +68,16 @@ def dynamic_form():
as_="span"
)),
),
height="100%"
height="100%",
),
on_mount=DynamicFormState.handle_reset,
on_submit=DynamicFormState.handle_submit,
reset_on_submit=True,
height="100%"
height="100%",
max_width="100%",
),
height="100%"
height="100%",
max_width="90vw",
)


Expand All @@ -86,9 +94,10 @@ def index() -> rx.Component:
padding=20,
),
height="100%",
padding=20,

),
height="100vh",
paddingX=10,
)


Expand Down
3 changes: 2 additions & 1 deletion lahacks/pages/components/recipe_image.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import reflex as rx
from lahacks.states.form_state import DynamicFormState
from lahacks.styles.styles import button_style


@rx.page(route="/output/")
def output():
return rx.center(
rx.vstack(
rx.link(rx.button("Go Back", _hover={"cursor": "pointer"},), href="/",
rx.link(rx.button("Go Back", _hover={"cursor": "pointer"}, style=button_style), href="/",
),
rx.cond(
DynamicFormState.imageLink == "",
Expand Down
17 changes: 8 additions & 9 deletions lahacks/states/field_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@

class FieldState(rx.State):
ingredient: str = ""
quantity: int = 0
unit: str = "g"
details: str = ""
quantity: str = ""
unit: str = ""

def set_ingredient(self, ingredient: str):
self.ingredient = ingredient

def set_quantity(self, quantity: str):
if quantity == "":
self.quantity = 0
self.quantity = "0"
return
elif not quantity.isnumeric():
self.quantity = 0
self.quantity = "0"
return
elif int(quantity) < 0:
self.quantity = 0
self.quantity = "0"
return
self.quantity = int(quantity)
self.quantity = quantity

def set_unit(self, unit: str):
self.unit = unit

def reset_vals(self):
self.ingredient = ""
self.quantity = 0
self.unit = "g"
self.quantity = "0"
self.unit = ""
12 changes: 10 additions & 2 deletions lahacks/states/form_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class DynamicFormState(rx.State):
cantSubmit: bool = True
buttonText: str = "Submit"
imageLink: str = ""
onlyIngredients: bool = False

def set_only_ingredients(self, value: bool):
self.onlyIngredients = value

def add_field(self, ingredient: str, quantity: int, unit: str):
if not ingredient:
Expand Down Expand Up @@ -56,12 +60,16 @@ def handle_submit(self, form_data: dict):
self.imageLink = text2img(recipe_prompt)

def handle_reset(self):
FieldState.ingredient = ""
FieldState.quantity = ""
FieldState.unit = ""
yield
self.ai_response = ""
self.form_data = {}
self.form_fields = []
FieldState.ingredient = ""
FieldState.quantity = 0
self.imageLink = ""
self.cantSubmit = True
self.onlyIngredients = False
yield
if os.path.exists(file_path):
os.remove(file_path)
4 changes: 4 additions & 0 deletions lahacks/styles/styles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shadow = "rgba(0, 0, 0, 0.15) 0px 2px 8px"
button_style = dict(
box_shadow=shadow,
)
13 changes: 11 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
diffusers==0.27.2
flax==0.8.2
google-generativeai==0.5.2
grpcio-status==1.62.2
importlib_resources==6.4.0
networkx==3.3
openai==1.23.2
opt-einsum==3.3.0
optax==0.2.2
orbax-checkpoint==0.5.10
pillow==10.3.0
reflex==0.4.8.post1
regex==2024.4.16
safetensors==0.4.3
sympy==1.12
tokenizers==0.19.1
update==0.0.1

0 comments on commit 0a6b8b6

Please sign in to comment.