Spaces:
Running

File size: 3,509 Bytes
b146445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""μƒˆ aihub_A.jsonl 에 더 이상 μ—†λŠ” aihub λ¬Έν•­(등급이 A -> B/C 둜 바뀐 것)을 DB μ—μ„œ status='retired' 둜 λ°”κΏ‰λ‹ˆλ‹€.
μ‚­μ œν•˜μ§€ μ•Šκ³  '퇴역' ν‘œμ‹œλ§Œ ν•˜λ―€λ‘œ 풀이 기둝은 κ·ΈλŒ€λ‘œ 남고, 학생·ꡐ사 ν™”λ©΄μ—λŠ” λ‚˜μ˜€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ (화면은 draft/published 만 읽음).

    python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A.jsonl            # 미리보기(λ³€κ²½ μ—†μŒ)
    python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A.jsonl --apply    # μ‹€μ œ 반영
B κ·Έλ¦Ό λ¬Έν•­(aihub_B_sel.jsonl)을 DB 에 넣은 λ’€μ—λŠ” λ°˜λ“œμ‹œ 두 νŒŒμΌμ„ ν•¨κ»˜ μ€€λ‹€ (μ•ˆ 그러면 B 문항이 λͺ¨λ‘ 퇴역됨):
    python scripts\\07_retire_missing.py --file data\\aihub_normalized\\aihub_A_fixed.jsonl,data\\aihub_normalized\\aihub_B_sel.jsonl
ν™˜κ²½λ³€μˆ˜: SUPABASE_URL, SUPABASE_SERVICE_KEY  (06_load_supabase.py 와 κ°™μŒ)
"""
import argparse
import json
import os
import sys
import urllib.parse
import urllib.request
from pathlib import Path

if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")


def req(method, url, key, body=None, extra=None):
    h = {"apikey": key, "Content-Type": "application/json", **(extra or {})}
    if key.count(".") == 2:
        h["Authorization"] = f"Bearer {key}"
    r = urllib.request.Request(url, data=json.dumps(body).encode() if body is not None else None, headers=h, method=method)
    with urllib.request.urlopen(r, timeout=60) as resp:
        return resp.read().decode("utf-8")


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--file", required=True,
                    help="적재 파일. μ—¬λŸ¬ 개면 μ‰Όν‘œλ‘œ (예: aihub_A_fixed.jsonl,aihub_B_sel.jsonl) β€” "
                         "DB 에 μžˆλŠ” AI Hub λ¬Έν•­ νŒŒμΌμ„ λͺ¨λ‘ μ£Όμ§€ μ•ŠμœΌλ©΄ λΉ μ§„ 파일의 문항이 퇴역됨")
    ap.add_argument("--apply", action="store_true")
    a = ap.parse_args()
    url, key = os.environ.get("SUPABASE_URL", "").strip().rstrip("/"), os.environ.get("SUPABASE_SERVICE_KEY", "").strip()
    if not url or not key:
        sys.exit("SUPABASE_URL κ³Ό SUPABASE_SERVICE_KEY ν™˜κ²½λ³€μˆ˜κ°€ ν•„μš”ν•©λ‹ˆλ‹€.")
    keep = set()
    for one in [s.strip() for s in a.file.split(",") if s.strip()]:
        keep |= {json.loads(l)["source_ref"] for l in Path(one).open(encoding="utf-8") if l.strip()}
    base = url + "/rest/v1/questions"
    db, start = [], 0
    while True:
        q = f"{base}?select=source_ref&source=eq.aihub&status=in.(draft,review)&order=source_ref"
        part = json.loads(req("GET", q, key, extra={"Range-Unit": "items", "Range": f"{start}-{start + 999}"}))
        db += [r["source_ref"] for r in part]
        if len(part) < 1000:
            break
        start += 1000
    gone = sorted(set(db) - keep)
    print(f"DB 의 aihub draft/review {len(db)}개, μƒˆ 파일 {len(keep)}개 -> 퇴역 λŒ€μƒ {len(gone)}개")
    for g in gone[:20]:
        print("  ", g)
    if not a.apply:
        print("(λ―Έλ¦¬λ³΄κΈ°μž…λ‹ˆλ‹€. λ°˜μ˜ν•˜λ €λ©΄ --apply λ₯Ό λΆ™μ΄μ„Έμš”)")
        return
    for i in range(0, len(gone), 50):
        chunk = gone[i:i + 50]
        refs = ",".join('"' + c + '"' for c in chunk)
        quoted = urllib.parse.quote(refs, safe=',"')
        req("PATCH", base + "?source=eq.aihub&source_ref=in.(" + quoted + ")", key,
            {"status": "retired"}, {"Prefer": "return=minimal"})
    print("반영 μ™„λ£Œ")


if __name__ == "__main__":
    main()