ManvithReddy commited on
Commit
6716c4c
Β·
verified Β·
1 Parent(s): abdd8db

Uploading FoodExtract demo app.py

Browse files
Files changed (3) hide show
  1. README.md +30 -6
  2. app.py +84 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,36 @@
1
  ---
2
- title: FoodExtract V1
3
- emoji: 🏒
4
- colorFrom: indigo
5
- colorTo: indigo
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
  ---
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,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Load dependencies
3
+ import time
4
+ import transformers
5
+ import torch
6
+ import spaces
7
+
8
+ import gradio as gr
9
+
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer
11
+ from transformers import pipeline
12
+
13
+ @spaces.GPU
14
+ def pred_on_text(input_text):
15
+ start_time = time.time()
16
+
17
+ raw_output = loaded_model_pipeline(text_inputs=[{"role": "user",
18
+ "content": input_text}],
19
+ max_new_tokens=256,
20
+ disable_compile=True)
21
+ end_time = time.time()
22
+ total_time = round(end_time - start_time, 4)
23
+
24
+ generated_text = raw_output[0]["generated_text"][1]["content"]
25
+
26
+ return generated_text, raw_output, total_time
27
+
28
+ # Load the model (from our Hugging Face Repo)
29
+ MODEL_PATH = "ManvithReddy/FoodExtract-gemma-3-270m-fine-tune-v1"
30
+
31
+ # Load the model into a pipeline
32
+ loaded_model = AutoModelForCausalLM.from_pretrained(
33
+ pretrained_model_name_or_path=MODEL_PATH,
34
+ dtype="auto",
35
+ device_map="auto",
36
+ attn_implementation="eager"
37
+ )
38
+
39
+ # Load the tokenizer
40
+ tokenizer = AutoTokenizer.from_pretrained(
41
+ MODEL_PATH
42
+ )
43
+
44
+ # Create model pipeline
45
+ loaded_model_pipeline = pipeline("text-generation",
46
+ model=loaded_model,
47
+ tokenizer=tokenizer)
48
+
49
+ 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).
50
+
51
+ Our model has been fine-tuned on the [FoodExtract-1k dataset](https://huggingface.co/datasets/mrdbourke/FoodExtract-1k).
52
+
53
+ * 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")
54
+ * Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
55
+
56
+ For example:
57
+
58
+ * Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
59
+ * Output:
60
+
61
+ ```
62
+ food_or_drink: 1
63
+ tags: fi, di
64
+ foods: eggs, bacon, toast
65
+ drinks: orange juice
66
+ ```
67
+ """
68
+
69
+ demo = gr.Interface(fn=pred_on_text,
70
+ inputs=gr.TextArea(lines=4, label="Input Text"),
71
+ outputs=[gr.TextArea(lines=4, label="Generated Text"),
72
+ gr.TextArea(lines=7, label="Raw Output"),
73
+ gr.Number(label="Generation Time (s)")],
74
+ title="🍳 Structured FoodExtract with a Fine-Tuned Gemma 3 270M",
75
+ description=description,
76
+ examples=[["Hello world! This is my first fine-tuned LLM!"],
77
+ ["A plate of food with grilled barramundi, salad with avocado, olives, tomatoes and Italian dressing"],
78
+ ["British Breakfast with baked beans, fried eggs, black pudding, sausages, bacon, mushrooms, a cup of tea and toast and fried tomatoes"],
79
+ ["Steak tacos"],
80
+ ["A photo of a dog sitting on a beach"]]
81
+ )
82
+
83
+ if __name__ == "__main__":
84
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch
4
+ accelerate