Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,45 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
demo.launch()
|
|
|
1 |
+
# Credits to IDEA Research for the model:
|
2 |
+
# https://huggingface.co/IDEA-Research/grounding-dino-tiny
|
3 |
+
|
4 |
+
from base64 import b64decode
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import spaces
|
9 |
+
from PIL import Image
|
10 |
+
import torch
|
11 |
+
from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection
|
12 |
+
|
13 |
+
model_id = "IDEA-Research/grounding-dino-tiny"
|
14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
+
|
16 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
17 |
+
model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(device)
|
18 |
+
|
19 |
+
def predict(base64: str, queries: str, box_threshold: float, text_threshold: float):
|
20 |
+
decoded_img = b64decode(base64)
|
21 |
+
image_stream = BytesIO(decoded_img)
|
22 |
+
image = Image.open(image_stream)
|
23 |
+
|
24 |
+
inputs = processor(images=image, text=queries, return_tensors="pt").to(device)
|
25 |
+
with torch.no_grad():
|
26 |
+
outputs = model(**inputs)
|
27 |
+
|
28 |
+
results = processor.post_process_grounded_object_detection(
|
29 |
+
outputs,
|
30 |
+
inputs.input_ids,
|
31 |
+
box_threshold=box_threshold,
|
32 |
+
text_threshold=text_threshold,
|
33 |
+
target_sizes=[image.size[::-1]]
|
34 |
+
)
|
35 |
+
return results
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=predict,
|
39 |
+
inputs=[
|
40 |
+
gr.Text(label="Image (B64)"),
|
41 |
+
gr.Text(label="Queries", placeholder="A photo of a dog,A photo of a cat")
|
42 |
+
],
|
43 |
+
outputs=gr.JSON(label="Predictions"),
|
44 |
+
)
|
45 |
demo.launch()
|