Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ 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")
|
|
@@ -24,7 +24,7 @@ def api_autotag(image, max_tags):
|
|
| 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=
|
| 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", [])
|
|
@@ -39,7 +39,8 @@ with gr.Blocks(title="SAM3 AutoTag") as demo:
|
|
| 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.
|
|
|
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
demo.queue().launch(show_error=True)
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
@spaces.GPU(duration=120)
|
| 19 |
+
def api_autotag(image, max_tags, num_beams=3):
|
| 20 |
if image is None:
|
| 21 |
return {"error": "no image provided"}
|
| 22 |
image = image.convert("RGB")
|
|
|
|
| 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=int(num_beams))
|
| 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", [])
|
|
|
|
| 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 |
+
nb = gr.Slider(1, 8, value=3, step=1, label="Beams (higher = more precise)")
|
| 43 |
+
gr.Button("Tag").click(api_autotag, [inp, mt, nb], out, api_name="api_autotag")
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
demo.queue().launch(show_error=True)
|