Spaces:
Sleeping
Sleeping
File size: 17,082 Bytes
73d2dd0 44bc7e4 73d2dd0 d678ec4 2b7543b e72c121 4bf3314 e10d86a ad3f02e e10d86a e72c121 e58d934 434948d e72c121 e58d934 bf9dd3c c591b30 d678ec4 434948d ea69799 d678ec4 434948d c591b30 c5cf242 c591b30 c5cf242 9fa2d2e c591b30 ad3f02e 70052eb eb18f40 787329f eb18f40 787329f eb18f40 787329f c591b30 73d2dd0 2302449 96b6173 ec66728 0581f3f 698b0ea 0581f3f 97750f2 8c7db08 4a11f75 d31805d 97750f2 761b71a 97750f2 65cc2e2 8c7db08 761b71a 97750f2 65cc2e2 761b71a 97750f2 65cc2e2 8c7db08 65cc2e2 0fb53a8 c9eeb14 0fb53a8 c9eeb14 f4fbadd 9271f34 c9eeb14 f4fbadd c9eeb14 1bff3e7 40a1eec d43d86a 1bff3e7 7653c54 7bff1d2 7653c54 7bff1d2 991ca11 7bff1d2 7653c54 7bff1d2 991ca11 7bff1d2 7653c54 7bff1d2 991ca11 7bff1d2 7653c54 7bff1d2 991ca11 65cc2e2 761b71a 97750f2 eb18f40 a4a9d93 5251c6f eb18f40 5251c6f a4a9d93 5251c6f 04e6d96 5251c6f e5ef471 5251c6f 2302449 d678ec4 73d2dd0 02b212b 6f70394 97f3ce3 02b212b 0581f3f f620b50 a4a9d93 97750f2 65cc2e2 f620b50 40a1eec 7bff1d2 f620b50 70052eb f620b50 0fb53a8 c9eeb14 eb18f40 86206de c591b30 f14787d d678ec4 f14787d d678ec4 f14787d d678ec4 f14787d d678ec4 73d2dd0 d678ec4 f14787d 73d2dd0 0f1f17a 4bf3314 0f1f17a 4bf3314 04e6d96 73d2dd0 c7b8427 73d2dd0 |
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
import os
import gradio as gr
import json
import re
from datetime import datetime
import openai
# Assistant Creation function
def create_assistant_json(uploaded_file, assistant_name, assistant_message):
client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
# Check if a file was uploaded
print(uploaded_file)
df = open(uploaded_file, "rb")
file = client.files.create(file=df,
purpose='assistants')
assistant = client.beta.assistants.create(
name=assistant_name,
instructions=assistant_message,
model="gpt-4-0125-preview",
tools=[
{
"type": "retrieval" # This adds the knowledge base as a tool
}
],
file_ids=[file.id])
return assistant.id
def play_music():
music_path = "RPReplay_Final1712757356.mp3"
return music_path, gr.update(visible=True)
def generate_cocktail(mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests):
client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
instruction = "Please provide a cocktail recipe given the mood and preference of the user.\n\n"
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"
output_format = "Please strictly follow this output format:\n\nCocktail Name:[name]\nQuote:[one sentence quote related to the cocktail and the mood description]\nIngredients:[ingredient 1]\n[ingredient 2]\n...\nInstruction:1. [step 1]\n2. [step 2]\n...\nNotes:[notes]"
prompt = instruction + user_prompt + output_format
messages=[
{"role": "system", "content": "You are a helpful bartender assistant."},
{"role": "user", "content": prompt}
]
try:
response = client.chat.completions.create(
model="gpt-4-0125-preview",
messages=messages,
max_tokens=1024)
name, quote, ingredients, instruction, notes = extract_info(response.choices[0].message.content)
return format_cocktail_output(name, quote, ingredients, instruction, notes),True
except Exception as e:
return f'<p style="color: white; font-size: 20px;">{str(e)}</p>'
def extract_info(output_text):
pattern = r"Cocktail Name:(.*?)Quote:(.*?)Ingredients:(.*?)Instruction:(.*?)Notes:(.*?)$"
match = re.search(pattern, output_text, re.DOTALL)
if match:
name = match.group(1)
quote = match.group(2)
ingredients = match.group(3).replace('\n', '<br>')
instruction = match.group(4).replace('\n', '<br>')
notes = match.group(5)
return name, quote, ingredients, instruction, notes
else:
return None
def format_cocktail_output(name, quote, ingredients, instruction, notes):
# Construct the HTML output
html_output = f'''
<div style="text-align: center; font-family: 'Verdana', sans-serif; color: white;">
<h1 style="font-size: 48px; color: white;">{name}</h1>
<p style="font-size: 36px; margin-top: -15px; font-style: italic; color: white;">{quote}</p>
<p style="font-size: 20px; color: white;">
<strong style="color: white;">Ingredients:</strong><br>
{ingredients}<br>
<strong style="color: white;">Instruction:</strong><br>
{instruction}<br>
<strong style="color: white;">Notes:</strong><br>
{notes}<br>
</p>
</div>
'''
return html_output
# Creating the Gradio interface
with gr.Blocks(css='''
.gradio-container {
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');
}
.centered-image {
display: block;
margin-left: auto;
margin-right: auto;
margin-bottom: -20px;
max-width: 500px; /* Maximum width of the image */
width: 100%;
}
.custom-input {
background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(252, 249, 190, 0.8)); /* Or any color you'd like */
opacity: 0.8
color: #333; /* Text color */
padding: 10px; /* Padding inside the input */
border: 2px solid #ddd; /* Border color */
border-radius: 5px; /* Rounded corners */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
transition: all 0.3s ease; /* Smooth transition for interactions */
}
.custom-input:focus {
border-color: #F0E68C; /* Highlight color when the input is focused */
box-shadow: 0 0 8px rgba(240, 230, 140, 0.8); /* Glow effect on focus */
}
.custom-checkbox-group1 {
background: linear-gradient(to right, rgba(252, 249, 190, 0.8), rgba(255, 220, 169, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.custom-checkbox-group1 input[type="checkbox"] + label {
margin-right: 10px; /* Space between checkboxes */
cursor: pointer; /* Mouse pointer changes to indicate clickable area */
}
.custom-checkbox-group1 input[type="checkbox"]:checked + label {
color: #FF6347; /* Text color when checkbox is checked */
font-weight: bold;
}
.custom-checkbox-group2 {
background: linear-gradient(to right, rgba(255, 220, 169, 0.8), rgba(250, 171, 120, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.custom-input1 {
background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(255, 220, 169, 0.8));
color: #333; /* Text color */
padding: 10px; /* Padding inside the input */
border: 20px solid #FFDA42; /* Initial border color, change as needed */
border-color: #FFDA42
border-radius: 5px; /* Rounded corners */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
transition: all 0.3s ease; /* Smooth transition for interactions */
}
.custom-input2 {
background: linear-gradient(to right, rgba(255, 220, 169, 0.8), rgba(250, 171, 120, 0.8));
color: #333; /* Text color */
padding: 10px; /* Padding inside the input */
border: 20px solid #FFDA42; /* Initial border color, change as needed */
border-color: #FFDA42
border-radius: 5px; /* Rounded corners */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */
transition: all 0.3s ease; /* Smooth transition for interactions */
}
.custom-input2:focus {
border-color: #FFDA42; /* Highlight color when the input is focused */
box-shadow: 0 0 8px rgba(240, 230, 140, 0.8); /* Glow effect on focus */
}
.slider-sweetness {
background: linear-gradient(to right, rgba(232, 243, 214, 0.8), rgba(252, 249, 190, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.slider-sweetness::-webkit-slider-runnable-track {
background: rgba(200, 152, 152, 0.8);
}
.slider-sour {
background: linear-gradient(to right, rgba(252, 249, 190, 0.8), rgba(251, 230, 180, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.slider-sour::-webkit-slider-runnable-track {
background: rgba(200, 152, 152, 0.8);
}
.slider-savory {
background: linear-gradient(to right,rgba(251, 230, 180, 0.8), rgba(255, 220, 169, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.slider-savory::-webkit-slider-runnable-track {
background: rgba(200, 152, 152, 0.8);
}
.slider-bitter {
background: linear-gradient(to right,rgba(255, 220, 169, 0.8), rgba(252, 200, 135, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.slider-bitter::-webkit-slider-runnable-track {
background: rgba(200, 152, 152, 0.8);
}
.slider-soberness_level {
background: linear-gradient(to right, rgba(252, 200, 135, 0.8), rgba(250, 171, 120, 0.8));
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.slider-soberness_level::-webkit-slider-runnable-track {
background: rgba(200, 152, 152, 0.8);
}
.custom-checkbox-group2 input[type="checkbox"] + label {
margin-right: 10px; /* Space between checkboxes */
cursor: pointer; /* Mouse pointer changes to indicate clickable area */
}
.custom-checkbox-group2 input[type="checkbox"]:checked + label {
color: #FF6347; /* Text color when checkbox is checked */
font-weight: bold;
}
.generate-button {
background: linear-gradient(to right, #F0E68C, #E0FFFF, #FF6347);
color: black;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
text-transform: uppercase;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
outline: none; /* Removes the outline on focus for some browsers */
}
.generate-button:hover, .generate-button:active {
background: linear-gradient(to right, #E0FFFF, #FF6347, #F0E68C);
/* Combines the glow from each color in the gradient */
box-shadow:
0 0 15px rgba(240, 230, 140, 0.7),
0 0 25px rgba(240, 230, 140, 0.7),
0 0 15px rgba(224, 255, 255, 0.7),
0 0 25px rgba(224, 255, 255, 0.7),
0 0 15px rgba(255, 99, 71, 0.7),
0 0 25px rgba(255, 99, 71, 0.7);
transform: translateY(2px); /* Optional: Slightly push the button down on active */
}
.generate-button:active {
/* You may want to add an extra inset shadow to give a sense of depth when clicked */
box-shadow:
0 0 15px rgba(240, 230, 140, 0.7),
0 0 25px rgba(240, 230, 140, 0.7),
0 0 15px rgba(224, 255, 255, 0.7),
0 0 25px rgba(224, 255, 255, 0.7),
0 0 15px rgba(255, 99, 71, 0.7),
0 0 25px rgba(255, 99, 71, 0.7),
0 0 20px rgba(30, 255, 255, 0.5) inset;
}
''') as demo:
with gr.Row():
gr.HTML('''
<div style="text-align: center; margin: 0;">
<img src="https://huggingface.co/spaces/WhartonHackAIthon/MoodShaker/resolve/main/MoodShaker_Slogan.png" alt="MoodShaker Cocktail Generator" class="centered-image">
</div>
''')
# gr.HTML('''
# <h2 style='text-align: center; color: white;'>MoodShaker Cocktail Generator</h2>
# <p style='text-align: center; color: white;'>Enter your preferences and let AI create a unique cocktail recipe for you!</p>
# ''')
with gr.Row():
# mood = gr.HTML('''
# <div class="mood-input">
# <input type="text" class="gradio-textbox" label="Mood">
# <span></span>
# </div>
# ''')
mood = gr.Textbox(label="How are you feeling today?", elem_classes=["custom-input"])
flavor_association = gr.CheckboxGroup(label="Flavor Association", choices=["Fruity", "Herbal", "Spicy", "Floral", "Nutty", "Woody", "Earthy"], elem_classes=["custom-checkbox-group1"])
drinking_experience = gr.CheckboxGroup(label="Drinking Experience", choices=["Refreshing", "Warming", "Comforting", "Energizing", "Relaxing"], elem_classes=["custom-checkbox-group2"])
with gr.Row():
sweetness = gr.Slider(label="Sweetness", minimum=0, maximum=10, elem_id="slider-sweetness",elem_classes=["slider-sweetness"])
sour = gr.Slider(label="Sour", minimum=0, maximum=10, elem_id="slider-sour", elem_classes=["slider-sour"])
savory = gr.Slider(label="Savory", minimum=0, maximum=10, elem_id="slider-savory", elem_classes=["slider-savory"])
bitter = gr.Slider(label="Bitter", minimum=0, maximum=10, elem_id="slider-bitter", elem_classes=["slider-bitter"])
soberness_level = gr.Slider(label="Level of Soberness", minimum=0, maximum=10, value=10, elem_id="slider-soberness_level", elem_classes=["slider-soberness_level"])
# with gr.Row():
# flavor_association = gr.CheckboxGroup(label="Flavor Association", choices=["Fruity", "Herbal", "Spicy", "Floral", "Nutty", "Woody", "Earthy"])
# drinking_experience = gr.CheckboxGroup(label="Drinking Experience", choices=["Refreshing", "Warming", "Comforting", "Energizing", "Relaxing"])
with gr.Row():
allergies = gr.Textbox(label="Allergies", scale=2, elem_classes=["custom-input1"])
additional_requests = gr.Textbox(label="Anything else you would like to address", scale=2, elem_classes=["custom-input2"])
generate_button = gr.Button("Generate Your Cocktail Recipe", scale=1, elem_classes=["generate-button"])
with gr.Row():
output_recipe = gr.HTML(label="Your Cocktail Recipe")
output_recipe = gr.HTML(label="Your Cocktail Recipe")
#modified
# generate_button.click(
# fn=generate_cocktail,
# inputs=[mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests],
# outputs=[output_recipe, play_button]
# )
play_button = gr.Button("Play Background Music", visible=False) # Initially not visible
background_music = gr.Audio(label="Background Music", autoplay=True, visible=False) # Initially not visible
def on_generate_click(*args):
recipe, show_play_button = generate_cocktail(*args)
return recipe, gr.update(visible=show_play_button)
generate_button.click(
fn=on_generate_click,
inputs=[mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests],
outputs=[output_recipe, play_button]
)
play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
# sweetness .range-slider {background: #FAD02E;}
# sour .range-slider {background: #4CAF50;}
# savory .range-slider {background: #795548;}
# bitter .range-slider {background: #F44336;}
# soberness_level .range-slider {background: #2196F3;}
# 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:
# 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...")
# gr.Markdown("## After creating the ID head to [OpenAI_Assistant_Chat](https://huggingface.co/spaces/jadend/OpenAI_Assistant_Chat).")
# with gr.Row():
# # file_input = gr.File(label="Upload your file", type="filepath")
# description = gr.Textbox(label="The User Input")
# # chatbot = gr.Textbox(label="Chatbot Response")
# generate_button = gr.Button("Generate Your Cocktail Recipe")
# output_id = gr.Textbox(label="Your Cocktail Recipe", value="")
# generate_button.click(
# fn=generate_response,
# inputs=description,
# outputs=output_id
# )
if __name__ == "__main__":
demo.launch(#enable_queue=False,
# Creates an auth screen
auth_message="Welcome! Enter a Username and Password"
).queue() |