tywei08 commited on
Commit
497e545
·
verified ·
1 Parent(s): 5866b44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -264
app.py CHANGED
@@ -5,34 +5,15 @@ import re
5
  from datetime import datetime
6
  import openai
7
 
8
-
9
- # Assistant Creation function
10
- def create_assistant_json(uploaded_file, assistant_name, assistant_message):
11
- client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
12
- # Check if a file was uploaded
13
- print(uploaded_file)
14
- df = open(uploaded_file, "rb")
15
- file = client.files.create(file=df,
16
- purpose='assistants')
17
-
18
- assistant = client.beta.assistants.create(
19
- name=assistant_name,
20
- instructions=assistant_message,
21
- model="gpt-4-0125-preview",
22
- tools=[
23
- {
24
- "type": "retrieval" # This adds the knowledge base as a tool
25
- }
26
- ],
27
- file_ids=[file.id])
28
-
29
- return assistant.id
30
-
31
  def play_music():
 
32
  music_path = "RPReplay_Final1712757356.mp3"
33
  return music_path, gr.update(visible=True)
34
-
 
35
  def generate_cocktail(mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests):
 
36
  client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
37
  instruction = "Please provide a cocktail recipe given the mood and preference of the user.\n\n"
38
  user_prompt = f"Mood: {mood}\nTaste: Sweetness {sweetness}/10, Sour {sour}/10, Savory {savory}/10, Bitter {bitter}/10\nFlavor Association: {flavor_association}\nDrinking Experience: {drinking_experience}\nLevel of Soberness: {soberness_level}\nAllergies: {allergies}\nAdditional Requests: {additional_requests}\n\nMake sure to avoid all allergic ingredients.\n\n"
