Sempy32 commited on
Commit
6f23c17
·
verified ·
1 Parent(s): 4fadb43

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +5 -5
  2. app.py +45 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
  title: SAM3 AutoTag
3
- emoji: 📉
4
- colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.15.2
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: SAM3 AutoTag
3
+ emoji: 🏷️
4
+ colorFrom: green
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 6.6.0
 
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Florence-2 tagging for SAM 3 auto-discovery
11
  ---
12
 
13
+ `api_autotag(image, max_tags)` -> JSON {labels: [...], tags: [{name, count}]} via Florence-2 <OD>.
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Florence-2 object tagger (ZeroGPU): proposes class names from an image."""
2
+ import os
3
+ from collections import Counter
4
+
5
+ import gradio as gr
6
+ import spaces
7
+ import torch
8
+ from transformers import AutoProcessor, Florence2ForConditionalGeneration
9
+
10
+ MODEL_ID = "florence-community/Florence-2-large"
11
+
12
+ # Built at import on CPU; moved to CUDA inside the @spaces.GPU function.
13
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
14
+ model = Florence2ForConditionalGeneration.from_pretrained(MODEL_ID)
15
+ model.eval()
16
+
17
+
18
+ @spaces.GPU(duration=120)
19
+ def api_autotag(image, max_tags):
20
+ if image is None:
21
+ return {"error": "no image provided"}
22
+ image = image.convert("RGB")
23
+ device = "cuda"
24
+ model.to(device)
25
+ inputs = processor(text="<OD>", images=image, return_tensors="pt").to(device)
26
+ with torch.no_grad():
27
+ gen = model.generate(**inputs, max_new_tokens=1024, num_beams=3)
28
+ text = processor.batch_decode(gen, skip_special_tokens=False)[0]
29
+ parsed = processor.post_process_generation(text, task="<OD>", image_size=image.size)
30
+ labels = parsed.get("<OD>", {}).get("labels", [])
31
+ counts = Counter(str(l).strip().lower() for l in labels if str(l).strip())
32
+ tags = [{"name": n, "count": c} for n, c in counts.most_common(int(max_tags))]
33
+ return {"model": MODEL_ID, "tags": tags, "labels": [t["name"] for t in tags]}
34
+
35
+
36
+ with gr.Blocks(title="SAM3 AutoTag") as demo:
37
+ gr.Markdown("# Florence-2 AutoTag\nUpload an image; returns detected object class names.")
38
+ with gr.Row():
39
+ inp = gr.Image(type="pil", label="Image")
40
+ out = gr.JSON(label="Tags")
41
+ mt = gr.Slider(1, 50, value=20, step=1, label="Max tags")
42
+ gr.Button("Tag").click(api_autotag, [inp, mt], out, api_name="api_autotag")
43
+
44
+ if __name__ == "__main__":
45
+ demo.queue().launch(show_error=True)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ transformers==5.9.0
2
+ torch==2.11.0
3
+ torchvision
4
+ gradio==6.6.0
5
+ spaces
6
+ timm
7
+ einops
8
+ pillow
9
+ numpy