Spaces:
Sleeping
Sleeping
Tenbatsu24 commited on
Commit Β·
7eb7023
1
Parent(s): d647dfd
change: footer location and coming soon message for missing model
Browse files
app.py
CHANGED
|
@@ -5,7 +5,8 @@ import numpy as np
|
|
| 5 |
import gradio as gr
|
| 6 |
import torchvision.transforms.functional as TF
|
| 7 |
|
| 8 |
-
from PIL import Image
|
|
|
|
| 9 |
from transformers import AutoModel
|
| 10 |
from sklearn.decomposition import PCA
|
| 11 |
|
|
@@ -53,7 +54,49 @@ def get_model(repo_id: str, revision: str) -> torch.nn.Module:
|
|
| 53 |
|
| 54 |
|
| 55 |
# ββ image helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
|
| 58 |
def resize_image_for_patches(
|
| 59 |
image: Image.Image,
|
|
@@ -151,8 +194,7 @@ def run(pil_image: Image.Image, epoch: str, weight_type: str, image_size: int):
|
|
| 151 |
results[idx] = pca_vis(model, image_tensor, image_size)
|
| 152 |
except Exception as e:
|
| 153 |
print(f"Error processing {repo_id} ({revision}): {e}")
|
| 154 |
-
|
| 155 |
-
results[idx] = error_canvas
|
| 156 |
|
| 157 |
yield tuple(results)
|
| 158 |
idx += 1
|
|
@@ -270,6 +312,12 @@ with gr.Blocks(css=CSS, title="SSL ViT PCA Visualiser") as demo:
|
|
| 270 |
3 components, then scaled with sigmoid for colour display.
|
| 271 |
Results stream seamlessly into view as individual variants complete.
|
| 272 |
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
""")
|
| 274 |
|
| 275 |
with gr.Column(scale=3):
|
|
@@ -305,15 +353,6 @@ with gr.Blocks(css=CSS, title="SSL ViT PCA Visualiser") as demo:
|
|
| 305 |
f'<div class="model-label"><a href="https://huggingface.co/{MODEL_IDS["ViT-B/16"]["LeJEPA"]}" target="_blank">LeJEPA (B/16)</a></div>')
|
| 306 |
out_lejepa_b = gr.Image(show_label=False, interactive=False)
|
| 307 |
|
| 308 |
-
# Custom Clean Footer layout containing links to organization and codebase
|
| 309 |
-
gr.HTML("""
|
| 310 |
-
<div class="custom-footer">
|
| 311 |
-
Models: <a href="https://huggingface.co/OK-AI" target="_blank">OK-AI on HuggingFace</a>
|
| 312 |
-
Β·
|
| 313 |
-
Code: <a href="https://github.com/Open-Knowledge-AI/lite_ssl" target="_blank">lite_ssl Github</a>
|
| 314 |
-
</div>
|
| 315 |
-
""")
|
| 316 |
-
|
| 317 |
# Wire outputs orderly following the exact resolution pattern tracking inside the `run` loop
|
| 318 |
output_targets = [
|
| 319 |
out_dino_s, out_ibot_s, out_lejepa_s,
|
|
@@ -329,11 +368,11 @@ with gr.Blocks(css=CSS, title="SSL ViT PCA Visualiser") as demo:
|
|
| 329 |
if os.path.exists("examples"):
|
| 330 |
gr.Examples(
|
| 331 |
examples=[
|
| 332 |
-
[f"examples/{f}"
|
| 333 |
for f in sorted(os.listdir("examples"))
|
| 334 |
if f.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))
|
| 335 |
],
|
| 336 |
-
inputs=[input_image
|
| 337 |
)
|
| 338 |
|
| 339 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import torchvision.transforms.functional as TF
|
| 7 |
|
| 8 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
+
|
| 10 |
from transformers import AutoModel
|
| 11 |
from sklearn.decomposition import PCA
|
| 12 |
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
# ββ image helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
def create_coming_soon_image(
|
| 58 |
+
image_size,
|
| 59 |
+
text="COMING SOON",
|
| 60 |
+
background_color=(40, 20, 20),
|
| 61 |
+
text_color="white",
|
| 62 |
+
):
|
| 63 |
+
"""
|
| 64 |
+
Create a placeholder image with centered text.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
image_size (int): Width and height of the square image.
|
| 68 |
+
text (str): Text to display.
|
| 69 |
+
background_color (tuple): RGB background color.
|
| 70 |
+
text_color (str|tuple): Text color.
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
PIL.Image.Image
|
| 74 |
+
"""
|
| 75 |
+
image = Image.new("RGB", (image_size, image_size), color=background_color)
|
| 76 |
+
draw = ImageDraw.Draw(image)
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
font = ImageFont.truetype("arial.ttf", size=max(24, image_size // 12))
|
| 80 |
+
except Exception:
|
| 81 |
+
font = ImageFont.load_default()
|
| 82 |
+
|
| 83 |
+
bbox = draw.textbbox((0, 0), text, font=font)
|
| 84 |
+
text_width = bbox[2] - bbox[0]
|
| 85 |
+
text_height = bbox[3] - bbox[1]
|
| 86 |
+
|
| 87 |
+
x = (image_size - text_width) // 2
|
| 88 |
+
y = (image_size - text_height) // 2
|
| 89 |
+
|
| 90 |
+
draw.text(
|
| 91 |
+
(x, y),
|
| 92 |
+
text,
|
| 93 |
+
fill=text_color,
|
| 94 |
+
font=font,
|
| 95 |
+
stroke_width=2,
|
| 96 |
+
stroke_fill="black",
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
return image
|
| 100 |
|
| 101 |
def resize_image_for_patches(
|
| 102 |
image: Image.Image,
|
|
|
|
| 194 |
results[idx] = pca_vis(model, image_tensor, image_size)
|
| 195 |
except Exception as e:
|
| 196 |
print(f"Error processing {repo_id} ({revision}): {e}")
|
| 197 |
+
results[idx] = create_coming_soon_image(image_size)
|
|
|
|
| 198 |
|
| 199 |
yield tuple(results)
|
| 200 |
idx += 1
|
|
|
|
| 312 |
3 components, then scaled with sigmoid for colour display.
|
| 313 |
Results stream seamlessly into view as individual variants complete.
|
| 314 |
</p>
|
| 315 |
+
|
| 316 |
+
<div class="custom-footer">
|
| 317 |
+
Models: <a href="https://huggingface.co/OK-AI" target="_blank">OK-AI on HuggingFace</a>
|
| 318 |
+
Β·
|
| 319 |
+
Code: <a href="https://github.com/Open-Knowledge-AI/lite_ssl" target="_blank">lite_ssl Github</a>
|
| 320 |
+
</div>
|
| 321 |
""")
|
| 322 |
|
| 323 |
with gr.Column(scale=3):
|
|
|
|
| 353 |
f'<div class="model-label"><a href="https://huggingface.co/{MODEL_IDS["ViT-B/16"]["LeJEPA"]}" target="_blank">LeJEPA (B/16)</a></div>')
|
| 354 |
out_lejepa_b = gr.Image(show_label=False, interactive=False)
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
# Wire outputs orderly following the exact resolution pattern tracking inside the `run` loop
|
| 357 |
output_targets = [
|
| 358 |
out_dino_s, out_ibot_s, out_lejepa_s,
|
|
|
|
| 368 |
if os.path.exists("examples"):
|
| 369 |
gr.Examples(
|
| 370 |
examples=[
|
| 371 |
+
[f"examples/{f}"]
|
| 372 |
for f in sorted(os.listdir("examples"))
|
| 373 |
if f.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))
|
| 374 |
],
|
| 375 |
+
inputs=[input_image],
|
| 376 |
)
|
| 377 |
|
| 378 |
if __name__ == "__main__":
|