feat: 수령 자서전 데이터 파이프라인과 맞춤 요약 추가
This commit is contained in:
1 parent
5386222f1b
commit
946e9e9474
17 files changed
+437
-48
No files matched your search
@@ -39,6 +39,7 @@ import csv
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import unicodedata
|
||||
@@ -49,6 +50,8 @@ from pathlib import Path
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from app.engine.training_data import pseudonymous_id, redact_direct_identifiers # noqa: E402
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(message)s")
|
||||
logger = logging.getLogger("build-ai-dataset")
|
||||
|
||||
@@ -57,7 +60,7 @@ LABEL_AI = 1
|
||||
|
||||
#: 헤더 자동탐지 힌트 (부분일치, 소문자 비교). 실제 파일을 못 본 상태라
|
||||
#: 오탐 가능성이 있으니 --inspect 로 먼저 확인하고 --text-column 으로 고정할 것.
|
||||
TEXT_HINTS = ("에피소드", "본문", "내용", "원고", "텍스트", "story", "text", "body", "content")
|
||||
TEXT_HINTS = ("본문", "에피소드", "내용", "원고", "텍스트", "story", "text", "body", "content")
|
||||
BOOK_HINTS = ("도서", "책", "서명", "제목", "book", "title", "작품")
|
||||
ID_HINTS = ("id", "번호", "no", "식별")
|
||||
AUTHOR_HINTS = ("저자", "작가", "author", "writer")
|
||||
@@ -178,6 +181,9 @@ def load_human_from_xlsx(
|
||||
id_column: str | None,
|
||||
sheet: str | None,
|
||||
min_chars: int,
|
||||
group_column: str | None = None,
|
||||
anonymization_salt: str | None = None,
|
||||
provenance: str = "unknown",
|
||||
) -> list[Record]:
|
||||
wb = _load_workbook(path)
|
||||
sheets = [sheet] if sheet else wb.sheetnames
|
||||
@@ -202,11 +208,13 @@ def load_human_from_xlsx(
|
||||
bcol = book_column or _match_column(header, BOOK_HINTS)
|
||||
icol = id_column or _match_column(header, ID_HINTS)
|
||||
acol = _match_column(header, AUTHOR_HINTS)
|
||||
gcol = group_column or acol or icol or bcol
|
||||
|
||||
ti = header.index(tcol)
|
||||
bi = header.index(bcol) if bcol in header else None
|
||||
ii = header.index(icol) if icol in header else None
|
||||
ai = header.index(acol) if acol in header else None
|
||||
gi = header.index(gcol) if gcol in header else None
|
||||
|
||||
logger.info(
|
||||
"[%s] text=%r book=%r id=%r author=%r", name, tcol, bcol, icol, acol
|
||||
@@ -216,22 +224,48 @@ def load_human_from_xlsx(
|
||||
if ti >= len(row):
|
||||
continue
|
||||
text = normalize(row[ti] if row[ti] is not None else "")
|
||||
if anonymization_salt:
|
||||
text = redact_direct_identifiers(text)
|
||||
if len(text) < min_chars:
|
||||
continue
|
||||
book = ""
|
||||
if bi is not None and bi < len(row) and row[bi] is not None:
|
||||
book = str(row[bi]).strip()
|
||||
# 책 정보가 없으면 시트명으로라도 묶는다. group 없는 분할은 금지.
|
||||
group = f"book:{book}" if book else f"sheet:{name}"
|
||||
meta = {"sheet": name, "row": rownum, "source_file": path.name}
|
||||
raw_group = ""
|
||||
if gi is not None and gi < len(row) and row[gi] is not None:
|
||||
raw_group = str(row[gi]).strip()
|
||||
if anonymization_salt and raw_group:
|
||||
group = pseudonymous_id(raw_group, anonymization_salt)
|
||||
elif raw_group:
|
||||
group = f"source:{raw_group}"
|
||||
else:
|
||||
group = f"sheet:{name}"
|
||||
meta = {
|
||||
"sheet": name,
|
||||
"row": rownum,
|
||||
"source_file": path.name,
|
||||
"provenance": provenance,
|
||||
"human_verified": True,
|
||||
"ai_assistance": False,
|
||||
}
|
||||
if ii is not None and ii < len(row) and row[ii] is not None:
|
||||
meta["row_id"] = str(row[ii]).strip()
|
||||
raw_id = str(row[ii]).strip()
|
||||
if anonymization_salt:
|
||||
meta["row_id_hash"] = pseudonymous_id(raw_id, anonymization_salt)
|
||||
else:
|
||||
meta["row_id"] = raw_id
|
||||
if ai is not None and ai < len(row) and row[ai] is not None:
|
||||
meta["author"] = str(row[ai]).strip()
|
||||
raw_author = str(row[ai]).strip()
|
||||
if anonymization_salt:
|
||||
meta["author_hash"] = pseudonymous_id(raw_author, anonymization_salt)
|
||||
else:
|
||||
meta["author"] = raw_author
|
||||
records.append(
|
||||
Record(
|
||||
text=text, label=LABEL_HUMAN, origin="human",
|
||||
source_group=group, book=book, meta=meta,
|
||||
source_group=group,
|
||||
book="" if anonymization_salt else book,
|
||||
meta=meta,
|
||||
)
|
||||
)
|
||||
wb.close()
|
||||
@@ -410,6 +444,12 @@ def main() -> int:
|
||||
ap.add_argument("--text-column", default=None, help="본문 컬럼명 (미지정 시 자동탐지)")
|
||||
ap.add_argument("--book-column", default=None, help="도서 컬럼명 (분할 그룹 기준)")
|
||||
ap.add_argument("--id-column", default=None, help="행 식별자 컬럼명")
|
||||
ap.add_argument("--group-column", default=None,
|
||||
help="분할 그룹 컬럼. 자서전.net은 id를 지정해 작성자 누출 방지")
|
||||
ap.add_argument("--anonymize", action="store_true",
|
||||
help="그룹/행 ID를 HMAC 가명화하고 본문의 직접 식별자를 제거")
|
||||
ap.add_argument("--anonymization-salt-env", default="DATA_ANONYMIZATION_SALT")
|
||||
ap.add_argument("--human-provenance", default="combooks_confirmed_human")
|
||||
ap.add_argument("--ai-jsonl", type=Path, action="append", default=[], help="AI 샘플 JSONL (반복 가능)")
|
||||
ap.add_argument("--ai-csv", type=Path, action="append", default=[], help="AI 샘플 CSV (반복 가능)")
|
||||
ap.add_argument("--ai-text-field", default="text", help="AI 파일의 본문 필드명")
|
||||
@@ -430,6 +470,15 @@ def main() -> int:
|
||||
return 0
|
||||
|
||||
records: list[Record] = []
|
||||
anonymization_salt = None
|
||||
if args.anonymize:
|
||||
anonymization_salt = os.environ.get(args.anonymization_salt_env, "")
|
||||
if not anonymization_salt:
|
||||
logger.error(
|
||||
"--anonymize 사용 시 %s 환경변수가 필요합니다.",
|
||||
args.anonymization_salt_env,
|
||||
)
|
||||
return 2
|
||||
if args.xlsx:
|
||||
if not args.xlsx.exists():
|
||||
logger.error("xlsx 없음: %s", args.xlsx)
|
||||
@@ -437,6 +486,7 @@ def main() -> int:
|
||||
records += load_human_from_xlsx(
|
||||
args.xlsx, args.text_column, args.book_column,
|
||||
args.id_column, args.sheet, args.min_chars,
|
||||
args.group_column, anonymization_salt, args.human_provenance,
|
||||
)
|
||||
|
||||
jsonl = [p for p in args.ai_jsonl if p.exists()]
|
||||
|
||||
Reference in new issue
Block a user