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
8eca49d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f7687e
bb04c5f
 
 
 
 
 
 
4f7687e
 
 
 
 
 
 
 
 
bb04c5f
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# main.py

import json
import os
import time
from functools import lru_cache
from urllib.parse import quote
import yaml
from fastapi import FastAPI, Request, Form, HTTPException, Query
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from evaluation.dataset_loader import DatasetLoader

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(BASE_DIR, "config.yaml")

app = FastAPI(title="Semantic Search Engine")

app.mount("/static", StaticFiles(directory=os.path.join(BASE_DIR, "static")), name="static")
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))

# ── load search engine once at startup ──────────────────────────────────────
ENGINE_ERROR = None


@lru_cache(maxsize=1)
def get_engine():
    global ENGINE_ERROR
    try:
        from searcher.search_engine import SearchEngine
        ENGINE_ERROR = None
        return SearchEngine(CONFIG_PATH)
    except Exception as e:
        ENGINE_ERROR = str(e)
        print(f"[Startup] Search engine unavailable: {e}")
        return None


def resolve_path(path: str) -> str:
    if os.path.isabs(path):
        return path
    return os.path.join(BASE_DIR, path)


def get_config() -> dict:
    with open(CONFIG_PATH, "r", encoding="utf-8") as f:
        return yaml.safe_load(f)


# ── load dataset queries at startup ─────────────────────────────────────────
# These are the actual queries from SciFact and NFCorpus
# We use them to show "which dataset queries matched your search"

def load_dataset_queries() -> dict:
    """
    Load all queries from SciFact and NFCorpus at startup.

    Returns:
        dict β€” {
            "scifact":  {query_id: query_text, ...},
            "nfcorpus": {query_id: query_text, ...},
        }
    """
    all_queries = {}

    config = get_config()
    watch_paths = config.get("watch_paths", [])
    datasets = {
        "scifact":  resolve_path(watch_paths[0]) if len(watch_paths) > 0 else resolve_path("data/scifact"),
        "nfcorpus": resolve_path(watch_paths[1]) if len(watch_paths) > 1 else resolve_path("data/nfcorpus"),
    }

    for name, path in datasets.items():
        if os.path.exists(path):
            try:
                loader             = DatasetLoader(path)
                all_queries[name]  = loader.load_queries()
                print(f"[Startup] Loaded {len(all_queries[name])} queries from {name}")
            except Exception as e:
                print(f"[Startup] Could not load {name} queries: {e}")
                all_queries[name] = {}
        else:
            print(f"[Startup] Dataset path not found: {path}")
            all_queries[name] = {}

    return all_queries


# load once at startup β€” available globally
DATASET_QUERIES = {}


@lru_cache(maxsize=8)
def load_dataset_corpus(dataset_name: str) -> dict:
    config = get_config()
    watch_paths = config.get("watch_paths", [])
    datasets = {
        "scifact":  resolve_path(watch_paths[0]) if len(watch_paths) > 0 else resolve_path("data/scifact"),
        "nfcorpus": resolve_path(watch_paths[1]) if len(watch_paths) > 1 else resolve_path("data/nfcorpus"),
    }

    dataset_path = datasets.get(dataset_name)
    if not dataset_path or not os.path.exists(dataset_path):
        return {}

    return DatasetLoader(dataset_path).load_corpus()


@app.on_event("startup")
async def startup_event():
    refresh_dataset_queries()
    ensure_index_ready()
    get_engine.cache_clear()
    get_engine()


# ── helpers ──────────────────────────────────────────────────────────────────

def load_eval_results() -> dict:
    results_dir = resolve_path("results")
    candidate_files = [
        os.path.join(results_dir, "eval_all.json"),
        os.path.join(results_dir, "eval_report.json"),
    ]

    for path in candidate_files:
        if os.path.exists(path):
            with open(path, "r", encoding="utf-8") as f:
                data = json.load(f)

            if path.endswith("eval_all.json"):
                return data

            # Single-dataset reports use mode->metrics shape. Wrap them so the
            # dashboard can render them like the combined eval output.
            if isinstance(data, dict) and any(
                key in data for key in ("full", "dense", "sparse", "hybrid")
            ):
                return {"report": data}

    if os.path.isdir(results_dir):
        merged = {}
        for filename in sorted(os.listdir(results_dir)):
            if not (filename.startswith("eval_") and filename.endswith(".json")):
                continue
            if filename in {"eval_all.json", "eval_report.json"}:
                continue

            dataset_name = filename[len("eval_"):-len(".json")]
            path = os.path.join(results_dir, filename)

            try:
                with open(path, "r", encoding="utf-8") as f:
                    data = json.load(f)
            except Exception as e:
                print(f"[Dashboard] Could not load {path}: {e}")
                continue

            if isinstance(data, dict):
                merged[dataset_name] = data

        if merged:
            print(f"[Dashboard] Loaded evaluation data from {len(merged)} per-dataset report(s)")
            return merged

    print(f"[Dashboard] No evaluation results found in {results_dir}")
    return {}


