Spaces:
Paused
Paused
Uploading FoodExtract demo app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,14 @@
|
|
| 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 |
|
|
@@ -26,6 +26,7 @@ def pred_on_text(input_text):
|
|
| 26 |
return generated_text, raw_output, total_time
|
| 27 |
|
| 28 |
# Load the model (from our Hugging Face Repo)
|
|
|
|
| 29 |
MODEL_PATH = "mrdbourke/FoodExtract-gemma-3-270m-fine-tune-v1"
|
| 30 |
|
| 31 |
# Load the model into a pipeline
|
|
@@ -36,6 +37,7 @@ loaded_model = AutoModelForCausalLM.from_pretrained(
|
|
| 36 |
attn_implementation="eager"
|
| 37 |
)
|
| 38 |
|
|
|
|
| 39 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 40 |
MODEL_PATH
|
| 41 |
)
|
|
@@ -46,19 +48,24 @@ loaded_model_pipeline = pipeline("text-generation",
|
|
| 46 |
tokenizer=tokenizer)
|
| 47 |
|
| 48 |
# Create the demo
|
| 49 |
-
description = """Extract food and drink items from text with a fine-tuned SLM (Small Language Model).
|
|
|
|
|
|
|
| 50 |
|
| 51 |
* 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")
|
| 52 |
* Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
|
| 53 |
|
| 54 |
For example:
|
| 55 |
|
| 56 |
-
Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
|
| 57 |
-
Output:
|
|
|
|
|
|
|
| 58 |
food_or_drink: 1
|
| 59 |
tags: fi, di
|
| 60 |
foods: eggs, bacon, toast
|
| 61 |
drinks: orange juice
|
|
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
demo = gr.Interface(fn=pred_on_text,
|
|
|
|
| 3 |
import time
|
| 4 |
import transformers
|
| 5 |
import torch
|
| 6 |
+
import spaces # Optional: run our model on the GPU (this will be much faster inference)
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 11 |
from transformers import pipeline
|
| 12 |
|
| 13 |
+
@spaces.GPU # Optional: run our model on the GPU (this will be much faster inference)
|
| 14 |
def pred_on_text(input_text):
|
| 15 |
start_time = time.time()
|
| 16 |
|
|
|
|
| 26 |
return generated_text, raw_output, total_time
|
| 27 |
|
| 28 |
# Load the model (from our Hugging Face Repo)
|
| 29 |
+
# Note: You may have to replace my username `mrdbourke` for your own
|
| 30 |
MODEL_PATH = "mrdbourke/FoodExtract-gemma-3-270m-fine-tune-v1"
|
| 31 |
|
| 32 |
# Load the model into a pipeline
|
|
|
|
| 37 |
attn_implementation="eager"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Load the tokenizer
|
| 41 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 42 |
MODEL_PATH
|
| 43 |
)
|
|
|
|
| 48 |
tokenizer=tokenizer)
|
| 49 |
|
| 50 |
# Create the demo
|
| 51 |
+
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).
|
| 52 |
+
|
| 53 |
+
Our model has been fine-tuned on the [FoodExtract-1k dataset](https://huggingface.co/datasets/mrdbourke/FoodExtract-1k).
|
| 54 |
|
| 55 |
* 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")
|
| 56 |
* Output (str): Generated text with food/not_food classification as well as noun extracted food and drink items and various food tags.
|
| 57 |
|
| 58 |
For example:
|
| 59 |
|
| 60 |
+
* Input: "For breakfast I had eggs, bacon and toast and a glass of orange juice"
|
| 61 |
+
* Output:
|
| 62 |
+
|
| 63 |
+
```
|
| 64 |
food_or_drink: 1
|
| 65 |
tags: fi, di
|
| 66 |
foods: eggs, bacon, toast
|
| 67 |
drinks: orange juice
|
| 68 |
+
```
|
| 69 |
"""
|
| 70 |
|
| 71 |
demo = gr.Interface(fn=pred_on_text,
|