Spaces:
No application file
No application file
Commit History
-
Rename app.py to build_index.py
Verified signature6b92d5c -
build_index.py
Verified signatureimport os import io import json import time import faiss import torch import pandas as pd import numpy as np from PIL import Image import requests from sentence_transformers import SentenceTransformer DATA_DIR = os.path.join(os.path.dirname(__file__), "data") CSV_PATH = os.path.join(DATA_DIR, "products.csv") EMB_PATH = os.path.join(DATA_DIR, "embeddings.npy") INDEX_PATH = os.path.join(DATA_DIR, "index.faiss") MAP_PATH = os.path.join(DATA_DIR, "row_mapping.json") IMG_CACHE = os.path.join(DATA_DIR, "images") MODEL_NAME = "clip-ViT-B-32" def ensure_dir(path): if not os.path.exists(path): os.makedirs(path, exist_ok=True) def download_image(url, timeout=15): r = requests.get(url, timeout=timeout) r.raise_for_status() return Image.open(io.BytesIO(r.content)).convert("RGB") def embed_images(model, images): # SentenceTransformer(CLIP) encodes PIL images directly via model.encode with is_image=True with torch.no_grad(): embs = model.encode(images, ba…5cf50c0 -
initial commit
Verified signaturec9a0f85