Skip to content

Commit

Permalink
what it should've been :)
Browse files Browse the repository at this point in the history
  • Loading branch information
BustosAndrew committed Apr 23, 2024
1 parent 8c7800a commit e333533
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 773 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.db
*.py[cod]
.web
__pycache__/
__pycache__/
.env
5 changes: 2 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ name = "pypi"

[packages]
reflex = "*"
jax = "*"
google-generativeai = "*"
flax = "*"
diffusers = "*"
openai = "*"
update = "*"

[dev-packages]

Expand Down
838 changes: 129 additions & 709 deletions Pipfile.lock

Large diffs are not rendered by default.

Binary file added lahacks/.DS_Store
Binary file not shown.
15 changes: 14 additions & 1 deletion lahacks/api/gemini.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import google.generativeai as genai
import os

genai.configure(api_key="AIzaSyDNOIh-OOaMZb0GG1iNqiTuTAKlejdkL9U")
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))

# Set up the model
generation_config = {
Expand Down Expand Up @@ -39,5 +40,17 @@ def generate_recipe(req: dict):
"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.",
"Ingredients: " + req["ingredients"],
]
response = model.generate_content(prompt_parts, stream=True)

for chunk in response:
yield chunk


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.",
"Recipe details: " + req,
]
response = model.generate_content(prompt_parts)

return response.text
24 changes: 13 additions & 11 deletions lahacks/lahacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def dynamic_form():
rx.input(
placeholder="Quantity",
name="quantity",
value=f"{FieldState.quantity}",
type="number",
min=0,
# value=f"{FieldState.quantity}",
on_change=FieldState.set_quantity
),
rx.select(["g", "oz"], name="unit", value=FieldState.unit,
Expand All @@ -45,20 +43,24 @@ def dynamic_form():
rx.text("Press the + button to add your ingredient."),
rx.spacer(),
rx.hstack(
rx.button("Submit", type="submit",
rx.button(DynamicFormState.buttonText, type="submit",
disabled=DynamicFormState.cantSubmit),
rx.button(
"Reset", on_click=DynamicFormState.handle_reset),
rx.cond(DynamicFormState.submitted,
rx.text("Generating...")),
"Reset", on_click=DynamicFormState.handle_reset, type="button"),
rx.cond(
DynamicFormState.submitted,
rx.text("Recipe generating!"),
),
),
rx.cond(
DynamicFormState.ai_response != "",
rx.link(
"Your recipe results are generated!",
rx.box(rx.text("View your ", size="4", as_="span"), rx.link(
"generated recipe!",
href="/output/",
size='4'
),
underline="always",
size="4",
as_="span"
)),
),
height="100%"
),
Expand Down
17 changes: 10 additions & 7 deletions lahacks/pages/components/recipe_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
from lahacks.states.form_state import DynamicFormState


@rx.page(route="/output/[res]")
@rx.page(route="/output/")
def output():
return rx.center(
# rx.image(
# src="/img/meal1.jpg",
# alt="Recipe Image",
# width="300px",
# height="200px",
# )
rx.vstack(
rx.link(rx.button("Go Back", _hover={"cursor": "pointer"},), href="/",
),
rx.cond(
DynamicFormState.imageLink == "",
rx.text("Generating Image..."),
rx.image(
src=DynamicFormState.imageLink,
alt="Recipe Image",
width="100%",
height="100%",
)),
rx.heading("Generated Recipe"),
rx.markdown(DynamicFormState.ai_response),
height="100%",
Expand Down
Binary file removed lahacks/pages/generated_image_0.png
Binary file not shown.
47 changes: 12 additions & 35 deletions lahacks/pages/text2img.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from openai import OpenAI

from diffusers import FlaxStableDiffusionPipeline
client = OpenAI()

pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jax.numpy.bfloat16
)

def text2img(prompt):
print(prompt)
response = client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
n=1,
)

def text2img(prompt, params):
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50

num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)

# shard inputs and rng
params = replicate(params)
prng_seed = jax.random.split(prng_seed, num_samples)
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, params, prng_seed,
num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(
images.reshape((num_samples,) + images.shape[-3:])))

# Save each image
for i, image in enumerate(images):
# Define the path where you want to save the image
image_path = f"/assets/meal{i}.png"
image.save(image_path) # Save the image
print(f"Image saved as {image_path}")


# text2img(prompt, params)
return response.data[0].url
13 changes: 11 additions & 2 deletions lahacks/states/field_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ class FieldState(rx.State):
def set_ingredient(self, ingredient: str):
self.ingredient = ingredient

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

def set_unit(self, unit: str):
self.unit = unit
Expand Down
33 changes: 29 additions & 4 deletions lahacks/states/form_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import reflex as rx
from lahacks.states.field_state import FieldState
from lahacks.api.gemini import generate_recipe
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 All @@ -9,6 +13,8 @@ class DynamicFormState(rx.State):
ai_response: str = ""
submitted: bool = False
cantSubmit: bool = True
buttonText: str = "Submit"
imageLink: str = ""

def add_field(self, ingredient: str, quantity: int, unit: str):
if not ingredient:
Expand All @@ -19,19 +25,35 @@ def add_field(self, ingredient: str, quantity: int, unit: str):

def handle_submit(self, form_data: dict):
self.submitted = True
self.cantSubmit = True
self.buttonText = "Generating..."
yield
if not self.form_fields:
self.submitted = False
self.cantSubmit = False
self.buttonText = "Submit"
return
self.ai_response = ""
self.form_data = form_data
self.ai_response = generate_recipe({

chunks = generate_recipe({
"ingredients": ", ".join([
f"{field[1]}{field[2]} {field[0]}"
for field in self.form_fields
]),
})
self.form_fields = []
self.submitted = False

for chunk in chunks:
self.ai_response += chunk.text
yield

if self.ai_response:
self.form_fields = []
self.submitted = False
self.buttonText = "Submit"
yield
recipe_prompt = generate_prompt(self.ai_response)
self.imageLink = text2img(recipe_prompt)

def handle_reset(self):
self.ai_response = ""
Expand All @@ -40,3 +62,6 @@ def handle_reset(self):
FieldState.ingredient = ""
FieldState.quantity = 0
self.cantSubmit = True
yield
if os.path.exists(file_path):
os.remove(file_path)

0 comments on commit e333533

Please sign in to comment.