Spaces:
Runtime error
Runtime error
Update GUI with 2 models
#1
by
sergiopaniego
HF Staff
- opened
- README.md +1 -1
- app.py +89 -101
- requirements.txt +5 -4
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: π
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.34.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
app.py
CHANGED
|
@@ -1,129 +1,117 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
from functools import partial
|
| 6 |
|
| 7 |
-
import
|
| 8 |
import gradio as gr
|
| 9 |
-
import
|
|
|
|
|
|
|
| 10 |
from datasets import load_dataset
|
| 11 |
-
from
|
|
|
|
| 12 |
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
|
| 13 |
-
from PIL import Image, ImageDraw
|
| 14 |
|
| 15 |
|
| 16 |
-
# ---
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# --- Utils ---
|
| 30 |
def parse_paligemma_label(label, width, height):
|
| 31 |
-
# Extract location codes
|
| 32 |
loc_pattern = r"<loc(\d{4})>"
|
| 33 |
locations = [int(loc) for loc in re.findall(loc_pattern, label)]
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
category = label.split(">")[-1].strip()
|
| 37 |
-
|
| 38 |
-
# Order in PaliGemma format is: y1, x1, y2, x2
|
| 39 |
-
y1_norm, x1_norm, y2_norm, x2_norm = locations
|
| 40 |
-
|
| 41 |
-
# Convert normalized coordinates to image coordinates
|
| 42 |
-
x1 = (x1_norm / 1024) * width
|
| 43 |
-
y1 = (y1_norm / 1024) * height
|
| 44 |
-
x2 = (x2_norm / 1024) * width
|
| 45 |
-
y2 = (y2_norm / 1024) * height
|
| 46 |
-
|
| 47 |
return category, [x1, y1, x2, y2]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
draw_image = image.copy()
|
| 53 |
draw = ImageDraw.Draw(draw_image)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
draw.rectangle(bbox, outline="red", width=2)
|
| 58 |
-
draw.text((bbox[0], max(0, bbox[1] - 10)), category, fill="red")
|
| 59 |
-
|
| 60 |
return draw_image
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
images.append([sample["image"]])
|
| 68 |
-
prompts.append(f"{processor.tokenizer.boi_token} detect \n\n")
|
| 69 |
-
|
| 70 |
-
batch = processor(images=images, text=prompts, return_tensors="pt", padding=True)
|
| 71 |
-
batch["pixel_values"] = batch["pixel_values"].to(dtype)
|
| 72 |
-
return batch, images
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# --- Initialize ---
|
| 76 |
-
cfg = Configuration()
|
| 77 |
-
|
| 78 |
-
processor = AutoProcessor.from_pretrained(cfg.checkpoint_id)
|
| 79 |
-
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 80 |
-
cfg.checkpoint_id,
|
| 81 |
-
torch_dtype=cfg.dtype,
|
| 82 |
-
device_map="cpu",
|
| 83 |
-
)
|
| 84 |
-
model.eval()
|
| 85 |
-
|
| 86 |
-
test_dataset = load_dataset(cfg.dataset_id, split="test")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
def get_sample():
|
| 90 |
-
sample = random.choice(test_dataset)
|
| 91 |
-
images = [[sample["image"]]]
|
| 92 |
-
prompts = [f"{processor.tokenizer.boi_token} detect \n\n"]
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
# --- Prediction Logic ---
|
| 101 |
-
@spaces.GPU
|
| 102 |
-
def run_prediction():
|
| 103 |
-
model.to(cfg.device)
|
| 104 |
-
|
| 105 |
-
batch, raw_image = get_sample()
|
| 106 |
-
batch = {k: v.to(cfg.device) if isinstance(v, torch.Tensor) else v for k, v in batch.items()}
|
| 107 |
|
| 108 |
with torch.no_grad():
|
| 109 |
-
generation = model.generate(**
|
| 110 |
-
decoded = processor.batch_decode(generation, skip_special_tokens=True)[0]
|
| 111 |
-
|
| 112 |
-
image = raw_image # β
FIXED: raw_image is already a PIL.Image
|
| 113 |
-
width, height = image.size
|
| 114 |
-
|
| 115 |
-
result_image = visualize_bounding_boxes(image, decoded, width, height)
|
| 116 |
-
return result_image
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
+
import re
|
|
|
|
| 3 |
|
| 4 |
+
import albumentations as A
|
| 5 |
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
from PIL import Image, ImageDraw
|
| 9 |
from datasets import load_dataset
|
| 10 |
+
from gradio.themes.soft import Soft
|
| 11 |
+
from spaces import GPU
|
| 12 |
from transformers import AutoProcessor, Gemma3ForConditionalGeneration
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
+
# --- Config ---
|
| 16 |
+
dataset_id = "ariG23498/license-detection-paligemma"
|
| 17 |
+
model_id = "google/gemma-3-4b-pt"
|
| 18 |
+
|
| 19 |
+
MODEL_OPTIONS = {
|
| 20 |
+
"π΅ Fine-tuned": "sergiopaniego/gemma-3-4b-pt-object-detection",
|
| 21 |
+
"π£ Fine-tuned (updated tokenizer with `<loc>` tokens)": "sergiopaniego/gemma-3-4b-pt-object-detection-loc-tokens",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
resize_size = 512 if "SmolVLM" in model_id else 896
|
| 25 |
+
transform = A.Compose([A.Resize(height=resize_size, width=resize_size)])
|
| 26 |
+
|
| 27 |
+
dataset = load_dataset(dataset_id, split="test")
|
| 28 |
+
loaded_models = {}
|
| 29 |
|
| 30 |
+
def load_model(checkpoint_id):
|
| 31 |
+
if checkpoint_id not in loaded_models:
|
| 32 |
+
processor = AutoProcessor.from_pretrained(checkpoint_id)
|
| 33 |
+
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 34 |
+
checkpoint_id,
|
| 35 |
+
torch_dtype="auto",
|
| 36 |
+
device_map="auto",
|
| 37 |
+
)
|
| 38 |
+
model.eval()
|
| 39 |
+
loaded_models[checkpoint_id] = (processor, model)
|
| 40 |
+
return loaded_models[checkpoint_id]
|
| 41 |
|
|
|
|
| 42 |
def parse_paligemma_label(label, width, height):
|
|
|
|
| 43 |
loc_pattern = r"<loc(\d{4})>"
|
| 44 |
locations = [int(loc) for loc in re.findall(loc_pattern, label)]
|
| 45 |
+
if len(locations) != 4:
|
| 46 |
+
return None, None
|
| 47 |
+
y1, x1, y2, x2 = locations
|
| 48 |
+
x1, x2 = (x1 / 1024) * width, (x2 / 1024) * width
|
| 49 |
+
y1, y2 = (y1 / 1024) * height, (y2 / 1024) * height
|
| 50 |
category = label.split(">")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return category, [x1, y1, x2, y2]
|
| 52 |
|
| 53 |
+
def visualize_bounding_boxes(image, label):
|
| 54 |
+
width, height = image.size
|
| 55 |
+
category, bbox = parse_paligemma_label(label, width, height)
|
| 56 |
draw_image = image.copy()
|
| 57 |
draw = ImageDraw.Draw(draw_image)
|
| 58 |
+
if bbox:
|
| 59 |
+
draw.rectangle(bbox, outline="red", width=2)
|
| 60 |
+
draw.text((bbox[0], max(0, bbox[1] - 10)), category, fill="red")
|
|
|
|
|
|
|
|
|
|
| 61 |
return draw_image
|
| 62 |
|
| 63 |
+
@GPU
|
| 64 |
+
def detect_random_image(model_choice):
|
| 65 |
+
checkpoint_id = MODEL_OPTIONS[model_choice]
|
| 66 |
+
processor, model = load_model(checkpoint_id)
|
| 67 |
|
| 68 |
+
sample = random.choice(dataset)
|
| 69 |
+
image = sample["image"]
|
| 70 |
+
transformed = transform(image=np.array(image))
|
| 71 |
+
image_resized = Image.fromarray(transformed["image"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
prompt = f"{processor.tokenizer.boi_token} detect \n\n"
|
| 74 |
+
inputs = processor(images=[[image_resized]], text=[prompt], return_tensors="pt", padding=True)
|
| 75 |
|
| 76 |
+
if "pixel_values" not in inputs:
|
| 77 |
+
return image_resized
|
| 78 |
|
| 79 |
+
inputs = {k: v.to(model.device) if torch.is_tensor(v) else v for k, v in inputs.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
with torch.no_grad():
|
| 82 |
+
generation = model.generate(**inputs, max_new_tokens=100, disable_compile=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
decoded = processor.batch_decode(generation, skip_special_tokens=True)[0]
|
| 85 |
+
return visualize_bounding_boxes(image_resized, decoded)
|
| 86 |
+
|
| 87 |
+
css_hide_share = """
|
| 88 |
+
button#gradio-share-link-button-0 {
|
| 89 |
+
display: none !important;
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
# --- Gradio Blocks Interface ---
|
| 94 |
+
with gr.Blocks(theme=Soft(), css=css_hide_share) as demo:
|
| 95 |
+
|
| 96 |
+
gr.Markdown("# Gemma3 Object Detector")
|
| 97 |
+
gr.Markdown("""
|
| 98 |
+
### π About the Models
|
| 99 |
+
This demo compares two fine-tuned versions of **Gemma 3 (4B)** for object detection:
|
| 100 |
+
- **π΅ Fine-tuned for object detection**: trained to predict bounding boxes and class labels using the original tokenizer.
|
| 101 |
+
- **π£ Fine-tuned (updated tokenizer with `<loc>` tokens)**: same task, but uses a tokenizer updated to better encode spatial information through `<locYYYY>` tokens.
|
| 102 |
+
Select a model and click **Generate** to visualize its prediction on a random test image.
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
with gr.Column():
|
| 106 |
+
model_selector = gr.Radio(
|
| 107 |
+
choices=list(MODEL_OPTIONS.keys()),
|
| 108 |
+
value="With loc tokens",
|
| 109 |
+
label="Model"
|
| 110 |
+
)
|
| 111 |
+
generate_btn = gr.Button(value="Generate")
|
| 112 |
+
output_image = gr.Image(type="pil", label="Detected Bounding Box", height=500)
|
| 113 |
+
|
| 114 |
+
generate_btn.click(fn=detect_random_image, inputs=model_selector, outputs=output_image)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
spaces
|
| 3 |
transformers
|
| 4 |
-
accelerate
|
| 5 |
datasets
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
|
|
|
| 2 |
transformers
|
|
|
|
| 3 |
datasets
|
| 4 |
+
Pillow
|
| 5 |
+
albumentations
|
| 6 |
+
gradio
|
| 7 |
+
accelerate
|