File size: 1,348 Bytes
e83d360
 
 
 
a083e2f
e83d360
 
 
 
 
 
 
 
 
 
 
 
 
a083e2f
 
 
0119d95
 
 
 
 
 
 
 
 
 
e83d360
a083e2f
 
 
 
e83d360
 
 
a083e2f
a2b5338
 
 
a083e2f
a2b5338
dc42813
e83d360
0119d95
 
 
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import spaces, torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
from typing import Literal


@spaces.GPU
def load_model():
    return AutoModelForCausalLM.from_pretrained(
        "vikhyatk/moondream2",
        revision="2025-04-14",
        trust_remote_code=True,
        device_map={"": "cuda"},
    )


@spaces.GPU
def detect(
    im: Image.Image, object_name: str, mode: Literal["point", "object_detection"]
):
    """
    Open Vocabulary Detection using moondream2

    Args:
        im: Pillow Image
        object_name: the object you would like to detect
        mode: point or object_detection
    Returns:
        list: a list of bounding boxes (xyxy) or points (xy) coordinates that are normalized
    """
    model = load_model()
    if mode == "point":
        return model.point(im, object_name)["points"]
    elif mode == "object_detection":
        return model.detect(im, object_name)["objects"]


demo = gr.Interface(
    fn=detect,
    inputs=[
        gr.Image(label="Input Image", type="pil"),
        gr.Textbox(label="Object to Detect"),
        gr.Dropdown(label="Mode", choices=["point", "object_detection"]),
    ],
    outputs=gr.JSON(label="Output JSON"),
)
demo.launch(
    mcp_server=True, app_kwargs={"docs_url": "/docs"}  # add FastAPI Swagger API Docs
)