File size: 12,660 Bytes
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""Photographer's Archive β€” Gradio app entry point."""

import json
import logging
import os
import shutil
import tempfile

import gradio as gr

from caption_store import all_entries, entry_count, get_all_collections, get_entries_by_collection
from ingest import ingest_folder
from search import MIN_RELEVANCE, search

logging.basicConfig(level=logging.INFO)

IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".tiff"}

STAGING_DIR = os.path.join(tempfile.gettempdir(), "photographers_archive_uploads")
os.makedirs(STAGING_DIR, exist_ok=True)


def run_ingest(uploaded_files, collection_name, is_new_collection, new_collection_name):
    if not uploaded_files:
        yield "Please upload at least one image or a folder of images.", gr.update(), gr.update()
        return

    # Determine collection name
    final_collection = new_collection_name.strip() if is_new_collection else collection_name
    if not final_collection:
        final_collection = "General"

    staged = []
    for file_path in uploaded_files:
        ext = os.path.splitext(file_path)[-1].lower()
        if ext in IMAGE_EXTENSIONS:
            dest = os.path.join(STAGING_DIR, os.path.basename(file_path))
            shutil.copy2(file_path, dest)
            staged.append(dest)

    if not staged:
        yield "No supported images found in upload (jpg, jpeg, png, webp, tiff).", gr.update(), gr.update()
        return

    yield f"Staging images to collection '{final_collection}'...", gr.update(), gr.update()

    try:
        for processed, total, msg in ingest_folder(STAGING_DIR, collection=final_collection):
            if total > 0:
                pct = int(processed / total * 100)
                yield f"[{processed}/{total}] ({pct}%) {msg}", gr.update(), gr.update()
            else:
                yield msg, gr.update(), gr.update()
        
        rows, _ = load_caption_browser()
        cols = gr.Dropdown(choices=get_all_collections(), value=final_collection)
        yield f"βœ… Done. Ingested to '{final_collection}'. Store has {entry_count()} images.", rows, cols
    except ValueError as e:
        yield f"Error: {e}", gr.update(), gr.update()


def _parse_meta(raw: str) -> dict | None:
    """Try to parse raw caption as JSON, with comma-fix fallback."""
    import re
    try:
        return json.loads(raw)
    except (json.JSONDecodeError, TypeError):
        pass
    try:
        fixed = re.sub(r'"\s*\n(\s*")', r'",\n\1', raw)
        return json.loads(fixed)
    except (json.JSONDecodeError, TypeError):
        return None


def load_caption_browser():
    entries = all_entries()
    if not entries:
        return [], "No captions yet."
    rows = []
    for path, data in entries.items():
        raw = data["caption"]
        meta = _parse_meta(raw)
        if meta:
            summary = meta.get("summary") or "β€”"
            subj = meta.get("subjects", {})
            attire = ", ".join(subj.get("attire", [])) or "β€”"
            tags = ", ".join(meta.get("search_tags", [])) or "β€”"
        else:
            summary = raw[:300] if raw else "β€”"
            attire = "β€”"
            tags = "β€”"
        rows.append([os.path.basename(path), summary, attire, tags])
    return rows, f"{len(rows)} caption(s) in store."


def run_search(query: str, collection: str = "All"):
    if not query or not query.strip():
        return [], "Please enter a search query."
    if entry_count() == 0:
        return [], "No images indexed yet. Upload and ingest some photos first."
    
    col_filter = None if collection == "All" else collection
    results = search(query.strip(), collection=col_filter)
    
    if not results:
        return [], f"No matching photos found in {collection} (threshold: {MIN_RELEVANCE})."
    gallery_items = [
        (r["path"], f"Score: {r['score']:.2f} β€” {r['caption'][:120]}…")
        for r in results
    ]
    return gallery_items, f"{len(results)} result(s) found."


