Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- .gitattributes +5 -0
- README.md +14 -7
- app.py +114 -0
- examples/cake.jpg +3 -0
- examples/gourmet_burger.jpg +3 -0
- examples/macarons.jpg +0 -0
- examples/pancakes_berries.jpg +3 -0
- examples/pizza_board.jpg +3 -0
- examples/sushi_nigiri.jpg +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/cake.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/gourmet_burger.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/pancakes_berries.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/pizza_board.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/sushi_nigiri.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,20 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.22.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: OliveGemma
|
| 3 |
+
emoji: 🫒
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.22.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
short_description: Mediterranean & European diet recognition VLM
|
| 10 |
+
python_version: "3.12"
|
| 11 |
+
startup_duration_timeout: 30m
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# OliveGemma 🫒
|
| 15 |
+
|
| 16 |
+
OliveGemma is a 3B visual-language model for fine-grained food recognition, built on the PaliGemma-2-3B backbone with LoRA fine-tuning on 17,340 images across 216 Mediterranean & European dish categories.
|
| 17 |
+
|
| 18 |
+
Upload a food image and ask a question — the model will identify the dish, list likely ingredients, describe visual evidence, and more.
|
| 19 |
+
|
| 20 |
+
**Model:** [`JamesZar/OliveGemma-3B`](https://huggingface.co/JamesZar/OliveGemma-3B) · **Paper:** [OliveGemma: A 3 Billion VLM for Recognising the Mediterranean & European Diet](https://huggingface.co/papers/2608.03428) · **Code:** [GitHub](https://github.com/tsiokris/OliveGemma)
|
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces # MUST come before any CUDA-touching import
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "JamesZar/OliveGemma-3B"
|
| 7 |
+
|
| 8 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 9 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(
|
| 10 |
+
MODEL_ID, torch_dtype=torch.bfloat16
|
| 11 |
+
).to("cuda").eval()
|
| 12 |
+
|
| 13 |
+
QUESTIONS = [
|
| 14 |
+
"What is the name of this dish?",
|
| 15 |
+
"What are the likely ingredients of this dish?",
|
| 16 |
+
"What visible ingredients can you see?",
|
| 17 |
+
"What visual evidence supports this dish?",
|
| 18 |
+
"How is this dish different from a visually similar one?",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@spaces.GPU(duration=60)
|
| 23 |
+
def recognize(image, question: str, max_new_tokens: int = 64) -> str:
|
| 24 |
+
"""Recognise a Mediterranean or European dish from an image and answer a question about it.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
image: A food photograph.
|
| 28 |
+
question: What to ask the model about the food (dish name, ingredients, etc.).
|
| 29 |
+
max_new_tokens: Maximum number of new tokens to generate.
|
| 30 |
+
"""
|
| 31 |
+
from PIL import Image
|
| 32 |
+
|
| 33 |
+
if image is None:
|
| 34 |
+
return "Please upload an image."
|
| 35 |
+
if not isinstance(image, Image.Image):
|
| 36 |
+
image = Image.open(image)
|
| 37 |
+
image = image.convert("RGB")
|
| 38 |
+
|
| 39 |
+
# PaliGemma prompt format used during training
|
| 40 |
+
prompt = f"<image>answer en {question}\n"
|
| 41 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
|
| 42 |
+
in_len = inputs["input_ids"].shape[-1]
|
| 43 |
+
|
| 44 |
+
with torch.inference_mode():
|
| 45 |
+
out = model.generate(
|
| 46 |
+
**inputs,
|
| 47 |
+
max_new_tokens=int(max_new_tokens),
|
| 48 |
+
do_sample=False,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
answer = processor.decode(out[0][in_len:], skip_special_tokens=True).strip()
|
| 52 |
+
return answer
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
CSS = """
|
| 56 |
+
#col-container { max-width: 1100px; margin: 0 auto; }
|
| 57 |
+
.dark .gradio-container { color: var(--body-text-color); }
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
|
| 61 |
+
gr.Markdown(
|
| 62 |
+
"# OliveGemma 🫒\n"
|
| 63 |
+
"A 3B visual-language model for fine-grained Mediterranean & European food recognition. "
|
| 64 |
+
"Upload a food photo and ask about the dish name, ingredients, or visual evidence.\n\n"
|
| 65 |
+
"Model: [`JamesZar/OliveGemma-3B`](https://huggingface.co/JamesZar/OliveGemma-3B) · "
|
| 66 |
+
"Paper: [2608.03428](https://huggingface.co/papers/2608.03428) · "
|
| 67 |
+
"Code: [GitHub](https://github.com/tsiokris/OliveGemma)"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with gr.Column(elem_id="col-container"):
|
| 71 |
+
with gr.Row():
|
| 72 |
+
image_input = gr.Image(type="pil", label="Food image", scale=1)
|
| 73 |
+
with gr.Column(scale=1):
|
| 74 |
+
question_input = gr.Dropdown(
|
| 75 |
+
choices=QUESTIONS,
|
| 76 |
+
value=QUESTIONS[0],
|
| 77 |
+
label="Question",
|
| 78 |
+
interactive=True,
|
| 79 |
+
)
|
| 80 |
+
recognize_btn = gr.Button("Recognise", variant="primary")
|
| 81 |
+
|
| 82 |
+
output_text = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 83 |
+
|
| 84 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 85 |
+
max_tokens = gr.Slider(
|
| 86 |
+
minimum=16, maximum=256, value=64, step=16,
|
| 87 |
+
label="Max new tokens",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
gr.Examples(
|
| 91 |
+
examples=[
|
| 92 |
+
["examples/pizza_board.jpg", QUESTIONS[0]],
|
| 93 |
+
["examples/sushi_nigiri.jpg", QUESTIONS[0]],
|
| 94 |
+
["examples/pancakes_berries.jpg", QUESTIONS[0]],
|
| 95 |
+
["examples/gourmet_burger.jpg", QUESTIONS[1]],
|
| 96 |
+
["examples/macarons.jpg", QUESTIONS[0]],
|
| 97 |
+
["examples/cake.jpg", QUESTIONS[2]],
|
| 98 |
+
],
|
| 99 |
+
inputs=[image_input, question_input],
|
| 100 |
+
outputs=output_text,
|
| 101 |
+
fn=recognize,
|
| 102 |
+
cache_examples=True,
|
| 103 |
+
cache_mode="lazy",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
recognize_btn.click(
|
| 107 |
+
fn=recognize,
|
| 108 |
+
inputs=[image_input, question_input, max_tokens],
|
| 109 |
+
outputs=output_text,
|
| 110 |
+
api_name="recognize",
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch(mcp_server=True)
|
examples/cake.jpg
ADDED
|
Git LFS Details
|
examples/gourmet_burger.jpg
ADDED
|
Git LFS Details
|
examples/macarons.jpg
ADDED
|
examples/pancakes_berries.jpg
ADDED
|
Git LFS Details
|
examples/pizza_board.jpg
ADDED
|
Git LFS Details
|
examples/sushi_nigiri.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
accelerate
|
| 3 |
+
pillow
|
| 4 |
+
torchvision
|