from functools import lru_cache from pathlib import Path from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore") # 서버 바인딩 host: str = "0.0.0.0" port: int = 8000 log_level: str = "info" # debug / info / warning / error reload: bool = False # 개발용 자동 재시작 root_path: str = "" # 리버스 프록시 sub-path (예: /plagiarism) # --- 인증 (#1) --- # api_key 만으로는 "빈 값 = 무인증"이 조용히 성립한다. 운영에서는 # require_api_key=true 로 명시적 fail-closed 를 걸어, 키가 없으면 앱이 # 아예 뜨지 않게 한다. api_key: str = "" # 설정 시 X-API-Key 필수 require_api_key: bool = False # true 인데 api_key 가 비면 기동 실패 public_health: bool = True # /v1/health 를 인증 없이 공개할지 public_docs: bool = True # /docs, /openapi.json, /redoc 공개 여부 engine_version: str = "o2o-plagiarism-2.0.0-pdf-v1.2" reference_corpus_dir: str = "./data/reference" taxonomy_dir: str = "./data/taxonomy" autobiography_patterns_path: str = "./data/autobiography/common_patterns.txt" corpus_db_path: str = "./data/runtime/corpus.sqlite3" persistent_index_dir: str = "./data/runtime/index" use_persistent_index: bool = False persistent_similarity_threshold: float = 0.65 persistent_min_exact_span: int = 80 persistent_min_coverage: float = 0.30 # 상위 N개 후보만 SequenceMatcher 정밀 비교 + union coverage 에 참여시킨다. # 이 값이 곧 요청당 O(질의길이 × 세그먼트길이) 연산의 상한이다 (#3/#5). persistent_rerank_top_k: int = 20 precedents_path: str = "./data/precedents/precedents.jsonl" ai_detector_model_path: str = "./data/models/ai_detector.joblib" ai_detector_allow_heuristic: bool = False ai_detector_use_pos: bool = True # PDF VII-4 권장: 정밀도 우선 보수적 임계값 similarity_threshold: float = 0.85 # 임계값이 실데이터로 캘리브레이션되었는지. false 면 API 응답에 # provisional=true 로 노출된다 (#9). 79권 FP 분포 측정 후 true 로 전환. similarity_threshold_calibrated: bool = False # KoSimCSE / KoSBERT (PDF VII-3 권장) - 한국어 오픈소스 임베딩 use_kosimcse: bool = False kosimcse_model: str = "BM-K/KoSimCSE-roberta-multitask" kosimcse_max_length: int = 512 # OpenAI (옵션 - 자체 모델 없을 때 폴백) openai_api_key: str = "" openai_extraction_model: str = "gpt-4o-mini" openai_embedding_model: str = "text-embedding-3-small" use_llm_extractor: bool = False use_embedding_similarity: bool = False # 삼중 유사도 가중치 (실측 기반) weight_text_sim: float = 0.30 weight_lemma_sim: float = 0.45 weight_char_sim: float = 0.15 weight_motif_sim: float = 0.10 # PDF VII-3 캐스케이딩 use_lsh_filter: bool = True lsh_threshold: float = 0.3 # 1차 필터는 느슨하게 (재현율 우선) lsh_top_k: int = 50 # 2단계 고도화: 군집화 기반 부분 표절(요소 교체) 신호 (계획서 p.21) use_clustering: bool = True cluster_link_threshold: float = 0.35 # PDF VII-4 자서전 모드 autobiography_mode: bool = True enable_entity_masking: bool = True @property def corpus_path(self) -> Path: return Path(self.reference_corpus_dir).resolve() @property def taxonomy_path(self) -> Path: return Path(self.taxonomy_dir).resolve() @property def corpus_db(self) -> Path: return Path(self.corpus_db_path).resolve() @property def persistent_index_path(self) -> Path: return Path(self.persistent_index_dir).resolve() @property def precedent_path(self) -> Path: return Path(self.precedents_path).resolve() @property def has_openai(self) -> bool: return bool(self.openai_api_key.strip()) @lru_cache def get_settings() -> Settings: return Settings()