Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,15 @@ import gradio as gr
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def flux_to_gray(flux_array):
|
| 7 |
a = np.array(flux_array, dtype=np.float32)
|
| 8 |
-
|
| 9 |
-
# remove 1-length dimensions like (1,H,W) or (H,W,1)
|
| 10 |
a = np.squeeze(a)
|
| 11 |
-
|
| 12 |
-
# if still 3D (e.g., C,H,W or H,W,C), collapse the smallest axis
|
| 13 |
if a.ndim == 3:
|
| 14 |
-
axis = int(np.argmin(a.shape))
|
| 15 |
a = np.nanmean(a, axis=axis)
|
| 16 |
-
|
| 17 |
-
# handle NaNs/infs and scale to 0..255
|
| 18 |
a = np.nan_to_num(a, nan=0.0, posinf=0.0, neginf=0.0)
|
| 19 |
lo = np.nanpercentile(a, 1)
|
| 20 |
hi = np.nanpercentile(a, 99)
|
|
@@ -24,20 +20,28 @@ def flux_to_gray(flux_array):
|
|
| 24 |
arr = (norm * 255).astype(np.uint8)
|
| 25 |
return Image.fromarray(arr, mode="L")
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
ds = load_dataset("MultimodalUniverse/jwst", split="train", streaming=True)
|
| 29 |
rec = next(iter(ds))
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
inputs=None,
|
| 38 |
-
outputs=[
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
demo.launch()
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
|
| 7 |
+
# reuse the same grayscale conversion
|
| 8 |
def flux_to_gray(flux_array):
|
| 9 |
a = np.array(flux_array, dtype=np.float32)
|
|
|
|
|
|
|
| 10 |
a = np.squeeze(a)
|
|
|
|
|
|
|
| 11 |
if a.ndim == 3:
|
| 12 |
+
axis = int(np.argmin(a.shape))
|
| 13 |
a = np.nanmean(a, axis=axis)
|
|
|
|
|
|
|
| 14 |
a = np.nan_to_num(a, nan=0.0, posinf=0.0, neginf=0.0)
|
| 15 |
lo = np.nanpercentile(a, 1)
|
| 16 |
hi = np.nanpercentile(a, 99)
|
|
|
|
| 20 |
arr = (norm * 255).astype(np.uint8)
|
| 21 |
return Image.fromarray(arr, mode="L")
|
| 22 |
|
| 23 |
+
# load a well-known CLIP model
|
| 24 |
+
model = SentenceTransformer("clip-ViT-B-32")
|
| 25 |
+
|
| 26 |
+
def test_single_embedding():
|
| 27 |
ds = load_dataset("MultimodalUniverse/jwst", split="train", streaming=True)
|
| 28 |
rec = next(iter(ds))
|
| 29 |
+
pil = flux_to_gray(rec["image"]["flux"]).convert("RGB") # CLIP expects RGB
|
| 30 |
+
emb = model.encode(pil, convert_to_numpy=True)
|
| 31 |
+
info = f"OK. Image embedding shape: {emb.shape}"
|
| 32 |
+
caption = f"object_id: {rec.get('object_id')}"
|
| 33 |
+
return pil, caption, info
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
+
fn=test_single_embedding,
|
| 37 |
inputs=None,
|
| 38 |
+
outputs=[
|
| 39 |
+
gr.Image(type="pil", label="Preview"),
|
| 40 |
+
gr.Textbox(label="Info", lines=1),
|
| 41 |
+
gr.Textbox(label="Embedding", lines=1),
|
| 42 |
+
],
|
| 43 |
+
title="JWST → CLIP embedding check",
|
| 44 |
+
description="Embeds one JWST image with CLIP to confirm the pipeline."
|
| 45 |
)
|
| 46 |
|
| 47 |
demo.launch()
|