Skip to content

Commit ce3ad35

Browse files
committed
fix Ruff / Type check
1 parent dfcf4f3 commit ce3ad35

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

scripts/copy_metadata_to_clip_db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def main() -> None:
2626
""")
2727

2828
row = target.execute("SELECT COUNT(*) FROM images").fetchone()
29-
print(f"CLIP DB now has {row[0]} images at {CLIP_DB_PATH}")
29+
count = row[0] if row else 0
30+
print(f"CLIP DB now has {count} images at {CLIP_DB_PATH}")
3031

3132
target.execute("DETACH source")
3233
target.close()

src/pyconjp_image_search/embedding/clip.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _normalize(embeddings: np.ndarray) -> np.ndarray:
3131
def embed_images(self, image_paths: list[Path]) -> np.ndarray:
3232
"""Embed a batch of images. Returns L2-normalized projected vectors (768-dim)."""
3333
images = [Image.open(p).convert("RGB") for p in image_paths]
34-
inputs = self.processor(images=images, return_tensors="pt")
34+
inputs = self.processor(images=images, return_tensors="pt") # type: ignore[operator]
3535
inputs = {k: v.to(self.device) for k, v in inputs.items()}
3636
with torch.no_grad():
3737
# Explicitly run vision_model + visual_projection to get 768-dim
@@ -45,12 +45,15 @@ def embed_images(self, image_paths: list[Path]) -> np.ndarray:
4545

4646
def embed_text(self, text: str) -> np.ndarray:
4747
"""Embed a single text query. Returns L2-normalized projected vector (1, 768)."""
48-
inputs = self.processor(text=[text], return_tensors="pt", padding=True, truncation=True)
48+
proc_kwargs = dict(text=[text], return_tensors="pt", padding=True, truncation=True)
49+
inputs = self.processor(**proc_kwargs) # type: ignore[operator]
4950
inputs = {k: v.to(self.device) for k, v in inputs.items()}
5051
with torch.no_grad():
5152
# Explicitly run text_model + text_projection to guarantee
5253
# 768-dim projected features matching JS CLIPTextModelWithProjection.
53-
text_out = self.model.text_model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
54+
text_out = self.model.text_model(
55+
input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"]
56+
)
5457
projected = self.model.text_projection(text_out.pooler_output)
5558
embedding = projected.cpu().numpy()
5659
return self._normalize(embedding).astype(np.float32)

src/pyconjp_image_search/search/app.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ def _do_search_cropped(
572572
text_embedding_state = gr.State(None)
573573
text_selected_index_state = gr.State(None)
574574

575-
def do_text_search(query: str, selected_events: list[str], model_choice: str) -> tuple:
575+
def do_text_search(
576+
query: str, selected_events: list[str], model_choice: str
577+
) -> tuple:
576578
if not query.strip():
577579
return (
578580
[],
@@ -1005,15 +1007,25 @@ def _on_tab_switch():
10051007

10061008
text_find_similar_btn.click(
10071009
fn=_do_find_similar,
1008-
inputs=[text_selected_index_state, text_metadata_state, text_event_filter, model_selector],
1010+
inputs=[
1011+
text_selected_index_state,
1012+
text_metadata_state,
1013+
text_event_filter,
1014+
model_selector,
1015+
],
10091016
outputs=_find_similar_outputs,
10101017
).then(
10111018
fn=_on_close_preview,
10121019
outputs=_text_close_outputs,
10131020
)
10141021
img_find_similar_btn.click(
10151022
fn=_do_find_similar,
1016-
inputs=[image_selected_index_state, image_metadata_state, image_event_filter, model_selector],
1023+
inputs=[
1024+
image_selected_index_state,
1025+
image_metadata_state,
1026+
image_event_filter,
1027+
model_selector,
1028+
],
10171029
outputs=_find_similar_outputs,
10181030
).then(
10191031
fn=_on_close_preview,

0 commit comments

Comments
 (0)