Spaces:
Running
Running
adrianpierce
commited on
Commit
•
1863ed9
1
Parent(s):
fea45d9
Update Home.py
Browse files
Home.py
CHANGED
@@ -6,15 +6,6 @@ import json
|
|
6 |
from openai import OpenAI
|
7 |
import secrets
|
8 |
|
9 |
-
# f = open('test_input.json')
|
10 |
-
# json_test = json.load(f)
|
11 |
-
# st.json(json_test)
|
12 |
-
|
13 |
-
# # The 'with' statement ensures that the file is properly closed after its suite finishes
|
14 |
-
# with open('/data/test_output.json', 'w') as f:
|
15 |
-
# # Use json.dump() to write the dictionary to the file
|
16 |
-
# json.dump(new_data, f, indent=4) # `indent=4` is for pretty-printing the JSON file
|
17 |
-
|
18 |
client = OpenAI(
|
19 |
api_key = st.secrets["open_ai_key"]
|
20 |
)
|
@@ -23,9 +14,6 @@ client = OpenAI(
|
|
23 |
if 'gpt_response' not in st.session_state:
|
24 |
st.session_state.gpt_response = None
|
25 |
|
26 |
-
if "copied" not in st.session_state:
|
27 |
-
st.session_state.copied = []
|
28 |
-
|
29 |
# functions
|
30 |
def create_detailed_prompt(user_direction, exclusions, serving_size, difficulty):
|
31 |
if difficulty == "Quick & Easy":
|
@@ -117,32 +105,21 @@ def generate_recipe(user_inputs):
|
|
117 |
function_call={"name":"provide_recipe"}, # auto is default, but we'll be explicit
|
118 |
)
|
119 |
|
120 |
-
def on_copy_click(text):
|
121 |
-
st.session_state.copied.append(text)
|
122 |
-
pyperclip.copy(text)
|
123 |
-
st.toast(f"Copied to clipboard!", icon='✅' )
|
124 |
-
|
125 |
def create_safe_filename(recipe_name):
|
126 |
-
#
|
127 |
safe_name = recipe_name.lower()
|
128 |
-
|
129 |
-
# Replace spaces with underscores
|
130 |
safe_name = safe_name.replace(" ", "_")
|
131 |
-
|
132 |
-
# Remove or replace other non-alphanumeric characters (excluding underscore)
|
133 |
safe_name = re.sub(r"[^a-zA-Z0-9_]", "", safe_name)
|
134 |
-
|
135 |
-
# Truncate the name if it's very long
|
136 |
safe_name = (safe_name[:50]) if len(safe_name) > 50 else safe_name
|
137 |
-
|
138 |
-
|
139 |
-
unique_token = secrets.token_hex(8) # This creates a 16-character hexadecimal string
|
140 |
-
|
141 |
-
# Append the token to the safe recipe name
|
142 |
-
safe_filename = f"{unique_token}_{safe_name}" # Assuming you want a .txt file
|
143 |
-
|
144 |
return safe_filename
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
146 |
# app
|
147 |
st.title("Let's get cooking")
|
148 |
user_direction = st.text_area(
|
@@ -220,11 +197,5 @@ if st.session_state.gpt_response is not None:
|
|
220 |
for instruction in recipe['instructions']:
|
221 |
recipe_md += f"{instruction['step_number']}. {instruction['instruction']} \n"
|
222 |
recipe['md'] = recipe_md
|
223 |
-
st.markdown(recipe_md)
|
224 |
-
|
225 |
-
# write to file
|
226 |
-
filename = create_safe_filename(recipe["name"])
|
227 |
-
# with open(f"/data/generated/{filename}.json", "w") as f:
|
228 |
-
# json.dump(recipe, f)
|
229 |
-
with open(f'/data/{filename}.json', 'w') as f:
|
230 |
-
json.dump(recipe, f, indent=4) # `indent=4` is for pretty-printing the JSON file
|
|
|
6 |
from openai import OpenAI
|
7 |
import secrets
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
client = OpenAI(
|
10 |
api_key = st.secrets["open_ai_key"]
|
11 |
)
|
|
|
14 |
if 'gpt_response' not in st.session_state:
|
15 |
st.session_state.gpt_response = None
|
16 |
|
|
|
|
|
|
|
17 |
# functions
|
18 |
def create_detailed_prompt(user_direction, exclusions, serving_size, difficulty):
|
19 |
if difficulty == "Quick & Easy":
|
|
|
105 |
function_call={"name":"provide_recipe"}, # auto is default, but we'll be explicit
|
106 |
)
|
107 |
|
|
|
|
|
|
|
|
|
|
|
108 |
def create_safe_filename(recipe_name):
|
109 |
+
# format and generate random URL-safe text string
|
110 |
safe_name = recipe_name.lower()
|
|
|
|
|
111 |
safe_name = safe_name.replace(" ", "_")
|
|
|
|
|
112 |
safe_name = re.sub(r"[^a-zA-Z0-9_]", "", safe_name)
|
|
|
|
|
113 |
safe_name = (safe_name[:50]) if len(safe_name) > 50 else safe_name
|
114 |
+
unique_token = secrets.token_hex(8)
|
115 |
+
safe_filename = f"{unique_token}_{safe_name}"
|
|
|
|
|
|
|
|
|
|
|
116 |
return safe_filename
|
117 |
+
|
118 |
+
def save_recipe():
|
119 |
+
filename = create_safe_filename(recipe["name"])
|
120 |
+
with open(f'/data/{filename}.json', 'w') as f:
|
121 |
+
json.dump(recipe, f, indent=4)
|
122 |
+
|
123 |
# app
|
124 |
st.title("Let's get cooking")
|
125 |
user_direction = st.text_area(
|
|
|
197 |
for instruction in recipe['instructions']:
|
198 |
recipe_md += f"{instruction['step_number']}. {instruction['instruction']} \n"
|
199 |
recipe['md'] = recipe_md
|
200 |
+
st.markdown(recipe_md)
|
201 |
+
st.button("Save Recipe", on_click=save_recipe, disabled=False)
|
|
|
|
|
|
|
|
|
|
|
|