File size: 27,620 Bytes
7959ec4
76955d2
 
7959ec4
76955d2
 
 
7959ec4
76955d2
 
 
126d30e
 
 
76955d2
7959ec4
 
 
 
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7959ec4
 
76955d2
7959ec4
 
 
 
 
 
 
 
 
76955d2
 
 
7959ec4
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183a996
76955d2
 
 
fcbf394
b0f7495
 
 
 
fcbf394
b0f7495
fcbf394
 
 
 
 
 
76955d2
 
126d30e
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126d30e
 
 
 
 
 
 
 
76955d2
 
 
 
fcbf394
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183a996
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7959ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126d30e
7959ec4
 
126d30e
 
7959ec4
 
 
126d30e
 
7959ec4
 
126d30e
 
7959ec4
 
 
126d30e
7959ec4
 
126d30e
 
7959ec4
 
 
 
 
 
 
 
 
 
 
126d30e
7959ec4
 
126d30e
7959ec4
 
 
 
126d30e
7959ec4
 
 
 
 
 
126d30e
7959ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c701f8c
126d30e
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7959ec4
 
126d30e
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7959ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76955d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7959ec4
 
76955d2
 
183a996
76955d2
183a996
 
 
 
 
 
 
 
 
 
 
 
76955d2
 
 
 
 
 
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import json
import re
import time
from pathlib import Path

import gradio as gr
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

from rag_chain import (
    retrieve_docs, retrieve_corrective, stream_llm, query, add_to_kb,
    vectorstore_exists, get_all_philosophers, get_kb_stats, get_umap_data,
    ABSTAIN_MESSAGE,
)
from config import (
    LLM_OPTIONS, DEFAULT_LLM, EMBEDDING_OPTIONS, DEFAULT_EMBEDDING,
    USE_RERANKER, RERANKER_MODEL,
)

# ---------------------------------------------------------------------------
# Display helpers
# ---------------------------------------------------------------------------

_PROVIDER_COLOR = {
    "Google": "#4285F4",
    "Groq":   "#FF4B36",
    "OpenRouter": "#6366F1",
}

_COMPARE_DEFAULT_B = "Llama 4 Scout 17B  [Groq]"

_THINK_STYLE = (
    "color:var(--body-text-color-subdued);font-size:0.88em;"
    "border-left:3px solid var(--border-color-primary);padding-left:12px;margin:6px 0"
)
_SUMMARY_STYLE = (
    "cursor:pointer;color:var(--body-text-color-subdued);"
    "font-style:italic;user-select:none"
)


def _format_think_blocks(text: str) -> str:
    """Render <think>…</think> as collapsible, muted sections.

    Mid-stream (</think> not yet seen): open <details> showing live reasoning.
    Complete block: closed <details> with 'click to expand' label.
    """
    if "<think>" not in text:
        return text

    if "</think>" not in text:
        # Partial β€” think block still streaming
        idx = text.index("<think>")
        pre, thinking = text[:idx], text[idx + 7:]
        return (
            pre
            + f'<details open><summary style="{_SUMMARY_STYLE}">πŸ€” Thinking…</summary>'
            + f'<div style="{_THINK_STYLE}">{thinking}</div></details>'
        )

    def _wrap(m: re.Match) -> str:
        content = m.group(1).strip()
        return (
            f'<details><summary style="{_SUMMARY_STYLE}">'
            "πŸ€” Chain of thought (click to expand)</summary>"
            f'<div style="{_THINK_STYLE}">{content}</div></details>\n\n'
        )

    return re.sub(r"<think>(.*?)</think>", _wrap, text, flags=re.DOTALL)


def _score_bar(score: float, width: int = 10) -> str:
    filled = max(0, min(width, round(score * width)))
    return "β–ˆ" * filled + "β–‘" * (width - filled)


