clip_processor.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. try:
  2. from PIL import Image, UnidentifiedImageError
  3. from sentence_transformers import SentenceTransformer
  4. except ImportError:
  5. raise ImportError("Images requires extra dependencies. Install with `pip install 'embedchain[images]'") from None
  6. MODEL_NAME = "clip-ViT-B-32"
  7. class ClipProcessor:
  8. @staticmethod
  9. def load_model():
  10. """Load data from a director of images."""
  11. # load model and image preprocessing
  12. model = SentenceTransformer(MODEL_NAME)
  13. return model
  14. @staticmethod
  15. def get_image_features(image_url, model):
  16. """
  17. Applies the CLIP model to evaluate the vector representation of the supplied image
  18. """
  19. try:
  20. # load image
  21. image = Image.open(image_url)
  22. except FileNotFoundError:
  23. raise FileNotFoundError("The supplied file does not exist`")
  24. except UnidentifiedImageError:
  25. raise UnidentifiedImageError("The supplied file is not an image`")
  26. image_features = model.encode(image)
  27. meta_data = {"url": image_url}
  28. return {"content": image_url, "embedding": image_features.tolist(), "meta_data": meta_data}
  29. @staticmethod
  30. def get_text_features(query):
  31. """
  32. Applies the CLIP model to evaluate the vector representation of the supplied text
  33. """
  34. model = ClipProcessor.load_model()
  35. text_features = model.encode(query)
  36. return text_features.tolist()