|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright The LanceDB Authors |
| 3 | + |
| 4 | +import pytest |
| 5 | +try: |
| 6 | + import lancedb |
| 7 | + import numpy as np |
| 8 | + import pyarrow as pa |
| 9 | + import io |
| 10 | + from PIL import Image |
| 11 | +except ImportError: |
| 12 | + pass |
| 13 | + |
| 14 | +# --8<-- [start:multimodal_imports] |
| 15 | +import lancedb |
| 16 | +import pyarrow as pa |
| 17 | +import pandas as pd |
| 18 | +import numpy as np |
| 19 | +import io |
| 20 | +from PIL import Image |
| 21 | +# --8<-- [end:multimodal_imports] |
| 22 | + |
| 23 | +def test_multimodal_ingestion(db_path_factory): |
| 24 | + # Ensure dependencies are available |
| 25 | + pytest.importorskip("PIL") |
| 26 | + pytest.importorskip("lancedb") |
| 27 | + pytest.importorskip("numpy") |
| 28 | + |
| 29 | + # --8<-- [start:create_dummy_data] |
| 30 | + # Create some dummy images |
| 31 | + def create_dummy_image(color): |
| 32 | + img = Image.new('RGB', (100, 100), color=color) |
| 33 | + buf = io.BytesIO() |
| 34 | + img.save(buf, format='PNG') |
| 35 | + return buf.getvalue() |
| 36 | + |
| 37 | + # Create dataset with metadata, vectors, and image blobs |
| 38 | + data = [ |
| 39 | + { |
| 40 | + "id": 1, |
| 41 | + "filename": "red_square.png", |
| 42 | + "vector": np.random.rand(128).astype(np.float32), |
| 43 | + "image_blob": create_dummy_image('red'), |
| 44 | + "label": "red" |
| 45 | + }, |
| 46 | + { |
| 47 | + "id": 2, |
| 48 | + "filename": "blue_square.png", |
| 49 | + "vector": np.random.rand(128).astype(np.float32), |
| 50 | + "image_blob": create_dummy_image('blue'), |
| 51 | + "label": "blue" |
| 52 | + } |
| 53 | + ] |
| 54 | + # --8<-- [end:create_dummy_data] |
| 55 | + |
| 56 | + # --8<-- [start:define_schema] |
| 57 | + # Define schema explictly to ensure image_blob is treated as binary |
| 58 | + schema = pa.schema([ |
| 59 | + pa.field("id", pa.int32()), |
| 60 | + pa.field("filename", pa.string()), |
| 61 | + pa.field("vector", pa.list_(pa.float32(), 128)), |
| 62 | + pa.field("image_blob", pa.binary()), # Important: Use pa.binary() for blobs |
| 63 | + pa.field("label", pa.string()) |
| 64 | + ]) |
| 65 | + # --8<-- [end:define_schema] |
| 66 | + |
| 67 | + db_uri = db_path_factory("multimodal_db") |
| 68 | + db = lancedb.connect(db_uri) |
| 69 | + |
| 70 | + # --8<-- [start:ingest_data] |
| 71 | + tbl = db.create_table("images", data=data, schema=schema, mode="overwrite") |
| 72 | + # --8<-- [end:ingest_data] |
| 73 | + |
| 74 | + assert len(tbl) == 2 |
| 75 | + |
| 76 | + # --8<-- [start:search_data] |
| 77 | + # Search for similar images |
| 78 | + query_vector = np.random.rand(128).astype(np.float32) |
| 79 | + results = tbl.search(query_vector).limit(1).to_pandas() |
| 80 | + # --8<-- [end:search_data] |
| 81 | + |
| 82 | + # --8<-- [start:process_results] |
| 83 | + # Convert back to PIL Image |
| 84 | + for _, row in results.iterrows(): |
| 85 | + image_bytes = row['image_blob'] |
| 86 | + image = Image.open(io.BytesIO(image_bytes)) |
| 87 | + print(f"Retrieved image: {row['filename']}, Size: {image.size}") |
| 88 | + # You can now use 'image' with other libraries or display it |
| 89 | + # --8<-- [end:process_results] |
| 90 | + |
| 91 | + assert len(results) == 1 |
| 92 | + |
| 93 | +def test_blob_api_definition(db_path_factory): |
| 94 | + # --8<-- [start:blob_api_schema] |
| 95 | + import pyarrow as pa |
| 96 | + |
| 97 | + # Define schema with Blob API metadata for lazy loading |
| 98 | + schema = pa.schema([ |
| 99 | + pa.field("id", pa.int64()), |
| 100 | + pa.field( |
| 101 | + "video", |
| 102 | + pa.large_binary(), |
| 103 | + metadata={"lance-encoding:blob": "true"} # Enable Blob API |
| 104 | + ), |
| 105 | + ]) |
| 106 | + # --8<-- [end:blob_api_schema] |
| 107 | + |
| 108 | + # --8<-- [start:blob_api_ingest] |
| 109 | + import lancedb |
| 110 | + import lance |
| 111 | + |
| 112 | + db = lancedb.connect(db_path_factory("blob_db")) |
| 113 | + |
| 114 | + # Create sample data |
| 115 | + data = [ |
| 116 | + {"id": 1, "video": b"fake_video_bytes_1"}, |
| 117 | + {"id": 2, "video": b"fake_video_bytes_2"} |
| 118 | + ] |
| 119 | + |
| 120 | + # Create the table |
| 121 | + tbl = db.create_table("videos", data=data, schema=schema) |
| 122 | + # --8<-- [end:blob_api_ingest] |
| 123 | + assert len(tbl) == 2 |
0 commit comments