Spaces:
Running
Running
Download scripts/07_retire_missing.py from lilyjeongwon/math-tutor: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/07_retire_missing.py
- Command line
-
hf download hf://spaces/lilyjeongwon/math-tutor/scripts/07_retire_missing.py
-
curl -L -o 07_retire_missing.py https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/07_retire_missing.py
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() | |