def _format_sources(docs: list, scores: list[float]) -> str:
    if not docs:
        return ""
    seen: set = set()
    lines: list[str] = []
    for doc, score in zip(docs, scores):
        key = doc.metadata.get("source", "Unknown source")
        if key not in seen:
            seen.add(key)
            tag = f"`{score:.2f}` " if score >= 0 else "`BM25` "
            lines.append(f"- {tag}{key}")
    return "\n\n---\n**Sources:**\n" + "\n".join(lines)


def _format_retrieved_chunks(docs: list, scores: list[float]) -> str:
    if not docs:
        return "_No chunks retrieved._"

    pos_scores = [s for s in scores if s >= 0]
    avg = sum(pos_scores) / len(pos_scores) if pos_scores else 0.0
    has_bm25 = any(s < 0 for s in scores)
    if USE_RERANKER:
        method = "Hybrid (RRF) β†’ Cross-Encoder Rerank"
        score_label = "avg relevance"
    elif has_bm25:
        method = "Hybrid BM25 + Semantic"
        score_label = "avg similarity"
    else:
        method = "Semantic"
        score_label = "avg similarity"

    lines = [
        f"**{len(docs)} chunks** &nbsp;Β·&nbsp; {method}"
        f" &nbsp;Β·&nbsp; {score_label}: **{avg:.3f}**\n"
    ]
    for i, (doc, score) in enumerate(zip(docs, scores), 1):
        phil  = doc.metadata.get("philosopher", "?")
        title = doc.metadata.get("title", "?")
        if score >= 0:
            tag = f"`{score:.3f}` {_score_bar(score)}"
        else:
            tag = "`BM25 ` ──────────"
        text = doc.page_content[:200].replace("\n", " ").strip()
        lines.append(
            f"**{i}.** {tag} &nbsp; *{phil}* Β· {title}  \n"
            f"&nbsp;&nbsp;&nbsp;&nbsp;*\"{text}...\"*\n"
        )
    return "\n".join(lines)


def _format_metrics(
    retrieve_s: float, llm_s: float, n_docs: int, n_sources: int
) -> str:
    return (
        f"⏱ &nbsp;Retrieval **{retrieve_s:.2f}s** &nbsp;·&nbsp; "
        f"LLM **{llm_s:.2f}s** &nbsp;Β·&nbsp; "
        f"Total **{retrieve_s + llm_s:.2f}s** &nbsp;Β·&nbsp; "
        f"**{n_docs}** chunks from **{n_sources}** source(s)"
    )


def _kb_markdown() -> str:
    stats = get_kb_stats()
    if not stats["total"]:
        return "_Knowledge base is empty._"
    lines = []
    for phil in sorted(stats["sources"]):
        lines.append(f"**{phil}**")
        for title in sorted(stats["sources"][phil]):
            lines.append(f"&nbsp;&nbsp;- *{title}*")
    lines.append(f"\n_{stats['total']:,} total chunks_")
    return "\n\n".join(lines)


# ---------------------------------------------------------------------------
# Event handlers
# ---------------------------------------------------------------------------

