Spaces:
Sleeping
Sleeping
Uploading FoodExtract-Vision demo app.py from YouTube tutorial video
Browse files
app.py
CHANGED
|
@@ -1,78 +1,185 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import spaces
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
BASE_MODEL_ID = "HuggingFaceTB/SmolVLM2-500M-Video-Instruct"
|
|
|
|
| 7 |
FINE_TUNED_MODEL_ID = "ninjals/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v1-VIDEO"
|
| 8 |
OUTPUT_TOKENS = 256
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
original_pipeline =
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
ft_pipe = pipeline(
|
| 28 |
-
"image-text-to-text",
|
| 29 |
-
model=FINE_TUNED_MODEL_ID,
|
| 30 |
-
device_map="auto",
|
| 31 |
-
dtype=DTYPE, # ✅ keep fp16 (don’t use bf16)
|
| 32 |
-
)
|
| 33 |
|
| 34 |
def create_message(input_image):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
' "is_food": 0,\n'
|
| 41 |
-
' "image_title": "",\n'
|
| 42 |
-
' "food_items": [],\n'
|
| 43 |
-
' "drink_items": []\n'
|
| 44 |
-
'}\n'
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
return [{
|
| 48 |
-
"role": "user",
|
| 49 |
-
"content": [
|
| 50 |
-
{"type": "image", "image": input_image},
|
| 51 |
-
{"type": "text", "text": prompt},
|
| 52 |
-
],
|
| 53 |
-
}]
|
| 54 |
|
| 55 |
@spaces.GPU
|
| 56 |
def extract_foods_from_image(input_image):
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
outputs_pretrained = base_out[0][0]["generated_text"][-1]["content"]
|
| 66 |
-
outputs_fine_tuned = ft_out[0][0]["generated_text"][-1]["content"]
|
| 67 |
return outputs_pretrained, outputs_fine_tuned
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=extract_foods_from_image,
|
| 71 |
inputs=gr.Image(type="pil"),
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
-
demo.launch(show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
|
| 4 |
import spaces
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
BASE_MODEL_ID = "HuggingFaceTB/SmolVLM2-500M-Video-Instruct"
|
| 8 |
+
# FINE_TUNED_MODEL_ID = "mrdbourke/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v1"
|
| 9 |
FINE_TUNED_MODEL_ID = "ninjals/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v1-VIDEO"
|
| 10 |
OUTPUT_TOKENS = 256
|
| 11 |
|
| 12 |
+
# Load original base model (no fine-tuning)
|
| 13 |
+
print(f"[INFO] Loading Original Model")
|
| 14 |
+
original_pipeline = pipeline(
|
| 15 |
+
"image-text-to-text",
|
| 16 |
+
model=BASE_MODEL_ID,
|
| 17 |
+
dtype=torch.float16,
|
| 18 |
+
device_map="auto"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Load fine-tuned model
|
| 22 |
+
print(f"[INFO] Loading Fine-tuned Model")
|
| 23 |
+
ft_pipe = pipeline(
|
| 24 |
+
"image-text-to-text",
|
| 25 |
+
model=FINE_TUNED_MODEL_ID,
|
| 26 |
+
dtype=torch.bfloat16,
|
| 27 |
+
device_map="auto"
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def create_message(input_image):
|
| 31 |
+
return [{'role': 'user',
|
| 32 |
+
'content': [{'type': 'image',
|
| 33 |
+
'image': input_image},
|
| 34 |
+
{'type': 'text',
|
| 35 |
+
'text': "Classify the given input image into food or not and if edible food or drink items are present, extract those to a list. If no food/drink items are visible, return empty lists.\n\nOnly return valid JSON in the following form:\n\n```json\n{\n 'is_food': 0, # int - 0 or 1 based on whether food/drinks are present (0 = no foods visible, 1 = foods visible)\n 'image_title': '', # str - short food-related title for what foods/drinks are visible in the image, leave blank if no foods present\n 'food_items': [], # list[str] - list of visible edible food item nouns\n 'drink_items': [] # list[str] - list of visible edible drink item nouns\n}\n```\n"}]}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
@spaces.GPU
|
| 38 |
def extract_foods_from_image(input_image):
|
| 39 |
+
input_image = input_image.resize(size=(512, 512))
|
| 40 |
+
input_message = create_message(input_image=input_image)
|
| 41 |
|
| 42 |
+
# Get outputs from base model (not fine-tuned)
|
| 43 |
+
original_pipeline_output = original_pipeline(text=[input_message],
|
| 44 |
+
max_new_tokens=OUTPUT_TOKENS)
|
| 45 |
|
| 46 |
+
outputs_pretrained = original_pipeline_output[0][0]["generated_text"][-1]["content"]
|
| 47 |
+
|
| 48 |
+
# Get outputs from fine-tuned model (fine-tuned on food images)
|
| 49 |
+
ft_pipe_output = ft_pipe(text=[input_message],
|
| 50 |
+
max_new_tokens=OUTPUT_TOKENS)
|
| 51 |
+
outputs_fine_tuned = ft_pipe_output[0][0]["generated_text"][-1]["content"]
|
| 52 |
|
|
|
|
|
|
|
| 53 |
return outputs_pretrained, outputs_fine_tuned
|
| 54 |
|
| 55 |
+
demo_title = "🥑➡️📝 FoodExtract-Vision with a fine-tuned SmolVLM2-500M"
|
| 56 |
+
demo_description = """* **Base model:** https://huggingface.co/HuggingFaceTB/SmolVLM-500M-Instruct
|
| 57 |
+
* **Fine-tuning dataset:** https://huggingface.co/datasets/mrdbourke/FoodExtract-1k-Vision (1k food images and 500 not food images)
|
| 58 |
+
* **Fine-tuned model:** https://huggingface.co/ninjals/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v1-VIDEO
|
| 59 |
+
|
| 60 |
+
## Overview
|
| 61 |
+
|
| 62 |
+
Extract food and drink items in a structured way from images.
|
| 63 |
+
|
| 64 |
+
The original model outputs fail to capture the desired structure. But the fine-tuned model sticks to the output structure quite well.
|
| 65 |
+
|
| 66 |
+
However, the fine-tuned model could definitely be improved with respects to its ability to extract the right food/drink items.
|
| 67 |
+
|
| 68 |
+
Both models use the input prompt:
|
| 69 |
+
|
| 70 |
+
````
|
| 71 |
+
Classify the given input image into food or not and if edible food or drink items are present, extract those to a list. If no food/drink items are visible, return empty lists.
|
| 72 |
+
|
| 73 |
+
Only return valid JSON in the following form:
|
| 74 |
+
|
| 75 |
+
```json
|
| 76 |
+
{
|
| 77 |
+
'is_food': 0, # int - 0 or 1 based on whether food/drinks are present (0 = no foods visible, 1 = foods visible)
|
| 78 |
+
'image_title': '', # str - short food-related title for what foods/drinks are visible in the image, leave blank if no foods present
|
| 79 |
+
'food_items': [], # list[str] - list of visible edible food item nouns
|
| 80 |
+
'drink_items': [] # list[str] - list of visible edible drink item nouns
|
| 81 |
+
}
|
| 82 |
+
```
|
| 83 |
+
````
|
| 84 |
+
|
| 85 |
+
Except one model has been fine-tuned on the structured data whereas the other hasn't.
|
| 86 |
+
|
| 87 |
+
Notable next steps would be:
|
| 88 |
+
* **Remove the input prompt:** Just train the model to go straight from image -> text (no text prompt on input), this would save on inference tokens.
|
| 89 |
+
* **Fine-tune on more real-world data:** Right now the model is only trained on 1k food images (from Food101) and 500 not food (random internet images), training on real world data would likely significantly improve performance.
|
| 90 |
+
* **Fix the repetitive generation:** The model can sometimes get stuck in a repetitive generation pattern, e.g. "onions", "onions", "onions", etc. We could look into patterns to help reduce this.
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
demo = gr.Interface(
|
| 94 |
fn=extract_foods_from_image,
|
| 95 |
inputs=gr.Image(type="pil"),
|
| 96 |
+
title=demo_title,
|
| 97 |
+
description=demo_description,
|
| 98 |
+
outputs=[gr.Textbox(lines=4, label="Original Model (not fine-tuned)"),
|
| 99 |
+
gr.Textbox(lines=4, label="Fine-tuned Model")],
|
| 100 |
+
examples=[["examples/camera.jpeg"],
|
| 101 |
+
["examples/Tandoori-Chicken.jpg"],
|
| 102 |
+
["examples/fries.jpeg"]],
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
demo.launch(share=False, show_error=True)
|
| 107 |
+
|
| 108 |
+
# %%writefile demos/FoodExtract-Vision-v1/app.py
|
| 109 |
+
# import torch
|
| 110 |
+
# import gradio as gr
|
| 111 |
+
# import spaces
|
| 112 |
+
# from transformers import pipeline
|
| 113 |
+
|
| 114 |
+
# BASE_MODEL_ID = "HuggingFaceTB/SmolVLM2-500M-Video-Instruct"
|
| 115 |
+
# FINE_TUNED_MODEL_ID = "ninjals/FoodExtract-Vision-SmolVLM2-500M-fine-tune-v1-VIDEO"
|
| 116 |
+
# OUTPUT_TOKENS = 256
|
| 117 |
+
|
| 118 |
+
# DTYPE = torch.float16 # ✅ safest default
|
| 119 |
+
|
| 120 |
+
# original_pipeline = None
|
| 121 |
+
# ft_pipe = None
|
| 122 |
+
|
| 123 |
+
# def _load_pipes():
|
| 124 |
+
# global original_pipeline, ft_pipe
|
| 125 |
+
# if original_pipeline is None:
|
| 126 |
+
# print("[INFO] Loading Original Model")
|
| 127 |
+
# original_pipeline = pipeline(
|
| 128 |
+
# "image-text-to-text",
|
| 129 |
+
# model=BASE_MODEL_ID,
|
| 130 |
+
# device_map="auto",
|
| 131 |
+
# dtype=DTYPE,
|
| 132 |
+
# )
|
| 133 |
+
# if ft_pipe is None:
|
| 134 |
+
# print("[INFO] Loading Fine-tuned Model")
|
| 135 |
+
# ft_pipe = pipeline(
|
| 136 |
+
# "image-text-to-text",
|
| 137 |
+
# model=FINE_TUNED_MODEL_ID,
|
| 138 |
+
# device_map="auto",
|
| 139 |
+
# dtype=DTYPE, # ✅ keep fp16 (don’t use bf16)
|
| 140 |
+
# )
|
| 141 |
+
|
| 142 |
+
# def create_message(input_image):
|
| 143 |
+
# prompt = (
|
| 144 |
+
# "Classify the given input image into food or not and if edible food or drink items are present, "
|
| 145 |
+
# "extract those to a list. If no food/drink items are visible, return empty lists.\n\n"
|
| 146 |
+
# 'Only return valid JSON in the following form:\n\n'
|
| 147 |
+
# '{\n'
|
| 148 |
+
# ' "is_food": 0,\n'
|
| 149 |
+
# ' "image_title": "",\n'
|
| 150 |
+
# ' "food_items": [],\n'
|
| 151 |
+
# ' "drink_items": []\n'
|
| 152 |
+
# '}\n'
|
| 153 |
+
# )
|
| 154 |
+
|
| 155 |
+
# return [{
|
| 156 |
+
# "role": "user",
|
| 157 |
+
# "content": [
|
| 158 |
+
# {"type": "image", "image": input_image},
|
| 159 |
+
# {"type": "text", "text": prompt},
|
| 160 |
+
# ],
|
| 161 |
+
# }]
|
| 162 |
+
|
| 163 |
+
# @spaces.GPU
|
| 164 |
+
# def extract_foods_from_image(input_image):
|
| 165 |
+
# _load_pipes()
|
| 166 |
+
|
| 167 |
+
# input_image = input_image.resize((512, 512))
|
| 168 |
+
# input_message = create_message(input_image)
|
| 169 |
+
|
| 170 |
+
# base_out = original_pipeline(text=[input_message], max_new_tokens=OUTPUT_TOKENS, do_sample=False)
|
| 171 |
+
# ft_out = ft_pipe(text=[input_message], max_new_tokens=OUTPUT_TOKENS, do_sample=False)
|
| 172 |
+
|
| 173 |
+
# outputs_pretrained = base_out[0][0]["generated_text"][-1]["content"]
|
| 174 |
+
# outputs_fine_tuned = ft_out[0][0]["generated_text"][-1]["content"]
|
| 175 |
+
# return outputs_pretrained, outputs_fine_tuned
|
| 176 |
+
|
| 177 |
+
# demo = gr.Interface(
|
| 178 |
+
# fn=extract_foods_from_image,
|
| 179 |
+
# inputs=gr.Image(type="pil"),
|
| 180 |
+
# outputs=[gr.Textbox(lines=4), gr.Textbox(lines=4)],
|
| 181 |
+
# examples=[["examples/camera.jpeg"], ["examples/Tandoori-Chicken.jpg"], ["examples/fries.jpeg"]],
|
| 182 |
+
# )
|
| 183 |
|
| 184 |
+
# if __name__ == "__main__":
|
| 185 |
+
# demo.launch(show_error=True)
|