def load_collections_view(collection_name):
    if not collection_name or collection_name == "All":
        entries = all_entries()
    else:
        entries = get_entries_by_collection(collection_name)
    
    if not entries:
        return [], f"No images in collection '{collection_name}'."
    
    gallery_items = [(path, os.path.basename(path)) for path in entries.keys()]
    return gallery_items, f"{len(entries)} image(s) in '{collection_name}'."


ABOUT_TEXT = """

## ShutterSearch



A local-first photo search tool powered by **MiniCPM-V-4.6** (≀7B VLM).



**How it works:**

1. Upload photos using the Ingest page β€” select an existing collection or create a new one.

   The VLM runs on Modal's GPU and generates rich captions. Captions are cached locally.

2. Use the Search page to find photos by content, mood, or composition using natural language.

3. Browse your organized archive in the Collections page.



Built for the Hugging Face [Build Small](https://huggingface.co/build-small) hackathon.

"""


# --- UI Layout ---
with gr.Blocks() as demo:
    # Navigation state
    current_page = gr.State("home")

    with gr.Sidebar():
        gr.Markdown("# πŸ“· ShutterSearch")
        gr.Markdown("Modern Photo Archive")
        nav_home = gr.Button("🏠 Home", variant="ghost", size="lg")
        nav_ingest = gr.Button("πŸ“₯ Ingest", variant="ghost", size="lg")
        nav_search = gr.Button("πŸ” Search", variant="ghost", size="lg")
        nav_collections = gr.Button("πŸ“ Collections", variant="ghost", size="lg")
        nav_captions = gr.Button("πŸ“ Captions", variant="ghost", size="lg")
        nav_about = gr.Button("ℹ️ About", variant="ghost", size="lg")
        
        gr.HTML("<hr>")
        stats = gr.Label(value=f"Total Photos: {entry_count()}", label="Archive Stats")

    # --- Pages ---
    with gr.Column() as home_page:
        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown("""

                # Welcome to ShutterSearch

                ### Your intelligent local-first photo archive.

                

                ShutterSearch uses state-of-the-art vision models to understand your photography. 

                Keep your photos organized in collections and find them instantly with natural language search.

                

                - **Semantic Search**: Find "sunset on a beach" even if you didn't tag it.

                - **Automatic Captioning**: Powered by MiniCPM-V-4.6.

                - **Privacy First**: Everything runs on your terms.

                """)
                start_btn = gr.Button("Start Ingesting", variant="primary", size="lg")
            with gr.Column(scale=1):
                # Placeholder for random image
                gr.Image("https://images.unsplash.com/photo-1542038784456-1ea8e935640e?q=80&w=1000&auto=format&fit=crop", 
                         label="Photography", show_label=False, interactive=False)

    with gr.Column(visible=False) as ingest_page:
        gr.Markdown("# πŸ“₯ Ingest Photos")
        with gr.Row():
            with gr.Column(scale=2):
                upload = gr.File(
                    label="Upload Images",
                    file_count="multiple",
                    file_types=[".jpg", ".jpeg", ".png", ".webp", ".tiff"],
                    height=300,
                )
            with gr.Column(scale=1):
                gr.Markdown("### Collection Settings")
                coll_dropdown = gr.Dropdown(
                    choices=get_all_collections(), 
                    label="Select Existing Collection",
                    value="General"
                )
                use_new_coll = gr.Checkbox(label="Create New Collection", value=False)
                new_coll_name = gr.Textbox(label="New Collection Name", visible=False)
                
                ingest_btn = gr.Button("πŸš€ Start Ingestion", variant="primary", size="lg")
        
        ingest_status = gr.Textbox(label="Status", interactive=False, lines=5)

    with gr.Column(visible=False) as search_page:
        gr.Markdown("# πŸ” AI Search")
        with gr.Row():
            search_query = gr.Textbox(
                placeholder="Describe what you're looking for... (e.g. 'moody forest portrait')",
                label="Search Query",
                scale=4
            )
            search_col_filter = gr.Dropdown(
                choices=["All"] + get_all_collections(),
                value="All",
                label="Filter by Collection",
                scale=1
            )
            search_btn = gr.Button("Search", variant="primary", scale=1)
        
        search_status_msg = gr.Markdown("Enter a query to start searching.")
        search_gallery = gr.Gallery(label="Search Results", columns=4, height="auto")

    with gr.Column(visible=False) as collections_page:
        gr.Markdown("# πŸ“ Collections")
        with gr.Row():
            view_coll_selector = gr.Dropdown(
                choices=get_all_collections(),
                value=get_all_collections()[0] if get_all_collections() else "General",
                label="Select Collection to Browse",
                scale=4
            )
            refresh_coll_btn = gr.Button("πŸ”„ Refresh", scale=1)
        
        coll_status = gr.Markdown("Browse your organized photos.")
        coll_gallery = gr.Gallery(label="Collection Photos", columns=5, height="auto")

    with gr.Column(visible=False) as captions_page:
        gr.Markdown("# πŸ“ Caption Browser")
        with gr.Row():
            refresh_cap_btn = gr.Button("πŸ”„ Refresh Data", variant="secondary")
            cap_status = gr.Textbox(interactive=False, show_label=False, scale=4)
            
        caption_table = gr.Dataframe(
            headers=["File", "Summary", "Attire", "Tags"],
            datatype=["str", "str", "str", "str"],
            wrap=True,
            interactive=False,
            column_widths=["15%", "45%", "20%", "20%"],
        )

    with gr.Column(visible=False) as about_page:
        gr.Markdown("# ℹ️ About ShutterSearch")
        gr.Markdown(ABOUT_TEXT)

    # --- Navigation Logic ---
    pages = [home_page, ingest_page, search_page, collections_page, captions_page, about_page]
    
    def switch_page(page_name):
        updates = [gr.update(visible=(name == page_name)) for name in ["home", "ingest", "search", "collections", "captions", "about"]]
        return updates

    nav_home.click(fn=lambda: switch_page("home"), outputs=pages)
    nav_ingest.click(fn=lambda: switch_page("ingest"), outputs=pages)
    nav_search.click(fn=lambda: switch_page("search") + [gr.update(choices=["All"] + get_all_collections())], outputs=pages + [search_col_filter])
    nav_collections.click(fn=lambda: switch_page("collections") + [gr.update(choices=get_all_collections())], outputs=pages + [view_coll_selector])
    nav_captions.click(fn=lambda: switch_page("captions"), outputs=pages)
    nav_about.click(fn=lambda: switch_page("about"), outputs=pages)
    
    start_btn.click(fn=lambda: switch_page("ingest"), outputs=pages)

    # --- Page Interactions ---
    use_new_coll.change(fn=lambda x: gr.update(visible=x), inputs=use_new_coll, outputs=new_coll_name)
    
    ingest_btn.click(
        fn=run_ingest, 
        inputs=[upload, coll_dropdown, use_new_coll, new_coll_name], 
        outputs=[ingest_status, caption_table, coll_dropdown]
    ).then(
        fn=lambda: f"Total Photos: {entry_count()}", outputs=stats
    )

    search_btn.click(fn=run_search, inputs=[search_query, search_col_filter], outputs=[search_gallery, search_status_msg])
    search_query.submit(fn=run_search, inputs=[search_query, search_col_filter], outputs=[search_gallery, search_status_msg])

    view_coll_selector.change(fn=load_collections_view, inputs=view_coll_selector, outputs=[coll_gallery, coll_status])
    refresh_coll_btn.click(fn=load_collections_view, inputs=view_coll_selector, outputs=[coll_gallery, coll_status])

    refresh_cap_btn.click(fn=load_caption_browser, outputs=[caption_table, cap_status])
    
    demo.load(fn=load_caption_browser, outputs=[caption_table, cap_status])
    demo.load(fn=lambda: load_collections_view(get_all_collections()[0] if get_all_collections() else "General"), outputs=[coll_gallery, coll_status])

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft(primary_hue="green", secondary_hue="emerald"))