def respond_stream(message: str, history: list, philosopher: str, llm_label: str):
    if not message.strip():
        yield history, "", gr.update(), gr.update()
        return

    if not vectorstore_exists():
        err = "⏳ Knowledge base is still being built on first run (~10 min). Please wait and try again."
        yield history + [{"role": "assistant", "content": err}], "", gr.update(), gr.update()
        return

    # β€” Build retrieval query β€”
    # Always enrich with the previous user message when there is history.
    # Follow-up questions ("bahas lebih lanjut", "how does this relate…") contain
    # pronouns / references that are meaningless without context; even standalone
    # questions benefit from the extra topic signal staying in the same thread.
    retrieval_query = message
    if history:
        last_user = next(
            (t["content"] for t in reversed(history) if t["role"] == "user"), ""
        )
        if last_user:
            retrieval_query = f"{last_user} {message}"

    # β€” Retrieval (fast, happens before streaming) β€”
    t0 = time.perf_counter()
    docs, scores, confidence = retrieve_corrective(retrieval_query, philosopher)
    retrieve_time = time.perf_counter() - t0
    context_str = "\n\n".join(d.page_content for d in docs)

    chunks_md = _format_retrieved_chunks(docs, scores)

    history = history + [
        {"role": "user", "content": message},
        {
            "role": "assistant",
            "content": (
                "<em style='color:var(--body-text-color-subdued)'>"
                "⏳ Retrieving context and generating response…"
                "</em>"
            ),
        },
    ]
    # Show user bubble + loading message immediately
    yield history, "", gr.update(value=chunks_md), gr.update()

    # Corrective RAG β€” abstain when retrieval confidence is too low (off-corpus
    # question) instead of hallucinating from weak context.
    if confidence == "low":
        history[-1]["content"] = ABSTAIN_MESSAGE
        metrics_md = _format_metrics(retrieve_time, 0.0, len(docs), 0)
        yield history, "", gr.update(value=chunks_md), gr.update(value=metrics_md)
        return

    provider, model_id = LLM_OPTIONS.get(llm_label, LLM_OPTIONS[DEFAULT_LLM])
    t1 = time.perf_counter()
    full_response = ""
    try:
        for text_chunk in stream_llm(provider, model_id, context_str, message, history=history[:-2]):
            full_response += text_chunk
            history[-1]["content"] = _format_think_blocks(full_response)
            yield history, "", gr.update(value=chunks_md), gr.update()

        llm_time = time.perf_counter() - t1
        unique_sources = len({d.metadata.get("source") for d in docs})
        metrics_md = _format_metrics(retrieve_time, llm_time, len(docs), unique_sources)

        history[-1]["content"] = (
            _format_think_blocks(full_response) + _format_sources(docs, scores)
        )
        yield history, "", gr.update(value=chunks_md), gr.update(value=metrics_md)

    except Exception as exc:
        history[-1]["content"] = f"⚠️ **Error:** {exc}"
        yield history, "", gr.update(value=chunks_md), gr.update()


def compare_respond(message: str, philosopher: str, llm_a: str, llm_b: str):
    if not message.strip():
        return "Enter a question above.", "", "Enter a question above.", ""
    if not vectorstore_exists():
        msg = "⏳ Knowledge base is still being built on first run (~10 min). Please wait and try again."
        return msg, "", msg, ""

    def _run(llm_label: str) -> tuple[str, str]:
        t0 = time.perf_counter()
        result = query(message, philosopher, llm_label)
        elapsed = time.perf_counter() - t0
        n_src = len({d.metadata.get("source") for d in result["context"]})
        sem_scores = [s for s in result["scores"] if s >= 0]
        avg = sum(sem_scores) / len(sem_scores) if sem_scores else 0.0
        metrics = (
            f"⏱ **{elapsed:.2f}s** &nbsp;·&nbsp; "
            f"**{len(result['context'])}** chunks from **{n_src}** source(s)"
            f" &nbsp;Β·&nbsp; avg similarity **{avg:.3f}**"
        )
        return result["answer"], metrics

    ans_a, met_a = _run(llm_a)
    ans_b, met_b = _run(llm_b)
    return ans_a, met_a, ans_b, met_b


def upload_source(file, author: str, title: str):
    if file is None:
        return gr.update(value="Please upload a file first."), gr.update()
    if not author.strip() or not title.strip():
        return gr.update(value="Please fill in both Author and Title."), gr.update()
    try:
        n = add_to_kb(file, author.strip(), title.strip())
        msg = f"Added {n:,} chunks from *{title}* by {author}."
    except Exception as e:
        msg = f"Error: {e}"
    return (
        gr.update(value=msg),
        gr.update(choices=get_all_philosophers(), value="All"),
    )


def refresh_kb():
    return gr.update(value=_kb_markdown())


