OmPrakashSingh1704 commited on
Commit
3307cd1
1 Parent(s): e61e024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -85
app.py CHANGED
@@ -1,25 +1,30 @@
1
  import streamlit as st
2
- import re, ast,os
3
  import pandas as pd
4
  from huggingface_hub import login, InferenceClient
5
  import pickle
6
- from sklearn.metrics.pairwise import cosine_similarity
7
 
8
  st.set_page_config(layout="wide")
9
 
 
10
  login(token=os.getenv("TOKEN"))
11
 
 
12
  with open('l.pkl', 'rb') as file:
13
  similarity = pickle.load(file)
14
 
 
15
  with open('items_dict.pkl', 'rb') as file:
16
  items_dict = pickle.load(file)
17
 
18
- data=pd.read_csv('marketing_sample_for_walmart_com-product_details__20200101_20200331__30k_data.csv')
 
19
 
20
  # Initialize the inference client for the Mixtral model
21
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
22
 
 
23
  if 'recipe' not in st.session_state:
24
  st.session_state.recipe = None
25
 
@@ -27,7 +32,7 @@ if 'recipe_saved' not in st.session_state:
27
  st.session_state.recipe_saved = None
28
 
29
  if 'user_direction' not in st.session_state:
30
- st.session_state.user_direction = None
31
 
32
  if 'serving_size' not in st.session_state:
33
  st.session_state.serving_size = 2
@@ -36,7 +41,7 @@ if 'selected_difficulty' not in st.session_state:
36
  st.session_state.selected_difficulty = "Quick & Easy"
37
 
38
  if 'exclusions' not in st.session_state:
39
- st.session_state.exclusions = None
40
 
41
 
42
  def create_detailed_prompt(user_direction, exclusions, serving_size, difficulty):
@@ -69,60 +74,6 @@ def generate_recipe(user_inputs):
69
  prompt = create_detailed_prompt(user_inputs['user_direction'], user_inputs['exclusions'],
70
  user_inputs['serving_size'], user_inputs['difficulty'])
71
 
72
- functions = [
73
- {
74
- "name": "provide_recipe",
75
- "description": "Provides a detailed recipe strictly adhering to the user input/specifications, especially ingredient exclusions and the recipe difficulty",
76
- "parameters": {
77
- "type": "object",
78
- "properties": {
79
- "name": {
80
- "type": "string",
81
- "description": "A creative name for the recipe"
82
- },
83
- "description": {
84
- "type": "string",
85
- "description": "a brief one-sentence description of the provided recipe"
86
- },
87
- "ingredients": {
88
- "type": "array",
89
- "items": {
90
- "type": "object",
91
- "properties": {
92
- "name": {
93
- "type": "string",
94
- "description": "Quantity and name of the ingredient"
95
- }
96
- }
97
- }
98
- },
99
- "instructions": {
100
- "type": "array",
101
- "items": {
102
- "type": "object",
103
- "properties": {
104
- "step_number": {
105
- "type": "number",
106
- "description": "The sequence number of this step"
107
- },
108
- "instruction": {
109
- "type": "string",
110
- "description": "Detailed description of what to do in this step"
111
- }
112
- }
113
- }
114
- }
115
- },
116
- "required": [
117
- "name",
118
- "description",
119
- "ingredients",
120
- "instructions"
121
- ],
122
- },
123
- }
124
- ]
125
-
126
  generate_kwargs = dict(
127
  temperature=0.9,
128
  max_new_tokens=10000,
@@ -131,16 +82,17 @@ def generate_recipe(user_inputs):
131
  do_sample=True,
132
  )
133
 
134
- prompt += f"\nPlease format the output in JSON. The JSON should include fields for 'name', 'description', 'ingredients', and 'instructions', with each field structured as described below.\n\n{functions}"
135
-
136
  response = client.text_generation(prompt, **generate_kwargs)
137
- st.session_state.recipe = response
 
 
 
138
  st.session_state.recipe_saved = False
139
 
140
 
141
  def clear_inputs():
142
- st.session_state.user_direction = None
143
- st.session_state.exclusions = None
144
  st.session_state.serving_size = 2
