File size: 1,414 Bytes
1cf82f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build the demo cache with the REAL pipeline: local classifier + Gemini.

For every seed post: classify locally (content-only, author-agnostic), and if
flagged, generate an educational analysis via Gemini (text only). Persists to
data/cache.json. Same routine the backend exposes at POST /rebuild-cache.

Requires torch + transformers installed; Gemini is optional (falls back to a
local heuristic analysis if GEMINI_API_KEY is unset).

Run:  python backend/scripts/build_cache.py
"""
from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from analyzer import available as gemini_available  # noqa: E402
from cache import get_cache, load_posts  # noqa: E402
from rebuild import rebuild_cache  # noqa: E402


def main() -> None:
    posts = load_posts()
    cache = get_cache()
    print(f"Gemini available: {gemini_available()}")
    print(f"Building cache for {len(posts)} posts ...\n")

    def progress(pid: str, classification: dict) -> None:
        tag = "FLAG" if classification["flagged"] else "ok  "
        print(f"  [{tag}] {pid:>3}  conf={classification['confidence']:.2f}")

    summary = rebuild_cache(cache, posts, on_post=progress)
    print(f"\nDone. {summary['flagged']} flagged / {summary['posts']} posts "
          f"on {summary['device']}. Cache written.")


if __name__ == "__main__":
    main()