File size: 6,324 Bytes
4a02afe
 
 
 
 
 
 
1619a2a
 
 
 
ac707f1
4a02afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f39285
4a02afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Persistent caption store backed by a local JSON file."""

import json
import os
import time
from typing import Optional

# if os.path.exists("/data"):
#     CAPTION_STORE_PATH = "/data/captions.json"
# else:   
#     
CAPTION_STORE_PATH = "./captions.json"


def _load() -> dict:
    if not os.path.exists(CAPTION_STORE_PATH):
        return {}
    with open(CAPTION_STORE_PATH, "r", encoding="utf-8") as f:
        return json.load(f)


def _save(store: dict) -> None:
    with open(CAPTION_STORE_PATH, "w", encoding="utf-8") as f:
        json.dump(store, f, indent=2, ensure_ascii=False)


def _as_list(val) -> list:
    """Ensure a value is always a list — handles string/list/None from model output."""
    if not val:
        return []
    if isinstance(val, list):
        return val
    return [val]  # single string → wrap


def _try_parse_json(raw: str) -> dict | None:
    """Parse JSON, with a fallback that inserts missing commas between fields."""
    try:
        return json.loads(raw)
    except json.JSONDecodeError:
        pass
    # Common model mistake: missing comma after a string value before next key
    import re
    fixed = re.sub(r'"\s*\n(\s*")', r'",\n\1', raw)
    try:
        return json.loads(fixed)
    except json.JSONDecodeError:
        return None


def flatten_metadata(meta: dict) -> str:
    """Convert structured JSON metadata into a rich text string for embedding."""
    parts = []

    if meta.get("summary"):
        parts.append(meta["summary"])

    subj = meta.get("subjects", {})
    # NEW: Include headcount explicitly for numerical search queries (e.g., "two people")
    if subj.get("people_count") is not None:
        parts.append(f"People count: {subj['people_count']}")
    if _as_list(subj.get("attire")):
        parts.append("Attire: " + ", ".join(_as_list(subj["attire"])))
    if _as_list(subj.get("primary_subjects")):
        parts.append("Subjects: " + ", ".join(_as_list(subj["primary_subjects"])))
    if _as_list(subj.get("relationships")):
        parts.append("Relationships: " + ", ".join(_as_list(subj["relationships"])))

    scene = meta.get("scene", {})
    if scene.get("location_type"):
        parts.append("Location: " + scene["location_type"])
    if scene.get("environment"):
        parts.append("Environment: " + scene["environment"])
    if _as_list(scene.get("setting_details")):
        parts.append("Setting: " + ", ".join(_as_list(scene["setting_details"])))

    actions = meta.get("actions", {})
    if actions.get("primary_action"):
        parts.append("Action: " + actions["primary_action"])
    if _as_list(actions.get("body_language")):
        parts.append("Body language: " + ", ".join(_as_list(actions["body_language"])))

    lighting = meta.get("lighting", {})
    if lighting.get("lighting_style"):
        parts.append("Lighting: " + lighting["lighting_style"])
    if lighting.get("time_of_day_estimate"):
        parts.append("Time of day: " + lighting["time_of_day_estimate"])

    mood = meta.get("mood", {})
    if _as_list(mood.get("primary_emotions")):
        parts.append("Emotions: " + ", ".join(_as_list(mood["primary_emotions"])))
    if mood.get("atmosphere"):
        parts.append("Atmosphere: " + mood["atmosphere"])

    comp = meta.get("composition", {})
    if comp.get("shot_type"):
        parts.append("Shot: " + comp["shot_type"])
    if comp.get("camera_angle"):
        parts.append("Angle: " + comp["camera_angle"])

    tech = meta.get("technical_cues", {})
    if _as_list(tech.get("color_palette")):
        parts.append("Colors: " + ", ".join(_as_list(tech["color_palette"])))
    # NEW: Missing depth of field (crucial for portrait photography search!)
    if tech.get("depth_of_field"):
        parts.append("Depth of field: " + tech["depth_of_field"])

    if _as_list(meta.get("search_tags")):
        parts.append("Tags: " + ", ".join(_as_list(meta["search_tags"])))
    if _as_list(meta.get("archive_keywords")):
        parts.append("Keywords: " + ", ".join(_as_list(meta["archive_keywords"])))

    return " | ".join(parts)


def get_entry(image_path: str) -> Optional[dict]:
    return _load().get(image_path)


def upsert_entry(image_path: str, caption: str, mtime: float, collection: str = "General") -> None:
    """

    caption may be raw JSON string (new structured format) or plain text (legacy).

    We store both the raw string and a flattened search_text.

    """
    store = _load()

    # Try to parse as structured JSON (with comma-fix fallback)
    search_text = caption
    meta = _try_parse_json(caption)
    if meta:
        search_text = flatten_metadata(meta)

    store[image_path] = {
        "caption": caption,        # raw (JSON string or plain text)
        "search_text": search_text,  # flattened for embedding
        "mtime": mtime,
        "collection": collection,
        "ingested_at": time.time(),
    }
    _save(store)


def mark_error(image_path: str, error: str, collection: str = "General") -> None:
    store = _load()
    store[image_path] = {
        "caption": None,
        "error": error,
        "mtime": os.path.getmtime(image_path) if os.path.exists(image_path) else 0,
        "collection": collection,
        "ingested_at": time.time(),
    }
    _save(store)


def all_entries() -> dict:
    """Return only entries that have a valid caption."""
    return {
        path: data
        for path, data in _load().items()
        if data.get("caption")
    }


def entry_count() -> int:
    return len(all_entries())


def get_all_collections() -> list[str]:
    """Return a sorted list of all unique collection names."""
    store = _load()
    collections = set()
    for entry in store.values():
        coll = entry.get("collection")
        if coll:
            collections.add(coll)
    if not collections:
        return ["General"]
    return sorted(list(collections))


def get_entries_by_collection(collection: str) -> dict:
    """Return all valid entries belonging to a specific collection."""
    return {
        path: data
        for path, data in all_entries().items()
        if data.get("collection") == collection
    }