Files
syops/backend/edkb_system/config.py
T
2026-05-12 08:16:42 +08:00

54 lines
1.4 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
from pydantic import BaseModel, Field
class RouterConfig(BaseModel):
scan_likelihood_threshold: float = 0.6
flow_likelihood_threshold: float = 0.5
table_likelihood_threshold: float = 0.5
class OCRConfig(BaseModel):
enable: bool = True
engine: str = "rapidocr"
min_confidence: float = 0.3
class ChunkConfig(BaseModel):
enable: bool = True
max_chars: int = 1200
overlap_chars: int = 150
add_heading_path_prefix: bool = True
class CleanConfig(BaseModel):
dedup_min_repeats: int = 3
merge_short_line_chars: int = 15
class SystemConfig(BaseModel):
router: RouterConfig = Field(default_factory=RouterConfig)
ocr: OCRConfig = Field(default_factory=OCRConfig)
chunk: ChunkConfig = Field(default_factory=ChunkConfig)
clean: CleanConfig = Field(default_factory=CleanConfig)
@classmethod
def load(cls, path: str | None) -> "SystemConfig":
if not path:
return cls()
p = Path(path)
raw = p.read_text(encoding="utf-8")
if p.suffix.lower() in {".yaml", ".yml"}:
import yaml
data = yaml.safe_load(raw) or {}
elif p.suffix.lower() == ".json":
data = json.loads(raw)
else:
raise ValueError("config only supports yaml/yml/json")
return cls.model_validate(data)