Spaces:
Running
Running
improve interface
Browse files
app.py
CHANGED
@@ -22,6 +22,7 @@ MODELS = [
|
|
22 |
"laion/CLIP-ViT-B-32-laion2B-s34B-b79K",
|
23 |
]
|
24 |
|
|
|
25 |
def _fig_to_pil(fig) -> Image.Image:
|
26 |
buf = io.BytesIO()
|
27 |
fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)
|
@@ -48,36 +49,61 @@ def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid
|
|
48 |
prompt_preview = viz.default_message(full_output=False)
|
49 |
return imgs, prompt_preview
|
50 |
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
with gr.Row():
|
56 |
-
model_id = gr.Dropdown(
|
57 |
-
label="Model repo_id",
|
58 |
-
choices=MODELS,
|
59 |
-
value=MODELS[0],
|
60 |
-
allow_custom_value=True,
|
61 |
-
filterable=True,
|
62 |
-
)
|
63 |
-
add_grid = gr.Checkbox(label="Show patch grid", value=True)
|
64 |
-
use_sample = gr.Checkbox(label="Use HF logo sample", value=True)
|
65 |
-
|
66 |
-
image = gr.Image(label="Upload custom image", type="pil", height=140, width=140, sources=["upload"])
|
67 |
|
68 |
-
|
69 |
-
return False # uncheck the sample toggle when a custom image is set
|
70 |
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
if __name__ == "__main__":
|
83 |
-
demo.launch()
|
|
|
|
22 |
"laion/CLIP-ViT-B-32-laion2B-s34B-b79K",
|
23 |
]
|
24 |
|
25 |
+
|
26 |
def _fig_to_pil(fig) -> Image.Image:
|
27 |
buf = io.BytesIO()
|
28 |
fig.savefig(buf, format="png", bbox_inches="tight", dpi=160)
|
|
|
49 |
prompt_preview = viz.default_message(full_output=False)
|
50 |
return imgs, prompt_preview
|
51 |
|
52 |
+
def _resolve_and_run(model_pick, custom_model, image, use_sample, add_grid):
|
53 |
+
model_id = (custom_model or "").strip() or (model_pick or "").strip()
|
54 |
+
if not model_id:
|
55 |
+
raise gr.Error("Pick a model or enter one.")
|
56 |
+
return _run(model_id, image, use_sample, add_grid)
|
57 |
|
58 |
+
def _on_image_change(_):
|
59 |
+
return False # uncheck "use sample" when a custom image is set
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
theme = gr.themes.Soft(primary_hue="orange", neutral_hue="gray")
|
|
|
62 |
|
63 |
+
with gr.Blocks(title="Transformers Processor Visualizer", theme=theme) as demo:
|
64 |
+
gr.Markdown("### Visualize what a processor feeds a vision–text model (uses the existing `ImageVisualizer`).")
|
65 |
|
66 |
+
with gr.Row():
|
67 |
+
# LEFT: clickable models + custom field
|
68 |
+
with gr.Column(scale=1, min_width=260):
|
69 |
+
model_pick = gr.Radio(
|
70 |
+
label="Models",
|
71 |
+
choices=MODELS,
|
72 |
+
value=MODELS[0],
|
73 |
+
interactive=True,
|
74 |
+
)
|
75 |
+
custom_model = gr.Textbox(
|
76 |
+
label="Or type a model id",
|
77 |
+
placeholder="owner/repo",
|
78 |
+
lines=1,
|
79 |
+
)
|
80 |
+
# RIGHT: controls + outputs
|
81 |
+
with gr.Column(scale=3):
|
82 |
+
with gr.Row():
|
83 |
+
add_grid = gr.Checkbox(label="Show patch grid", value=True)
|
84 |
+
use_sample = gr.Checkbox(label="Use HF logo sample", value=True)
|
85 |
+
image = gr.Image(
|
86 |
+
label="Upload custom image",
|
87 |
+
type="pil",
|
88 |
+
height=140,
|
89 |
+
sources=["upload"],
|
90 |
+
)
|
91 |
+
gr.Markdown("### Render")
|
92 |
+
gallery = gr.Gallery(label="Processor output", columns=[1], height=900)
|
93 |
+
prompt = gr.Textbox(label="Compact chat template preview", lines=2)
|
94 |
+
|
95 |
+
# Reactive updates: change model, toggle options, upload -> update immediately
|
96 |
+
model_pick.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
|
97 |
+
custom_model.submit(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
|
98 |
+
add_grid.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
|
99 |
+
use_sample.change(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
|
100 |
+
image.change(_on_image_change, inputs=image, outputs=use_sample).then(
|
101 |
+
_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt]
|
102 |
+
)
|
103 |
+
|
104 |
+
# Initial render so there is output before any interaction
|
105 |
+
demo.load(_resolve_and_run, inputs=[model_pick, custom_model, image, use_sample, add_grid], outputs=[gallery, prompt])
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
+
demo.launch()
|
109 |
+
|