@@ -40,242 +21,63 @@ def generate_cocktail(mood, sweetness, sour, savory, bitter, flavor_association,
40
  prompt = instruction + user_prompt + output_format
41
 
42
  messages=[
43
- {"role": "system", "content": "You are a helpful bartender assistant."},
44
- {"role": "user", "content": prompt}
45
- ]
46
  try:
47
  response = client.chat.completions.create(
48
  model="gpt-4-0125-preview",
49
  messages=messages,
50
  max_tokens=1024)
51
  name, quote, ingredients, instruction, notes = extract_info(response.choices[0].message.content)
52
- return format_cocktail_output(name, quote, ingredients, instruction, notes),True
53
  except Exception as e:
54
  return f'<p style="color: white; font-size: 20px;">{str(e)}</p>'
55
-
56
-
57
 
 
58
  def extract_info(output_text):
 
59
  pattern = r"Cocktail Name:(.*?)Quote:(.*?)Ingredients:(.*?)Instruction:(.*?)Notes:(.*?)$"
60
  match = re.search(pattern, output_text, re.DOTALL)
61
  if match:
62
- name = match.group(1)
63
- quote = match.group(2)
64
- ingredients = match.group(3).replace('\n', '<br>')
65
- instruction = match.group(4).replace('\n', '<br>')
66
- notes = match.group(5)
67
  return name, quote, ingredients, instruction, notes
68
  else:
69
  return None
70
-
 
71
  def format_cocktail_output(name, quote, ingredients, instruction, notes):
72
- # Construct the HTML output
73
  html_output = f'''
74
  <div style="text-align: center; font-family: 'Verdana', sans-serif; color: white;">
75
- <h1 style="font-size: 48px; color: white;">{name}</h1>
76
- <p style="font-size: 36px; margin-top: -15px; font-style: italic; color: white;">{quote}</p>
77
- <p style="font-size: 20px; color: white;">
78
- <strong style="color: white;">Ingredients:</strong><br>
79
- {ingredients}<br>
80
- <strong style="color: white;">Instruction:</strong><br>
81
- {instruction}<br>
82
- <strong style="color: white;">Notes:</strong><br>
83
- {notes}<br>
84
- </p>
85
  </div>
86
  '''
87
  return html_output
88
 
89
- # Creating the Gradio interface
90
- with gr.Blocks(css='''
91
- .gradio-container {
92
- background: url('https://images.unsplash.com/photo-1514361726087-38371321b5cd?q=80&w=2370&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D');
93
- }
94
- .centered-image {
95
- display: block;
96
- margin-left: auto;
97
- margin-right: auto;
98
- margin-bottom: -20px;
99
- max-width: 500px; /* Maximum width of the image */
100
- width: 100%;
101
- }
102
- .custom-input {
103
- background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(252, 249, 190, 0.8)); /* Or any color you'd like */
104
- opacity: 0.8
105
- color: #333; /* Text color */
106
- padding: 10px; /* Padding inside the input */
107
- border: 2px solid #ddd; /* Border color */
108
- border-radius: 5px; /* Rounded corners */
109
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
110
- transition: all 0.3s ease; /* Smooth transition for interactions */
111
- }
112
- .custom-input:focus {
113
- border-color: #F0E68C; /* Highlight color when the input is focused */
114
- box-shadow: 0 0 8px rgba(240, 230, 140, 0.8); /* Glow effect on focus */
115
- }
116
- .custom-checkbox-group1 {
117
- background: linear-gradient(to right, rgba(252, 249, 190, 0.8), rgba(255, 220, 169, 0.8));
118
- padding: 10px;
119
- border: 2px solid #ddd;
120
- border-radius: 5px;
121
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
122
- }
123
- .custom-checkbox-group1 input[type="checkbox"] + label {
124
- margin-right: 10px; /* Space between checkboxes */
125
- cursor: pointer; /* Mouse pointer changes to indicate clickable area */
126
- }
127
- .custom-checkbox-group1 input[type="checkbox"]:checked + label {
128
- color: #FF6347; /* Text color when checkbox is checked */
129
- font-weight: bold;
130
- }
131
- .custom-checkbox-group2 {
132
- background: linear-gradient(to right, rgba(255, 220, 169, 0.8), rgba(250, 171, 120, 0.8));
133
- padding: 10px;
134
- border: 2px solid #ddd;
135
- border-radius: 5px;
136
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
137
- }
138
- .custom-input1 {
139
- background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(255, 220, 169, 0.8));
140
- color: #333; /* Text color */
141
- padding: 10px; /* Padding inside the input */
142
- border: 20px solid #FFDA42; /* Initial border color, change as needed */
143
- border-color: #FFDA42
144
- border-radius: 5px; /* Rounded corners */
145
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
146
- transition: all 0.3s ease; /* Smooth transition for interactions */
147
- }
148
- .custom-input2 {
149
- background: linear-gradient(to right, rgba(255, 220, 169, 0.8), rgba(250, 171, 120, 0.8));
150
- color: #333; /* Text color */
151
- padding: 10px; /* Padding inside the input */
152
- border: 20px solid #FFDA42; /* Initial border color, change as needed */
153
- border-color: #FFDA42
154
- border-radius: 5px; /* Rounded corners */
155
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
156
- transition: all 0.3s ease; /* Smooth transition for interactions */
157
- }
158
-
159
- .custom-input2:focus {
160
- border-color: #FFDA42; /* Highlight color when the input is focused */
161
- box-shadow: 0 0 8px rgba(240, 230, 140, 0.8); /* Glow effect on focus */
162
- }
163
-
164
- .slider-sweetness {
165
- background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(252, 249, 190, 0.8));
166
- padding: 10px;
167
- border: 2px solid #ddd;
168
- border-radius: 5px;
169
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
170
- }
171
- .slider-sweetness::-webkit-slider-runnable-track {
172
- background: rgba(200, 152, 152, 0.8);
173
- }
174
- .slider-sour {
175
- background: linear-gradient(to right, rgba(252, 249, 190, 0.8), rgba(251, 230, 180, 0.8));
176
- padding: 10px;
177
- border: 2px solid #ddd;
178
- border-radius: 5px;
179
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
180
- }
181
- .slider-sour::-webkit-slider-runnable-track {
182
- background: rgba(200, 152, 152, 0.8);
183
- }
184
- .slider-savory {
185
- background: linear-gradient(to right,rgba(251, 230, 180, 0.8), rgba(255, 220, 169, 0.8));
186
- padding: 10px;
187
- border: 2px solid #ddd;
188
- border-radius: 5px;
189
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
190
- }
191
- .slider-savory::-webkit-slider-runnable-track {
192
- background: rgba(200, 152, 152, 0.8);
193
- }
194
- .slider-bitter {
195
- background: linear-gradient(to right,rgba(255, 220, 169, 0.8), rgba(252, 200, 135, 0.8));
196
- padding: 10px;
197
- border: 2px solid #ddd;
198
- border-radius: 5px;
199
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
200
- }
201
- .slider-bitter::-webkit-slider-runnable-track {
202
- background: rgba(200, 152, 152, 0.8);
203
- }
204
- .slider-soberness_level {
205
- background: linear-gradient(to right, rgba(252, 200, 135, 0.8), rgba(250, 171, 120, 0.8));
206
- padding: 10px;
207
- border: 2px solid #ddd;
208
- border-radius: 5px;
209
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
210
- }
211
- .slider-soberness_level::-webkit-slider-runnable-track {
212
- background: rgba(200, 152, 152, 0.8);
213
- }
214
- .custom-checkbox-group2 input[type="checkbox"] + label {
215
- margin-right: 10px; /* Space between checkboxes */
216
- cursor: pointer; /* Mouse pointer changes to indicate clickable area */
217
- }
218
- .custom-checkbox-group2 input[type="checkbox"]:checked + label {
219
- color: #FF6347; /* Text color when checkbox is checked */
220
- font-weight: bold;
221
- }
222
- .generate-button {
223
- background: linear-gradient(to right, #F0E68C, #E0FFFF, #FF6347);
224
- color: black;
225
- padding: 10px 20px;
226
- border: none;
227
- border-radius: 5px;
228
- cursor: pointer;
229
- font-weight: bold;
230
- text-transform: uppercase;
231
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
232
- transition: all 0.3s ease;
233
- outline: none; /* Removes the outline on focus for some browsers */
234
- }
235
- .generate-button:hover, .generate-button:active {
236
- background: linear-gradient(to right, #E0FFFF, #FF6347, #F0E68C);
237
- /* Combines the glow from each color in the gradient */
238
- box-shadow:
239
- 0 0 15px rgba(240, 230, 140, 0.7),
240
- 0 0 25px rgba(240, 230, 140, 0.7),
241
- 0 0 15px rgba(224, 255, 255, 0.7),
242
- 0 0 25px rgba(224, 255, 255, 0.7),
243
- 0 0 15px rgba(255, 99, 71, 0.7),
244
- 0 0 25px rgba(255, 99, 71, 0.7);
245
- transform: translateY(2px); /* Optional: Slightly push the button down on active */
246
- }
247
- .generate-button:active {
248
- /* You may want to add an extra inset shadow to give a sense of depth when clicked */
249
- box-shadow:
250
- 0 0 15px rgba(240, 230, 140, 0.7),
251
- 0 0 25px rgba(240, 230, 140, 0.7),
252
- 0 0 15px rgba(224, 255, 255, 0.7),
253
- 0 0 25px rgba(224, 255, 255, 0.7),
254
- 0 0 15px rgba(255, 99, 71, 0.7),
255
- 0 0 25px rgba(255, 99, 71, 0.7),
256
- 0 0 20px rgba(30, 255, 255, 0.5) inset;
257
- }
258
-
259
- ''') as demo:
260
 
 
 
261
  with gr.Row():
262
  gr.HTML('''
263
  <div style="text-align: center; margin: 0;">
264
  <img src="https://huggingface.co/spaces/WhartonHackAIthon/MoodShaker/resolve/main/MoodShaker_Slogan.png" alt="MoodShaker Cocktail Generator" class="centered-image">
265
  </div>
266
  ''')
267
- # gr.HTML('''
268
- # <h2 style='text-align: center; color: white;'>MoodShaker Cocktail Generator</h2>
269
- # <p style='text-align: center; color: white;'>Enter your preferences and let AI create a unique cocktail recipe for you!</p>
270
- # ''')
271
 
272
  with gr.Row():
273
- # mood = gr.HTML('''
274
- # <div class="mood-input">
275
- # <input type="text" class="gradio-textbox" label="Mood">
276
- # <span></span>
277
- # </div>
278
- # ''')
279
  mood = gr.Textbox(label="How are you feeling today?", elem_classes=["custom-input"])
280
  flavor_association = gr.CheckboxGroup(label="Flavor Association", choices=["Fruity", "Herbal", "Spicy", "Floral", "Nutty", "Woody", "Earthy"], elem_classes=["custom-checkbox-group1"])
281
  drinking_experience = gr.CheckboxGroup(label="Drinking Experience", choices=["Refreshing", "Warming", "Comforting", "Energizing", "Relaxing"], elem_classes=["custom-checkbox-group2"])
@@ -287,9 +89,6 @@ with gr.Blocks(css='''
287
  bitter = gr.Slider(label="Bitter", minimum=0, maximum=10, elem_id="slider-bitter", elem_classes=["slider-bitter"])
288
  soberness_level = gr.Slider(label="Level of Soberness", minimum=0, maximum=10, value=10, elem_id="slider-soberness_level", elem_classes=["slider-soberness_level"])
289
 
290
- # with gr.Row():
291
- # flavor_association = gr.CheckboxGroup(label="Flavor Association", choices=["Fruity", "Herbal", "Spicy", "Floral", "Nutty", "Woody", "Earthy"])
292
- # drinking_experience = gr.CheckboxGroup(label="Drinking Experience", choices=["Refreshing", "Warming", "Comforting", "Energizing", "Relaxing"])
293
  with gr.Row():
294
  allergies = gr.Textbox(label="Allergies", scale=2, elem_classes=["custom-input1"])
295
  additional_requests = gr.Textbox(label="Anything else you would like to address", scale=2, elem_classes=["custom-input2"])
@@ -300,14 +99,6 @@ with gr.Blocks(css='''
300
 
301
  output_recipe = gr.HTML(label="Your Cocktail Recipe")
302
 
303
-
304
- #modified
305
- # generate_button.click(
306
- # fn=generate_cocktail,
307
- # inputs=[mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests],
308
- # outputs=[output_recipe, play_button]
309
- # )
310
-
311
  play_button = gr.Button("Play Background Music", visible=False) # Initially not visible
312
  background_music = gr.Audio(label="Background Music", autoplay=True, visible=False) # Initially not visible
313
 
@@ -325,31 +116,6 @@ with gr.Blocks(css='''
325
  play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
326
 
327
 
328
-
329
- # sweetness .range-slider {background: #FAD02E;}
330
- # sour .range-slider {background: #4CAF50;}
331
- # savory .range-slider {background: #795548;}
332
- # bitter .range-slider {background: #F44336;}
333
- # soberness_level .range-slider {background: #2196F3;}
334
-
335
-
336
- # with gr.Blocks(css=".gradio-container {background: url(https://static.vecteezy.com/system/resources/thumbnails/030/814/051/small/wooden-table-and-blur-tropical-green-grass-background-product-display-montage-high-quality-8k-fhd-ai-generated-photo.jpg)}") as demo:
337
- # gr.Markdown("## To create an OpenAI Assistant please fill in the following sections. Upload a file to give the Assistant knowledge and a focus on something outside of it's normal training. Then add an assistant name and message. The Assistant message should guide the model into in a role. An example would be, You are a helpful Asssitant who is knowledgable in the field of...")
338
- # gr.Markdown("## After creating the ID head to [OpenAI_Assistant_Chat](https://huggingface.co/spaces/jadend/OpenAI_Assistant_Chat).")
339
- # with gr.Row():
340
- # # file_input = gr.File(label="Upload your file", type="filepath")
341
- # description = gr.Textbox(label="The User Input")
342
- # # chatbot = gr.Textbox(label="Chatbot Response")
343
- # generate_button = gr.Button("Generate Your Cocktail Recipe")
344
- # output_id = gr.Textbox(label="Your Cocktail Recipe", value="")
345
-
346
- # generate_button.click(
347
- # fn=generate_response,
348
- # inputs=description,
349
- # outputs=output_id
350
- # )
351
-
352
-
353
 
354
  if __name__ == "__main__":
355
  demo.launch(#enable_queue=False,
 
5
  from datetime import datetime
6
  import openai
7
 
8
+ # Function to play background music
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def play_music():
10
+ """Returns the path to the music file and makes the audio player visible."""
11
  music_path = "RPReplay_Final1712757356.mp3"
12
  return music_path, gr.update(visible=True)
13
+
14
+ # Main function to generate a cocktail recipe based on user preferences
15
  def generate_cocktail(mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests):
16
+ """Generates a cocktail recipe using OpenAI's GPT-4 based on user input."""
17
  client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
18
  instruction = "Please provide a cocktail recipe given the mood and preference of the user.\n\n"
19
  user_prompt = f"Mood: {mood}\nTaste: Sweetness {sweetness}/10, Sour {sour}/10, Savory {savory}/10, Bitter {bitter}/10\nFlavor Association: {flavor_association}\nDrinking Experience: {drinking_experience}\nLevel of Soberness: {soberness_level}\nAllergies: {allergies}\nAdditional Requests: {additional_requests}\n\nMake sure to avoid all allergic ingredients.\n\n"
 
21
  prompt = instruction + user_prompt + output_format
22
 
23
  messages=[
24
+ {"role": "system", "content": "You are a helpful bartender assistant."},
25
+ {"role": "user", "content": prompt}
26
+ ]
27
  try:
28
  response = client.chat.completions.create(
29
  model="gpt-4-0125-preview",
30
  messages=messages,
31
  max_tokens=1024)
32
  name, quote, ingredients, instruction, notes = extract_info(response.choices[0].message.content)
33
+ return format_cocktail_output(name, quote, ingredients, instruction, notes), True
34
  except Exception as e:
35
  return f'<p style="color: white; font-size: 20px;">{str(e)}</p>'
 
 
36
 
37
+ # Extract information from the response generated by OpenAI
38
  def extract_info(output_text):
39
+ """Extracts the cocktail recipe information from the response text."""
40
  pattern = r"Cocktail Name:(.*?)Quote:(.*?)Ingredients:(.*?)Instruction:(.*?)Notes:(.*?)$"
41
  match = re.search(pattern, output_text, re.DOTALL)
42
  if match:
43
+ name = match.group(1).strip()
44
+ quote = match.group(2).strip()
45
+ ingredients = match.group(3).strip().replace('\n', '<br>')
46
+ instruction = match.group(4).strip().replace('\n', '<br>')
47
+ notes = match.group(5).strip()
48
  return name, quote, ingredients, instruction, notes
49
  else:
50
  return None
51
+
52
+ # Format the cocktail recipe for display
53
  def format_cocktail_output(name, quote, ingredients, instruction, notes):
54
+ """Formats the cocktail recipe into HTML for display."""
55
  html_output = f'''
56
  <div style="text-align: center; font-family: 'Verdana', sans-serif; color: white;">
57
+ <h1>{name}</h1>
58
+ <p style="font-size: 36px; margin-top: -15px; font-style: italic;">{quote}</p>
59
+ <div style="font-size: 20px;">
60
+ <strong>Ingredients:</strong><br>{ingredients}<br>
61
+ <strong>Instruction:</strong><br>{instruction}<br>
62
+ <strong>Notes:</strong><br>{notes}<br>
63
+ </div>
 
 
 
64
  </div>
65
  '''
66
  return html_output
67
 
68
+ with open('style.css', 'r') as file:
69
+ css_styles = file.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # Creating the Gradio interface
72
+ with gr.Blocks(css=css_styles) as demo:
73
  with gr.Row():
74
  gr.HTML('''
75
  <div style="text-align: center; margin: 0;">
76
  <img src="https://huggingface.co/spaces/WhartonHackAIthon/MoodShaker/resolve/main/MoodShaker_Slogan.png" alt="MoodShaker Cocktail Generator" class="centered-image">
77
  </div>
78
  ''')
 
 
 
 
79
 
80
  with gr.Row():
 
 
 
 
 
 
81
  mood = gr.Textbox(label="How are you feeling today?", elem_classes=["custom-input"])
82
  flavor_association = gr.CheckboxGroup(label="Flavor Association", choices=["Fruity", "Herbal", "Spicy", "Floral", "Nutty", "Woody", "Earthy"], elem_classes=["custom-checkbox-group1"])
83
  drinking_experience = gr.CheckboxGroup(label="Drinking Experience", choices=["Refreshing", "Warming", "Comforting", "Energizing", "Relaxing"], elem_classes=["custom-checkbox-group2"])
 
89
  bitter = gr.Slider(label="Bitter", minimum=0, maximum=10, elem_id="slider-bitter", elem_classes=["slider-bitter"])
90
  soberness_level = gr.Slider(label="Level of Soberness", minimum=0, maximum=10, value=10, elem_id="slider-soberness_level", elem_classes=["slider-soberness_level"])
91
 
 
 
 
92
  with gr.Row():
93
  allergies = gr.Textbox(label="Allergies", scale=2, elem_classes=["custom-input1"])
94
  additional_requests = gr.Textbox(label="Anything else you would like to address", scale=2, elem_classes=["custom-input2"])
 
99
 
100
  output_recipe = gr.HTML(label="Your Cocktail Recipe")
101
 
 
 
 
 
 
 
 
 
102
  play_button = gr.Button("Play Background Music", visible=False) # Initially not visible
103
  background_music = gr.Audio(label="Background Music", autoplay=True, visible=False) # Initially not visible
104
 
 
116
  play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
117
 
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  if __name__ == "__main__":
121
  demo.launch(#enable_queue=False,