File size: 12,541 Bytes
416d6fd
688a87d
e05052e
 
d430aa2
e05052e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e53af6
bdb8c95
0e53af6
df5f888
 
 
0e53af6
3e074ca
0e53af6
 
 
97f30ad
0e53af6
abc934b
 
3e074ca
abc934b
688a87d
0e53af6
 
 
 
df5f888
 
 
 
 
5912d41
 
df5f888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5912d41
df5f888
 
5912d41
 
 
 
 
 
df5f888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
def5662
92bb3e8
0e53af6
92bb3e8
 
 
 
 
 
51b253e
0e53af6
92bb3e8
e05052e
abc934b
 
 
 
13b9696
9a9a80e
abc934b
 
2c6d54d
 
 
 
 
 
 
2ea650c
abc934b
 
9a9a80e
 
e05052e
 
 
 
9a9a80e
 
 
dcacf66
0e53af6
7219c6d
9a9a80e
 
 
 
 
 
 
0e53af6
df5f888
 
 
0e53af6
abc934b
d382dff
 
 
 
 
 
 
 
 
 
 
0dfd343
d382dff
 
 
 
 
 
 
 
 
 
 
 
 
c6c8487
89703a3
3c806f9
 
 
 
ed4ba53
06436a8
89703a3
 
 
 
 
4e84543
89703a3
4e84543
89703a3
 
06436a8
c6c8487
 
77b0c52
9a9a80e
 
 
 
 
 
038e09d
e05052e
 
df5f888
 
 
 
 
 
9a9a80e
 
 
038e09d
31151c4
0ff1ff1
038e09d
c57ffa7
 
d55d757
c57ffa7
 
9a9a80e
f957cd9
9a9a80e
 
 
df5f888
 
 
c57ffa7
 
abc934b
 
 
 
 
 
 
 
 
 
 
 
57972e0
abc934b
92bb3e8
 
 
 
df5f888
abc934b
 
fdd69a9
 
c4a68e7
8373470
fdd69a9
9a9a80e
fdd69a9
 
abc934b
fdd69a9
abc934b
e05052e
9a9a80e
 
 
 
 
 
 
 
 
e05052e
 
 
d382dff
 
 
 
 
 
 
 
 
 
 
 
 
c6c8487
 
 
 
 
b3c367a
c6c8487
 
 
95c40f8
 
 
89703a3
 
 
 
 
e05052e
 
abc934b
 
df5f888
 
 
 
 
c2ea813
e05052e
 
df5f888
 
 
 
 
 
 
 
 
57972e0
df5f888
 
 
5912d41
57972e0
 
 
5912d41
 
 
 
 
 
df5f888
 
 
 
 
 
e05052e
 
 
 
 
0e53af6
01fb1e2
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
REVISION = "eac9f97a23e8003134437c7ce08f10749f18988e"

try:
    import spaces
    
    IN_SPACES = True
except ImportError:
    from functools import wraps
    import inspect

    class spaces:
        @staticmethod
        def GPU(duration):
            def decorator(func):
                @wraps(func)  # Preserves the original function's metadata
                def wrapper(*args, **kwargs):
                    if inspect.isgeneratorfunction(func):
                        # If the decorated function is a generator, yield from it
                        yield from func(*args, **kwargs)
                    else:
                        # For regular functions, just return the result
                        return func(*args, **kwargs)

                return wrapper

            return decorator

    IN_SPACES = False

import torch
import os
import gradio as gr
import json

from queue import Queue
from threading import Thread
from transformers import AutoModelForCausalLM
from PIL import ImageDraw
from torchvision.transforms.v2 import Resize

os.environ["HF_TOKEN"] = os.environ.get("TOKEN_FROM_SECRET") or True
moondream = AutoModelForCausalLM.from_pretrained(
    "vikhyatk/moondream-next",
    trust_remote_code=True,
    torch_dtype=torch.bfloat16,
    device_map={"": "cuda"},
    revision=REVISION
)
moondream.eval()


