33import io
44import os
55from contextlib import asynccontextmanager
6- from typing import Any , Literal
6+ from typing import Any , AsyncIterator , Literal
77
88import cocoindex
99import torch
1919QDRANT_URL = os .getenv ("QDRANT_URL" , "http://localhost:6334/" )
2020QDRANT_COLLECTION = "ImageSearch"
2121CLIP_MODEL_NAME = "openai/clip-vit-large-patch14"
22- CLIP_MODEL_DIMENSION = 768
2322
2423
2524@functools .cache
@@ -37,13 +36,13 @@ def embed_query(text: str) -> list[float]:
3736 inputs = processor (text = [text ], return_tensors = "pt" , padding = True )
3837 with torch .no_grad ():
3938 features = model .get_text_features (** inputs )
40- return features [0 ].tolist ()
39+ return features [0 ].tolist () # type: ignore
4140
4241
4342@cocoindex .op .function (cache = True , behavior_version = 1 , gpu = True )
4443def embed_image (
4544 img_bytes : bytes ,
46- ) -> cocoindex .Vector [cocoindex .Float32 , Literal [CLIP_MODEL_DIMENSION ]]:
45+ ) -> cocoindex .Vector [cocoindex .Float32 , Literal [768 ]]:
4746 """
4847 Convert image to embedding using CLIP model.
4948 """
@@ -52,7 +51,7 @@ def embed_image(
5251 inputs = processor (images = image , return_tensors = "pt" )
5352 with torch .no_grad ():
5453 features = model .get_image_features (** inputs )
55- return features [0 ].tolist ()
54+ return features [0 ].tolist () # type: ignore
5655
5756
5857# CocoIndex flow: Ingest images, extract captions, embed, export to Qdrant
@@ -112,7 +111,7 @@ def image_object_embedding_flow(
112111
113112
114113@asynccontextmanager
115- async def lifespan (app : FastAPI ) -> None :
114+ async def lifespan (app : FastAPI ) -> AsyncIterator [ None ] :
116115 load_dotenv ()
117116 cocoindex .init ()
118117 image_object_embedding_flow .setup (report_to_stdout = True )
@@ -141,11 +140,11 @@ async def lifespan(app: FastAPI) -> None:
141140
142141
143142# --- Search API ---
144- @app .get ("/search" )
143+ @app .get ("/search" ) # type: ignore[untyped-decorator]
145144def search (
146145 q : str = Query (..., description = "Search query" ),
147146 limit : int = Query (5 , description = "Number of results" ),
148- ) -> Any :
147+ ) -> dict [ str , Any ] :
149148 # Get the embedding for the query
150149 query_embedding = embed_query (q )
151150
0 commit comments