test_clip_processor.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import tempfile
  3. import urllib
  4. from PIL import Image
  5. from embedchain.models.clip_processor import ClipProcessor
  6. class TestClipProcessor:
  7. def test_load_model(self):
  8. # Test that the `load_model()` method loads the CLIP model and image preprocessing correctly.
  9. model = ClipProcessor.load_model()
  10. assert model is not None
  11. def test_get_image_features(self):
  12. # Clone the image to a temporary folder.
  13. with tempfile.TemporaryDirectory() as tmp_dir:
  14. urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg", "image.jpg")
  15. image = Image.open("image.jpg")
  16. image.save(os.path.join(tmp_dir, "image.jpg"))
  17. # Get the image features.
  18. model = ClipProcessor.load_model()
  19. ClipProcessor.get_image_features(os.path.join(tmp_dir, "image.jpg"), model)
  20. # Delete the temporary file.
  21. os.remove(os.path.join(tmp_dir, "image.jpg"))
  22. os.remove("image.jpg")
  23. def test_get_text_features(self):
  24. # Test that the `get_text_features()` method returns a list containing the text embedding.
  25. query = "This is a text query."
  26. text_features = ClipProcessor.get_text_features(query)
  27. # Assert that the text embedding is not None.
  28. assert text_features is not None
  29. # Assert that the text embedding is a list of floats.
  30. assert isinstance(text_features, list)
  31. # Assert that the text embedding has the correct length.
  32. assert len(text_features) == 512