def convert_to_entities(text, coords):
    """
    Converts a string with special markers into an entity representation.
    Markers:
    - <|coord|> pairs indicate coordinate markers
    - <|start_ground_points|> indicates the start of grounding
    - <|start_ground_text|> indicates the start of a ground term
    - <|end_ground|> indicates the end of a ground term

    Returns:
    - Dictionary with cleaned text and entities with their character positions
    """
    # Initialize variables
    cleaned_text = ""
    entities = []
    entity = []

    # Track current position in cleaned text
    current_pos = 0
    # Track if we're currently processing an entity
    in_entity = False
    entity_start = 0

    i = 0
    while i < len(text):
        # Check for markers
        if text[i : i + 9] == "<|coord|>":
            i += 9
            entity.append(coords.pop(0))
            continue

        elif text[i : i + 23] == "<|start_ground_points|>":
            in_entity = True
            entity_start = current_pos
            i += 23
            continue
        
        elif text[i : i + 21] == "<|start_ground_text|>":
            entity_start = current_pos
            i += 21
            continue

        elif text[i : i + 14] == "<|end_ground|>":
            # Store entity position
            entities.append(
                {
                    "entity": json.dumps(entity),
                    "start": entity_start,
                    "end": current_pos,
                }
            )
            entity = []
            in_entity = False
            i += 14
            continue

        # Add character to cleaned text
        cleaned_text += text[i]
        current_pos += 1
        i += 1

    return {"text": cleaned_text, "entities": entities}


@spaces.GPU(duration=30)
def answer_question(img, prompt, reasoning):
    buffer = ""
    resp = moondream.query(img, prompt, stream=True, reasoning=reasoning)
    reasoning_text = resp["reasoning"]["text"] if reasoning else "[reasoning disabled]"
    entities = [
        {"start": g["start_idx"], "end": g["end_idx"], "entity": json.dumps(g["points"])}
        for g in resp["reasoning"]["grounding"]
    ] if reasoning else []
    for new_text in resp["answer"]:
        buffer += new_text
        yield buffer.strip(), {"text": reasoning_text, "entities": entities}


@spaces.GPU(duration=10)
def caption(img, mode):
    if img is None:
        yield ""
        return

    buffer = ""
    if mode == "Short":
        l = "short"
    elif mode == "Long":
        l = "long"
    else:
        l = "normal"
    for t in moondream.caption(img, length=l, stream=True)["caption"]:
        buffer += t
        yield buffer.strip()

@spaces.GPU(duration=10)
def detect(img, object):
    if img is None:
        yield "", gr.update(visible=False, value=None)
        return

    w, h = img.size
    if w > 768 or h > 768:
        img = Resize(768)(img)
        w, h = img.size

    objs = moondream.detect(img, object)["objects"]
    draw_image = ImageDraw.Draw(img)
    for o in objs:
        draw_image.rectangle(
            (o["x_min"] * w, o["y_min"] * h, o["x_max"] * w, o["y_max"] * h),
            outline="red",
            width=3,
        )

    yield {"text": f"{len(objs)} detected", "entities": []}, gr.update(
        visible=True, value=img
    )


@spaces.GPU(duration=10)
def point(img, object):
    if img is None:
        yield "", gr.update(visible=False, value=None)
        return

    w, h = img.size
    if w > 768 or h > 768:
        img = Resize(768)(img)
        w, h = img.size

    objs = moondream.point(img, object, settings={"max_objects": 200})["points"]
    draw_image = ImageDraw.Draw(img)
    for o in objs:
        draw_image.ellipse(
            (o["x"] * w - 5, o["y"] * h - 5, o["x"] * w + 5, o["y"] * h + 5),
            fill="red",
            outline="blue",
            width=2,
        )

    yield {"text": f"{len(objs)} detected", "entities": []}, gr.update(
        visible=True, value=img
    )

@spaces.GPU(duration=10)
def localized_query(img, x, y, question):
    if img is None:
        yield "", {"text": "", "entities": []}, gr.update(visible=False, value=None)
        return

    answer = moondream.query(img, question, spatial_refs=[(x, y)])["answer"]
    
    w, h = img.size
    x, y = x * w, y * h
    img_clone = img.copy()
    draw = ImageDraw.Draw(img_clone)
    draw.ellipse(
        (x - 5, y - 5, x + 5, y + 5),
        fill="red",
        outline="blue",
    )
    
    yield answer, {"text": "", "entities": []}, gr.update(visible=True, value=img_clone)


js = ""

css = """
    .output-text span p {
        font-size: 1.4rem !important;
    }

    .chain-of-thought {
        opacity: 0.7 !important;
    }
    .chain-of-thought span.label {
        display: none;
    }
    .chain-of-thought span.textspan {
        padding-right: 0;
    }
"""

