Molbap HF Staff commited on
Commit
fc889e7
·
1 Parent(s): f6bb196
Files changed (1) hide show
  1. app.py +40 -38
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 = ImageVisualizer(model_id)
24
 
25
  captured = []
26
  orig_show = plt.show
27
 
28
  def _capture_show(*_, **__):
29
- fig = plt.gcf()
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
- imgs = [_fig_to_pil(fig) for fig in captured] if captured else []
 
39
  prompt_preview = viz.default_message(full_output=False)
40
- return imgs, prompt_preview
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 "use sample" when a custom image is set
 
 
 
 
 
 
 
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("### Visualize what a processor feeds a vision–text model (uses the existing `ImageVisualizer`).")
55
 
56
  with gr.Row():
57
- # LEFT: clickable models + custom field
58
- with gr.Column(scale=1, min_width=260):
59
- model_pick = gr.Radio(
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
- label="Upload custom image",
77
- type="pil",
78
- height=140,
79
- sources=["upload"],
80
- )
81
- gr.Markdown("### Render")
82
- gallery = gr.Gallery(label="Processor output", columns=[1], height=900)
83
  prompt = gr.Textbox(label="Compact chat template preview", lines=2)
84
 
85
- # Reactive updates: change model, toggle options, upload -> update immediately
86
- model_pick.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
87
- custom_model.submit(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
88
- add_grid.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
89
- use_sample.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
90
  image.change(_on_image_change, inputs=image, outputs=use_sample).then(
91
- _resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt]
92
  )
93
 
94
- # Initial render so there is output before any interaction
95
- demo.load(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
 
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()