Spaces:
Sleeping
Sleeping
Simon commited on
Commit ·
c4d56d5
1
Parent(s): 8759ac4
Error fix
Browse files
app.py
CHANGED
|
@@ -36,6 +36,17 @@ def compute_semantic_similarity(img_a: Image.Image, img_b: Image.Image) -> float
|
|
| 36 |
with torch.no_grad():
|
| 37 |
image_features = clip_model.get_image_features(**inputs)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Normalize and compute cosine similarity
|
| 40 |
image_features = F.normalize(image_features, p=2, dim=-1)
|
| 41 |
score = F.cosine_similarity(image_features[0].unsqueeze(0), image_features[1].unsqueeze(0))
|
|
|
|
| 36 |
with torch.no_grad():
|
| 37 |
image_features = clip_model.get_image_features(**inputs)
|
| 38 |
|
| 39 |
+
# --- FIX: Handle Transformers 5.x object returns ---
|
| 40 |
+
if not isinstance(image_features, torch.Tensor):
|
| 41 |
+
if hasattr(image_features, "image_embeds"):
|
| 42 |
+
image_features = image_features.image_embeds
|
| 43 |
+
elif hasattr(image_features, "pooler_output"):
|
| 44 |
+
image_features = image_features.pooler_output
|
| 45 |
+
else:
|
| 46 |
+
# Fallback for tuple-like object behavior
|
| 47 |
+
image_features = image_features[1] if isinstance(image_features, tuple) and len(image_features) > 1 else image_features[0]
|
| 48 |
+
# ---------------------------------------------------
|
| 49 |
+
|
| 50 |
# Normalize and compute cosine similarity
|
| 51 |
image_features = F.normalize(image_features, p=2, dim=-1)
|
| 52 |
score = F.cosine_similarity(image_features[0].unsqueeze(0), image_features[1].unsqueeze(0))
|