def refresh_dataset_queries() -> None:
    global DATASET_QUERIES
    DATASET_QUERIES = load_dataset_queries()


def ensure_index_ready() -> None:
    config = get_config()
    data_dir = resolve_path(config["data_dir"])
    faiss_path = os.path.join(data_dir, "index.faiss")

    if os.path.exists(faiss_path):
        print(f"[Startup] Existing FAISS index found at {faiss_path}")
        return

    watch_paths = [resolve_path(path) for path in config.get("watch_paths", [])]
    available_paths = [path for path in watch_paths if os.path.exists(path)]

    if not available_paths:
        print("[Startup] Skipping indexing because no configured dataset paths are available.")
        return

    print("[Startup] No FAISS index found. Running indexing pipeline...")
    from indexer.pipeline import IndexingPipeline

    pipeline = IndexingPipeline(CONFIG_PATH)
    pipeline.run()

    if os.path.exists(faiss_path):
        print(f"[Startup] Index build complete: {faiss_path}")
    else:
        print(f"[Startup] Index build did not produce {faiss_path}")


def extract_doc_id(filepath: str) -> str:
    if "://" in filepath:
        return filepath.split("://", 1)[1]
    return filepath


def get_dataset_from_filepath(filepath: str) -> str:
    if "scifact://"  in filepath: return "scifact"
    if "nfcorpus://" in filepath: return "nfcorpus"
    return "filesystem"


def get_file_icon(filepath: str) -> str:
    if "scifact://"  in filepath: return "πŸ”¬"
    if "nfcorpus://" in filepath: return "πŸ₯"
    ext   = filepath.lower().split(".")[-1] if "." in filepath else ""
    icons = {
        "pdf": "πŸ“„", "docx": "πŸ“", "txt": "πŸ“ƒ",
        "pptx": "πŸ“Š", "xlsx": "πŸ“‹", "py": "🐍",
    }
    return icons.get(ext, "πŸ“„")


def build_open_url(filepath: str) -> str:
    dataset = get_dataset_from_filepath(filepath)
    if dataset in {"scifact", "nfcorpus"}:
        doc_id = extract_doc_id(filepath)
        return f"/document?dataset={quote(dataset)}&doc_id={quote(doc_id)}"
    return f"/document?path={quote(filepath)}"


def find_matching_dataset_queries(
    user_query: str,
    top_results: list,
) -> list:
    """
    Find which dataset queries are semantically related to what the user typed.

    Strategy β€” two passes:
        1. Exact / substring match  β€” query text contains user words
        2. Doc-based match          β€” if a result doc came from dataset X,
                                      show the queries that reference that doc
                                      from the qrels (loaded separately)

    We use simple word overlap here (no extra model call needed).

    Returns:
        list of dicts β€” [
            {
                "query_id":   "1234",
                "query_text": "Does vitamin D cause cancer?",
                "dataset":    "scifact",
                "match_type": "text"   or "doc"
            },
            ...
        ]
    """
    matched   = []
    seen_ids  = set()

    # words from user query β€” lowercase, skip short words
    user_words = set(
        w.lower() for w in user_query.split()
        if len(w) > 3
    )

    # Pass 1 β€” text overlap match
    # check every dataset query for word overlap with user query
    for dataset_name, queries in DATASET_QUERIES.items():
        for qid, qtext in queries.items():
            q_words = set(w.lower() for w in qtext.split() if len(w) > 3)
            overlap = user_words & q_words

            # need at least 1 word overlap
            if overlap and qid not in seen_ids:
                matched.append({
                    "query_id":   qid,
                    "query_text": qtext,
                    "dataset":    dataset_name,
                    "match_type": "text",
                    "overlap":    len(overlap),
                })
                seen_ids.add(qid)

    # sort by overlap count β€” most overlapping queries first
    matched.sort(key=lambda x: x["overlap"], reverse=True)

    # return top 8 matched queries max
    return matched[:8]


# ── routes ───────────────────────────────────────────────────────────────────

@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
    return templates.TemplateResponse(request, "index.html", {
        "request":          request,
        "scifact_count":    len(DATASET_QUERIES.get("scifact",  {})),
        "nfcorpus_count":   len(DATASET_QUERIES.get("nfcorpus", {})),
        "error":            ENGINE_ERROR,
    })


