File size: 6,805 Bytes
2cdd0e3 5bd8c02 2cdd0e3 b0eb97c 2cdd0e3 f3d9354 2cdd0e3 b0eb97c 2cdd0e3 88bb334 f6eed90 2cdd0e3 b0eb97c 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 b0eb97c 2cdd0e3 f3d9354 b0eb97c f3d9354 2cdd0e3 b0eb97c 2cdd0e3 b0eb97c 2cdd0e3 b0eb97c 2cdd0e3 f5e2210 2cdd0e3 5bd8c02 2cdd0e3 eb0247f 5bd8c02 eb0247f 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 5bd8c02 2cdd0e3 | 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 | """Flask annotation app for blind summary evaluation.
Serves a web UI where annotators evaluate AI-generated summaries.
Annotations are anonymous and shared -- each summary is evaluated once.
Annotations are persisted in a SQLite database.
Optional password protection: set APP_PASSWORD as an environment variable.
Authentication uses HMAC tokens stored in the browser via localStorage,
avoiding cookies/sessions that can break behind reverse proxies.
"""
import hashlib
import hmac
import json
import os
import sqlite3
from functools import cache
from pathlib import Path
from flask import Flask, Response, jsonify, request, send_file
DATASET_PATH = Path(os.environ.get("DATASET_PATH", "2026-04-23_prompt_evaluation_dataset.jsonl"))
DB_PATH = Path(os.environ.get("DB_PATH", "/data/annotations.db"))
ANNOTATION_FIELDS = (
"bewertung", "korrekt", "relevant", "vollstaendig", "kohaerenz", "anmerkungen",
)
APP_PASSWORD = os.environ.get("APP_PASSWORD", "")
SECRET_KEY = os.environ.get("SECRET_KEY", os.urandom(24).hex())
app = Flask(__name__)
# ---------------------------------------------------------------------------
# Database
# ---------------------------------------------------------------------------
def get_db() -> sqlite3.Connection:
"""Open a SQLite connection with row factory and WAL mode."""
db = sqlite3.connect(str(DB_PATH))
db.row_factory = sqlite3.Row
db.execute("PRAGMA journal_mode=WAL")
db.execute("""
CREATE TABLE IF NOT EXISTS annotations (
eval_id TEXT PRIMARY KEY,
bewertung TEXT,
korrekt TEXT,
relevant TEXT,
vollstaendig TEXT,
kohaerenz TEXT,
anmerkungen TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
return db
# ---------------------------------------------------------------------------
# Dataset (immutable, cached)
# ---------------------------------------------------------------------------
@cache
def load_dataset() -> tuple[dict, ...]:
"""Load evaluation items from JSONL. Cached because the dataset never changes."""
items = []
with open(DATASET_PATH, encoding="utf-8") as f:
for line in f:
if line.strip():
row = json.loads(line)
has_prior = bool(row.get("bewertung"))
row["has_prior_judgement"] = has_prior
if has_prior:
for field in ANNOTATION_FIELDS:
row[f"prior_{field}"] = row.get(field)
items.append(row)
items.sort(key=lambda x: x["eval_id"])
return tuple(items)
def fetch_annotations(db: sqlite3.Connection) -> dict[str, dict]:
"""Fetch all annotations, keyed by eval_id."""
rows = db.execute("SELECT * FROM annotations").fetchall()
return {row["eval_id"]: dict(row) for row in rows}
def merge_items_with_annotations(
items: tuple[dict, ...],
annotations: dict[str, dict],
) -> list[dict]:
"""Return items with annotation values merged in (does not mutate originals)."""
merged = []
for item in items:
ann = annotations.get(item["eval_id"])
entry = {**item, "evaluated": ann is not None}
if ann:
for field in ANNOTATION_FIELDS:
entry[field] = ann.get(field)
merged.append(entry)
return merged
# ---------------------------------------------------------------------------
# Optional password protection (token-based, no cookies/sessions)
# ---------------------------------------------------------------------------
def _make_auth_token() -> str:
"""Create an HMAC token derived from the app password and secret key."""
key = SECRET_KEY.encode() if isinstance(SECRET_KEY, str) else SECRET_KEY
return hmac.new(key, APP_PASSWORD.encode(), hashlib.sha256).hexdigest()
@app.before_request
def check_auth():
if not APP_PASSWORD:
return None
if request.path in ("/", "/login"):
return None
if request.path.startswith("/api/login"):
return None
token = request.headers.get("Authorization", "").removeprefix("Bearer ")
if token == _make_auth_token():
return None
return jsonify({"error": "unauthorized"}), 401
@app.route("/api/login", methods=["POST"])
def api_login():
data = request.get_json()
if data and data.get("password") == APP_PASSWORD:
return jsonify({"token": _make_auth_token()})
return jsonify({"error": "wrong_password"}), 401
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.after_request
def no_cache_api(response):
"""Prevent browser from caching API responses."""
if request.path.startswith("/api/"):
response.headers["Cache-Control"] = "no-store"
return response
@app.route("/")
def index():
return send_file("index.html")
@app.route("/api/entries")
def get_entries():
"""Return all evaluation items with annotation data merged in."""
items = load_dataset()
db = get_db()
annotations = fetch_annotations(db)
db.close()
return jsonify(merge_items_with_annotations(items, annotations))
@app.route("/api/annotate", methods=["POST"])
def annotate():
"""Save or update an annotation."""
data = request.get_json()
db = get_db()
db.execute(
"""INSERT OR REPLACE INTO annotations
(eval_id, bewertung, korrekt, relevant, vollstaendig, kohaerenz, anmerkungen)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(
data["eval_id"],
data.get("bewertung"),
data.get("korrekt"),
data.get("relevant"),
data.get("vollstaendig"),
data.get("kohaerenz"),
data.get("anmerkungen"),
),
)
db.commit()
db.close()
return jsonify({"status": "ok"})
@app.route("/api/progress")
def progress():
"""Return annotation progress."""
total = len(load_dataset())
db = get_db()
count = db.execute("SELECT COUNT(*) FROM annotations").fetchone()[0]
db.close()
return jsonify({"total": total, "annotated": count})
@app.route("/api/export")
def export_annotations():
"""Export all annotations as downloadable JSONL."""
db = get_db()
rows = db.execute("SELECT * FROM annotations ORDER BY eval_id").fetchall()
db.close()
lines = [json.dumps(dict(row), ensure_ascii=False) for row in rows]
return Response(
"\n".join(lines) + "\n",
mimetype="application/jsonl",
headers={"Content-Disposition": "attachment; filename=annotations.jsonl"},
)
if __name__ == "__main__":
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
app.run(host="0.0.0.0", port=7860)
|