Spaces:
Running
Running
Uploading FoodExtract demo app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import time
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
MODEL_PATH = "Janushi/FoodExtract-gemma-3-270m-fine-tune-v1"
|
| 9 |
+
|
| 10 |
+
loaded_model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
pretrained_model_name_or_path=MODEL_PATH,
|
| 12 |
+
dtype="auto",
|
| 13 |
+
device_map="auto",
|
| 14 |
+
attn_implementation="eager"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 18 |
+
|
| 19 |
+
loaded_model_pipeline = pipeline(
|
| 20 |
+
"text-generation",
|
| 21 |
+
model=loaded_model,
|
| 22 |
+
tokenizer=tokenizer
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def pred_on_text(input_text):
|
| 26 |
+
start_time = time.time()
|
| 27 |
+
raw_output = loaded_model_pipeline(
|
| 28 |
+
text_inputs=[{"role": "user", "content": input_text}],
|
| 29 |
+
max_new_tokens=256,
|
| 30 |
+
disable_compile=True
|
| 31 |
+
)
|
| 32 |
+
end_time = time.time()
|
| 33 |
+
total_time = round(end_time - start_time, 4)
|
| 34 |
+
generated_text = raw_output[0]["generated_text"][1]["content"]
|
| 35 |
+
return generated_text, raw_output, total_time
|
| 36 |
+
|
| 37 |
+
description = """Extract food and drink items from text using a fine-tuned Gemma-3-270M.
|
| 38 |
+
Fine-tuned on mrdbourke/FoodExtract-1k dataset.
|
| 39 |
+
|
| 40 |
+
**Input:** Any text or image caption
|
| 41 |
+
**Output:** Structured food/drink extraction
|
| 42 |
+
|
| 43 |
+
**Example:**
|
| 44 |
+
- Input: "eggs, bacon and toast with orange juice"
|
| 45 |
+
- Output: food_or_drink: 1, foods: eggs, bacon, toast, drinks: orange juice
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=pred_on_text,
|
| 50 |
+
inputs=gr.TextArea(lines=4, label="Input Text"),
|
| 51 |
+
outputs=[
|
| 52 |
+
gr.TextArea(lines=4, label="Generated Text"),
|
| 53 |
+
gr.TextArea(lines=7, label="Raw Output"),
|
| 54 |
+
gr.Number(label="Generation Time (s)")
|
| 55 |
+
],
|
| 56 |
+
title="🍳 BiteSight — Food Extraction with Fine-Tuned Gemma-3-270M",
|
| 57 |
+
description=description,
|
| 58 |
+
examples=[
|
| 59 |
+
["A plate of grilled tofu, salad with avocado and tomatoes"],
|
| 60 |
+
["Indian breakfast with roti, tea and fried potatoes"],
|
| 61 |
+
["cheese tacos"],
|
| 62 |
+
["A photo of a dog sitting on a beach"]
|
| 63 |
+
]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch(share=False)
|