Spaces:
Sleeping
Sleeping
Commit
·
9945f18
1
Parent(s):
4aacf70
Removed torch code
Browse files
cloudzy/embedding/image_embedding.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
from transformers import AutoModel, AutoProcessor
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import requests
|
| 4 |
-
import numpy as np
|
| 5 |
-
import torch
|
| 6 |
-
from io import BytesIO
|
| 7 |
-
|
| 8 |
-
# Load model and processor directly
|
| 9 |
-
model = AutoModel.from_pretrained("jinaai/jina-clip-v2", trust_remote_code=True)
|
| 10 |
-
processor = AutoProcessor.from_pretrained("jinaai/jina-clip-v2", trust_remote_code=True)
|
| 11 |
-
|
| 12 |
-
texts = ["Woman taking pictures on a road trip.", "delicious fruits glowing under sunlight"]
|
| 13 |
-
# Process and encode text
|
| 14 |
-
text_inputs = processor(text=texts, return_tensors="pt", padding=True)
|
| 15 |
-
with torch.no_grad():
|
| 16 |
-
text_embeddings = model.get_text_features(**text_inputs)
|
| 17 |
-
text_embeddings = text_embeddings.cpu().numpy()
|
| 18 |
-
print("Text embeddings shape:", text_embeddings.shape)
|
| 19 |
-
|
| 20 |
-
image_paths = [
|
| 21 |
-
"/Users/komeilfathi/Documents/hf_deploy_test/cloudzy_ai_challenge/uploads/img_1_20251026_014959_886.jpg",
|
| 22 |
-
"/Users/komeilfathi/Documents/hf_deploy_test/cloudzy_ai_challenge/uploads/img_9_20251024_185602_319.webp"
|
| 23 |
-
]
|
| 24 |
-
images = []
|
| 25 |
-
for path in image_paths:
|
| 26 |
-
try:
|
| 27 |
-
img = Image.open(path).convert("RGB")
|
| 28 |
-
images.append(img)
|
| 29 |
-
print(f"✓ Loaded image from {path}")
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"✗ Failed to load image from {path}: {e}")
|
| 32 |
-
|
| 33 |
-
# Process and encode images
|
| 34 |
-
if images:
|
| 35 |
-
image_inputs = processor(images=images, return_tensors="pt")
|
| 36 |
-
with torch.no_grad():
|
| 37 |
-
image_embeddings = model.get_image_features(**image_inputs)
|
| 38 |
-
image_embeddings = image_embeddings.cpu().numpy()
|
| 39 |
-
print("Image embeddings shape:", image_embeddings.shape)
|
| 40 |
-
else:
|
| 41 |
-
print("⚠ No images loaded successfully")
|
| 42 |
-
image_embeddings = np.array([])
|
| 43 |
-
|
| 44 |
-
def cosine_similarity(a, b):
|
| 45 |
-
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
|
| 46 |
-
|
| 47 |
-
if len(image_embeddings) > 0:
|
| 48 |
-
for i, t_emb in enumerate(text_embeddings):
|
| 49 |
-
for j, i_emb in enumerate(image_embeddings):
|
| 50 |
-
sim = cosine_similarity(t_emb, i_emb)
|
| 51 |
-
print(f"Similarity between text {i} and image {j}: {sim:.4f}")
|
| 52 |
-
else:
|
| 53 |
-
print("No images to compare similarity with")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|