Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,76 +7,101 @@ from format.format_output import format_output
|
|
| 7 |
from validate.validate_ingredients import validate_ingredients
|
| 8 |
from device.get_device_id import get_device_id
|
| 9 |
|
| 10 |
-
# Load the model and tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("Ashikan/dut-recipe-generator")
|
| 12 |
model = AutoModelForCausalLM.from_pretrained("Ashikan/dut-recipe-generator")
|
| 13 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=get_device_id())
|
| 14 |
|
| 15 |
@spaces.GPU
|
| 16 |
-
def perform_model_inference(ingredients_list
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# If recipe name is provided
|
| 24 |
-
elif recipe_name:
|
| 25 |
-
input_text = "Generate ingredients and cooking method for the recipe: " + recipe_name
|
| 26 |
-
else:
|
| 27 |
-
return "Invalid input"
|
| 28 |
-
|
| 29 |
-
# Generate output from the model
|
| 30 |
output = pipe(input_text, max_length=1024, temperature=0.1, do_sample=True, truncation=True)[0]["generated_text"]
|
|
|
|
| 31 |
return format_output(output)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return {
|
| 42 |
-
generated_recipe: gr.Markdown(value=generated_text, label="Generated Recipe",
|
| 43 |
-
elem_id="recipe-container", visible=True)
|
| 44 |
-
}
|
| 45 |
-
else:
|
| 46 |
-
error_text = "## Invalid ingredients. Please include at least 2 ingredients in a comma-separated list. e.g. brown rice, onions, garlic"
|
| 47 |
-
return {
|
| 48 |
-
generated_recipe: gr.Markdown(value=error_text, elem_id="recipe-container", visible=True)
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
# If recipe name is provided
|
| 52 |
-
if recipe_name:
|
| 53 |
-
generated_text = perform_model_inference(recipe_name=recipe_name)
|
| 54 |
return {
|
| 55 |
generated_recipe: gr.Markdown(value=generated_text, label="Generated Recipe",
|
| 56 |
-
elem_id="recipe-container", visible=True)
|
|
|
|
| 57 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
}
|
|
|
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
with gr.Blocks(css="./css/styles.css") as recipegen:
|
| 66 |
-
gr.Image("./assets/dut.png", interactive=False, show_share_button=False, show_download_button=False,
|
| 67 |
-
show_fullscreen_button=False, show_label=False, elem_id="dut-logo", height=256)
|
| 68 |
-
gr.Markdown("# Recipe Generator", elem_id="header")
|
| 69 |
gr.Markdown("### An AI Model Attempting To Produce Healthier, Diabetic-Friendly Recipes",
|
| 70 |
elem_id="header-sub-heading")
|
| 71 |
-
gr.Markdown("Start by entering a comma-separated list of ingredients
|
| 72 |
-
|
| 73 |
-
with gr.Column()
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
examples = gr.Examples(
|
| 82 |
elem_id="examples",
|
|
@@ -86,13 +111,33 @@ with gr.Blocks(css="./css/styles.css") as recipegen:
|
|
| 86 |
"strawberries, vanilla, honey, rolled oats, almonds, butter",
|
| 87 |
"hake, spring onion, lemon"
|
| 88 |
],
|
| 89 |
-
inputs=[
|
| 90 |
)
|
| 91 |
|
|
|
|
| 92 |
generate_button.click(
|
| 93 |
fn=generate_recipe,
|
| 94 |
-
inputs=[
|
| 95 |
-
outputs=[
|
| 96 |
)
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
recipegen.launch(share=True)
|
|
|
|
|
|
| 7 |
from validate.validate_ingredients import validate_ingredients
|
| 8 |
from device.get_device_id import get_device_id
|
| 9 |
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("Ashikan/dut-recipe-generator")
|
| 11 |
model = AutoModelForCausalLM.from_pretrained("Ashikan/dut-recipe-generator")
|
| 12 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=get_device_id())
|
| 13 |
|
| 14 |
@spaces.GPU
|
| 15 |
+
def perform_model_inference(ingredients_list):
|
| 16 |
+
for ingredient_index in range(len(ingredients_list)):
|
| 17 |
+
ingredients_list[ingredient_index] = ingredients_list[ingredient_index].strip()
|
| 18 |
+
|
| 19 |
+
input_text = '{"prompt": ' + json.dumps(ingredients_list)
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
output = pipe(input_text, max_length=1024, temperature=0.1, do_sample=True, truncation=True)[0]["generated_text"]
|
| 22 |
+
|
| 23 |
return format_output(output)
|
| 24 |
|
| 25 |
+
|
| 26 |
+
def generate_recipe(ingredients):
|
| 27 |
+
ingredients_list = ingredients.lower().split(',')
|
| 28 |
+
is_ingredients_valid = validate_ingredients(ingredients_list)
|
| 29 |
+
|
| 30 |
+
if is_ingredients_valid:
|
| 31 |
+
generated_text = perform_model_inference(ingredients_list)
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return {
|
| 34 |
generated_recipe: gr.Markdown(value=generated_text, label="Generated Recipe",
|
| 35 |
+
elem_id="recipe-container", visible=True),
|
| 36 |
+
"state": "completed"
|
| 37 |
}
|
| 38 |
+
else:
|
| 39 |
+
error_text = "## Invalid ingredients. Please include at least 2 ingredients in a comma-separated list. e.g. brown rice, onions, garlic"
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
generated_recipe: gr.Markdown(value=error_text, elem_id="recipe-container", visible=True),
|
| 43 |
+
"state": "completed"
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(css="""
|
| 48 |
+
#chat-container {
|
| 49 |
+
background-color: #f7f7f8;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
padding: 20px;
|
| 52 |
+
max-width: 800px;
|
| 53 |
+
margin: auto;
|
| 54 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.chat-message {
|
| 58 |
+
border-radius: 8px;
|
| 59 |
+
padding: 10px;
|
| 60 |
+
margin-bottom: 10px;
|
| 61 |
+
font-family: Arial, sans-serif;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.user-message {
|
| 65 |
+
background-color: #e6f7ff;
|
| 66 |
+
text-align: left;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.bot-message {
|
| 70 |
+
background-color: #f0f0f0;
|
| 71 |
+
text-align: left;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.gr-button {
|
| 75 |
+
background-color: #0073e6 !important;
|
| 76 |
+
color: white !important;
|
| 77 |
+
border: none !important;
|
| 78 |
+
border-radius: 5px !important;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.gr-textbox input {
|
| 82 |
+
border-radius: 5px;
|
| 83 |
+
border: 1px solid #ccc;
|
| 84 |
+
padding: 10px;
|
| 85 |
+
}
|
| 86 |
|
| 87 |
+
.gradio-spinner {
|
| 88 |
+
display: block;
|
| 89 |
+
margin: auto;
|
| 90 |
}
|
| 91 |
+
""") as recipegen:
|
| 92 |
|
| 93 |
+
gr.Markdown("# Recipe Generator", elem_id="header")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
gr.Markdown("### An AI Model Attempting To Produce Healthier, Diabetic-Friendly Recipes",
|
| 95 |
elem_id="header-sub-heading")
|
| 96 |
+
gr.Markdown("Start by entering a comma-separated list of ingredients below.", elem_id="header-instructions")
|
| 97 |
+
|
| 98 |
+
with gr.Column(elem_id="chat-container"):
|
| 99 |
+
# Create a chat interface to simulate ChatGPT
|
| 100 |
+
chatbot = gr.Chatbot(elem_id="chatbot-container", label="Recipe Generator Chat")
|
| 101 |
+
user_input = gr.Textbox(label="Enter Ingredients (comma-separated)", placeholder="e.g. chicken, onions, garlic", autofocus=True, max_lines=1, elem_id="ingredients-input")
|
| 102 |
+
generate_button = gr.Button(value="Generate Recipe")
|
| 103 |
+
clear_button = gr.Button(value="Clear") # Clear button to reset the chat
|
| 104 |
+
loading_spinner = gr.Spinner(visible=False) # This will be used to show the loading spinner
|
| 105 |
|
| 106 |
examples = gr.Examples(
|
| 107 |
elem_id="examples",
|
|
|
|
| 111 |
"strawberries, vanilla, honey, rolled oats, almonds, butter",
|
| 112 |
"hake, spring onion, lemon"
|
| 113 |
],
|
| 114 |
+
inputs=[user_input]
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Link the chatbot and input to the generate_recipe function, adding loading indicator
|
| 118 |
generate_button.click(
|
| 119 |
fn=generate_recipe,
|
| 120 |
+
inputs=[user_input],
|
| 121 |
+
outputs=[chatbot, loading_spinner]
|
| 122 |
)
|
| 123 |
|
| 124 |
+
# Show loading spinner while processing
|
| 125 |
+
def show_loading_spinner():
|
| 126 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 127 |
+
|
| 128 |
+
# Hide loading spinner once result is available
|
| 129 |
+
def hide_loading_spinner():
|
| 130 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 131 |
+
|
| 132 |
+
# Set actions for the loading spinner
|
| 133 |
+
generate_button.click(fn=show_loading_spinner, outputs=[loading_spinner])
|
| 134 |
+
generate_button.click(fn=hide_loading_spinner, inputs=[chatbot], outputs=[loading_spinner])
|
| 135 |
+
|
| 136 |
+
# Clear inputs and outputs when the "Clear" button is clicked
|
| 137 |
+
def clear_all():
|
| 138 |
+
return gr.update(value=""), gr.update(value=""), gr.update(value="", visible=False) # Clear input, output, and hide spinner
|
| 139 |
+
|
| 140 |
+
clear_button.click(fn=clear_all, outputs=[user_input, chatbot, loading_spinner])
|
| 141 |
+
|
| 142 |
recipegen.launch(share=True)
|
| 143 |
+
|