with gr.Blocks(title="moondream vl (new)", css=css, js=js) as demo:
    if IN_SPACES:
        # gr.HTML("<style>body, body gradio-app { background: none !important; }</style>")
        pass

    gr.Markdown(
        """
        # 🌔 test space, pls ignore
        """
    )
    mode_radio = gr.Radio(
        ["Caption", "Query", "Detect", "Point", "Localized"],
        show_label=False,
        value=lambda: "Caption",
    )

    input_image = gr.State(None)

    with gr.Row():
        with gr.Column():

            @gr.render(inputs=[mode_radio])
            def show_inputs(mode):
                if mode == "Query":
                    with gr.Group():
                        with gr.Row():
                            prompt = gr.Textbox(
                                label="Input",
                                value="How many people are in this image?",
                                scale=4,
                            )
                            submit = gr.Button("Submit")
                        reasoning = gr.Checkbox(label="Enable reasoning")
                        img = gr.Image(type="pil", label="Upload an Image")
                    submit.click(answer_question, [img, prompt, reasoning], [output, thought])
                    prompt.submit(answer_question, [img, prompt, reasoning], [output, thought])
                    reasoning.change(answer_question, [img, prompt, reasoning], [output, thought])
                    img.change(answer_question, [img, prompt, reasoning], [output, thought])
                    img.change(lambda img: img, [img], [input_image])
                elif mode == "Caption":
                    with gr.Group():
                        with gr.Row():
                            caption_mode = gr.Radio(
                                ["Short", "Normal", "Long"],
                                label="Caption Length",
                                value=lambda: "Normal",
                                scale=4,
                            )
                            submit = gr.Button("Submit")
                        img = gr.Image(type="pil", label="Upload an Image")
                    submit.click(caption, [img, caption_mode], output)
                    img.change(caption, [img, caption_mode], output)
                elif mode == "Detect":
                    with gr.Group():
                        with gr.Row():
                            prompt = gr.Textbox(
                                label="Object",
                                value="Cat",
                                scale=4,
                            )
                            submit = gr.Button("Submit")
                        img = gr.Image(type="pil", label="Upload an Image")
                    submit.click(detect, [img, prompt], [thought, ann])
                    prompt.submit(detect, [img, prompt], [thought, ann])
                    img.change(detect, [img, prompt], [thought, ann])
                elif mode == "Point":
                    with gr.Group():
                        with gr.Row():
                            prompt = gr.Textbox(
                                label="Object",
                                value="Cat",
                                scale=4,
                            )
                            submit = gr.Button("Submit")
                        img = gr.Image(type="pil", label="Upload an Image")
                    submit.click(point, [img, prompt], [thought, ann])
                    prompt.submit(point, [img, prompt], [thought, ann])
                    img.change(point, [img, prompt], [thought, ann])
                elif mode == "Localized":
                    with gr.Group():
                        with gr.Row():
                            prompt = gr.Textbox(
                                label="Input",
                                value="What is this?",
                                scale=4,
                            )
                            submit = gr.Button("Submit")
                        img = gr.Image(type="pil", label="Upload an Image")
                        x_slider = gr.Slider(label="x", minimum=0, maximum=1)
                        y_slider = gr.Slider(label="y", minimum=0, maximum=1)
                    submit.click(localized_query, [img, x_slider, y_slider, prompt], [output, thought, ann])
                    prompt.submit(localized_query, [img, x_slider, y_slider, prompt], [output, thought, ann])
                    x_slider.change(localized_query, [img, x_slider, y_slider, prompt], [output, thought, ann])
                    y_slider.change(localized_query, [img, x_slider, y_slider, prompt], [output, thought, ann])
                    img.change(localized_query, [img, x_slider, y_slider, prompt], [output, thought, ann])
                else:
                    gr.Markdown("Coming soon!")

        with gr.Column():
            thought = gr.HighlightedText(
                elem_classes=["chain-of-thought"],
                label="Thinking tokens",
                interactive=False,
            )
            output = gr.Markdown(label="Response", elem_classes=["output-text"], line_breaks=True)
            ann = gr.Image(visible=False)

        def on_select(img, evt: gr.SelectData):
            if img is None or evt.value[1] is None:
                return gr.update(visible=False, value=None)

            w, h = img.size
            if w > 768 or h > 768:
                img = Resize(768)(img)
                w, h = img.size

            points = json.loads(evt.value[1])

            img_clone = img.copy()
            draw = ImageDraw.Draw(img_clone)
            
            for point in points:
                x = int(point[0] * w)
                y = int(point[1] * h)
                draw.ellipse(
                    (x - 3, y - 3, x + 3, y + 3),
                    fill="red",
                    outline="red",
                )


            return gr.update(visible=True, value=img_clone)

        thought.select(on_select, [input_image], [ann])
        input_image.change(lambda: gr.update(visible=False), [], [ann])

    mode_radio.change(
        lambda: ("", "", gr.update(visible=False, value=None)),
        [],
        [output, thought, ann],
    )

demo.queue().launch()