def build_umap_plot():
    data = get_umap_data()
    if data is None:
        return None
    df = pd.DataFrame(data)
    fig = px.scatter(
        df, x="x", y="y",
        color="philosopher",
        hover_data={"title": True, "preview": True, "x": False, "y": False},
        title="Knowledge Base β€” Semantic Embedding Space (UMAP 2D)",
        labels={"x": "UMAP-1", "y": "UMAP-2"},
        opacity=0.75,
        template="plotly_dark",
        color_discrete_sequence=px.colors.qualitative.Bold,
    )
    fig.update_traces(marker=dict(size=5))
    fig.update_layout(
        height=540,
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
        title_font=dict(size=14),
        font=dict(color="rgba(220,220,220,0.9)"),
        legend=dict(
            title_text="",
            yanchor="top", y=0.99, xanchor="left", x=0.01,
            bgcolor="rgba(20,20,20,0.5)",
            bordercolor="rgba(255,255,255,0.12)",
            borderwidth=1,
        ),
        xaxis=dict(gridcolor="rgba(255,255,255,0.07)", zeroline=False),
        yaxis=dict(gridcolor="rgba(255,255,255,0.07)", zeroline=False),
        margin=dict(l=40, r=20, t=48, b=36),
    )
    return fig


# ---------------------------------------------------------------------------
# RAGAS evaluation results
# ---------------------------------------------------------------------------

_EVAL_PATH = Path(__file__).parent / "eval_results.json"
_METRIC_LABELS = {
    "faithfulness": "Faithfulness",
    "answer_relevancy": "Answer Relevancy",
    "context_precision": "Context Precision",
    "context_recall": "Context Recall",
}
_METRIC_DESC = {
    "faithfulness": "Share of answer claims supported by retrieved context (anti-hallucination)",
    "answer_relevancy": "How directly the answer addresses the question",
    "context_precision": "Are the relevant chunks ranked near the top?",
    "context_recall": "Share of the reference answer covered by retrieved context",
}


def _load_eval() -> dict | None:
    if not _EVAL_PATH.exists():
        return None
    try:
        return json.loads(_EVAL_PATH.read_text(encoding="utf-8"))
    except Exception:
        return None


def build_eval_table() -> str:
    data = _load_eval()
    if data is None:
        return (
            "_No evaluation results yet. Run_ `python evaluate.py` _to generate "
            "`eval_results.json` (RAGAS metrics, ~12 min)._"
        )
    cfgs = list(data["configs"].keys())
    first, last = cfgs[0], cfgs[-1]
    meta = data.get("metadata", {})

    header = "| Metric | " + " | ".join(cfgs) + " | Ξ” (full) |"
    sep = "|---|" + "|".join([":---:"] * (len(cfgs) + 1)) + "|"
    lines = [
        f"**Evaluated with `{meta.get('framework', 'ragas')}`** &nbsp;Β·&nbsp; "
        f"{meta.get('n_questions', '?')} questions "
        f"&nbsp;Β·&nbsp; judge: `{meta.get('judge_model', '?')}`\n",
        header, sep,
    ]
    for m in _METRIC_LABELS:
        vals = " | ".join(f"{data['configs'][c].get(m, 0.0):.3f}" for c in cfgs)
        d = data.get("deltas", {}).get(m, data["configs"][last].get(m, 0.0) - data["configs"][first].get(m, 0.0))
        arrow = "🟒" if d > 0.005 else ("πŸ”΄" if d < -0.005 else "βšͺ")
        lines.append(
            f"| **{_METRIC_LABELS[m]}**<br><sub>{_METRIC_DESC[m]}</sub> "
            f"| {vals} | {arrow} {d:+.3f} |"
        )
    lines.append(
        f"\n_Ξ” = full pipeline vs. baseline. Generated {meta.get('generated_at', '?')} "
        "Β· metrics follow [RAGAS](https://docs.ragas.io) definitions (LLM-as-judge)._"
    )
    return "\n".join(lines)