145
  st.session_state.selected_difficulty = "Quick & Easy"
146
 
@@ -149,7 +101,7 @@ st.title("Let's get cooking")
149
  st.session_state.user_direction = st.text_area(
150
  "What do you want to cook? Describe anything - a dish, cuisine, event, or vibe.",
151
  value=st.session_state.user_direction,
152
- placeholder="quick snack, asian style bowl with either noodles or rice, something italian",
153
  )
154
 
155
  st.session_state.serving_size = st.number_input(
@@ -174,19 +126,14 @@ difficulty_dictionary = {
174
 
175
  st.session_state.selected_difficulty = st.radio(
176
  "Choose a difficulty level for your recipe.",
177
- [
178
- list(difficulty_dictionary.keys())[0],
179
- list(difficulty_dictionary.keys())[1],
180
- list(difficulty_dictionary.keys())[2]
181
- ],
182
- captions=[
183
- difficulty_dictionary["Quick & Easy"]["description"],
184
- difficulty_dictionary["Intermediate"]["description"],
185
- difficulty_dictionary["Professional"]["description"]
186
- ],
187
  index=list(difficulty_dictionary).index(st.session_state.selected_difficulty)
188
  )
189
 
 
 
 
 
190
  st.session_state.exclusions = st.text_area(
191
  "Any ingredients you want to exclude?",
192
  value=st.session_state.exclusions,
@@ -194,12 +141,12 @@ st.session_state.exclusions = st.text_area(
194
  )
195
 
196
  fancy_exclusions = ""
197
-
198
  if st.session_state.selected_difficulty == "Professional":
199
  exclude_fancy = st.checkbox(
200
  "Exclude cliche professional ingredients? (gold leaf, truffle, edible flowers, microgreens)",
201
  value=True)
202
- fancy_exclusions = "gold leaf, truffle, edible flowers, microgreens, gold dust"
 
203
 
204
  user_inputs = {
205
  "user_direction": st.session_state.user_direction,
@@ -207,6 +154,7 @@ user_inputs = {
207
  "serving_size": st.session_state.serving_size,
208
  "difficulty": st.session_state.selected_difficulty
209
  }
 
210
  button_cols_submit = st.columns([1, 1, 4])
211
  with button_cols_submit[0]:
212
  st.button(label='Submit', on_click=generate_recipe, kwargs=dict(user_inputs=user_inputs), type="primary",
@@ -216,10 +164,10 @@ with button_cols_submit[1]:
216
  with button_cols_submit[2]:
217
  st.empty()
218
 
219
- if st.session_state.recipe is not None:
220
  st.divider()
221
- print(st.session_state.recipe, "\n\n\n\n")
222
- recipe = ast.literal_eval(st.session_state.recipe)
223
  name_and_dis = f'# {recipe["name"]}\n\n'
224
  name_and_dis += f'{recipe["description"]}\n\n'
225
  ingredients = '## Ingredients:\n'
@@ -232,9 +180,6 @@ if st.session_state.recipe is not None:
232
  st.write(name_and_dis)
233
  col01, col02 = st.columns(2)
234
  with col01:
235
- cont = st.container(border=True, height=500)
236
- cont.write(ingredients)
237
  with col02:
238
- cont = st.container(border=True, height=500)
239
- cont.write(instructions)
240
- st.write("")
 
1
  import streamlit as st
2
+ import os
3
  import pandas as pd
4
  from huggingface_hub import login, InferenceClient
5
  import pickle
6
+ import json
7
 
8
  st.set_page_config(layout="wide")
9
 
10
+ # Authenticate with Hugging Face
11
  login(token=os.getenv("TOKEN"))
12
 
13
+ # Load similarity data
14
  with open('l.pkl', 'rb') as file:
15
  similarity = pickle.load(file)
16
 
17
+ # Load items dictionary
18
  with open('items_dict.pkl', 'rb') as file:
19
  items_dict = pickle.load(file)
20
 
21
+ # Load dataset
22
+ data = pd.read_csv('marketing_sample_for_walmart_com-product_details__20200101_20200331__30k_data.csv')
23
 
24
  # Initialize the inference client for the Mixtral model
25
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
26
 
27
+ # Initialize session state variables
28
  if 'recipe' not in st.session_state:
29
  st.session_state.recipe = None
30
 
 
32
  st.session_state.recipe_saved = None
33
 
34
  if 'user_direction' not in st.session_state:
35
+ st.session_state.user_direction = ""
36
 
37
  if 'serving_size' not in st.session_state:
38
  st.session_state.serving_size = 2
 
41
  st.session_state.selected_difficulty = "Quick & Easy"
42
 
43
  if 'exclusions' not in st.session_state:
44
+ st.session_state.exclusions = ""
45
 
46
 
47
  def create_detailed_prompt(user_direction, exclusions, serving_size, difficulty):
 
74
  prompt = create_detailed_prompt(user_inputs['user_direction'], user_inputs['exclusions'],
75
  user_inputs['serving_size'], user_inputs['difficulty'])
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  generate_kwargs = dict(
78
  temperature=0.9,
79
  max_new_tokens=10000,
 
82
  do_sample=True,
83
  )
84
 
 
 
85
  response = client.text_generation(prompt, **generate_kwargs)
86
+ try:
87
+ st.session_state.recipe = json.loads(response)
88
+ except json.JSONDecodeError:
89
+ st.session_state.recipe = None
90
  st.session_state.recipe_saved = False
91
 
92
 
93
  def clear_inputs():
94
+ st.session_state.user_direction = ""
95
+ st.session_state.exclusions = ""
96
  st.session_state.serving_size = 2
97
  st.session_state.selected_difficulty = "Quick & Easy"
98
 
 
101
  st.session_state.user_direction = st.text_area(
102
  "What do you want to cook? Describe anything - a dish, cuisine, event, or vibe.",
103
  value=st.session_state.user_direction,
104
+ placeholder="quick snack, Asian style bowl with either noodles or rice, something Italian",
105
  )
106
 
107
  st.session_state.serving_size = st.number_input(
 
126
 
127
  st.session_state.selected_difficulty = st.radio(
128
  "Choose a difficulty level for your recipe.",
129
+ options=list(difficulty_dictionary.keys()),
 
 
 
 
 
 
 
 
 
130
  index=list(difficulty_dictionary).index(st.session_state.selected_difficulty)
131
  )
132
 
133
+ for level, details in difficulty_dictionary.items():
134
+ if level == st.session_state.selected_difficulty:
135
+ st.caption(details["description"])
136
+
137
  st.session_state.exclusions = st.text_area(
138
  "Any ingredients you want to exclude?",
139
  value=st.session_state.exclusions,
 
141
  )
142
 
143
  fancy_exclusions = ""
 
144
  if st.session_state.selected_difficulty == "Professional":
145
  exclude_fancy = st.checkbox(
146
  "Exclude cliche professional ingredients? (gold leaf, truffle, edible flowers, microgreens)",
147
  value=True)
148
+ if exclude_fancy:
149
+ fancy_exclusions = "gold leaf, truffle, edible flowers, microgreens, gold dust"
150
 
151
  user_inputs = {
152
  "user_direction": st.session_state.user_direction,
 
154
  "serving_size": st.session_state.serving_size,
155
  "difficulty": st.session_state.selected_difficulty
156
  }
157
+
158
  button_cols_submit = st.columns([1, 1, 4])
159
  with button_cols_submit[0]:
160
  st.button(label='Submit', on_click=generate_recipe, kwargs=dict(user_inputs=user_inputs), type="primary",
 
164
  with button_cols_submit[2]:
165
  st.empty()
166
 
167
+ if st.session_state.recipe:
168
  st.divider()
169
+ print(st.session_state.recipe)
170
+ recipe = st.session_state.recipe
171
  name_and_dis = f'# {recipe["name"]}\n\n'
172
  name_and_dis += f'{recipe["description"]}\n\n'
173
  ingredients = '## Ingredients:\n'
 
180
  st.write(name_and_dis)
181
  col01, col02 = st.columns(2)
182
  with col01:
183
+ st.write(ingredients)
 
184
  with col02:
185
+ st.write(instructions)