Spaces:
Running
Running
preload
Browse files
app.py
CHANGED
@@ -1,4 +1,7 @@
|
|
|
|
|
|
1 |
import io
|
|
|
2 |
from typing import Optional
|
3 |
|
4 |
import gradio as gr
|
@@ -19,15 +22,18 @@ def _fig_to_pil(fig) -> Image.Image:
|
|
19 |
buf.seek(0)
|
20 |
return Image.open(buf).convert("RGB")
|
21 |
|
|
|
|
|
|
|
|
|
22 |
def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid: bool):
|
23 |
-
viz =
|
24 |
|
25 |
captured = []
|
26 |
orig_show = plt.show
|
27 |
|
28 |
def _capture_show(*_, **__):
|
29 |
-
|
30 |
-
captured.append(fig)
|
31 |
|
32 |
try:
|
33 |
plt.show = _capture_show
|
@@ -35,9 +41,10 @@ def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid
|
|
35 |
finally:
|
36 |
plt.show = orig_show
|
37 |
|
38 |
-
|
|
|
39 |
prompt_preview = viz.default_message(full_output=False)
|
40 |
-
return
|
41 |
|
42 |
def _resolve_and_run(model_pick, custom_model, image, use_sample, add_grid):
|
43 |
model_id = (custom_model or "").strip() or (model_pick or "").strip()
|
@@ -46,54 +53,49 @@ def _resolve_and_run(model_pick, custom_model, image, use_sample, add_grid):
|
|
46 |
return _run(model_id, image, use_sample, add_grid)
|
47 |
|
48 |
def _on_image_change(_):
|
49 |
-
return False # uncheck
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
theme = gr.themes.Soft(primary_hue="orange", neutral_hue="gray")
|
52 |
|
53 |
with gr.Blocks(title="Transformers Processor Visualizer", theme=theme) as demo:
|
54 |
-
gr.Markdown("
|
55 |
|
56 |
with gr.Row():
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
label="Models",
|
61 |
-
choices=MODELS,
|
62 |
-
value=MODELS[0],
|
63 |
-
interactive=True,
|
64 |
-
)
|
65 |
-
custom_model = gr.Textbox(
|
66 |
-
label="Or type a model id",
|
67 |
-
placeholder="owner/repo",
|
68 |
-
lines=1,
|
69 |
-
)
|
70 |
-
# RIGHT: controls + outputs
|
71 |
with gr.Column(scale=3):
|
72 |
with gr.Row():
|
73 |
add_grid = gr.Checkbox(label="Show patch grid", value=True)
|
74 |
use_sample = gr.Checkbox(label="Use HF logo sample", value=True)
|
75 |
-
image = gr.Image(
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
gallery = gr.Gallery(label="Processor output", columns=[1], height=900)
|
83 |
prompt = gr.Textbox(label="Compact chat template preview", lines=2)
|
84 |
|
85 |
-
#
|
86 |
-
model_pick.change(_resolve_and_run,
|
87 |
-
custom_model.submit(_resolve_and_run,
|
88 |
-
add_grid.change(_resolve_and_run,
|
89 |
-
use_sample.change(_resolve_and_run,
|
90 |
image.change(_on_image_change, inputs=image, outputs=use_sample).then(
|
91 |
-
_resolve_and_run,
|
92 |
)
|
93 |
|
94 |
-
#
|
95 |
-
demo.load(
|
|
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
demo.launch()
|
99 |
-
|
|
|
1 |
+
# pip install -U gradio transformers pillow matplotlib
|
2 |
+
|
3 |
import io
|
4 |
+
from functools import lru_cache
|
5 |
from typing import Optional
|
6 |
|
7 |
import gradio as gr
|
|
|
22 |
buf.seek(0)
|
23 |
return Image.open(buf).convert("RGB")
|
24 |
|
25 |
+
@lru_cache(maxsize=64)
|
26 |
+
def get_viz(model_id: str) -> ImageVisualizer:
|
27 |
+
return ImageVisualizer(model_id)
|
28 |
+
|
29 |
def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid: bool):
|
30 |
+
viz = get_viz(model_id)
|
31 |
|
32 |
captured = []
|
33 |
orig_show = plt.show
|
34 |
|
35 |
def _capture_show(*_, **__):
|
36 |
+
captured.append(plt.gcf())
|
|
|
37 |
|
38 |
try:
|
39 |
plt.show = _capture_show
|
|
|
41 |
finally:
|
42 |
plt.show = orig_show
|
43 |
|
44 |
+
left_img = _fig_to_pil(captured[0]) if len(captured) >= 1 else None
|
45 |
+
right_img = _fig_to_pil(captured[1]) if len(captured) >= 2 else None
|
46 |
prompt_preview = viz.default_message(full_output=False)
|
47 |
+
return left_img, right_img, prompt_preview
|
48 |
|
49 |
def _resolve_and_run(model_pick, custom_model, image, use_sample, add_grid):
|
50 |
model_id = (custom_model or "").strip() or (model_pick or "").strip()
|
|
|
53 |
return _run(model_id, image, use_sample, add_grid)
|
54 |
|
55 |
def _on_image_change(_):
|
56 |
+
return False # uncheck sample toggle
|
57 |
+
|
58 |
+
def _preload_models():
|
59 |
+
for mid in MODELS:
|
60 |
+
try:
|
61 |
+
get_viz(mid)
|
62 |
+
except Exception:
|
63 |
+
pass
|
64 |
|
65 |
theme = gr.themes.Soft(primary_hue="orange", neutral_hue="gray")
|
66 |
|
67 |
with gr.Blocks(title="Transformers Processor Visualizer", theme=theme) as demo:
|
68 |
+
gr.Markdown("## Visualize what a processor feeds a vision–text model")
|
69 |
|
70 |
with gr.Row():
|
71 |
+
with gr.Column(scale=1, min_width=280):
|
72 |
+
model_pick = gr.Radio(label="Models", choices=MODELS, value=MODELS[0], interactive=True)
|
73 |
+
custom_model = gr.Textbox(label="Or type a model id", placeholder="owner/repo", lines=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
with gr.Column(scale=3):
|
75 |
with gr.Row():
|
76 |
add_grid = gr.Checkbox(label="Show patch grid", value=True)
|
77 |
use_sample = gr.Checkbox(label="Use HF logo sample", value=True)
|
78 |
+
image = gr.Image(label="Upload custom image", type="pil", height=140, sources=["upload"])
|
79 |
+
gr.Markdown("## Output")
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
left_output = gr.Image(label="Processor output", type="pil", height=900)
|
83 |
+
right_output = gr.Image(label="Global image (if any)", type="pil", height=900)
|
84 |
+
|
|
|
85 |
prompt = gr.Textbox(label="Compact chat template preview", lines=2)
|
86 |
|
87 |
+
# reactive updates
|
88 |
+
model_pick.change(_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt])
|
89 |
+
custom_model.submit(_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt])
|
90 |
+
add_grid.change(_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt])
|
91 |
+
use_sample.change(_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt])
|
92 |
image.change(_on_image_change, inputs=image, outputs=use_sample).then(
|
93 |
+
_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt]
|
94 |
)
|
95 |
|
96 |
+
# preload models into cache and render once
|
97 |
+
demo.load(_preload_models, [], [])
|
98 |
+
demo.load(_resolve_and_run, [model_pick, custom_model, image, use_sample, add_grid], [left_output, right_output, prompt])
|
99 |
|
100 |
if __name__ == "__main__":
|
101 |
demo.launch()
|
|