feat: 수령 자서전 데이터 파이프라인과 맞춤 요약 추가
This commit is contained in:
1 parent
5386222f1b
commit
946e9e9474
17 files changed
+437
-48
No files matched your search
@@ -61,6 +61,7 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s: %(mes
|
||||
logger = logging.getLogger("gen-ai-samples")
|
||||
|
||||
from app.engine.ocr_normalize import normalize_ocr # noqa: E402
|
||||
from app.engine.training_data import pseudonymous_id, sanitize_prompt_metadata # noqa: E402
|
||||
|
||||
#: 프롬프트에 쓸 수 있는 컬럼. 본문 컬럼은 의도적으로 목록에 없다.
|
||||
META_COLUMNS = (
|
||||
@@ -70,7 +71,8 @@ META_COLUMNS = (
|
||||
"gpt_character_keyword",
|
||||
"생애사건_category",
|
||||
)
|
||||
TEXT_COLUMN_BLOCKLIST = ("에피소드", "본문", "내용", "원고", "text", "content")
|
||||
TEXT_COLUMN_BLOCKLIST = ("본문", "내용", "원고", "text", "content", "body")
|
||||
TEXT_COLUMN_EXACT_BLOCKLIST = ("에피소드", "story")
|
||||
|
||||
SYSTEM_PROMPT = (
|
||||
"당신은 한국어 자서전 원고를 쓰는 작가다. 주어진 소재로 자서전의 한 "
|
||||
@@ -99,8 +101,11 @@ def assert_no_body_columns(columns: list[str]) -> list[str]:
|
||||
주석만으로는 다음 사람이 META_COLUMNS 에 본문 컬럼을 한 줄 추가하는 것을
|
||||
못 막는다. 그 한 줄이 원고를 외부 API 로 내보내는 경로가 된다.
|
||||
"""
|
||||
leaked = [c for c in columns
|
||||
if any(bad in c.lower() for bad in TEXT_COLUMN_BLOCKLIST)]
|
||||
leaked = [
|
||||
c for c in columns
|
||||
if c.strip().lower() in TEXT_COLUMN_EXACT_BLOCKLIST
|
||||
or any(bad in c.lower() for bad in TEXT_COLUMN_BLOCKLIST)
|
||||
]
|
||||
if leaked:
|
||||
raise SystemExit(
|
||||
f"본문으로 보이는 컬럼이 프롬프트 소재에 있습니다: {leaked}. "
|
||||
@@ -158,6 +163,15 @@ def main() -> int:
|
||||
ap.add_argument("--xlsx", type=Path, required=True, help="소재를 뽑을 에피소드 xlsx")
|
||||
ap.add_argument("--text-column", default="에피소드",
|
||||
help="길이 분포 계산에만 쓴다. 본문은 API 로 보내지 않는다.")
|
||||
ap.add_argument(
|
||||
"--meta-column", action="append", default=[],
|
||||
help="프롬프트 소재 컬럼(반복 지정). 제목·키워드만 허용하며 본문은 차단",
|
||||
)
|
||||
ap.add_argument(
|
||||
"--group-column", default=None,
|
||||
help="human과 같은 작성자 그룹으로 묶을 컬럼(원본값은 저장·전송하지 않음)",
|
||||
)
|
||||
ap.add_argument("--group-salt-env", default="DATA_ANONYMIZATION_SALT")
|
||||
ap.add_argument("--model", action="append", default=[],
|
||||
help="생성 모델 (반복 지정 = 혼합). 최소 2개 권장")
|
||||
ap.add_argument("--base-url", default=None,
|
||||
@@ -179,13 +193,26 @@ def main() -> int:
|
||||
lengths[len(lengths) // 2], low_chars, high_chars, len(lengths),
|
||||
)
|
||||
|
||||
usable_meta = assert_no_body_columns([c for c in META_COLUMNS if c in frame.columns])
|
||||
requested_meta = args.meta_column or [c for c in META_COLUMNS if c in frame.columns]
|
||||
missing_meta = [c for c in requested_meta if c not in frame.columns]
|
||||
if missing_meta:
|
||||
raise SystemExit(f"프롬프트 소재 컬럼이 없습니다: {missing_meta}")
|
||||
usable_meta = assert_no_body_columns(requested_meta)
|
||||
if not usable_meta:
|
||||
raise SystemExit(
|
||||
f"소재로 쓸 컬럼이 없습니다. 기대: {META_COLUMNS} / 실제: {list(frame.columns)}"
|
||||
)
|
||||
logger.info("프롬프트 소재 컬럼: %s", usable_meta)
|
||||
|
||||
group_salt = os.environ.get(args.group_salt_env, "")
|
||||
if args.group_column:
|
||||
if args.group_column not in frame.columns:
|
||||
raise SystemExit(f"그룹 컬럼이 없습니다: {args.group_column}")
|
||||
if not group_salt:
|
||||
raise SystemExit(
|
||||
f"--group-column 사용 시 {args.group_salt_env} 환경변수가 필요합니다."
|
||||
)
|
||||
|
||||
models = args.model or ["gpt-4o-mini"]
|
||||
if len(models) == 1 and not args.dry_run:
|
||||
logger.warning(
|
||||
@@ -229,11 +256,20 @@ def main() -> int:
|
||||
if prompt_id in done:
|
||||
continue
|
||||
|
||||
meta = {c: str(frame[c].iloc[idx]) for c in usable_meta
|
||||
if isinstance(frame[c].iloc[idx], str) and frame[c].iloc[idx].strip()}
|
||||
meta = {
|
||||
c: sanitize_prompt_metadata(str(frame[c].iloc[idx]))
|
||||
for c in usable_meta
|
||||
if isinstance(frame[c].iloc[idx], str) and frame[c].iloc[idx].strip()
|
||||
}
|
||||
if not meta:
|
||||
continue
|
||||
|
||||
source_group = f"ai-topic:{prompt_id}"
|
||||
if args.group_column:
|
||||
raw_group = str(frame[args.group_column].iloc[idx] or "").strip()
|
||||
if raw_group and raw_group.lower() != "nan":
|
||||
source_group = pseudonymous_id(raw_group, group_salt)
|
||||
|
||||
target = rng.choice(lengths)
|
||||
style = STYLE_VARIANTS[_stable_int(prompt_id) % len(STYLE_VARIANTS)]
|
||||
model = models[attempted % len(models)]
|
||||
@@ -276,6 +312,9 @@ def main() -> int:
|
||||
"target_chars": target,
|
||||
"char_count": len(text),
|
||||
"style": style,
|
||||
"source_group": source_group,
|
||||
"generation_type": "pure_ai",
|
||||
"provenance": "ai_generated_controlled",
|
||||
"meta": meta,
|
||||
}, ensure_ascii=False) + "\n")
|
||||
sink.flush() # 중간에 죽어도 여기까지는 남는다
|
||||
|
||||
Reference in new issue
Block a user