Skip to content

Commit 10dfdd8

Browse files
committed
examples: review follow-up cleanup (simplify types, restore decorators, minimal ignores)
1 parent 7309315 commit 10dfdd8

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

examples/custom_output_files/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import cocoindex
66
from markdown_it import MarkdownIt
7-
from typing import cast
87

98
_markdown_it = MarkdownIt("gfm-like")
109

@@ -97,7 +96,8 @@ def mutate(
9796

9897
@cocoindex.op.function()
9998
def markdown_to_html(text: str) -> str:
100-
return cast(str, _markdown_it.render(text))
99+
# The underlying library lacks precise typing; the return here is known to be str.
100+
return _markdown_it.render(text) # type: ignore[no-any-return]
101101

102102

103103
@cocoindex.flow_def(name="CustomOutputFiles")

examples/fastapi_server_docker/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
104104
fastapi_app = FastAPI(lifespan=lifespan)
105105

106106

107+
@fastapi_app.get("/search") # type: ignore
107108
def search_endpoint(
108109
request: Request,
109110
q: str = Query(..., description="Search query"),
@@ -114,9 +115,5 @@ def search_endpoint(
114115
return {"results": results}
115116

116117

117-
# Attach route without using decorator to avoid untyped-decorator when FastAPI types are unavailable
118-
fastapi_app.get("/search")(search_endpoint)
119-
120-
121118
if __name__ == "__main__":
122119
uvicorn.run(fastapi_app, host="0.0.0.0", port=8080)

examples/image_search/main.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io
44
import os
55
from contextlib import asynccontextmanager
6-
from typing import Any, Literal, Final, TypeAlias, cast, AsyncIterator
6+
from typing import Any, cast, AsyncIterator
77

88
import cocoindex
99
import torch
@@ -19,8 +19,7 @@
1919
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6334/")
2020
QDRANT_COLLECTION = "ImageSearch"
2121
CLIP_MODEL_NAME = "openai/clip-vit-large-patch14"
22-
CLIP_MODEL_DIMENSION: Final[int] = 768
23-
CLIPVector: TypeAlias = cocoindex.Vector[cocoindex.Float32, Literal[768]]
22+
# Using simple list[float] for embeddings for readability in example code.
2423

2524

2625
@functools.cache
@@ -44,7 +43,7 @@ def embed_query(text: str) -> list[float]:
4443
@cocoindex.op.function(cache=True, behavior_version=1, gpu=True)
4544
def embed_image(
4645
img_bytes: bytes,
47-
) -> CLIPVector:
46+
) -> list[float]:
4847
"""
4948
Convert image to embedding using CLIP model.
5049
"""
@@ -53,7 +52,7 @@ def embed_image(
5352
inputs = processor(images=image, return_tensors="pt")
5453
with torch.no_grad():
5554
features = model.get_image_features(**inputs)
56-
return cast(CLIPVector, features[0].tolist())
55+
return cast(list[float], features[0].tolist())
5756

5857

5958
# CocoIndex flow: Ingest images, extract captions, embed, export to Qdrant
@@ -142,6 +141,7 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
142141

143142

144143
# --- Search API ---
144+
@app.get("/search") # type: ignore
145145
def search(
146146
q: str = Query(..., description="Search query"),
147147
limit: int = Query(5, description="Number of results"),
@@ -171,5 +171,4 @@ def search(
171171
}
172172

173173

174-
# Attach route without using decorator to avoid untyped-decorator when FastAPI types are unavailable
175-
app.get("/search")(search)
174+
# Route attached via decorator above for readability

examples/pdf_elements_embedding/main.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
from dataclasses import dataclass
88
from pypdf import PdfReader
99
from transformers import CLIPModel, CLIPProcessor
10-
from typing import Literal, TypeAlias, Final, cast
10+
from typing import cast
1111

1212

1313
QDRANT_GRPC_URL = "http://localhost:6334"
1414
QDRANT_COLLECTION_IMAGE = "PdfElementsEmbeddingImage"
1515
QDRANT_COLLECTION_TEXT = "PdfElementsEmbeddingText"
1616

1717
CLIP_MODEL_NAME = "openai/clip-vit-large-patch14"
18-
CLIP_MODEL_DIMENSION: Final = 768
19-
ClipVectorType: TypeAlias = cocoindex.Vector[cocoindex.Float32, Literal[768]]
2018

2119
IMG_THUMBNAIL_SIZE = (512, 512)
2220

@@ -29,7 +27,7 @@ def get_clip_model() -> tuple[CLIPModel, CLIPProcessor]:
2927

3028

3129
@cocoindex.op.function(cache=True, behavior_version=1, gpu=True)
32-
def clip_embed_image(img_bytes: bytes) -> ClipVectorType:
30+
def clip_embed_image(img_bytes: bytes) -> list[float]:
3331
"""
3432
Convert image to embedding using CLIP model.
3533
"""
@@ -38,18 +36,18 @@ def clip_embed_image(img_bytes: bytes) -> ClipVectorType:
3836
inputs = processor(images=image, return_tensors="pt")
3937
with torch.no_grad():
4038
features = model.get_image_features(**inputs)
41-
return cast(ClipVectorType, features[0].tolist())
39+
return cast(list[float], features[0].tolist())
4240

4341

44-
def clip_embed_query(text: str) -> ClipVectorType:
42+
def clip_embed_query(text: str) -> list[float]:
4543
"""
4644
Embed the caption using CLIP model.
4745
"""
4846
model, processor = get_clip_model()
4947
inputs = processor(text=[text], return_tensors="pt", padding=True)
5048
with torch.no_grad():
5149
features = model.get_text_features(**inputs)
52-
return cast(ClipVectorType, features[0].tolist())
50+
return cast(list[float], features[0].tolist())
5351

5452

5553
@cocoindex.transform_flow()

0 commit comments

Comments
 (0)