File size: 9,140 Bytes
5805964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import sys
from pathlib import Path

from dotenv import load_dotenv

load_dotenv()

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)-8s | %(message)s",
    datefmt="%H:%M:%S",
    handlers=[logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger(__name__)


# ---------------------------------------------------------------------------
# Check 1 β€” Knowledge base exists and has chunks
# ---------------------------------------------------------------------------
def check_knowledge_base() -> bool:
    kb_path = Path("models/knowledge_base")
    if not kb_path.exists():
        log.error("  [FAIL] Knowledge base not found at %s", kb_path)
        return False

    try:
        import chromadb
        client     = chromadb.PersistentClient(path=str(kb_path))
        collection = client.get_collection("ebolawatch_kb")
        count      = collection.count()

        if count == 0:
            log.error("  [FAIL] Knowledge base is empty")
            return False

        log.info("  [PASS] Knowledge base: %d chunks stored", count)
        return True

    except Exception as e:
        log.error("  [FAIL] Knowledge base error: %s", e)
        return False


# ---------------------------------------------------------------------------
# Check 2 β€” ONNX model file exists and is valid size
# ---------------------------------------------------------------------------
def check_onnx_model() -> bool:
    onnx_path = Path("models/onnx/immuniwatch_classifier.onnx")
    if not onnx_path.exists():
        log.error("  [FAIL] ONNX model not found: %s", onnx_path)
        return False

    size_mb = onnx_path.stat().st_size / (1024 * 1024)
    log.info("  [PASS] ONNX model: %.1f MB", size_mb)
    return True


# ---------------------------------------------------------------------------
# Check 3 β€” Thresholds file correct
# ---------------------------------------------------------------------------
def check_thresholds() -> bool:
    import json
    path = Path("models/onnx/thresholds.json")
    if not path.exists():
        log.error("  [FAIL] thresholds.json not found")
        return False

    with open(path) as f:
        data = json.load(f)

    biases = data.get("class_biases", {})
    required = ["factual", "misinformation", "irrelevant"]
    for label in required:
        if label not in biases:
            log.error("  [FAIL] Missing bias for label: %s", label)
            return False

    log.info("  [PASS] Thresholds: factual=%.1f misinfo=%.1f irrelevant=%.1f",
             biases["factual"], biases["misinformation"], biases["irrelevant"])
    return True


# ---------------------------------------------------------------------------
# Check 4 β€” System design constants
# ---------------------------------------------------------------------------
def check_system_design_constants() -> bool:
    passed = True

    from src.ingestion.deduplication import EXACT_TTL_S, JACCARD_THRESHOLD
    if JACCARD_THRESHOLD != 0.85:
        log.error("  [FAIL] JACCARD_THRESHOLD should be 0.85, got %s", JACCARD_THRESHOLD)
        passed = False
    else:
        log.info("  [PASS] Dedup Jaccard threshold: 0.85 (Section 4.3)")

    if EXACT_TTL_S != 86400:
        log.error("  [FAIL] EXACT_TTL_S should be 86400, got %s", EXACT_TTL_S)
        passed = False
    else:
        log.info("  [PASS] Dedup TTL: 24 hours (Section 4.3)")

    from src.intelligence.rag import SIMILARITY_THRESHOLD, TOP_K
    if TOP_K != 5:
        log.error("  [FAIL] TOP_K should be 5, got %s", TOP_K)
        passed = False
    else:
        log.info("  [PASS] RAG top-K: 5 (Section 5.3)")

    if SIMILARITY_THRESHOLD != 0.72:
        log.error("  [FAIL] SIMILARITY_THRESHOLD should be 0.72, got %s", SIMILARITY_THRESHOLD)
        passed = False
    else:
        log.info("  [PASS] RAG similarity threshold: 0.72 (Section 5.2)")

    from src.intelligence.counter import (
        LONG_MAX_WORDS,
        MEDIUM_MAX_WORDS,
        SHORT_MAX_CHARS,
    )
    if SHORT_MAX_CHARS != 280:
        log.error("  [FAIL] SHORT_MAX_CHARS should be 280, got %s", SHORT_MAX_CHARS)
        passed = False
    else:
        log.info("  [PASS] Counter SHORT: ≀280 chars (Section 6.5)")

    if MEDIUM_MAX_WORDS != 200:
        log.error("  [FAIL] MEDIUM_MAX_WORDS should be 200, got %s", MEDIUM_MAX_WORDS)
        passed = False
    else:
        log.info("  [PASS] Counter MEDIUM: ≀200 words (Section 6.5)")

    if LONG_MAX_WORDS != 500:
        log.error("  [FAIL] LONG_MAX_WORDS should be 500, got %s", LONG_MAX_WORDS)
        passed = False
    else:
        log.info("  [PASS] Counter LONG: ≀500 words (Section 6.5)")

    return passed


# ---------------------------------------------------------------------------
# Check 5 β€” Counter-response format compliance (no API calls)
# ---------------------------------------------------------------------------
def check_counter_format_compliance() -> bool:
    from src.intelligence.counter import (
        LONG_MAX_WORDS,
        MEDIUM_MAX_WORDS,
        SHORT_MAX_CHARS,
        _enforce_short,
        _enforce_word_limit,
    )

    passed = True

    # Test SHORT limit
    long_text = "Ebola dey dangerous for all pikin " * 20
    short = _enforce_short(long_text)
    if len(short) <= SHORT_MAX_CHARS:
        log.info("  [PASS] SHORT format enforced: %d chars ≀ 280", len(short))
    else:
        log.error("  [FAIL] SHORT format exceeded: %d chars", len(short))
        passed = False

    # Test MEDIUM limit
    medium = _enforce_word_limit(long_text, MEDIUM_MAX_WORDS)
    if len(medium.split()) <= MEDIUM_MAX_WORDS + 1:
        log.info("  [PASS] MEDIUM format enforced: %d words ≀ 200", len(medium.split()))
    else:
        log.error("  [FAIL] MEDIUM format exceeded: %d words", len(medium.split()))
        passed = False

    # Test LONG limit
    long = _enforce_word_limit(long_text, LONG_MAX_WORDS)
    if len(long.split()) <= LONG_MAX_WORDS + 1:
        log.info("  [PASS] LONG format enforced: %d words ≀ 500", len(long.split()))
    else:
        log.error("  [FAIL] LONG format exceeded: %d words", len(long.split()))
        passed = False

    return passed


# ---------------------------------------------------------------------------
# Check 6 β€” Required files exist
# ---------------------------------------------------------------------------
def check_required_files() -> bool:
    required = [
        "models/onnx/immuniwatch_classifier.onnx",
        "models/onnx/thresholds.json",
        "models/onnx/model_config.json",
        "src/models/classifier.py",
        "src/api/main.py",
        "src/api/routes.py",
        "src/api/schemas.py",
        "src/ingestion/worker.py",
        "src/ingestion/deduplication.py",
        "src/ingestion/connectors/base.py",
        "src/ingestion/connectors/youtube.py",
        "src/ingestion/connectors/sociavault.py",
        "src/ingestion/connectors/bluesky.py",
        "src/intelligence/ingestion.py",
        "src/intelligence/rag.py",
        "src/intelligence/counter.py",
        "src/intelligence/evaluation.py",
        "docker-compose.yml",
    ]

    all_found = True
    for path in required:
        if Path(path).exists():
            log.info("  [PASS] %s", path)
        else:
            log.error("  [FAIL] MISSING: %s", path)
            all_found = False

    return all_found


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def run_evaluation() -> None:
    log.info("=" * 55)
    log.info("EbolaWatch β€” Lightweight Evaluation")
    log.info("No heavy model loading β€” safe on CPU")
    log.info("=" * 55)

    checks = [
        ("Knowledge Base",              check_knowledge_base),
        ("ONNX Model",                  check_onnx_model),
        ("Thresholds",                  check_thresholds),
        ("System Design Constants",     check_system_design_constants),
        ("Counter Format Compliance",   check_counter_format_compliance),
        ("Required Files",              check_required_files),
    ]

    results = []
    for name, fn in checks:
        log.info("")
        log.info("[ %s ]", name)
        try:
            passed = fn()
        except Exception as e:
            log.error("  [FAIL] Unexpected error: %s", e)
            passed = False
        results.append((name, passed))

    # Summary
    log.info("")
    log.info("=" * 55)
    log.info("Evaluation Summary")
    log.info("=" * 55)
    passed_count = sum(1 for _, p in results if p)
    for name, passed in results:
        status = "[PASS]" if passed else "[FAIL]"
        log.info("  %s  %s", status, name)

    log.info("")
    log.info("  %d / %d checks passed", passed_count, len(checks))
    log.info("=" * 55)

    if passed_count == len(checks):
        log.info("System is ready for Docker deployment.")
    else:
        log.warning("%d check(s) need attention before deployment.",
                    len(checks) - passed_count)


if __name__ == "__main__":
    run_evaluation()