-
Notifications
You must be signed in to change notification settings - Fork 4
/
π€_AI_Chef.py
240 lines (172 loc) Β· 7.08 KB
/
π€_AI_Chef.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from dotenv import load_dotenv
import streamlit as st
import os
import openai
import csv
import json
import pandas as pd
import redis
import hashlib
import socket
import ast
st.set_page_config(
page_title="AI Chef",
page_icon="π€")
@st.cache_data
def userid():
ip_address = socket.gethostbyname(socket.gethostname())
hash_object = hashlib.sha256(ip_address.encode())
hex_dig = hash_object.hexdigest()
user_id = hex_dig[:8]
return user_id
user_id = userid()
load_dotenv()
r_host = os.environ.get('RD_HOST')
r_port = os.environ.get('RD_PORT')
r_pass = os.environ.get('RD_PASS')
@st.cache_data
def redis_call(host, port, password):
r = redis.Redis(
host=host,
port=port,
password=password)
keys = r.keys()
values = r.mget(keys)
data = {}
for key, value in zip(keys, values):
data[f"{key.decode()}"] = f"{value.decode()}"
return data
def subtruct_ingredients(ingredient_list):
r = redis.Redis(
host=r_host,
port=r_port,
password=r_pass)
try:
for item in ingredient_list:
ing_key = item[0]
ing_value = int(item[1])
# temporary fix for Chicken Breas extra space in the key in Redis
if ing_key == 'Chicken Breast':
ing_key = 'Chicken Breast '
redis_value = r.get(ing_key)
# converting JSON string to Dictionary
json_data = json.loads(redis_value)
current_value = float(json_data['quantity'])
new_value = current_value - ing_value
json_data['quantity'] = str(new_value)
# convert dictionary back to JSON string
new_value_str = json.dumps(json_data)
# Save updated value back to Redis
r.set(ing_key, new_value_str)
except Exception as e:
st.text(e)
keys = r.keys()
values = r.mget(keys)
data = {}
for key, value in zip(keys, values):
data[f"{key.decode()}"] = f"{value.decode()}"
return (data)
st.image("./assets/dalle_cover_lynx.png", use_column_width=True)
st.title("π€ AI Chef")
data = redis_call(r_host, r_port, r_pass)
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
if not OPENAI_API_KEY:
OPENAI_API_KEY = st.sidebar.text_input('Your OpenAI API')
cuisine = st.sidebar.selectbox('Type of Cuisine',
["Random", "Chinese", "French", "Greek", "Indian", "Italian", "Japanese", "Korean", "Mexican", "Middle Eastern", "Spanish", "Thai"])
diet = st.sidebar.selectbox('Type of Diet',
["Any", "Weight Loss", "Balanced", "Muscle Gain"])
portion = st.sidebar.select_slider('Number of Portions/People',
options=[1, 2, 3, 4, 5, "custom"])
if portion == "custom":
portion = st.sidebar.text_input("Number of People")
prep_time = st.sidebar.select_slider('Maximum Preparation Time (in minutes)',
options=[15, 30, 45, 60, 'Unlimited'])
if prep_time == 'Unlimited':
prep_time = 180
# openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY
@st.cache_data
def recipe_generator(data, cuisine, diet, portion, prep_time):
response = openai.Completion.create(
# model="text-ada-001",
model="text-davinci-003",
prompt=f"Create a recipe and cooking steps based on the items in this json file {data} in {cuisine} cuisine style, for a person on {diet} diet plan in mind. The recipe should be for {portion} persons, only one portion per person and within {prep_time} minutes of preparation and cooking time. Don't use all the ingredients and use the best possible combination economically. give the oil, spices, salt or chillies in minimal amount. provide the total calorie count of the meal per portion. the output should be a dictionary named {user_id}. there should be four keys, 'recipe_name', 'ingredients','cooking_steps' and 'calorie_count'. give 'ingredients' as a list ['ingredient','quantity' ,'unit'], 'cooking_steps' key should be a list of the cooking steps, 'calorie_count' key will have just the calorie value per person.",
temperature=0.7,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response
def is_valid_json(myjson):
try:
json.loads(myjson)
except ValueError:
return False
return True
def cook():
raw_output = recipe_generator(data, cuisine, diet, portion, prep_time)
recipe = raw_output['choices'][0]['text']
recipe = recipe.replace("'", "\"")
try:
recipe = ast.literal_eval(recipe.split(' = ')[1])
st.subheader(f"Recipe Name: {recipe['recipe_name']}")
st.markdown(f"**Calorie Count:** {recipe['calorie_count']}\n")
st.subheader("Ingredients:\n")
ingredients = recipe['ingredients']
ingredients_list = ""
for item in ingredients:
ingredients_list += f"* {item[0]}: {item[1]} {item[2]}\n"
st.markdown(f"{ingredients_list}\n")
st.subheader("Cooking Steps:\n")
steps_pretty = ""
for step in recipe['cooking_steps']:
steps_pretty += f"- {step}\n"
st.markdown(f"{steps_pretty}\n")
# after the action of accepting the cooking step, we need to update the data in redis
# subtruct_ingredients(ingredients)
# return recipe
except Exception as e:
# st.text('An Exception occured: ', e)
st.text(recipe.split(' = ')[1])
# return recipe.split(' = ')[1]
# if 'recipe' not in st.session_state:
# st.sidebar.button('Give me something to Cook!', key='cook')
# else:
# st.write(st.session_state['recipe'])
# c1, c2 = st.columns(2)
# if c1.button("Save Recipe"):
# recipe = cook()
# with open(f"./assets/recipes/{recipe['recipe_name'].lower()}.txt", "w") as r:
# r.write(f"Recipe Name: {recipe['recipe_name']}\n")
# r.write(f"Calorie Count: {recipe['calorie_count']}\n")
# r.write(f"Ingredients: \n{ingredients_list}\n")
# r.write(f"Cooking Steps: \n{steps_pretty}\n")
# st.success("Recipe Saved!")
# if st.button('I want something else'):
# # Rerun the task and update the session state
# st.session_state['recipe'] = cook()
# if st.sidebar.button('Clear All'):
# # Clear the session state
# st.session_state.pop('recipe', None)
# Create a session state object
if 'output' not in st.session_state:
st.session_state.key = None
# Define a function to run the long-running task and update the session state
def run_task():
st.session_state.output = cook()
# Define a function to save the output to a file
def save_output():
if st.session_state.output:
with open("./assets/recipes.txt", "a") as f:
json.dump(st.session_state.output, f)
f.write("\n\n")
# Create a button to run the task
if st.sidebar.button("Give me something to Cook!"):
run_task()
st.write(st.session_state.output)
# Create a button to save the output
if st.button("Save Recipe!"):
save_output()
st.success("Recipe Saved π")