File size: 2,630 Bytes
fb12ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""
query_vision_image.py
======================
Query the HF-native image_index using a text prompt.
Embeds the text with CLIP / Qwen3-VL, then performs cosine
similarity search against stored image embeddings.

Usage:
  python query_vision_image.py "sunset over water"
"""

import sys
import json
from pathlib import Path

from config import DEFAULT_PROJECT, EMBED_MODEL, EMBED_DIM
from vector_store import get_store
from embedding import embed_text

TOP_K = 5
MIN_SCORE = 0.15 # Adjusted for HF-native CLIP/Qwen scores

def search_images(query: str):
    print(f"\n{'='*60}")
    print(f"  ARIA Vision β€” Image Search (HF-Native)")
    print(f"{'='*60}")
    print(f"  Query: \"{query}\"")
    print(f"  Model: {EMBED_MODEL} ({EMBED_DIM}d)")
    print()

    print("  [1/3] Embedding query text...", end=" ", flush=True)
    query_vector = embed_text(query)
    print("βœ“")

    print("  [2/3] Searching image_index...", end=" ", flush=True)
    store = get_store(DEFAULT_PROJECT, "image_index")
    raw_results = store.search(query_vector, top_k=TOP_K)
    
    if not raw_results:
        print("no results.")
        print("\n  ⚠ No images found. Did you run ingest_sample_vision.py first?")
        return

    rows = [r for r in raw_results if r.get("score", 0) >= MIN_SCORE]
    print(f"βœ“ ({len(rows)} matches)")

    print(f"\n  [3/3] Results:")
    print(f"  {'─'*56}")
    print(f"  {'Rank':<6} {'File':<25} {'Size':<10} {'Resolution':<12} {'Score':<8}")
    print(f"  {'─'*56}")

    for i, row in enumerate(rows):
        file_name = row.get("file_name", Path(row.get("file_path", "?")).name)
        print(
            f"  {i+1:<6} {file_name[:24]:<25} "
            f"{row.get('file_size', '?'):<10} "
            f"{row.get('resolution', '?'):<12} "
            f"{row.get('score', 0):.4f}"
        )

    print(f"  {'─'*56}")

    output = {
        "mode": "Image",
        "query": query,
        "results": [
            {
                "file_path": r.get("file_path", ""),
                "file_name": r.get("file_name", ""),
                "file_size": r.get("file_size", ""),
                "resolution": r.get("resolution", ""),
                "score": round(r.get("score", 0), 4),
            }
            for r in rows
        ],
    }
    print(f"\n  JSON Response:")
    print(f"  {json.dumps(output, indent=2)}")
    print()

def main():
    if len(sys.argv) < 2:
        print("Usage: python query_vision_image.py \"your search query\"")
        sys.exit(1)

    query = " ".join(sys.argv[1:])
    search_images(query)

if __name__ == "__main__":
    main()