Molbap HF Staff commited on
Commit
bc0be6a
·
1 Parent(s): fc889e7

remove useless tick

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # pip install -U gradio transformers pillow matplotlib
2
 
3
  import io
4
  from functools import lru_cache
@@ -15,7 +14,6 @@ MODELS = [
15
  "HuggingFaceM4/Idefics3-8B-Llama3",
16
  ]
17
 
18
-
19
  def _fig_to_pil(fig) -> Image.Image:
20
  buf = io.BytesIO()
21
  fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)
@@ -26,7 +24,7 @@ def _fig_to_pil(fig) -> Image.Image:
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 = []
@@ -37,7 +35,8 @@ def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid
37
 
38
  try:
39
  plt.show = _capture_show
40
- viz.visualize(images=None if use_sample else image, add_grid=add_grid)
 
41
  finally:
42
  plt.show = orig_show
43
 
@@ -46,14 +45,11 @@ def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid
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()
51
  if not model_id:
52
  raise gr.Error("Pick a model or enter one.")
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:
@@ -74,28 +70,22 @@ with gr.Blocks(title="Transformers Processor Visualizer", theme=theme) as demo:
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()
 
 
1
 
2
  import io
3
  from functools import lru_cache
 
14
  "HuggingFaceM4/Idefics3-8B-Llama3",
15
  ]
16
 
 
17
  def _fig_to_pil(fig) -> Image.Image:
18
  buf = io.BytesIO()
19
  fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)
 
24
  def get_viz(model_id: str) -> ImageVisualizer:
25
  return ImageVisualizer(model_id)
26
 
27
+ def _run(model_id: str, image: Optional[Image.Image], add_grid: bool):
28
  viz = get_viz(model_id)
29
 
30
  captured = []
 
35
 
36
  try:
37
  plt.show = _capture_show
38
+ # if image is None, the visualizer will use its default sample
39
+ viz.visualize(images=image, add_grid=add_grid)
40
  finally:
41
  plt.show = orig_show
42
 
 
45
  prompt_preview = viz.default_message(full_output=False)
46
  return left_img, right_img, prompt_preview
47
 
48
+ def _resolve_and_run(model_pick, custom_model, image, add_grid):
49
  model_id = (custom_model or "").strip() or (model_pick or "").strip()
50
  if not model_id:
51
  raise gr.Error("Pick a model or enter one.")
52
+ return _run(model_id, image, add_grid)
 
 
 
53
 
54
  def _preload_models():
55
  for mid in MODELS:
 
70
  with gr.Column(scale=3):
71
  with gr.Row():
72
  add_grid = gr.Checkbox(label="Show patch grid", value=True)
 
73
  image = gr.Image(label="Upload custom image", type="pil", height=140, sources=["upload"])
74
  gr.Markdown("## Output")
 
75
  with gr.Row():
76
  left_output = gr.Image(label="Processor output", type="pil", height=900)
77
  right_output = gr.Image(label="Global image (if any)", type="pil", height=900)
 
78
  prompt = gr.Textbox(label="Compact chat template preview", lines=2)
79
 
80
  # reactive updates
81
+ model_pick.change(_resolve_and_run, [model_pick, custom_model, image, add_grid], [left_output, right_output, prompt])
82
+ custom_model.submit(_resolve_and_run, [model_pick, custom_model, image, add_grid], [left_output, right_output, prompt])
83
+ add_grid.change(_resolve_and_run, [model_pick, custom_model, image, add_grid], [left_output, right_output, prompt])
84
+ image.change(_resolve_and_run, [model_pick, custom_model, image, add_grid], [left_output, right_output, prompt])
 
 
 
85
 
86
  # preload models into cache and render once
87
  demo.load(_preload_models, [], [])
88
+ demo.load(_resolve_and_run, [model_pick, custom_model, image, add_grid], [left_output, right_output, prompt])
89
 
90
  if __name__ == "__main__":
91
+ demo.launch()