adrianpierce commited on
Commit
4d2c0f8
β€’
1 Parent(s): 6905d51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -2
app.py CHANGED
@@ -4,6 +4,8 @@ import pandas as pd
4
  import re
5
  import json
6
  from openai import OpenAI
 
 
7
 
8
  client = OpenAI(
9
  api_key = st.secrets["open_ai_key"]
@@ -111,7 +113,28 @@ def on_copy_click(text):
111
  st.session_state.copied.append(text)
112
  clipboard.copy(text)
113
  st.toast(f"Copied to clipboard!", icon='βœ…' )
114
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  # app
116
  st.title("Let's get cooking")
117
  user_direction = st.text_area(
@@ -188,5 +211,10 @@ if st.session_state.gpt_response is not None:
188
  recipe_md += f'\n## Instructions:\n'
189
  for instruction in recipe['instructions']:
190
  recipe_md += f"{instruction['step_number']}. {instruction['instruction']} \n"
 
191
  st.markdown(recipe_md)
192
- # st.button("πŸ“‹", on_click=on_copy_click, args=(full_response,))
 
 
 
 
 
4
  import re
5
  import json
6
  from openai import OpenAI
7
+ import secrets
8
+
9
 
10
  client = OpenAI(
11
  api_key = st.secrets["open_ai_key"]
 
113
  st.session_state.copied.append(text)
114
  clipboard.copy(text)
115
  st.toast(f"Copied to clipboard!", icon='βœ…' )
116
+
117
+ def create_safe_filename(recipe_name):
118
+ # Convert to lowercase
119
+ safe_name = recipe_name.lower()
120
+
121
+ # Replace spaces with underscores
122
+ safe_name = safe_name.replace(" ", "_")
123
+
124
+ # Remove or replace other non-alphanumeric characters (excluding underscore)
125
+ safe_name = re.sub(r"[^a-zA-Z0-9_]", "", safe_name)
126
+
127
+ # Truncate the name if it's very long
128
+ safe_name = (safe_name[:50]) if len(safe_name) > 50 else safe_name
129
+
130
+ # Generate a random URL-safe text string
131
+ unique_token = secrets.token_hex(8) # This creates a 16-character hexadecimal string
132
+
133
+ # Append the token to the safe recipe name
134
+ safe_filename = f"{unique_token}_{safe_name}" # Assuming you want a .txt file
135
+
136
+ return safe_filename
137
+
138
  # app
139
  st.title("Let's get cooking")
140
  user_direction = st.text_area(
 
211
  recipe_md += f'\n## Instructions:\n'
212
  for instruction in recipe['instructions']:
213
  recipe_md += f"{instruction['step_number']}. {instruction['instruction']} \n"
214
+ recipe['md'] = recipe_md
215
  st.markdown(recipe_md)
216
+ # st.button("πŸ“‹", on_click=on_copy_click, args=(full_response,))
217
+ # write to file
218
+ filename = create_safe_filename(recipe["name"])
219
+ with open(f"generated/{filename}.json", 'w') as f:
220
+ json.dump(recipe, f)