Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
@@ -11,6 +12,7 @@ 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)):
|
|
@@ -32,77 +34,28 @@ def generate_recipe(ingredients):
|
|
| 32 |
|
| 33 |
return {
|
| 34 |
generated_recipe: gr.Markdown(value=generated_text, label="Generated Recipe",
|
| 35 |
-
elem_id="recipe-container", visible=True)
|
| 36 |
-
loading_message: gr.Markdown(value="", visible=False) # Hide loading message after generation
|
| 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 |
-
loading_message: gr.Markdown(value="", visible=False) # Hide loading message
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def hide_loading_message():
|
| 50 |
-
return gr.Markdown(value="", visible=False)
|
| 51 |
-
|
| 52 |
-
with gr.Blocks(css="""
|
| 53 |
-
#chat-container {
|
| 54 |
-
background-color: #f7f7f8;
|
| 55 |
-
border-radius: 10px;
|
| 56 |
-
padding: 20px;
|
| 57 |
-
max-width: 800px;
|
| 58 |
-
margin: auto;
|
| 59 |
-
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
.chat-message {
|
| 63 |
-
border-radius: 8px;
|
| 64 |
-
padding: 10px;
|
| 65 |
-
margin-bottom: 10px;
|
| 66 |
-
font-family: Arial, sans-serif;
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
.user-message {
|
| 70 |
-
background-color: #e6f7ff;
|
| 71 |
-
text-align: left;
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
.bot-message {
|
| 75 |
-
background-color: #f0f0f0;
|
| 76 |
-
text-align: left;
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
.gr-button {
|
| 80 |
-
background-color: #0073e6 !important;
|
| 81 |
-
color: white !important;
|
| 82 |
-
border: none !important;
|
| 83 |
-
border-radius: 5px !important;
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
.gr-textbox input {
|
| 87 |
-
border-radius: 5px;
|
| 88 |
-
border: 1px solid #ccc;
|
| 89 |
-
padding: 10px;
|
| 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(
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
examples = gr.Examples(
|
| 107 |
elem_id="examples",
|
| 108 |
examples=[
|
|
@@ -111,28 +64,13 @@ with gr.Blocks(css="""
|
|
| 111 |
"strawberries, vanilla, honey, rolled oats, almonds, butter",
|
| 112 |
"hake, spring onion, lemon"
|
| 113 |
],
|
| 114 |
-
inputs=[
|
| 115 |
)
|
| 116 |
|
| 117 |
-
# Show loading message when clicking generate button
|
| 118 |
-
generate_button.click(
|
| 119 |
-
fn=show_loading_message,
|
| 120 |
-
outputs=[loading_message]
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
# Hide the loading message and generate the recipe
|
| 124 |
generate_button.click(
|
| 125 |
fn=generate_recipe,
|
| 126 |
-
inputs=[
|
| 127 |
-
outputs=[
|
| 128 |
)
|
| 129 |
|
| 130 |
-
# Clear inputs and outputs when the "Clear" button is clicked
|
| 131 |
-
def clear_all():
|
| 132 |
-
return gr.update(value=""), gr.update(value=""), gr.update(value="", visible=False) # Clear input, output, and hide spinner
|
| 133 |
-
|
| 134 |
-
clear_button.click(fn=clear_all, outputs=[user_input, chatbot, loading_message])
|
| 135 |
-
|
| 136 |
recipegen.launch(share=True)
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 1 |
import json
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 12 |
model = AutoModelForCausalLM.from_pretrained("Ashikan/dut-recipe-generator")
|
| 13 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=get_device_id())
|
| 14 |
|
| 15 |
+
|
| 16 |
@spaces.GPU
|
| 17 |
def perform_model_inference(ingredients_list):
|
| 18 |
for ingredient_index in range(len(ingredients_list)):
|
|
|
|
| 34 |
|
| 35 |
return {
|
| 36 |
generated_recipe: gr.Markdown(value=generated_text, label="Generated Recipe",
|
| 37 |
+
elem_id="recipe-container", visible=True)
|
|
|
|
| 38 |
}
|
| 39 |
else:
|
| 40 |
error_text = "## Invalid ingredients. Please include at least 2 ingredients in a comma-separated list. e.g. brown rice, onions, garlic"
|
| 41 |
|
| 42 |
return {
|
| 43 |
+
generated_recipe: gr.Markdown(value=error_text, elem_id="recipe-container", visible=True)
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(css="./css/styles.css") as recipegen:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
gr.Markdown("### An AI Model Attempting To Produce Healthier, Diabetic-Friendly Recipes",
|
| 49 |
elem_id="header-sub-heading")
|
| 50 |
gr.Markdown("Start by entering a comma-separated list of ingredients below.", elem_id="header-instructions")
|
| 51 |
|
| 52 |
+
with gr.Column() as column:
|
| 53 |
+
user_ingredients = gr.Textbox(label="Ingredients", autofocus=True, max_lines=1, elem_id="ingredients-input")
|
| 54 |
+
generate_button = gr.Button(value="Generate")
|
| 55 |
+
|
| 56 |
+
with gr.Column():
|
| 57 |
+
generated_recipe = gr.Markdown(visible=True)
|
| 58 |
+
|
|
|
|
| 59 |
examples = gr.Examples(
|
| 60 |
elem_id="examples",
|
| 61 |
examples=[
|
|
|
|
| 64 |
"strawberries, vanilla, honey, rolled oats, almonds, butter",
|
| 65 |
"hake, spring onion, lemon"
|
| 66 |
],
|
| 67 |
+
inputs=[user_ingredients]
|
| 68 |
)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
generate_button.click(
|
| 71 |
fn=generate_recipe,
|
| 72 |
+
inputs=[user_ingredients],
|
| 73 |
+
outputs=[generated_recipe]
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
recipegen.launch(share=True)
|
|
|
|
|
|