def build_eval_chart():
    data = _load_eval()
    if data is None:
        return None
    cfgs = list(data["configs"].keys())
    metrics = list(_METRIC_LABELS.keys())
    labels = [_METRIC_LABELS[m] for m in metrics]
    colors = ["#6366F1", "#22C55E", "#F59E0B", "#EF4444"]

    fig = go.Figure()
    for i, cfg in enumerate(cfgs):
        fig.add_bar(
            name=cfg,
            x=labels,
            y=[data["configs"][cfg].get(m, 0.0) for m in metrics],
            marker_color=colors[i % len(colors)],
            text=[f"{data['configs'][cfg].get(m, 0.0):.2f}" for m in metrics],
            textposition="outside",
        )
    fig.update_layout(
        barmode="group",
        template="plotly_dark",
        title="Retrieval Quality across pipeline stages",
        title_font=dict(size=14),
        height=460,
        yaxis=dict(range=[0, 1.05], title="score", gridcolor="rgba(255,255,255,0.08)"),
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
        font=dict(color="rgba(220,220,220,0.9)"),
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
        margin=dict(l=50, r=20, t=70, b=40),
    )
    return fig


def refresh_eval():
    return gr.update(value=build_eval_table()), build_eval_chart()


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

EXAMPLE_QUESTIONS = [
    "What is Nietzsche's view on nihilism and the death of God?",
    "How does Schopenhauer view suffering and the will to live?",
    "What does Hume say about causality and the limits of reason?",
    "Can we have certain knowledge of the external world?",
    "Is morality objective or invented?",
    "Explain the concept of Eternal Return",
    "How does Marcus Aurelius advise dealing with suffering?",
    "What is Plato's ideal society in The Republic?",
    "Compare Schopenhauer and Nietzsche on the will",
    "What is Kant's categorical imperative?",
    "How does Mill justify utilitarianism?",
    "What does Epictetus say about what is in our control?",
]

CSS = """
footer { display: none !important; }
.section-label {
    font-size: 0.78rem; font-weight: 700; letter-spacing: 0.07em;
    text-transform: uppercase; color: var(--body-text-color-subdued);
    margin-bottom: 2px;
}
.metric-bar p { font-size: 0.82rem; color: var(--body-text-color-subdued); margin: 4px 0; }
.status-box textarea { font-size: 0.82rem !important; }

/* Fix double scrollbar: prevent inner message wrappers from scrolling */
.chatbot .overflow-y-auto { scrollbar-width: thin; scrollbar-color: var(--border-color-primary) transparent; }
.chatbot .message-wrap { overflow: visible !important; }
.chatbot .message-wrap > div { overflow: visible !important; max-height: none !important; }
/* Prevent long markdown lines from adding a horizontal inner scroll */
.chatbot .prose { overflow-x: hidden !important; overflow-wrap: break-word; word-break: break-word; }
"""

