File size: 15,753 Bytes
fea499e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Video Intelligence Platform β€” Gradio UI
Interactive video search with Akinator-style refinement.
"""
import os
import json
import time
import tempfile
import numpy as np
import gradio as gr
from typing import Optional

from .config import Config
from .pipeline import IndexingPipeline
from .query_engine import QueryEngine, QueryResult
from .akinator import AkinatorRefiner
from .gemini_client import GeminiClient
from .visual_encoders import SigLIPEncoder
from .index_store import VideoIndex


# ── Global State ────────────────────────────────────────────────────────────
# (Gradio runs in a single process, so module-level state is fine)
pipeline: Optional[IndexingPipeline] = None
query_engine: Optional[QueryEngine] = None
akinator: Optional[AkinatorRefiner] = None
current_video_path: Optional[str] = None
current_results: list = []
akinator_state: Optional[dict] = None


def initialize_system(api_key: str) -> str:
    """Initialize all models and indices."""
    global pipeline, query_engine, akinator

    if not api_key.strip():
        return "❌ Please enter your Gemini API key"

    try:
        os.environ["GEMINI_API_KEY"] = api_key.strip()
        config = Config(gemini_api_key=api_key.strip())

        pipeline = IndexingPipeline(config)
        query_engine = QueryEngine(
            index=pipeline.index,
            gemini=pipeline.gemini,
            siglip=pipeline.siglip,
            top_k=config.top_k,
        )
        akinator = AkinatorRefiner(
            index=pipeline.index,
            gemini=pipeline.gemini,
            threshold=config.akinator_threshold,
        )

        return "βœ… System initialized! Models loaded. Upload a video to get started."
    except Exception as e:
        return f"❌ Initialization failed: {str(e)}"


def index_video(video_file, caption_every_n: int = 3, progress=gr.Progress()):
    """Index an uploaded video file."""
    global current_video_path

    if pipeline is None:
        return "❌ System not initialized. Enter your Gemini API key first.", ""

    if video_file is None:
        return "❌ No video uploaded", ""

    video_path = video_file if isinstance(video_file, str) else video_file.name
    current_video_path = video_path

    try:
        progress(0.1, desc="Extracting frames...")
        stats = pipeline.index_video(
            video_path,
            caption_every_n=max(1, int(caption_every_n)),
            detect_every_n=1,
        )

        stats_str = (
            f"βœ… **Video Indexed Successfully!**\n\n"
            f"- **Frames extracted:** {stats['frames']}\n"
            f"- **Objects detected:** {stats['detections']}\n"
            f"- **Visual embeddings:** {stats['visual_vectors']}\n"
            f"- **Caption embeddings:** {stats['caption_vectors']}\n"
            f"- **Time elapsed:** {stats['elapsed_sec']:.1f}s\n\n"
            f"πŸ” Ready to search! Try queries like:\n"
            f'- "person wearing white clothes"\n'
            f'- "red car"\n'
            f'- "person AND car" (boolean)\n'
            f'- "outdoor scene at night"'
        )

        return stats_str, video_path

    except Exception as e:
        return f"❌ Indexing failed: {str(e)}", ""


def search_video(query: str) -> tuple:
    """Search the indexed video."""
    global current_results, akinator_state

    if query_engine is None:
        return "❌ System not initialized", "", gr.update(visible=False), gr.update(visible=False)

    if not query.strip():
        return "❌ Enter a search query", "", gr.update(visible=False), gr.update(visible=False)

    try:
        results = query_engine.search(query.strip())
        current_results = results

        if not results:
            return "No results found for this query.", "", gr.update(visible=False), gr.update(visible=False)

        # Format results
        results_md = f"## πŸ” Found {len(results)} matching moments\n\n"
        
        for i, r in enumerate(results, 1):
            results_md += f"### {i}. ⏱️ {r.time_str} (score: {r.score:.3f})\n"
            if r.caption:
                results_md += f"> {r.caption[:200]}\n"
            if r.detections:
                results_md += f"🏷️ Objects: {', '.join(r.detections)}\n"
            results_md += f"πŸ“‘ Source: {r.match_source}\n\n"

        # Check if Akinator refinement is needed
        if len(results) > 10 and akinator is not None:
            akinator_result = akinator.start(results, query)
            akinator_state = akinator_result

            if akinator_result["status"] == "refining":
                question = akinator_result["question"]
                options = akinator_result["options"]
                options_md = f"### 🌳 Too many results! Let me help narrow them down.\n\n"
                options_md += f"**{question}**\n\n"
                for opt in options:
                    options_md += f"- {opt}\n"

                return (
                    results_md,
                    "",
                    gr.update(visible=True, value=options_md),
                    gr.update(visible=True, choices=options, value=None),
                )

        return results_md, "", gr.update(visible=False), gr.update(visible=False)

    except Exception as e:
        return f"❌ Search failed: {str(e)}", "", gr.update(visible=False), gr.update(visible=False)


def refine_results(choice: str, query: str) -> tuple:
    """Process Akinator refinement choice."""
    global akinator_state, current_results

    if akinator is None or akinator_state is None:
        return "No active refinement session", gr.update(visible=False), gr.update(visible=False)

    try:
        result = akinator.answer(choice, query)
        akinator_state = result

        if result["status"] == "done":
            # Show final refined results
            refined = result.get("results", [])
            results_md = f"## βœ… Refined to {len(refined)} results\n\n"

            # Show refinement history
            history = result.get("history", [])
            if history:
                results_md += "**Refinement path:**\n"
                for h in history:
                    results_md += f"- Q: {h['question']} β†’ A: {h['answer']} ({h['remaining']} remaining)\n"
                results_md += "\n"

            for i, r in enumerate(refined, 1):
                results_md += f"### {i}. ⏱️ {r['time_str']} (score: {r['score']:.3f})\n"
                if r.get("caption"):
                    results_md += f"> {r['caption'][:200]}\n"
                if r.get("detections"):
                    results_md += f"🏷️ Objects: {', '.join(r['detections'])}\n\n"

            return results_md, gr.update(visible=False), gr.update(visible=False)

        elif result["status"] == "refining":
            question = result["question"]
            options = result["options"]
            options_md = f"### 🌳 Narrowing down... ({result['count']} remaining)\n\n"
            options_md += f"**{question}**\n"

            return (
                options_md,
                gr.update(visible=True, value=options_md),
                gr.update(visible=True, choices=options, value=None),
            )

    except Exception as e:
        return f"❌ Refinement failed: {str(e)}", gr.update(visible=False), gr.update(visible=False)


def generate_rag_answer(query: str) -> str:
    """Generate a RAG-based answer using retrieved contexts."""
    global current_results

    if pipeline is None or not current_results:
        return "❌ No search results to generate answer from. Search first!"

    try:
        contexts = [r.to_dict() for r in current_results[:15]]  # Top 15 as context
        answer = pipeline.gemini.generate_rag_answer(query, contexts)
        return f"## πŸ€– RAG Answer\n\n{answer}"
    except Exception as e:
        return f"❌ RAG generation failed: {str(e)}"


def get_timestamp_link(video_path, timestamp_sec):
    """Generate a clickable timestamp."""
    return f"Jump to {int(timestamp_sec)}s"


# ── Build Gradio Interface ──────────────────────────────────────────────────

def create_ui():
    """Create the full Gradio interface."""

    with gr.Blocks(
        title="🎬 Video Intelligence Platform",
    ) as app:

        gr.Markdown("""
        # 🎬 Video Intelligence Platform
        ### Akinator-style Video Search with RAG
        
        **Upload a video β†’ Index it β†’ Search with natural language β†’ Get exact timestamps**
        
        Supports: boolean queries ("red car AND person"), attribute search ("person in white clothes"), 
        and interactive tree-based refinement when too many results are found.
        
        ---
        """)

        # ── Setup Section ───────────────────────────────────────────────
        with gr.Row():
            with gr.Column(scale=2):
                api_key_input = gr.Textbox(
                    label="πŸ”‘ Gemini API Key",
                    type="password",
                    placeholder="Enter your Gemini API key...",
                    info="Get one free at https://aistudio.google.com/apikey",
                )
                init_btn = gr.Button("πŸš€ Initialize System", variant="primary")
                init_status = gr.Markdown("")

        init_btn.click(initialize_system, inputs=[api_key_input], outputs=[init_status])

        gr.Markdown("---")

        # ── Video Upload & Indexing ─────────────────────────────────────
        with gr.Row():
            with gr.Column(scale=1):
                video_input = gr.Video(label="πŸ“Ή Upload Video")
                caption_frequency = gr.Slider(
                    minimum=1, maximum=10, value=3, step=1,
                    label="Caption every Nth frame",
                    info="Lower = more detailed but slower (uses Gemini API calls)",
                )
                index_btn = gr.Button("πŸ”„ Index Video", variant="primary")

            with gr.Column(scale=1):
                index_status = gr.Markdown("Upload a video and click 'Index Video' to start.")
                video_display = gr.Video(label="πŸŽ₯ Indexed Video", interactive=False, visible=True)

        index_btn.click(
            index_video,
            inputs=[video_input, caption_frequency],
            outputs=[index_status, video_display],
        )

        gr.Markdown("---")

        # ── Search Section ──────────────────────────────────────────────
        with gr.Row():
            with gr.Column(scale=2):
                query_input = gr.Textbox(
                    label="πŸ” Search Query",
                    placeholder='Try: "person wearing white clothes", "red car AND bicycle", "outdoor night scene"',
                    lines=2,
                )
                with gr.Row():
                    search_btn = gr.Button("πŸ” Search", variant="primary")
                    rag_btn = gr.Button("πŸ€– Generate RAG Answer", variant="secondary")

        # ── Results Section ─────────────────────────────────────────────
        with gr.Row():
            with gr.Column(scale=2):
                results_display = gr.Markdown(
                    "Results will appear here after searching.",
                    elem_classes=["results-box"],
                )
                rag_answer = gr.Markdown("")

        # ── Akinator Refinement Section ─────────────────────────────────
        akinator_question = gr.Markdown("", visible=False)
        akinator_choices = gr.Radio(
            choices=[], label="Select an option to narrow down results",
            visible=False,
        )
        refine_btn = gr.Button("🌳 Refine", visible=False)

        search_btn.click(
            search_video,
            inputs=[query_input],
            outputs=[results_display, rag_answer, akinator_question, akinator_choices],
        )

        rag_btn.click(
            generate_rag_answer,
            inputs=[query_input],
            outputs=[rag_answer],
        )

        # ── Example Queries ─────────────────────────────────────────────
        gr.Markdown("---")
        gr.Markdown("### πŸ’‘ Example Queries")
        gr.Examples(
            examples=[
                ["person wearing white clothes"],
                ["red car"],
                ["person AND car"],
                ["dog OR cat"],
                ["outdoor scene at night"],
                ["short girl with a bag"],
                ["crowd of people walking"],
            ],
            inputs=[query_input],
        )

        # ── Architecture Info ───────────────────────────────────────────
        with gr.Accordion("πŸ—οΈ Architecture Details", open=False):
            gr.Markdown("""
            ### How it works:
            
            **Indexing Pipeline:**
            1. **Frame Extraction** β€” Extract frames at 1 FPS using OpenCV
            2. **Object Detection** β€” Grounding DINO detects objects with attributes (colors, clothing, sizes)
            3. **Visual Embeddings** β€” SigLIP2 embeds each frame into a 1152-dim vector
            4. **Captioning** β€” Gemini 2.0 Flash generates detailed captions per frame
            5. **Caption Embeddings** β€” Gemini text-embedding-004 embeds captions into 768-dim vectors
            6. **Storage** β€” SQLite (structured) + FAISS (vectors)
            
            **Search Pipeline:**
            1. **Query Decomposition** β€” Gemini splits boolean queries ("A AND B") into sub-queries
            2. **Multi-Channel Search:**
               - Visual: SigLIP2 text→frame similarity (FAISS)
               - Caption: Gemini embedding text→caption similarity (FAISS)
               - Detection: SQL structured search on object labels
            3. **Score Fusion** β€” Weighted merge across channels
            4. **Boolean Ops** β€” AND (timestamp intersection), OR (union)
            
            **Akinator Refinement:**
            - When too many results found, uses information-gain-based feature splitting
            - Asks discriminative questions (indoor/outdoor? day/night? etc.)
            - Each answer narrows results like a decision tree
            
            **RAG Generation:**
            - Retrieved contexts β†’ Gemini 2.0 Flash β†’ grounded answer with timestamp citations
            
            **Models Used:**
            | Component | Model |
            |---|---|
            | Frame Embeddings | SigLIP2 (google/siglip2-so400m-patch14-384) |
            | Object Detection | Grounding DINO (IDEA-Research/grounding-dino-tiny) |
            | Captioning | Gemini 2.0 Flash |
            | Text Embeddings | Gemini text-embedding-004 |
            | Query/RAG | Gemini 2.0 Flash |
            """)

    return app


def main():
    """Launch the application."""
    app = create_ui()
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
    )


if __name__ == "__main__":
    main()