Spaces:
Running
Running
default models
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# pip install -U gradio transformers pillow matplotlib
|
| 2 |
-
|
| 3 |
import io
|
| 4 |
from typing import Optional
|
| 5 |
|
|
@@ -9,6 +7,20 @@ from PIL import Image
|
|
| 9 |
|
| 10 |
from transformers.utils.processor_visualizer_utils import ImageVisualizer
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def _fig_to_pil(fig) -> Image.Image:
|
| 14 |
buf = io.BytesIO()
|
|
@@ -16,16 +28,13 @@ def _fig_to_pil(fig) -> Image.Image:
|
|
| 16 |
buf.seek(0)
|
| 17 |
return Image.open(buf).convert("RGB")
|
| 18 |
|
| 19 |
-
|
| 20 |
def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid: bool):
|
| 21 |
viz = ImageVisualizer(model_id)
|
| 22 |
|
| 23 |
-
# Capture all matplotlib figures the visualizer produces without changing the utility.
|
| 24 |
captured = []
|
| 25 |
orig_show = plt.show
|
| 26 |
|
| 27 |
def _capture_show(*_, **__):
|
| 28 |
-
# collect the current figure then do not actually display
|
| 29 |
fig = plt.gcf()
|
| 30 |
captured.append(fig)
|
| 31 |
|
|
@@ -35,32 +44,40 @@ def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid
|
|
| 35 |
finally:
|
| 36 |
plt.show = orig_show
|
| 37 |
|
| 38 |
-
# Convert figures to PIL for Gradio
|
| 39 |
imgs = [_fig_to_pil(fig) for fig in captured] if captured else []
|
| 40 |
prompt_preview = viz.default_message(full_output=False)
|
| 41 |
return imgs, prompt_preview
|
| 42 |
|
| 43 |
|
| 44 |
with gr.Blocks(title="Transformers Processor Visualizer") as demo:
|
| 45 |
-
gr.Markdown("Switch models and see what the processor
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
model_id = gr.
|
| 49 |
label="Model repo_id",
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
add_grid = gr.Checkbox(label="Show patch grid", value=True)
|
| 54 |
use_sample = gr.Checkbox(label="Use HF logo sample", value=True)
|
| 55 |
|
| 56 |
-
image = gr.Image(label="
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
| 58 |
run_btn = gr.Button("Render")
|
| 59 |
|
| 60 |
gallery = gr.Gallery(label="Processor output")
|
| 61 |
prompt = gr.Textbox(label="Compact chat template preview")
|
| 62 |
-
|
| 63 |
run_btn.click(_run, inputs=[model_id, image, use_sample, add_grid], outputs=[gallery, prompt])
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
from typing import Optional
|
| 3 |
|
|
|
|
| 7 |
|
| 8 |
from transformers.utils.processor_visualizer_utils import ImageVisualizer
|
| 9 |
|
| 10 |
+
MODELS = [
|
| 11 |
+
"openai/clip-vit-base-patch32",
|
| 12 |
+
"HuggingFaceM4/Idefics3-8B-Llama3",
|
| 13 |
+
"llava-hf/llava-1.5-7b-hf",
|
| 14 |
+
"OpenGVLab/InternVL2-2B",
|
| 15 |
+
"OpenGVLab/InternVL3-8B-hf",
|
| 16 |
+
"Salesforce/blip-image-captioning-base",
|
| 17 |
+
"Salesforce/blip2-flan-t5-xl",
|
| 18 |
+
"Qwen/Qwen2-VL-2B-Instruct",
|
| 19 |
+
"Qwen/Qwen2.5-VL-3B-Instruct",
|
| 20 |
+
"meta-llama/Llama-3.2-11B-Vision",
|
| 21 |
+
"microsoft/Florence-2-base",
|
| 22 |
+
"laion/CLIP-ViT-B-32-laion2B-s34B-b79K",
|
| 23 |
+
]
|
| 24 |
|
| 25 |
def _fig_to_pil(fig) -> Image.Image:
|
| 26 |
buf = io.BytesIO()
|
|
|
|
| 28 |
buf.seek(0)
|
| 29 |
return Image.open(buf).convert("RGB")
|
| 30 |
|
|
|
|
| 31 |
def _run(model_id: str, image: Optional[Image.Image], use_sample: bool, add_grid: bool):
|
| 32 |
viz = ImageVisualizer(model_id)
|
| 33 |
|
|
|
|
| 34 |
captured = []
|
| 35 |
orig_show = plt.show
|
| 36 |
|
| 37 |
def _capture_show(*_, **__):
|
|
|
|
| 38 |
fig = plt.gcf()
|
| 39 |
captured.append(fig)
|
| 40 |
|
|
|
|
| 44 |
finally:
|
| 45 |
plt.show = orig_show
|
| 46 |
|
|
|
|
| 47 |
imgs = [_fig_to_pil(fig) for fig in captured] if captured else []
|
| 48 |
prompt_preview = viz.default_message(full_output=False)
|
| 49 |
return imgs, prompt_preview
|
| 50 |
|
| 51 |
|
| 52 |
with gr.Blocks(title="Transformers Processor Visualizer") as demo:
|
| 53 |
+
gr.Markdown("Switch models and see what the processor feeds them (uses the existing `ImageVisualizer`).")
|
| 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 |
+
def _on_image_change(img):
|
| 69 |
+
return False # uncheck the sample toggle when a custom image is set
|
| 70 |
|
| 71 |
+
image.change(_on_image_change, inputs=image, outputs=use_sample)
|
| 72 |
run_btn = gr.Button("Render")
|
| 73 |
|
| 74 |
gallery = gr.Gallery(label="Processor output")
|
| 75 |
prompt = gr.Textbox(label="Compact chat template preview")
|
| 76 |
+
# Render on demand
|
| 77 |
run_btn.click(_run, inputs=[model_id, image, use_sample, add_grid], outputs=[gallery, prompt])
|
| 78 |
|
| 79 |
+
# Also render once on load with defaults so there is an example before clicking
|
| 80 |
+
demo.load(_run, inputs=[model_id, image, use_sample, add_grid], outputs=[gallery, prompt])
|
| 81 |
+
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|