Spaces:
Running
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()
|