recipes_app / app.py
adrianpierce's picture
Update app.py
8fec388
raw
history blame
4.43 kB
# imports
import streamlit as st
import numpy as np
import pandas as pd
import re
import json
import openai
openai.api_key = st.secrets["open_ai_key"]
# state management
if 'gpt_response' not in st.session_state:
st.session_state.gpt_response = None
# app
st.title("Let's get cooking")
user_direction = st.text_area(
"What do you want to cook? Describe anything - a dish, cuisine, event, or vibe.",
placeholder="quick snack, asian style bowl with either noodles or rice, something italian",
)
serving_size = st.number_input(
"How many people are you cooking for?",
min_value=1,
max_value=100,
value=2,
step=1
)
difficulty_dictionary = {
"Quick & Easy": {
"description": "Easy recipes with straightforward instructions. Ideal for beginners or those seeking quick and simple cooking.",
"gpt_instruction": "Easy: provide a quick and easy recipe with simple/straightfoward ingredients and instructions."
},
"Intermediate": {
"description": "Recipes with some intricate steps that invite a little challenge. Perfect for regular cooks wanting to expand their repertoire with new ingredients and techniques.",
"gpt_instruction": "Intermediate: provide an intermediate recipe with some intricate ingredients and techniques."
},
"Professional": {
"description": "Complex recipes that demand a high level of skill and precision. Suited for seasoned cooks aspiring to professional-level sophistication and creativity.",
"gpt_instruction": "Professional: provide a restaurant quality dish that is innovative/experimental and uses a wide variety of ingredients and techniques."
}
}
selected_difficulty = st.radio(
"Choose a difficulty level for your recipe.",
[
list(difficulty_dictionary.keys())[0],
list(difficulty_dictionary.keys())[1],
list(difficulty_dictionary.keys())[2]
],
captions = [
difficulty_dictionary["Quick & Easy"]["description"],
difficulty_dictionary["Intermediate"]["description"],
difficulty_dictionary["Professional"]["description"]
]
)
exclusions = st.text_area(
"Any ingredients you want to exclude?",
placeholder="gluten, dairy, nuts, cilantro",
)
user_inputs = {
"user_direction" : user_direction,
"exclusions": exclusions,
"serving_size": serving_size,
"difficulty": difficulty_dictionary[selected_difficulty]['gpt_instruction']
}
def generate_recipe(user_inputs):
with st.spinner('Building the perfect recipe for you...'):
context = """You are an expert chef assistant that can make a wide variety of recipes based on user input.
Output a recipe in a valid JSON object with the following properties:
recipe_name (string): provide a name for the generated recipe
recipe_serving_size (string): the serving size of the recipe (example: "4 people")
recipe_time (string): the amount of time required to make the recipe (example: "60 minutes (Preparation: 20 minutes, Baking: 40 minutes)")
recipe_ingredients (string): python list of ingredients required to make the recipe
recipe_instructions (string): python list of instructions to make the recipe
"""
messages = [
{"role": "system", "content": context},
{"role": "user", "content": f'user_input={str(user_inputs)}'}
]
st.session_state.gpt_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.25
)
st.button(label='Submit', on_click=generate_recipe, kwargs=dict(user_inputs=user_inputs))
if st.session_state.gpt_response is not None:
st.divider()
loaded_recipe = json.loads(st.session_state.gpt_response['choices'][0]['message']['content'])
st.header(loaded_recipe['recipe_name'])
st.write(f"**Serving Size: {loaded_recipe['recipe_serving_size']}**")
st.write(f"**Time To Make: {loaded_recipe['recipe_time']}**")
st.subheader("Ingredients:")
md_ingredients = ''
for ingredient in loaded_recipe['recipe_ingredients']:
md_ingredients += "- " + ingredient + "\n"
st.markdown(md_ingredients)
st.subheader("Instructions:")
md_instructions = ''
for instruction in loaded_recipe['recipe_instructions']:
md_instructions += "- " + instruction + "\n"
st.markdown(md_instructions)