Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,32 @@ from datasets import load_dataset
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def show_first():
|
| 7 |
ds = load_dataset("MultimodalUniverse/jwst", split="train", streaming=True)
|
| 8 |
rec = next(iter(ds))
|
| 9 |
img = rec["image"]
|
| 10 |
-
|
| 11 |
-
# convert the flux array to a viewable grayscale image
|
| 12 |
-
flux = np.array(img["flux"], dtype=np.float32)
|
| 13 |
-
lo = np.percentile(flux, 1)
|
| 14 |
-
hi = np.percentile(flux, 99)
|
| 15 |
-
if hi <= lo: # fallback if data is odd
|
| 16 |
-
lo, hi = float(flux.min()), float(flux.max())
|
| 17 |
-
norm = np.clip((flux - lo) / (hi - lo + 1e-9), 0, 1)
|
| 18 |
-
arr = (norm * 255).astype(np.uint8)
|
| 19 |
-
pil = Image.fromarray(arr, mode="L")
|
| 20 |
-
|
| 21 |
caption = f"object_id: {rec.get('object_id')}, band: {img.get('band')}"
|
| 22 |
return pil, caption
|
| 23 |
|
|
@@ -26,7 +37,7 @@ demo = gr.Interface(
|
|
| 26 |
inputs=None,
|
| 27 |
outputs=[gr.Image(type="pil", label="Preview"), gr.Textbox(label="Info", lines=2)],
|
| 28 |
title="JWST sample preview",
|
| 29 |
-
description="First streamed record as image +
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
| 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)) # pick the likely channel axis
|
| 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)
|
| 21 |
+
if not np.isfinite(lo) or not np.isfinite(hi) or hi <= lo:
|
| 22 |
+
lo, hi = float(np.nanmin(a)), float(np.nanmax(a))
|
| 23 |
+
norm = np.clip((a - lo) / (hi - lo + 1e-9), 0, 1)
|
| 24 |
+
arr = (norm * 255).astype(np.uint8)
|
| 25 |
+
return Image.fromarray(arr, mode="L")
|
| 26 |
+
|
| 27 |
def show_first():
|
| 28 |
ds = load_dataset("MultimodalUniverse/jwst", split="train", streaming=True)
|
| 29 |
rec = next(iter(ds))
|
| 30 |
img = rec["image"]
|
| 31 |
+
pil = flux_to_gray(img["flux"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
caption = f"object_id: {rec.get('object_id')}, band: {img.get('band')}"
|
| 33 |
return pil, caption
|
| 34 |
|
|
|
|
| 37 |
inputs=None,
|
| 38 |
outputs=[gr.Image(type="pil", label="Preview"), gr.Textbox(label="Info", lines=2)],
|
| 39 |
title="JWST sample preview",
|
| 40 |
+
description="First streamed record as image + minimal metadata."
|
| 41 |
)
|
| 42 |
|
| 43 |
demo.launch()
|