File size: 2,443 Bytes
51c78c4
 
 
 
 
28b66d4
51c78c4
 
 
28b66d4
51c78c4
28b66d4
51c78c4
28b66d4
51c78c4
 
 
 
28b66d4
 
 
 
 
51c78c4
28b66d4
51c78c4
28b66d4
51c78c4
 
 
 
28b66d4
51c78c4
 
 
28b66d4
 
 
51c78c4
28b66d4
 
 
 
 
 
 
 
 
 
 
51c78c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28b66d4
 
 
51c78c4
 
 
28b66d4
51c78c4
 
28b66d4
51c78c4
 
28b66d4
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
# src/storage.py
import json
import os
from datetime import datetime
from pathlib import Path

from huggingface_hub import HfApi, hf_hub_download

DATASET_REPO = "Usmansafder/engchain-annotations"
HF_TOKEN = os.environ.get("HF_TOKEN")

if not HF_TOKEN:
    raise RuntimeError(
        "HF_TOKEN not set. Add it in Space Settings → Secrets with write access."
    )

api = HfApi(token=HF_TOKEN)

def _safe_slug(s: str) -> str:
    s = s.strip()
    s = "".join(c for c in s if c.isalnum() or c in (" ", "_", "-"))
    s = s.replace(" ", "_")
    return s or "unknown"

def _download_json_if_exists(path_in_repo: str, local_path: Path) -> list:
    """
    Returns list of existing reviews; empty list if file doesn't exist yet.
    """
    try:
        downloaded = hf_hub_download(
            repo_id=DATASET_REPO,
            filename=path_in_repo,
            repo_type="dataset",
            token=HF_TOKEN,
        )
        local_path.write_text(Path(downloaded).read_text(encoding="utf-8"), encoding="utf-8")
        data = json.loads(local_path.read_text(encoding="utf-8"))
        return data if isinstance(data, list) else []
    except Exception:
        return []

def save_review(annotator_name, branch, area, template_name, scores, decision, feedback):
    safe_name = _safe_slug(annotator_name)
    safe_branch = _safe_slug(branch)

    # Store per-reviewer, per-branch
    path_in_repo = f"reviews/{safe_name}/{safe_branch}.json"
    local_file = Path("/tmp") / f"{safe_name}_{safe_branch}.json"

    existing = _download_json_if_exists(path_in_repo, local_file)

    review_data = {
        "timestamp": datetime.now().isoformat(),
        "annotator_id": annotator_name,
        "branch": branch,
        "area": area,
        "template": template_name,
        "scores": {
            "physical_plausibility": scores[0],
            "mathematical_correctness": scores[1],
            "pedagogical_clarity": scores[2],
        },
        "decision": decision,
        "feedback": feedback,
    }

    existing.append(review_data)

    local_file.write_text(json.dumps(existing, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")

    api.upload_file(
        path_or_fileobj=str(local_file),
        path_in_repo=path_in_repo,
        repo_id=DATASET_REPO,
        repo_type="dataset",
        commit_message=f"Add review: {safe_name} ({safe_branch})",
    )

    return f"hf://datasets/{DATASET_REPO}/{path_in_repo}"