@app.post("/search", response_class=HTMLResponse)
async def search(
    request: Request,
    query:   str = Form(...),
    top_k:   int = Form(10),
    mode:    str = Form("full"),
):
    if not query.strip():
        return templates.TemplateResponse(request, "index.html", {
            "request":        request,
            "error":          "Please enter a search query.",
            "scifact_count":  len(DATASET_QUERIES.get("scifact", {})),
            "nfcorpus_count": len(DATASET_QUERIES.get("nfcorpus", {})),
        })

    engine = get_engine()
    if engine is None:
        return templates.TemplateResponse(request, "index.html", {
            "request":        request,
            "error":          (
                "Search is not ready yet. The semantic index is still missing or failed to build. "
                f"Startup details: {ENGINE_ERROR}"
            ),
            "scifact_count":  len(DATASET_QUERIES.get("scifact", {})),
            "nfcorpus_count": len(DATASET_QUERIES.get("nfcorpus", {})),
        })

    t0      = time.time()
    output  = engine.search(query.strip(), top_k=top_k)
    elapsed = round(time.time() - t0, 3)

    # format search results
    results = []
    for r in output.get("results", []):
        filepath = r.get("filepath", "")
        doc_id   = extract_doc_id(filepath)
        score    = r.get("rerank_score", r.get("rrf_score", r.get("dense_score", 0)))
        snippet  = r.get("chunk_text", r.get("text", "No preview available."))

        if len(snippet) > 200:
            snippet = snippet[:200].rsplit(" ", 1)[0] + "..."

        dataset = get_dataset_from_filepath(filepath)

        results.append({
            "doc_id":   doc_id,
            "filepath": filepath,
            "open_url": build_open_url(filepath),
            "score":    round(float(score), 4),
            "snippet":  snippet,
            "icon":     get_file_icon(filepath),
            "dataset":  dataset,
        })

    # find matching dataset queries
    matched_queries = find_matching_dataset_queries(query.strip(), results)

    # group matched queries by dataset for display
    matched_scifact  = [q for q in matched_queries if q["dataset"] == "scifact"]
    matched_nfcorpus = [q for q in matched_queries if q["dataset"] == "nfcorpus"]

    return templates.TemplateResponse(request, "results.html", {
        "request":          request,
        "query":            query,
        "results":          results,
        "total":            len(results),
        "elapsed":          elapsed,
        "mode":             mode,
        "top_k":            top_k,
        "matched_scifact":  matched_scifact,
        "matched_nfcorpus": matched_nfcorpus,
        "scifact_matches":  matched_scifact,
        "nfcorpus_matches": matched_nfcorpus,
        "total_matched":    len(matched_queries),
    })


@app.get("/dashboard", response_class=HTMLResponse)
async def dashboard(request: Request):
    eval_data = load_eval_results()

    datasets = []
    for dataset_name, mode_results in eval_data.items():
        full = mode_results.get("full", {})
        datasets.append({
            "name":      dataset_name,
            "ndcg":      full.get("NDCG@10",    0.0),
            "mrr":       full.get("MRR",         0.0),
            "map":       full.get("MAP@100",     0.0),
            "recall":    full.get("Recall@100",  0.0),
            "precision": full.get("P@10",        0.0),
            "queries":   full.get("num_queries", 0),
            "modes":     mode_results,
        })

    return templates.TemplateResponse(request, "dashboard.html", {
        "request":  request,
        "datasets": datasets,
    })


@app.get("/document", response_class=HTMLResponse)
async def document(
    request: Request,
    dataset: str | None = Query(default=None),
    doc_id: str | None = Query(default=None),
    path: str | None = Query(default=None),
):
    if dataset and doc_id:
        corpus = load_dataset_corpus(dataset)
        doc = corpus.get(doc_id)
        if doc is None:
            raise HTTPException(status_code=404, detail="Document not found in dataset corpus.")

        title = doc.get("title") or doc_id
        text = doc.get("text") or "No document text available."
        return templates.TemplateResponse(request, "document.html", {
            "request": request,
            "title": title,
            "doc_id": doc_id,
            "source": dataset,
            "filepath": f"{dataset}://{doc_id}",
            "text": text,
            "is_dataset": True,
        })

    if path:
        from indexer.extractor import Extractor

        resolved = resolve_path(path)
        if not os.path.exists(resolved):
            raise HTTPException(status_code=404, detail="File path no longer exists on disk.")

        text = Extractor().extract(resolved) or "No text could be extracted from this file."
        return templates.TemplateResponse(request, "document.html", {
            "request": request,
            "title": os.path.basename(resolved),
            "doc_id": os.path.basename(resolved),
            "source": "filesystem",
            "filepath": resolved,
            "text": text,
            "is_dataset": False,
        })

    raise HTTPException(status_code=400, detail="Provide either dataset/doc_id or path.")


@app.get("/health")
async def health():
    engine = get_engine()
    return {
        "status": "ok" if engine is not None else "degraded",
        "engine_ready": engine is not None,
        "engine_error": ENGINE_ERROR,
    }


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)




    # uvicorn main:app --reload --host 0.0.0.0 --port 8000