BhavaishKumar112 commited on
Commit
8f251c3
·
verified ·
1 Parent(s): a581271

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -57
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=None, recipe_name=None):
17
- # If ingredients are provided
18
- if ingredients_list:
19
- for ingredient_index in range(len(ingredients_list)):
20
- ingredients_list[ingredient_index] = ingredients_list[ingredient_index].strip()
21
- input_text = '{"prompt": ' + json.dumps(ingredients_list)
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
- def generate_recipe(ingredients=None, recipe_name=None):
34
- # If ingredients are provided
35
- if ingredients:
36
- ingredients_list = ingredients.lower().split(',')
37
- is_ingredients_valid = validate_ingredients(ingredients_list)
38
-
39
- if is_ingredients_valid:
40
- generated_text = perform_model_inference(ingredients_list=ingredients_list)
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
- return {
60
- generated_recipe: gr.Markdown(value="## Please enter either ingredients or a recipe name.",
61
- elem_id="recipe-container", visible=True)
62
  }
 
63
 
64
- # Gradio Interface
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 or a recipe name below.", elem_id="header-instructions")
72
-
73
- with gr.Column() as column:
74
- user_ingredients = gr.Textbox(label="Ingredients", placeholder="e.g. chicken, onion, garlic", autofocus=True, max_lines=1, elem_id="ingredients-input")
75
- user_recipe_name = gr.Textbox(label="Recipe Name", placeholder="e.g. Chicken Biryani", max_lines=1, elem_id="recipe-name-input")
76
- generate_button = gr.Button(value="Generate")
77
-
78
- with gr.Column():
79
- generated_recipe = gr.Markdown(visible=True)
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=[user_ingredients]
90
  )
91
 
 
92
  generate_button.click(
93
  fn=generate_recipe,
94
- inputs=[user_ingredients, user_recipe_name],
95
- outputs=[generated_recipe]
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
+