mrdbourke commited on
Commit
401f0f9
Β·
verified Β·
1 Parent(s): 9bd89d0

Uploading FoodExtract demo app.py

Browse files
Files changed (3) hide show
  1. README.md +30 -6
  2. app.py +77 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,36 @@
1
  ---
2
- title: FoodExtract V1
3
- emoji: πŸ’»
4
- colorFrom: red
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 6.2.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
  ---
2
+ title: FoodExtract Fine-tuned LLM Structued Data Extractor
3
+ emoji: πŸ“βž‘οΈπŸŸ
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: gradio
 
7
  app_file: app.py
8
  pinned: false
9
+ license: apache-2.0
10
  ---
11
 
12
+ """
13
+ Fine-tuned Gemma 3 270M to extract food and drink items from raw text.
14
+
15
+ Input can be any form of real text and output will be a formatted string such as the following:
16
+
17
+ ```
18
+ food_or_drink: 1
19
+ tags: fi, re
20
+ foods: tacos, milk, red apple, pineapple, cherries, fried chicken, steak, mayonnaise
21
+ drinks: iced latte, matcha latte
22
+ ```
23
+
24
+ The tags map to the following items:
25
+
26
+ ```
27
+ tags_dict = {'np': 'nutrition_panel',
28
+ 'il': 'ingredient list',
29
+ 'me': 'menu',
30
+ 're': 'recipe',
31
+ 'fi': 'food_items',
32
+ 'di': 'drink_items',
33
+ 'fa': 'food_advertistment',
34
+ 'fp': 'food_packaging'}
35
+ ```
36
+ """
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Load dependencies
3
+ import time
4
+ import transformers
5
+ import torch
6
+
7
+ import gradio as gr
8
+
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
10
+ from transformers import pipeline
11
+
12
+ def pred_on_text(input_text):
13
+ start_time = time.time()
14
+
15
+ raw_output = loaded_model_pipeline(text_inputs=[{"role": "user",
16
+ "content": input_text}],
17
+ max_new_tokens=256,
18
+ disable_compile=True)
19
+ end_time = time.time()
20
+ total_time = round(end_time - start_time, 4)
21
+
22
+ generated_text = raw_output[0]["generated_text"][1]["content"]
23
+
24
+ return generated_text, raw_output, total_time
25
+
26
+ # Load the model (from our Hugging Face Repo)
27
+ MODEL_PATH = "mrdbourke/FoodExtract-gemma-3-270m-fine-tune-v1"
28
+
29
+ # Load the model into a pipeline
30
+ loaded_model = AutoModelForCausalLM.from_pretrained(
31
+ pretrained_model_name_or_path=MODEL_PATH,
32
+ dtype="auto",
33
+ device_map="auto",
34
+ attn_implementation="eager"
35
+ )
36
+
37
+ tokenizer = AutoTokenizer.from_pretrained(
38
+ MODEL_PATH
39
+ )
40
+
41
+ # Create model pipeline
42
+ loaded_model_pipeline = pipeline("text-generation",
43
+ model=loaded_model,
44
+ tokenizer=tokenizer)
45
+
46
+ # Create the demo
47
+ description = """Extract food and drink items from text with a fine-tuned SLM (Small Language Model).
48
+
49
+ * 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")
50
+ * Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
51
+
52
+ For example:
53
+
54
+ Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
55
+ Output:
56
+ food_or_drink: 1
57
+ tags: fi, di
58
+ foods: eggs, bacon, toast
59
+ drinks: orange juice
60
+ """
61
+
62
+ demo = gr.Interface(fn=pred_on_text,
63
+ inputs=gr.TextArea(lines=4, label="Input Text"),
64
+ outputs=[gr.TextArea(lines=4, label="Generated Text"),
65
+ gr.TextArea(lines=7, label="Raw Output"),
66
+ gr.Number(label="Generation Time (s)")],
67
+ title="🍳 Structured FoodExtract with a Fine-Tuned Gemma 3 270M",
68
+ description=description,
69
+ examples=[["Hello world! This is my first fine-tuned LLM!"],
70
+ ["A plate of food with grilled barramundi, salad with avocado, olives, tomatoes and Italian dressing"],
71
+ ["British Breakfast with baked beans, fried eggs, black pudding, sausages, bacon, mushrooms, a cup of tea and toast and fried tomatoes"],
72
+ ["Steak tacos"],
73
+ ["A photo of a dog sitting on a beach"]]
74
+ )
75
+
76
+ if __name__ == "__main__":
77
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch