File size: 6,584 Bytes
a805048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Draws assets/architecture.png and assets/workflow.png with matplotlib (no external tools)."""
from pathlib import Path

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt  # noqa: E402
from matplotlib.patches import FancyArrowPatch, FancyBboxPatch  # noqa: E402

ASSETS = Path(__file__).resolve().parents[1] / "assets"
ASSETS.mkdir(exist_ok=True)
INK, MUTED, SURF = "#0b0b0b", "#52514e", "#fcfcfb"
BLUE, ORANGE, AQUA, YELLOW = "#cde2fb", "#fbdccd", "#c9eedf", "#fbe7b5"
EDGE = {BLUE: "#2a78d6", ORANGE: "#eb6834", AQUA: "#1baf7a", YELLOW: "#c98500", "#eeeeea": "#8a8986"}


def box(ax, x, y, w, h, text, fill, size=9, bold=False):
    ax.add_patch(FancyBboxPatch((x, y), w, h, boxstyle="round,pad=0.02,rounding_size=0.08",
                                fc=fill, ec=EDGE.get(fill, "#8a8986"), lw=1.3))
    ax.text(x + w / 2, y + h / 2, text, ha="center", va="center", fontsize=size, color=INK,
            fontweight="bold" if bold else "normal", wrap=True)


def arrow(ax, x1, y1, x2, y2, color=MUTED, style="-|>", ls="-"):
    ax.add_patch(FancyArrowPatch((x1, y1), (x2, y2), arrowstyle=style, mutation_scale=12, color=color,
                                 lw=1.3, linestyle=ls, shrinkA=2, shrinkB=2))


def architecture():
    fig, ax = plt.subplots(figsize=(13, 7.4))
    ax.set_xlim(0, 13)
    ax.set_ylim(0, 7.4)
    ax.axis("off")
    fig.patch.set_facecolor(SURF)
    ax.text(0.2, 7.05, "EvidenceGuard AI - system architecture", fontsize=14, fontweight="bold", color=INK)

    # ingestion layer
    ax.text(0.2, 6.45, "Ingestion layer", fontsize=10, color=MUTED, fontweight="bold")
    box(ax, 0.2, 5.4, 1.9, 0.85, "PDF upload /\nsample_data", "#eeeeea")
    box(ax, 2.5, 5.4, 2.1, 0.85, "Text extraction\n(PyMuPDF / pypdf)", BLUE)
    box(ax, 5.0, 5.4, 2.1, 0.85, "Cleaning +\nmetadata (title, date)", BLUE)
    box(ax, 7.5, 5.4, 2.1, 0.85, "Sentence-aware\nchunking (70 words)", BLUE)
    box(ax, 10.0, 5.95, 2.8, 0.6, "Embeddings -> FAISS index", BLUE, 8.5)
    box(ax, 10.0, 5.2, 2.8, 0.6, "BM25 keyword index", BLUE, 8.5)
    for x in (2.1, 4.6, 7.1):
        arrow(ax, x, 5.82, x + 0.4, 5.82)
    arrow(ax, 9.6, 5.9, 10.0, 6.2)
    arrow(ax, 9.6, 5.75, 10.0, 5.5)

    # system A
    ax.text(0.2, 4.65, "System A - Baseline RAG", fontsize=10, color=MUTED, fontweight="bold")
    box(ax, 0.2, 3.75, 2.2, 0.7, "Question", "#eeeeea")
    box(ax, 2.8, 3.75, 2.4, 0.7, "Dense retrieval\n(top-5, FAISS)", BLUE)
    box(ax, 5.6, 3.75, 2.4, 0.7, "LLM answer\n+ citations", ORANGE)
    box(ax, 8.4, 3.75, 2.0, 0.7, "Answer\n(always)", "#eeeeea")
    arrow(ax, 2.4, 4.1, 2.8, 4.1)
    arrow(ax, 5.2, 4.1, 5.6, 4.1)
    arrow(ax, 8.0, 4.1, 8.4, 4.1)

    # system B
    ax.text(0.2, 3.15, "System B - EvidenceGuard RAG", fontsize=10, color=MUTED, fontweight="bold")
    y = 2.05
    steps = [("Hybrid retrieval\nFAISS + BM25 (RRF)", BLUE), ("Cross-encoder\nreranking", BLUE),
             ("Evidence\nsufficiency", AQUA), ("Conflict search +\ncontradiction (NLI\n+ numeric rules)", AQUA),
             ("Grounded LLM\ngeneration", ORANGE), ("Citation\nvalidation", AQUA), ("Confidence +\nabstention", YELLOW)]
    x = 0.2
    w = 1.68
    for i, (t, c) in enumerate(steps):
        box(ax, x, y, w, 0.95, t, c, 8.2)
        if i:
            arrow(ax, x - 0.14, y + 0.47, x, y + 0.47)
        x += w + 0.14
    box(ax, 0.2, 0.5, 3.6, 0.95, "ANSWERED  /  PARTIAL  /\nCONFLICT WARNING  /  ABSTAINED", YELLOW, 8.5, True)
    box(ax, 4.2, 0.5, 4.2, 0.95, "Explanation: why it answered, partially answered\nor refused (scores, thresholds, missing terms)", "#eeeeea", 8.3)
    box(ax, 8.8, 0.5, 4.0, 0.95, "Gradio UI: chat, evidence panel,\nside-by-side comparison, evaluation dashboard", "#eeeeea", 8.3)
    arrow(ax, x - 0.14 - w / 2, y, 2.0, 1.45)
    arrow(ax, 3.8, 0.97, 4.2, 0.97)
    arrow(ax, 8.4, 0.97, 8.8, 0.97)
    arrow(ax, 11.4, 5.2, 11.4, 4.45, ls="--")
    ax.text(11.5, 4.75, "shared indexes", fontsize=8, color=MUTED)
    ax.text(0.2, 0.12, "Blue = retrieval  ·  Green = verification (EvidenceGuard contribution)  ·  Orange = LLM  ·  "
            "Yellow = decision. Every model has an offline fallback (LSA embeddings, lexical reranker, rule verifier, "
            "extractive composer).", fontsize=8, color=MUTED)
    fig.savefig(ASSETS / "architecture.png", dpi=160, bbox_inches="tight", facecolor=SURF)
    plt.close(fig)


def workflow():
    fig, ax = plt.subplots(figsize=(8.6, 10.5))
    ax.set_xlim(0, 8.6)
    ax.set_ylim(0, 10.5)
    ax.axis("off")
    fig.patch.set_facecolor(SURF)
    ax.text(0.2, 10.15, "EvidenceGuard AI - decision workflow", fontsize=14, fontweight="bold", color=INK)
    cx, w, h = 1.0, 4.0, 0.62
    ys = [9.2, 8.25, 7.3, 6.35, 5.4, 4.45, 3.5, 2.55]
    labels = [("User question (+ follow-up context)", "#eeeeea"),
              ("Hybrid retrieval: FAISS + BM25, fused by RRF", BLUE),
              ("Cross-encoder reranking -> top-5 passages", BLUE),
              ("Evidence sufficiency: relevance & coverage", AQUA),
              ("Conflict-search retrieval + contradiction check", AQUA),
              ("Context-grounded generation (conflict-aware)", ORANGE),
              ("Citation validation of every sentence", AQUA),
              ("Confidence = 0.4 rel + 0.4 cov + 0.2 support", YELLOW)]
    for (t, c), y in zip(labels, ys):
        box(ax, cx, y, w, h, t, c, 8.8)
    for a, b in zip(ys, ys[1:]):
        arrow(ax, cx + w / 2, a, cx + w / 2, b + h)
    # side exits
    box(ax, 5.6, 6.35, 2.8, h, "ABSTAIN: insufficient\nevidence (+ missing terms)", YELLOW, 8)
    arrow(ax, cx + w, 6.35 + h / 2, 5.6, 6.35 + h / 2)
    ax.text(5.08, 6.88, "insufficient", fontsize=7.5, color=MUTED)
    box(ax, 5.6, 4.45, 2.8, h, "Conflict note: both versions,\ndates, newer document", "#eeeeea", 8)
    arrow(ax, 5.6, 4.45 + h / 2, cx + w, 4.45 + h / 2, ls="--")
    box(ax, 5.6, 3.5, 2.8, h, "Drop unsupported sentences;\nnone left -> ABSTAIN", YELLOW, 8)
    arrow(ax, cx + w, 3.5 + h / 2, 5.6, 3.5 + h / 2)
    outs = [("ANSWERED", 0.2), ("PARTIAL", 2.2), ("CONFLICT\nWARNING", 4.2), ("ABSTAINED", 6.2)]
    for t, x in outs:
        box(ax, x, 1.1, 1.8, 0.75, t, YELLOW, 8.5, True)
        arrow(ax, cx + w / 2, 2.55, x + 0.9, 1.85)
    box(ax, 0.2, 0.15, 7.8, 0.6, "Explanation + evidence panel (passages, scores, conflicts, citation checks) shown to the user",
        "#eeeeea", 8.5)
    fig.savefig(ASSETS / "workflow.png", dpi=160, bbox_inches="tight", facecolor=SURF)
    plt.close(fig)


if __name__ == "__main__":
    architecture()
    workflow()
    print("diagrams written to", ASSETS)