Shaylin Chetty commited on
Commit
021e7cc
·
1 Parent(s): 0fbfc59

Intial commit

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
README.md CHANGED
@@ -1,10 +1,11 @@
1
  ---
2
- title: Dut Recipe Generator
3
- emoji: 🚀
4
- colorFrom: red
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 4.44.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: bigscience-bloom-rail-1.0
 
1
  ---
2
+ title: DUT Recipe Generator
3
+ emoji: 🍲
4
+ colorFrom: pink
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.42.0
8
+ python_version: 3.11
9
  app_file: app.py
10
  pinned: false
11
  license: bigscience-bloom-rail-1.0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import gradio as gr
4
+ import spaces
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
6
+
7
+ from format.format_output import format_output
8
+ from validate.validate_ingredients import validate_ingredients
9
+ from device.get_device_id import get_device_id
10
+
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
+
16
+ @spaces.GPU
17
+ def perform_model_inference(ingredients_list):
18
+ for ingredient_index in range(len(ingredients_list)):
19
+ ingredients_list[ingredient_index] = ingredients_list[ingredient_index].strip()
20
+
21
+ input_text = '{"prompt": ' + json.dumps(ingredients_list)
22
+
23
+ output = pipe(input_text, max_length=1024, temperature=0.1, do_sample=True, truncation=True)[0]["generated_text"]
24
+
25
+ return format_output(output)
26
+
27
+
28
+ def generate_recipe(ingredients):
29
+ ingredients_list = ingredients.lower().split(',')
30
+ is_ingredients_valid = validate_ingredients(ingredients_list)
31
+
32
+ if is_ingredients_valid:
33
+ generated_text = perform_model_inference(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.Image("./assets/dut.png", interactive=False, show_share_button=False, show_download_button=False,
49
+ show_fullscreen_button=False, show_label=False, elem_id="dut-logo", height=256)
50
+ gr.Markdown("# Durban University Of Technology Recipe Generator", elem_id="header")
51
+ gr.Markdown("### An AI Model Attempting To Produce Healthier, Diabetic-Friendly Recipes",
52
+ elem_id="header-sub-heading")
53
+ gr.Markdown("Start by entering a comma-separated list of ingredients below.", elem_id="header-instructions")
54
+ with gr.Column() as column:
55
+ user_ingredients = gr.Textbox(label="Ingredients", autofocus=True, max_lines=1, elem_id="ingredients-input")
56
+ generate_button = gr.Button(value="Generate")
57
+ with gr.Column():
58
+ generated_recipe = gr.Markdown(visible=True)
59
+ examples = gr.Examples(
60
+ elem_id="examples",
61
+ examples=[
62
+ "sweet potato, mushrooms, cheese, garlic",
63
+ "chicken breast, chili, onion, tomato, parmesan cheese",
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)
assets/dut.png ADDED
css/styles.css ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #dut-logo {
2
+ padding: 1em;
3
+ }
4
+
5
+ #header {
6
+ text-align: center;
7
+ }
8
+
9
+ #header-sub-heading {
10
+ text-align: center;
11
+ }
12
+
13
+ #header-instructions {
14
+ text-align: center;
15
+ }
16
+
17
+ #ingredients-input input {
18
+ font-size: 12pt;
19
+ }
20
+
21
+ #recipe-container {
22
+ padding: 1em !important;
23
+ border: 1px solid #e5e7eb !important;
24
+ border-radius: 5px !important;
25
+ }
26
+
27
+ #examples button div{
28
+ padding-left: 1.5em;
29
+ padding-right: 1.5em;
30
+ }
device/get_device_id.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ def get_device_id():
4
+ if torch.cuda.is_available():
5
+ return 0
6
+
7
+ return -1
format/__init__.py ADDED
File without changes
format/format_output.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+
4
+ def format_output(raw_response):
5
+ response_object = json.loads(raw_response)
6
+
7
+ title_block = __get_title_block(response_object["title"])
8
+ ingredients_block = __get_ingredients_block(response_object["ingredients"])
9
+ method_block = __get_method_block(response_object["method"])
10
+
11
+ return title_block + ingredients_block + method_block
12
+
13
+
14
+ def __get_title_block(title):
15
+ return f"# <ins>{title.title()}</ins>\n\n"
16
+
17
+
18
+ def __get_ingredients_block(ingredients):
19
+ ingredients_block = "## Ingredients:\n"
20
+
21
+ for ingredient in ingredients:
22
+ ingredients_block += f"- {ingredient.capitalize()}\n"
23
+
24
+ return f"{ingredients_block}\n\n"
25
+
26
+
27
+ def __get_method_block(method):
28
+ method_block = "## Method:\n"
29
+
30
+ for step in method:
31
+ sentences = step.split(".")
32
+
33
+ formatted_sentences = []
34
+
35
+ for sentence in sentences:
36
+ formatted_sentences.append(sentence.strip().capitalize())
37
+
38
+ joined_sentences = ". ".join(formatted_sentences)
39
+
40
+ method_block += f"- {joined_sentences}\n"
41
+
42
+ return f"{method_block}\n\n"
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ accelerate
3
+ gradio
4
+ spaces
5
+ torch
validate/__init__.py ADDED
File without changes
validate/validate_ingredients.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ def validate_ingredients(ingredients_list):
2
+ return len(ingredients_list) > 1