Chittrarasu's picture
deploy
e95d03f
raw
history blame
1.16 kB
from sentence_transformers import SentenceTransformer
from PIL import Image
from fastapi import UploadFile
from typing import List, Optional
import torch
import os
from huggingface_hub import HfFileSystem
import transformers.utils.hub
# Set cache directory globally for huggingface_hub
transformers.utils.hub.HUGGINGFACE_HUB_CACHE = "/app/cache/hub"
# Load model
model = SentenceTransformer("clip-ViT-B-32")
# Load model with custom cache directory
# model = SentenceTransformer("clip-ViT-B-32")
def get_text_embedding(text: str) -> Optional[List[float]]:
try:
embedding = model.encode(text, convert_to_tensor=True).cpu().numpy().tolist()
return embedding
except Exception as e:
print(f"Error generating text embedding: {e}")
return None
def get_image_embedding(image_file: UploadFile) -> Optional[List[float]]:
try:
image = Image.open(image_file.file).convert("RGB").resize((224, 224))
embedding = model.encode(image, convert_to_tensor=True).cpu().numpy().tolist()
return embedding
except Exception as e:
print(f"Error generating image embedding: {e}")
return None