Spaces:
Runtime error

File size: 1,747 Bytes
c407b02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import pandas as pd
import torch
from transformers import AutoModel, AutoProcessor
from PIL import Image
import requests
from io import BytesIO
from tqdm import tqdm
from pathlib import Path


# Get project root
BASE_DIR = Path(__file__).resolve().parents[2]

dataset_path = BASE_DIR / "data/processed/dataset_clean.csv"
embedding_path = BASE_DIR / "data/embeddings/image_embeddings.npy"


# Load dataset (limit to 200 for testing)
df = pd.read_csv(dataset_path).sample(200, random_state=42)


# Load SigLIP model
model_name = "google/siglip-base-patch16-224"
model = AutoModel.from_pretrained(model_name)
processor = AutoProcessor.from_pretrained(model_name)

model.eval()


embeddings = []

print("\nGenerating image embeddings...\n")


for _, row in tqdm(df.iterrows(), total=len(df)):

    url = row["photo_image_url"]

    try:

        response = requests.get(url, timeout=10)
        response.raise_for_status()

        image = Image.open(BytesIO(response.content)).convert("RGB")

        inputs = processor(images=image, return_tensors="pt")

        with torch.no_grad():
            outputs = model.get_image_features(**inputs)

        # Extract tensor if wrapped in output object
        if hasattr(outputs, "pooler_output"):
            features = outputs.pooler_output
        else:
            features = outputs

        # Normalize embedding
        embeddings.append(features.cpu().numpy()[0])
    except Exception as e:

        print("\nFAILED:", url)
        print(e)

        embeddings.append(np.zeros(768))


embeddings = np.array(embeddings)

print("\nEmbedding matrix shape:", embeddings.shape)


# Save embeddings
np.save(embedding_path, embeddings)

print("\nEmbeddings saved to:", embedding_path)