Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ 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)
|
|
@@ -20,14 +20,15 @@ def flux_to_gray(flux_array):
|
|
| 20 |
arr = (norm * 255).astype(np.uint8)
|
| 21 |
return Image.fromarray(arr, mode="L")
|
| 22 |
|
| 23 |
-
#
|
| 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")
|
| 30 |
-
|
|
|
|
| 31 |
info = f"OK. Image embedding shape: {emb.shape}"
|
| 32 |
caption = f"object_id: {rec.get('object_id')}"
|
| 33 |
return pil, caption, info
|
|
|
|
| 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)
|
| 10 |
a = np.squeeze(a)
|
|
|
|
| 20 |
arr = (norm * 255).astype(np.uint8)
|
| 21 |
return Image.fromarray(arr, mode="L")
|
| 22 |
|
| 23 |
+
# well known model from sentence-transformers
|
| 24 |
+
model = SentenceTransformer("clip-ViT-B-32") # alias for sentence-transformers/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")
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
emb = model.encode([pil], convert_to_numpy=True, normalize_embeddings=True) # list input
|
| 32 |
info = f"OK. Image embedding shape: {emb.shape}"
|
| 33 |
caption = f"object_id: {rec.get('object_id')}"
|
| 34 |
return pil, caption, info
|