-
Notifications
You must be signed in to change notification settings - Fork 289
examples: fix mypy errors in example entry points; add local type-check script (#1091) #1248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7309315
10dfdd8
3705fa1
d783678
ad5d369
869b248
f520e1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| # Resolve python in local venv | ||
| $repoRoot = Split-Path -Parent $PSScriptRoot | ||
| $python = Join-Path $repoRoot '.venv\Scripts\python.exe' | ||
| if (-not (Test-Path $python)) { | ||
| $python = 'python' | ||
| } | ||
|
|
||
| # Ensure mypy can resolve local cocoindex package sources | ||
| $env:MYPYPATH = Join-Path $repoRoot 'python' | ||
|
|
||
| # Collect example entry files | ||
| $examples = Join-Path $repoRoot 'examples' | ||
| $files = Get-ChildItem -Path $examples -Recurse -File | | ||
| Where-Object { $_.Name -in @('main.py','colpali_main.py') } | | ||
| Sort-Object FullName | ||
|
|
||
| $failed = @() | ||
| foreach ($f in $files) { | ||
| Write-Host (">>> Checking " + $f.FullName) | ||
| & $python -m mypy --ignore-missing-imports --follow-imports=silent $f.FullName | ||
| if ($LASTEXITCODE -ne 0) { | ||
| $failed += $f.FullName | ||
| } | ||
| } | ||
|
|
||
| if ($failed.Count -gt 0) { | ||
| Write-Host "\nFailures:" | ||
| $failed | ForEach-Object { Write-Host $_ } | ||
| exit 1 | ||
| } else { | ||
| Write-Host "\nAll example entry files passed mypy." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import datetime | ||
| import os | ||
| from contextlib import asynccontextmanager | ||
| from typing import Any | ||
| from typing import Any, AsyncIterator | ||
|
|
||
| import cocoindex | ||
| from dotenv import load_dotenv | ||
|
|
@@ -71,7 +71,7 @@ def image_object_embedding_flow( | |
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def lifespan(app: FastAPI) -> None: | ||
| async def lifespan(app: FastAPI) -> AsyncIterator[None]: | ||
| load_dotenv() | ||
| cocoindex.init() | ||
| image_object_embedding_flow.setup(report_to_stdout=True) | ||
|
|
@@ -100,11 +100,10 @@ async def lifespan(app: FastAPI) -> None: | |
|
|
||
|
|
||
| # --- Search API --- | ||
| @app.get("/search") | ||
| def search( | ||
| q: str = Query(..., description="Search query"), | ||
| limit: int = Query(5, description="Number of results"), | ||
| ) -> Any: | ||
| ) -> dict[str, Any]: | ||
| # Get the multi-vector embedding for the query | ||
| query_embedding = text_to_colpali_embedding.eval(q) | ||
| print( | ||
|
|
@@ -132,3 +131,7 @@ def search( | |
| for result in search_results.points | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| # Attach route without using decorator to avoid untyped-decorator when FastAPI types are unavailable | ||
| app.get("/search")(search) | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import io | ||
| import os | ||
| from contextlib import asynccontextmanager | ||
| from typing import Any, Literal | ||
| from typing import Any, cast, AsyncIterator | ||
|
|
||
| import cocoindex | ||
| import torch | ||
|
|
@@ -19,7 +19,7 @@ | |
| QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6334/") | ||
| QDRANT_COLLECTION = "ImageSearch" | ||
| CLIP_MODEL_NAME = "openai/clip-vit-large-patch14" | ||
| CLIP_MODEL_DIMENSION = 768 | ||
| # Using simple list[float] for embeddings for readability in example code. | ||
|
|
||
|
|
||
| @functools.cache | ||
|
|
@@ -37,13 +37,13 @@ def embed_query(text: str) -> list[float]: | |
| inputs = processor(text=[text], return_tensors="pt", padding=True) | ||
| with torch.no_grad(): | ||
| features = model.get_text_features(**inputs) | ||
| return features[0].tolist() | ||
| return cast(list[float], features[0].tolist()) | ||
|
|
||
|
|
||
| @cocoindex.op.function(cache=True, behavior_version=1, gpu=True) | ||
| def embed_image( | ||
| img_bytes: bytes, | ||
| ) -> cocoindex.Vector[cocoindex.Float32, Literal[CLIP_MODEL_DIMENSION]]: | ||
| ) -> list[float]: | ||
|
||
| """ | ||
| Convert image to embedding using CLIP model. | ||
| """ | ||
|
|
@@ -52,7 +52,7 @@ def embed_image( | |
| inputs = processor(images=image, return_tensors="pt") | ||
| with torch.no_grad(): | ||
| features = model.get_image_features(**inputs) | ||
| return features[0].tolist() | ||
| return cast(list[float], features[0].tolist()) | ||
|
|
||
|
|
||
| # CocoIndex flow: Ingest images, extract captions, embed, export to Qdrant | ||
|
|
@@ -112,7 +112,7 @@ def image_object_embedding_flow( | |
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def lifespan(app: FastAPI) -> None: | ||
| async def lifespan(app: FastAPI) -> AsyncIterator[None]: | ||
| load_dotenv() | ||
| cocoindex.init() | ||
| image_object_embedding_flow.setup(report_to_stdout=True) | ||
|
|
@@ -141,11 +141,11 @@ async def lifespan(app: FastAPI) -> None: | |
|
|
||
|
|
||
| # --- Search API --- | ||
| @app.get("/search") | ||
| @app.get("/search") # type: ignore | ||
| def search( | ||
| q: str = Query(..., description="Search query"), | ||
| limit: int = Query(5, description="Number of results"), | ||
| ) -> Any: | ||
| ) -> dict[str, Any]: | ||
| # Get the embedding for the query | ||
| query_embedding = embed_query(q) | ||
|
|
||
|
|
@@ -169,3 +169,6 @@ def search( | |
| for result in search_results | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| # Route attached via decorator above for readability | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
# type: ignorehere too, instead of acast.