with gr.Blocks(title="Philosopher Chat") as demo:

    gr.Markdown(
        """
# πŸ“š Philosopher Chat
**RAG chatbot grounded in Western philosophical primary texts**

Hybrid retrieval + cross-encoder reranking + corrective abstention &nbsp;Β·&nbsp; Streaming
&nbsp;Β·&nbsp; Multi-provider routing &nbsp;Β·&nbsp; RAGAS-evaluated &nbsp;Β·&nbsp; 12 primary texts Β· ~5 700 chunks
        """
    )

    with gr.Tabs():

        # ── Tab 1 ─ Chat ─────────────────────────────────────────────────
        with gr.Tab("πŸ’¬ Chat"):
            with gr.Row(equal_height=False):

                # Left: chat area
                with gr.Column(scale=3):
                    chatbot_ui = gr.Chatbot(
                        height=480,
                        show_label=False,
                        placeholder="*Ask a philosophical question to get started...*",
                    )
                    msg_input = gr.Textbox(
                        placeholder="Ask a philosophical question…",
                        show_label=False,
                        autofocus=True,
                        submit_btn=True,
                    )
                    metrics_display = gr.Markdown(
                        value="", elem_classes="metric-bar"
                    )
                    with gr.Accordion("πŸ“„ Retrieved Chunks & Scores", open=False):
                        retrieved_display = gr.Markdown(
                            value="_Submit a question to see retrieved context._"
                        )
                    with gr.Accordion("πŸ’‘ Example Questions", open=False):
                        gr.Examples(
                            examples=[[q] for q in EXAMPLE_QUESTIONS],
                            inputs=[msg_input],
                            label=None,
                        )

                # Right: settings sidebar
                with gr.Column(scale=1, min_width=240):
                    with gr.Group():
                        gr.Markdown("**βš™οΈ Chat Settings**", elem_classes="section-label")
                        llm_dropdown = gr.Dropdown(
                            choices=list(LLM_OPTIONS.keys()),
                            value=DEFAULT_LLM,
                            label="LLM Model",
                        )
                        embedding_display = gr.Dropdown(
                            choices=list(EMBEDDING_OPTIONS.keys()),
                            value=DEFAULT_EMBEDDING,
                            label="Embedding Model",
                            info="Change requires rebuilding index (ingest.py)",
                            interactive=False,
                        )
                        philosopher_filter = gr.Dropdown(
                            choices=get_all_philosophers(),
                            value="All",
                            label="Filter by Philosopher",
                        )

                    with gr.Group():
                        gr.Markdown("**ℹ️ Stack**", elem_classes="section-label")
                        gr.Markdown(
                            "- Retrieval: **Hybrid (RRF) + Rerank**\n"
                            "- Reranker: **BGE-reranker-v2-m3**\n"
                            "- Guard: **Corrective RAG / abstention**\n"
                            "- Embeddings: **EmbeddingGemma-300M**\n"
                            "- Vector DB: **ChromaDB**\n"
                            "- Framework: **LangChain LCEL**\n"
                            "- UI: **Gradio 6**"
                        )

        # ── Tab 2 ─ Compare Models ───────────────────────────────────────
        with gr.Tab("βš–οΈ Compare Models"):
            gr.Markdown(
                "Run the same question through two models and compare quality, "
                "latency, and retrieval coverage side by side."
            )
            with gr.Row():
                compare_input = gr.Textbox(
                    label="Question",
                    placeholder="Ask a philosophical question…",
                    scale=4,
                )
                compare_philosopher = gr.Dropdown(
                    choices=get_all_philosophers(),
                    value="All",
                    label="Philosopher Filter",
                    scale=1,
                )
            compare_btn = gr.Button("β–Ά Compare", variant="primary")

            with gr.Row():
                with gr.Column():
                    model_a = gr.Dropdown(
                        choices=list(LLM_OPTIONS.keys()),
                        value=DEFAULT_LLM,
                        label="Model A",
                    )
                    response_a = gr.Markdown(label="Response A")
                    metrics_a  = gr.Markdown(elem_classes="metric-bar")

                with gr.Column():
                    model_b = gr.Dropdown(
                        choices=list(LLM_OPTIONS.keys()),
                        value=_COMPARE_DEFAULT_B,
                        label="Model B",
                    )
                    response_b = gr.Markdown(label="Response B")
                    metrics_b  = gr.Markdown(elem_classes="metric-bar")

        # ── Tab 3 ─ Knowledge Base ───────────────────────────────────────
        with gr.Tab("πŸ—ΊοΈ Knowledge Base"):
            with gr.Row(equal_height=False):

                # Left: UMAP visualization
                with gr.Column(scale=2):
                    gr.Markdown(
                        "**Semantic Embedding Space**  \n"
                        "Each point is one text chunk. Clusters indicate semantic similarity β€” "
                        "nearby chunks share philosophical themes regardless of source."
                    )
                    umap_plot = gr.Plot()
                    umap_btn  = gr.Button(
                        "Generate Embedding Visualization", variant="secondary"
                    )
                    gr.Markdown(
                        "_UMAP projects ~5,700 Γ— 768-dim embeddings to 2D. "
                        "First run takes ~1–2 min on CPU._"
                    )

                # Right: stats + upload
                with gr.Column(scale=1, min_width=280):
                    with gr.Group():
                        with gr.Row():
                            gr.Markdown(
                                "**πŸ“š Knowledge Base**", elem_classes="section-label"
                            )
                            refresh_kb_btn = gr.Button("↻", size="sm", min_width=32)
                        kb_display = gr.Markdown(_kb_markdown())

                    with gr.Group():
                        gr.Markdown(
                            "**πŸ“€ Add Source**", elem_classes="section-label"
                        )
                        file_upload = gr.File(
                            label="Upload PDF or TXT",
                            file_types=[".pdf", ".txt"],
                        )
                        with gr.Row():
                            author_input = gr.Textbox(label="Author", scale=1)
                            title_input  = gr.Textbox(label="Title",  scale=1)
                        upload_btn = gr.Button(
                            "Add to Knowledge Base", variant="secondary", size="sm"
                        )
                        upload_status = gr.Textbox(
                            show_label=False,
                            interactive=False,
                            placeholder="Upload status will appear here…",
                            elem_classes="status-box",
                        )

        # ── Tab 4 ─ Evaluation ───────────────────────────────────────────
        with gr.Tab("πŸ“Š Evaluation"):
            gr.Markdown(
                "### Does reranking actually help?\n"
                "The retrieval pipeline is measured with four **RAGAS** metrics "
                "over a curated question set with reference answers β€” once with the "
                "cross-encoder reranker **off** (hybrid baseline) and once **on**. "
                "This quantifies the impact of each retrieval component instead of "
                "guessing. _(Computed offline by_ `evaluate.py`_; an LLM acts as judge.)_"
            )
            with gr.Row(equal_height=False):
                with gr.Column(scale=1):
                    eval_table = gr.Markdown(build_eval_table())
                with gr.Column(scale=1):
                    eval_chart = gr.Plot(build_eval_chart())
            refresh_eval_btn = gr.Button("↻ Reload results", size="sm")
            gr.Markdown(
                "**Metric definitions** &nbsp;Β·&nbsp; "
                "**Faithfulness**: answer grounded in context (anti-hallucination) &nbsp;Β·&nbsp; "
                "**Answer Relevancy**: answer addresses the question &nbsp;Β·&nbsp; "
                "**Context Precision**: relevant chunks ranked high &nbsp;Β·&nbsp; "
                "**Context Recall**: reference answer covered by context."
            )

    # ── Event wiring ─────────────────────────────────────────────────────

    msg_input.submit(
        respond_stream,
        inputs=[msg_input, chatbot_ui, philosopher_filter, llm_dropdown],
        outputs=[chatbot_ui, msg_input, retrieved_display, metrics_display],
    )

    compare_btn.click(
        compare_respond,
        inputs=[compare_input, compare_philosopher, model_a, model_b],
        outputs=[response_a, metrics_a, response_b, metrics_b],
    )

    umap_btn.click(build_umap_plot, outputs=umap_plot)

    refresh_kb_btn.click(refresh_kb, outputs=kb_display)

    upload_btn.click(
        upload_source,
        inputs=[file_upload, author_input, title_input],
        outputs=[upload_status, philosopher_filter],
    ).then(refresh_kb, outputs=kb_display)

    refresh_eval_btn.click(refresh_eval, outputs=[eval_table, eval_chart])


def _auto_ingest() -> None:
    """Trigger background KB build on first Spaces run (non-blocking)."""
    if not vectorstore_exists():
        print("[startup] Vectorstore missing β€” starting background ingest (~10 min)…")
        import threading

        def _run() -> None:
            try:
                import ingest
                ingest.main()
                print("[startup] Ingest complete. Knowledge base is now ready.")
            except Exception as exc:
                print(f"[startup] Ingest failed: {exc}")

        threading.Thread(target=_run, daemon=True).start()


_auto_ingest()

if __name__ == "__main__":
    demo.launch(css=CSS)