Chittrarasu's picture
deploy
8a14cfe
raw
history blame
1.1 kB
from sentence_transformers import SentenceTransformer
from PIL import Image
from fastapi import UploadFile
from typing import List, Optional
import torch
import os
# model = SentenceTransformer("clip-ViT-B-32")
# Set a writable cache directory
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
os.environ["HF_HOME"] = "/app/cache"
# Load model with custom cache directory
model = SentenceTransformer("clip-ViT-B-32", cache_dir="/app/cache")
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