File size: 2,115 Bytes
f2df7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2e833
 
f2df7d1
 
 
 
 
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
# pip install -U gradio transformers pillow matplotlib

import io
from typing import Optional

import gradio as gr
import matplotlib.pyplot as plt
from PIL import Image

from transformers.utils.processor_visualizer_utils import ImageVisualizer


def _fig_to_pil(fig) -> Image.Image:
    buf = io.BytesIO()
    fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)
    buf.seek(0)
    return Image.open(buf).convert("RGB")


def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid: bool):
    viz = ImageVisualizer(model_id)

    # Capture all matplotlib figures the visualizer produces without changing the utility.
    captured = []
    orig_show = plt.show

    def _capture_show(*_, **__):
        # collect the current figure then do not actually display
        fig = plt.gcf()
        captured.append(fig)

    try:
        plt.show = _capture_show
        viz.visualize(images=None if use_sample else image, add_grid=add_grid)
    finally:
        plt.show = orig_show

    # Convert figures to PIL for Gradio
    imgs = [_fig_to_pil(fig) for fig in captured] if captured else []
    prompt_preview = viz.default_message(full_output=False)
    return imgs, prompt_preview


with gr.Blocks(title="Transformers Processor Visualizer") as demo:
    gr.Markdown("Switch models and see what the processor actually feeds them (uses the existing `ImageVisualizer`).")

    with gr.Row():
        model_id = gr.Textbox(
            label="Model repo_id",
            value="openai/clip-vit-base-patch32",
            placeholder="owner/repo (e.g., llava-hf/llava-1.5-7b-hf)",
        )
        add_grid = gr.Checkbox(label="Show patch grid", value=True)
        use_sample = gr.Checkbox(label="Use HF logo sample", value=True)

    image = gr.Image(label="Or upload an image", type="pil")

    run_btn = gr.Button("Render")

    gallery = gr.Gallery(label="Processor output")
    prompt = gr.Textbox(label="Compact chat template preview")

    run_btn.click(_run, inputs=[model_id, image, use_sample, add_grid], outputs=[gallery, prompt])

if __name__ == "__main__":
    demo.launch()