Spaces:
Runtime error
Runtime error
Commit
Β·
46918cf
1
Parent(s):
245ed94
Prevent compile
Browse files
app.py
CHANGED
|
@@ -1,11 +1,16 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
from datasets import load_dataset
|
| 5 |
-
from
|
| 6 |
-
import albumentations as A
|
| 7 |
-
import gradio as gr
|
| 8 |
from spaces import GPU
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Config ---
|
| 11 |
dataset_id = "ariG23498/license-detection-paligemma"
|
|
@@ -27,7 +32,7 @@ def load_model(checkpoint_id):
|
|
| 27 |
processor = AutoProcessor.from_pretrained(checkpoint_id)
|
| 28 |
model = Gemma3ForConditionalGeneration.from_pretrained(
|
| 29 |
checkpoint_id,
|
| 30 |
-
torch_dtype=
|
| 31 |
device_map="auto",
|
| 32 |
)
|
| 33 |
model.eval()
|
|
@@ -74,7 +79,7 @@ def detect_random_image(model_choice):
|
|
| 74 |
inputs = {k: v.to(model.device) if torch.is_tensor(v) else v for k, v in inputs.items()}
|
| 75 |
|
| 76 |
with torch.no_grad():
|
| 77 |
-
generation = model.generate(**inputs, max_new_tokens=100)
|
| 78 |
|
| 79 |
decoded = processor.batch_decode(generation, skip_special_tokens=True)[0]
|
| 80 |
return visualize_bounding_boxes(image_resized, decoded)
|
|
@@ -86,15 +91,14 @@ button#gradio-share-link-button-0 {
|
|
| 86 |
"""
|
| 87 |
|
| 88 |
# --- Gradio Blocks Interface ---
|
| 89 |
-
with gr.Blocks(css=css_hide_share) as demo:
|
|
|
|
| 90 |
gr.Markdown("# Gemma3 Object Detector")
|
| 91 |
gr.Markdown("""
|
| 92 |
### π About the Models
|
| 93 |
This demo compares two fine-tuned versions of **Gemma 3 (4B)** for object detection:
|
| 94 |
-
|
| 95 |
- **π΅ Fine-tuned for object detection**: trained to predict bounding boxes and class labels using the original tokenizer.
|
| 96 |
- **π£ Fine-tuned (updated tokenizer with `<loc>` tokens)**: same task, but uses a tokenizer updated to better encode spatial information through `<locYYYY>` tokens.
|
| 97 |
-
|
| 98 |
Select a model and click **Generate** to visualize its prediction on a random test image.
|
| 99 |
""")
|
| 100 |
|
|
@@ -110,4 +114,4 @@ Select a model and click **Generate** to visualize its prediction on a random te
|
|
| 110 |
generate_btn.click(fn=detect_random_image, inputs=model_selector, outputs=output_image)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
-
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"
|
|
|
|
| 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()
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
|
|
|
| 114 |
generate_btn.click(fn=detect_random_image, inputs=model_selector, outputs=output_image)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
demo.launch()
|