Spaces:
Running

math-tutor / scripts /07_retire_missing.py
seongui2030
deploy 2026-12-03 20:26
b146445
Raw History Blame Contribute Delete
3.51 kB
"""μƒˆ 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()