diff --git a/python/python/lancedb/embeddings/jinaai.py b/python/python/lancedb/embeddings/jinaai.py index 25b5a940b..d038b6793 100644 --- a/python/python/lancedb/embeddings/jinaai.py +++ b/python/python/lancedb/embeddings/jinaai.py @@ -7,6 +7,7 @@ import io import base64 from functools import cached_property from urllib.parse import urlparse +from urllib.request import url2pathname from pathlib import Path from typing import TYPE_CHECKING, List, Union, Optional, Any, Dict @@ -87,14 +88,18 @@ class JinaEmbeddings(EmbeddingFunction): if isinstance(image, bytes): image_dict = {"image": base64.b64encode(image).decode("utf-8")} elif isinstance(image, (str, Path)): - parsed = urlparse(str(image)) + parsed = urlparse(image) if isinstance(image, str) else None PIL_Image = attempt_import_or_raise("PIL.Image", "pillow") - if parsed.scheme == "file": - pil_image = PIL_Image.open(parsed.path) - elif parsed.scheme == "" or (os.name == "nt" and len(parsed.scheme) == 1): + if ( + parsed is None + or not parsed.scheme + or (os.name == "nt" and len(parsed.scheme) == 1) + ): # A Windows drive letter parses as a one-character scheme # ("C:\\img.png" -> scheme="c"), so treat it as a local path. - pil_image = PIL_Image.open(image if os.name == "nt" else parsed.path) + pil_image = PIL_Image.open(image) + elif parsed.scheme == "file": + pil_image = PIL_Image.open(url2pathname(parsed.path)) elif parsed.scheme.startswith("http"): pil_image = PIL_Image.open(io.BytesIO(url_retrieve(image))) else: diff --git a/python/python/tests/test_embeddings.py b/python/python/tests/test_embeddings.py index afd07f8a1..aee3edfaf 100644 --- a/python/python/tests/test_embeddings.py +++ b/python/python/tests/test_embeddings.py @@ -1241,7 +1241,7 @@ def test_jina_generate_image_input_dict_local_path(tmp_path): Image = pytest.importorskip("PIL.Image") from lancedb.embeddings.jinaai import JinaEmbeddings - image_path = tmp_path / "test.png" + image_path = tmp_path / "test image #1.png" Image.new("RGB", (4, 4), color="red").save(image_path, format="PNG") for image in (str(image_path), image_path): @@ -1250,6 +1250,24 @@ def test_jina_generate_image_input_dict_local_path(tmp_path): assert isinstance(image_dict["image"], str) and len(image_dict["image"]) > 0 +def test_jina_embeds_a_file_uri_as_an_image(tmp_path): + image_module = pytest.importorskip("PIL.Image") + from lancedb.embeddings.jinaai import JinaEmbeddings + + image_path = tmp_path / "photo \u2603 #1%.png" + image = image_module.new("RGB", (4, 4), color="red") + image.save(image_path) + image_uri = image_path.as_uri() + expected_input = JinaEmbeddings._generate_image_input_dict(image) + embedding = JinaEmbeddings() + + with patch.object( + JinaEmbeddings, "_generate_embeddings", return_value=[[0.0]] + ) as embed: + assert embedding.generate_image_embedding(image_uri) == [0.0] + embed.assert_called_once_with(input=[expected_input]) + + def test_jina_embeddings_use_their_own_api_key(monkeypatch): """Each JinaEmbeddings instance must send its own API key.""" pytest.importorskip("requests")