DOIT55 commited on
Commit
585bf3b
·
verified ·
1 Parent(s): ff94fe5

Uploading FoodExtract demo app.py

Browse files
.ipynb_checkpoints/README-checkpoint.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile demos/FoodExtract/README.md
2
+ ---
3
+ title: FoodExtract Fine-tuned LLM Structued Data Extractor
4
+ emoji: 📝➡️🍟
5
+ colorFrom: green
6
+ colorTo: blue
7
+ sdk: gradio
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ ---
12
+
13
+ """
14
+ Fine-tuned Gemma 3 270M to extract food and drink items from raw text.
15
+
16
+ Input can be any form of real text and output will be a formatted string such as the following:
17
+
18
+ ```
19
+ food_or_drink: 1
20
+ tags: fi, re
21
+ foods: tacos, milk, red apple, pineapple, cherries, fried chicken, steak, mayonnaise
22
+ drinks: iced latte, matcha latte
23
+ ```
24
+
25
+ The tags map to the following items:
26
+
27
+ ```
28
+ tags_dict = {'np': 'nutrition_panel',
29
+ 'il': 'ingredient list',
30
+ 'me': 'menu',
31
+ 're': 'recipe',
32
+ 'fi': 'food_items',
33
+ 'di': 'drink_items',
34
+ 'fa': 'food_advertistment',
35
+ 'fp': 'food_packaging'}
36
+ ```
37
+ """
.ipynb_checkpoints/app-checkpoint.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile demos/FoodExtract/app.py
2
+
3
+ # Load dependencies
4
+ import time
5
+ import transformers
6
+ import torch
7
+ import spaces # Optional: run our model on the GPU (this will be much faster inference)
8
+
9
+ import gradio as gr
10
+
11
+ from transformers import AutoModelForCausalLM, AutoTokenizer
12
+ from transformers import pipeline
13
+
14
+ @spaces.GPU # Optional: run our model on the GPU (this will be much faster inference)
15
+ def pred_on_text(input_text):
16
+ start_time = time.time()
17
+
18
+ raw_output = loaded_model_pipeline(text_inputs=[{"role": "user",
19
+ "content": input_text}],
20
+ max_new_tokens=256,
21
+ disable_compile=True)
22
+ end_time = time.time()
23
+ total_time = round(end_time - start_time, 4)
24
+
25
+ generated_text = raw_output[0]["generated_text"][1]["content"]
26
+
27
+ return generated_text, raw_output, total_time
28
+
29
+ # Load the model (from our Hugging Face Repo)
30
+ # Note: You may have to replace my username `mrdbourke` for your own
31
+ MODEL_PATH = "doit55/FoodExtract-gemma-3-270m-fine-tune-v1"
32
+
33
+ # Load the model into a pipeline
34
+ loaded_model = AutoModelForCausalLM.from_pretrained(
35
+ pretrained_model_name_or_path=MODEL_PATH,
36
+ dtype="auto",
37
+ device_map="auto",
38
+ attn_implementation="eager"
39
+ )
40
+
41
+ # Load the tokenizer
42
+ tokenizer = AutoTokenizer.from_pretrained(
43
+ MODEL_PATH
44
+ )
45
+
46
+ # Create model pipeline
47
+ loaded_model_pipeline = pipeline("text-generation",
48
+ model=loaded_model,
49
+ tokenizer=tokenizer)
50
+
51
+ # Create the demo
52
+ description = """Extract food and drink items from text with a fine-tuned SLM (Small Language Model) or more specifically a fine-tuned [Gemma 3 270M](https://huggingface.co/google/gemma-3-270m-it).
53
+
54
+ Our model has been fine-tuned on the [FoodExtract-1k dataset](https://huggingface.co/datasets/mrdbourke/FoodExtract-1k).
55
+
56
+ * Input (str): Raw text strings or image captions (e.g. "A photo of a dog sitting on a beach" or "A breakfast plate with bacon, eggs and toast")
57
+ * Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
58
+
59
+ For example:
60
+
61
+ * Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
62
+ * Output:
63
+
64
+ ```
65
+ food_or_drink: 1
66
+ tags: fi, di
67
+ foods: eggs, bacon, toast
68
+ drinks: orange juice
69
+ ```
70
+ """
71
+
72
+ demo = gr.Interface(fn=pred_on_text,
73
+ inputs=gr.TextArea(lines=4, label="Input Text"),
74
+ outputs=[gr.TextArea(lines=4, label="Generated Text"),
75
+ gr.TextArea(lines=7, label="Raw Output"),
76
+ gr.Number(label="Generation Time (s)")],
77
+ title="🍳 Structured FoodExtract with a Fine-Tuned Gemma 3 270M",
78
+ description=description,
79
+ examples=[["Hello world! This is my first fine-tuned LLM!"],
80
+ ["A plate of food with grilled barramundi, salad with avocado, olives, tomatoes and Italian dressing"],
81
+ ["British Breakfast with baked beans, fried eggs, black pudding, sausages, bacon, mushrooms, a cup of tea and toast and fried tomatoes"],
82
+ ["Steak tacos"],
83
+ ["A photo of a dog sitting on a beach"]]
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch(share=False)
README.md CHANGED
@@ -1,12 +1,37 @@
 
1
  ---
2
- title: FoodExtract V1
3
- emoji: 🔥
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 6.4.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile demos/FoodExtract/README.md
2
  ---
3
+ title: FoodExtract Fine-tuned LLM Structued Data Extractor
4
+ emoji: 📝➡️🍟
5
+ colorFrom: green
6
+ colorTo: blue
7
  sdk: gradio
 
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
+ """
14
+ Fine-tuned Gemma 3 270M to extract food and drink items from raw text.
15
+
16
+ Input can be any form of real text and output will be a formatted string such as the following:
17
+
18
+ ```
19
+ food_or_drink: 1
20
+ tags: fi, re
21
+ foods: tacos, milk, red apple, pineapple, cherries, fried chicken, steak, mayonnaise
22
+ drinks: iced latte, matcha latte
23
+ ```
24
+
25
+ The tags map to the following items:
26
+
27
+ ```
28
+ tags_dict = {'np': 'nutrition_panel',
29
+ 'il': 'ingredient list',
30
+ 'me': 'menu',
31
+ 're': 'recipe',
32
+ 'fi': 'food_items',
33
+ 'di': 'drink_items',
34
+ 'fa': 'food_advertistment',
35
+ 'fp': 'food_packaging'}
36
+ ```
37
+ """
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile demos/FoodExtract/app.py
2
+
3
+ # Load dependencies
4
+ import time
5
+ import transformers
6
+ import torch
7
+ import spaces # Optional: run our model on the GPU (this will be much faster inference)
8
+
9
+ import gradio as gr
10
+
11
+ from transformers import AutoModelForCausalLM, AutoTokenizer
12
+ from transformers import pipeline
13
+
14
+ @spaces.GPU # Optional: run our model on the GPU (this will be much faster inference)
15
+ def pred_on_text(input_text):
16
+ start_time = time.time()
17
+
18
+ raw_output = loaded_model_pipeline(text_inputs=[{"role": "user",
19
+ "content": input_text}],
20
+ max_new_tokens=256,
21
+ disable_compile=True)
22
+ end_time = time.time()
23
+ total_time = round(end_time - start_time, 4)
24
+
25
+ generated_text = raw_output[0]["generated_text"][1]["content"]
26
+
27
+ return generated_text, raw_output, total_time
28
+
29
+ # Load the model (from our Hugging Face Repo)
30
+ # Note: You may have to replace my username `mrdbourke` for your own
31
+ MODEL_PATH = "doit55/FoodExtract-gemma-3-270m-fine-tune-v1"
32
+
33
+ # Load the model into a pipeline
34
+ loaded_model = AutoModelForCausalLM.from_pretrained(
35
+ pretrained_model_name_or_path=MODEL_PATH,
36
+ dtype="auto",
37
+ device_map="auto",
38
+ attn_implementation="eager"
39
+ )
40
+
41
+ # Load the tokenizer
42
+ tokenizer = AutoTokenizer.from_pretrained(
43
+ MODEL_PATH
44
+ )
45
+
46
+ # Create model pipeline
47
+ loaded_model_pipeline = pipeline("text-generation",
48
+ model=loaded_model,
49
+ tokenizer=tokenizer)
50
+
51
+ # Create the demo
52
+ description = """Extract food and drink items from text with a fine-tuned SLM (Small Language Model) or more specifically a fine-tuned [Gemma 3 270M](https://huggingface.co/google/gemma-3-270m-it).
53
+
54
+ Our model has been fine-tuned on the [FoodExtract-1k dataset](https://huggingface.co/datasets/mrdbourke/FoodExtract-1k).
55
+
56
+ * Input (str): Raw text strings or image captions (e.g. "A photo of a dog sitting on a beach" or "A breakfast plate with bacon, eggs and toast")
57
+ * Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
58
+
59
+ For example:
60
+
61
+ * Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
62
+ * Output:
63
+
64
+ ```
65
+ food_or_drink: 1
66
+ tags: fi, di
67
+ foods: eggs, bacon, toast
68
+ drinks: orange juice
69
+ ```
70
+ """
71
+
72
+ demo = gr.Interface(fn=pred_on_text,
73
+ inputs=gr.TextArea(lines=4, label="Input Text"),
74
+ outputs=[gr.TextArea(lines=4, label="Generated Text"),
75
+ gr.TextArea(lines=7, label="Raw Output"),
76
+ gr.Number(label="Generation Time (s)")],
77
+ title="🍳 Structured FoodExtract with a Fine-Tuned Gemma 3 270M",
78
+ description=description,
79
+ examples=[["Hello world! This is my first fine-tuned LLM!"],
80
+ ["A plate of food with grilled barramundi, salad with avocado, olives, tomatoes and Italian dressing"],
81
+ ["British Breakfast with baked beans, fried eggs, black pudding, sausages, bacon, mushrooms, a cup of tea and toast and fried tomatoes"],
82
+ ["Steak tacos"],
83
+ ["A photo of a dog sitting on a beach"]]
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.launch(share=False)