Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import gradio as gr
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
from sentence_transformers import SentenceTransformer
|
| 6 |
import torch
|
|
|
|
| 7 |
|
| 8 |
def flux_to_gray(flux_array):
|
| 9 |
a = np.array(flux_array, dtype=np.float32)
|
|
@@ -12,37 +12,32 @@ def flux_to_gray(flux_array):
|
|
| 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)
|
| 17 |
if not np.isfinite(lo) or not np.isfinite(hi) or hi <= lo:
|
| 18 |
lo, hi = float(np.nanmin(a)), float(np.nanmax(a))
|
| 19 |
norm = np.clip((a - lo) / (hi - lo + 1e-9), 0, 1)
|
| 20 |
arr = (norm * 255).astype(np.uint8)
|
| 21 |
return Image.fromarray(arr, mode="L")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
model =
|
|
|
|
| 25 |
|
| 26 |
-
def
|
| 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")
|
| 30 |
with torch.no_grad():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return pil, caption, info
|
| 35 |
|
| 36 |
demo = gr.Interface(
|
| 37 |
-
fn=
|
| 38 |
inputs=None,
|
| 39 |
-
outputs=[
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
],
|
| 44 |
-
title="JWST → CLIP embedding check",
|
| 45 |
-
description="Embeds one JWST image with CLIP to confirm the pipeline."
|
| 46 |
)
|
| 47 |
-
|
| 48 |
demo.launch()
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import torch
|
| 6 |
+
from transformers import CLIPModel, CLIPProcessor
|
| 7 |
|
| 8 |
def flux_to_gray(flux_array):
|
| 9 |
a = np.array(flux_array, dtype=np.float32)
|
|
|
|
| 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); hi = np.nanpercentile(a, 99)
|
|
|
|
| 16 |
if not np.isfinite(lo) or not np.isfinite(hi) or hi <= lo:
|
| 17 |
lo, hi = float(np.nanmin(a)), float(np.nanmax(a))
|
| 18 |
norm = np.clip((a - lo) / (hi - lo + 1e-9), 0, 1)
|
| 19 |
arr = (norm * 255).astype(np.uint8)
|
| 20 |
return Image.fromarray(arr, mode="L")
|
| 21 |
|
| 22 |
+
model_id = "openai/clip-vit-base-patch32"
|
| 23 |
+
model = CLIPModel.from_pretrained(model_id)
|
| 24 |
+
processor = CLIPProcessor.from_pretrained(model_id)
|
| 25 |
|
| 26 |
+
def test_clip():
|
| 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")
|
| 30 |
with torch.no_grad():
|
| 31 |
+
image_inputs = processor(images=pil, return_tensors="pt")
|
| 32 |
+
image_feats = model.get_image_features(**image_inputs) # [1, 512]
|
| 33 |
+
return pil, f"image_features shape: {tuple(image_feats.shape)}", f"object_id: {rec.get('object_id')}"
|
|
|
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
+
fn=test_clip,
|
| 37 |
inputs=None,
|
| 38 |
+
outputs=[gr.Image(type="pil", label="Preview"),
|
| 39 |
+
gr.Textbox(label="Shape", lines=1),
|
| 40 |
+
gr.Textbox(label="Info", lines=1)],
|
| 41 |
+
title="JWST → CLIP embedding check (transformers)"
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
|
|
|
| 43 |
demo.launch()
|