1717from transformers import CLIPModel , CLIPProcessor
1818
1919
20- QDRANT_GRPC_URL = os .getenv ("QDRANT_GRPC_URL" , "http://localhost:6334/" )
20+ QDRANT_URL = os .getenv ("QDRANT_URL" , "http://localhost:6334/" )
21+ QDRANT_COLLECTION = "cocoindex_image_search"
2122CLIP_MODEL_NAME = "openai/clip-vit-large-patch14"
23+ CLIP_MODEL_DIMENSION = 768
2224
2325
2426@functools .cache
@@ -40,7 +42,9 @@ def embed_query(text: str) -> list[float]:
4042
4143
4244@cocoindex .op .function (cache = True , behavior_version = 1 , gpu = True )
43- def embed_image (img_bytes : bytes ) -> cocoindex .Vector [cocoindex .Float32 , Literal [384 ]]:
45+ def embed_image (
46+ img_bytes : bytes ,
47+ ) -> cocoindex .Vector [cocoindex .Float32 , Literal [CLIP_MODEL_DIMENSION ]]:
4448 """
4549 Convert image to embedding using CLIP model.
4650 """
@@ -56,7 +60,7 @@ def embed_image(img_bytes: bytes) -> cocoindex.Vector[cocoindex.Float32, Literal
5660@cocoindex .flow_def (name = "ImageObjectEmbedding" )
5761def image_object_embedding_flow (
5862 flow_builder : cocoindex .FlowBuilder , data_scope : cocoindex .DataScope
59- ):
63+ ) -> None :
6064 data_scope ["images" ] = flow_builder .add_source (
6165 cocoindex .sources .LocalFile (
6266 path = "img" , included_patterns = ["*.jpg" , "*.jpeg" , "*.png" ], binary = True
@@ -73,14 +77,17 @@ def image_object_embedding_flow(
7377 filename = img ["filename" ],
7478 embedding = img ["embedding" ],
7579 )
80+
81+ qdrant_conn = cocoindex .add_auth_entry (
82+ "Qdrant" , cocoindex .storages .QdrantConnection (url = QDRANT_URL )
83+ )
7684 img_embeddings .export (
7785 "img_embeddings" ,
7886 cocoindex .storages .Qdrant (
79- collection_name = "image_search" ,
80- grpc_url = QDRANT_GRPC_URL ,
87+ connection = qdrant_conn ,
88+ collection_name = QDRANT_COLLECTION ,
8189 ),
8290 primary_key_fields = ["id" ],
83- setup_by_user = True ,
8491 )
8592
8693
@@ -103,7 +110,7 @@ def startup_event():
103110 load_dotenv ()
104111 cocoindex .init ()
105112 # Initialize Qdrant client
106- app .state .qdrant_client = QdrantClient (url = QDRANT_GRPC_URL , prefer_grpc = True )
113+ app .state .qdrant_client = QdrantClient (url = QDRANT_URL , prefer_grpc = True )
107114 app .state .live_updater = cocoindex .FlowLiveUpdater (image_object_embedding_flow )
108115 app .state .live_updater .start ()
109116
@@ -118,7 +125,7 @@ def search(
118125
119126 # Search in Qdrant
120127 search_results = app .state .qdrant_client .search (
121- collection_name = "image_search" ,
128+ collection_name = QDRANT_COLLECTION ,
122129 query_vector = ("embedding" , query_embedding ),
123130 limit = limit ,
124131 )
0 commit comments