customkun_any / config_loader.py
showeed's picture
Upload 8 files
11bcfa7 verified
"""
ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ่ชญใฟ่พผใฟใƒปExcel ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ็”Ÿๆˆใƒขใ‚ธใƒฅใƒผใƒซ
YAML ใพใŸใฏ Excel (.xlsx) ใฎใฉใกใ‚‰ใงใ‚‚ๅŒใ˜ dict ๆง‹้€ ใ‚’่ฟ”ใ™ใ€‚
glmocr.py / glmocr_ollama.py ๅŒๆ–นใ‹ใ‚‰ import ใ—ใฆไฝฟใ†ใ€‚
Excel ใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆ๏ผˆ3 ใ‚ทใƒผใƒˆๆง‹ๆˆ๏ผ‰:
settings โ€ฆ image / output_dir / extract_table
preprocess โ€ฆ ๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—ใฎ ON/OFF
sections โ€ฆ ๆŠฝๅ‡บใ‚ปใ‚ฏใ‚ทใƒงใƒณใƒปใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎไธ€่ฆงใƒ†ใƒผใƒ–ใƒซ
ไฝฟใ„ๆ–น:
from config_loader import load_config, create_excel_template
cfg = load_config(Path("configs/invoice.yaml")) # YAML
cfg = load_config(Path("configs/invoice.xlsx")) # Excel
create_excel_template(cfg, Path("configs/new.xlsx"))
"""
from __future__ import annotations
from pathlib import Path
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
import yaml
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# ๅ†…้ƒจใƒฆใƒผใƒ†ใ‚ฃใƒชใƒ†ใ‚ฃ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _to_bool(value) -> bool:
"""ใ‚ปใƒซๅ€คใ‚’ bool ใซๅค‰ๆ›ใ™ใ‚‹๏ผˆExcel ใฎ TRUE/FALSE ๆ–‡ๅญ—ๅˆ—ใซใ‚‚ๅฏพๅฟœ๏ผ‰ใ€‚"""
if isinstance(value, bool):
return value
return str(value).strip().upper() in ("TRUE", "YES", "1", "ใ€‡", "ON")
def _header_style(ws, row: int, fill_color: str = "4472C4"):
"""ๆŒ‡ๅฎš่กŒใ‚’ใƒ˜ใƒƒใƒ€ใƒผ่กŒใจใ—ใฆใ‚นใ‚ฟใ‚คใƒซใ‚’้ฉ็”จใ™ใ‚‹ใ€‚"""
fill = PatternFill(start_color=fill_color, end_color=fill_color, fill_type="solid")
font = Font(color="FFFFFF", bold=True)
border = Border(
bottom=Side(style="medium", color="FFFFFF"),
)
for cell in ws[row]:
cell.fill = fill
cell.font = font
cell.alignment = Alignment(horizontal="center", vertical="center")
cell.border = border
def _zebra_row(ws, row: int, is_odd: bool):
"""ๅถๆ•ฐ/ๅฅ‡ๆ•ฐ่กŒใง่ƒŒๆ™ฏ่‰ฒใ‚’ไบคไบ’ใซใ™ใ‚‹๏ผˆใ‚ผใƒ–ใƒฉใ‚นใƒˆใƒฉใ‚คใƒ—๏ผ‰ใ€‚"""
color = "EAF2FF" if is_odd else "FFFFFF"
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
for cell in ws[row]:
cell.fill = fill
def _set_column_widths(ws, widths: list[int]):
"""ๅˆ—ๅน…ใ‚’ไธ€ๆ‹ฌ่จญๅฎšใ™ใ‚‹ใ€‚"""
cols = list(ws.column_dimensions.keys())
for i, w in enumerate(widths):
col = cols[i] if i < len(cols) else chr(65 + i)
ws.column_dimensions[col].width = w
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# YAML ่ชญใฟ่พผใฟ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _load_yaml(path: Path) -> dict:
"""YAML ใƒ•ใ‚กใ‚คใƒซใ‚’ใƒญใƒผใƒ‰ใ—ใฆ dict ใ‚’่ฟ”ใ™ใ€‚"""
with path.open(encoding="utf-8") as f:
return yaml.safe_load(f)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Excel ่ชญใฟ่พผใฟ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _load_excel(path: Path) -> dict:
"""Excel ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ (.xlsx) ใ‚’ใƒญใƒผใƒ‰ใ—ใฆ YAML ไบ’ๆ›ใฎ dict ใ‚’่ฟ”ใ™ใ€‚
ๆœŸๅพ…ใ™ใ‚‹ใ‚ทใƒผใƒˆ:
settings : Aๅˆ—=ใ‚ญใƒผ, Bๅˆ—=ๅ€ค (ใƒ˜ใƒƒใƒ€ใƒผ่กŒใฏ่ชญใฟ้ฃ›ใฐใ™)
preprocess : Aๅˆ—=ใ‚นใƒ†ใƒƒใƒ—ๅ, Bๅˆ—=TRUE/FALSE
sections : Aๅˆ—=section_key, Bๅˆ—=section_label, Cๅˆ—=field_key
"""
wb = openpyxl.load_workbook(path, data_only=True)
cfg: dict = {}
# โ”€โ”€ settings ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if "settings" in wb.sheetnames:
ws = wb["settings"]
rows = list(ws.iter_rows(min_row=2, values_only=True)) # 1่กŒ็›ฎใฏใƒ˜ใƒƒใƒ€ใƒผ
for row in rows:
if not row[0]:
continue
key = str(row[0]).strip()
val = row[1]
if key == "extract_table":
cfg[key] = _to_bool(val)
else:
cfg[key] = str(val).strip() if val is not None else ""
# โ”€โ”€ preprocess ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if "preprocess" in wb.sheetnames:
ws = wb["preprocess"]
rows = list(ws.iter_rows(min_row=2, values_only=True))
preprocess: dict = {}
for row in rows:
if not row[0]:
continue
step = str(row[0]).strip()
preprocess[step] = _to_bool(row[1]) if row[1] is not None else True
cfg["preprocess"] = preprocess
# โ”€โ”€ sections ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
if "sections" in wb.sheetnames:
ws = wb["sections"]
rows = list(ws.iter_rows(min_row=2, values_only=True))
sections: dict = {}
for row in rows:
if not row[0]:
continue
sec_key = str(row[0]).strip()
sec_label = str(row[1]).strip() if row[1] else sec_key
field_key = str(row[2]).strip() if row[2] else None
if not field_key:
continue
if sec_key not in sections:
sections[sec_key] = {"label": sec_label, "fields": {}}
sections[sec_key]["fields"][field_key] = ""
cfg["sections"] = sections
return cfg
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# ๅ…ฌ้–‹้–ขๆ•ฐ: ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ่ชญใฟ่พผใฟ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def load_config(config_path: Path) -> dict:
"""YAML ใพใŸใฏ Excel (.xlsx) ใฎใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐใ‚’่ชญใฟ่พผใ‚“ใง dict ใ‚’่ฟ”ใ™ใ€‚
ใƒ•ใ‚กใ‚คใƒซๆ‹กๅผตๅญใง่‡ชๅ‹•ๅˆคๅˆฅใ™ใ‚‹:
.yaml / .yml โ†’ YAML ใจใ—ใฆ่ชญใฟ่พผใ‚€
.xlsx โ†’ Excel ใจใ—ใฆ่ชญใฟ่พผใ‚€
Args:
config_path: ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚น
Returns:
dict: ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ่พžๆ›ธ๏ผˆYAML ใจๅŒไธ€ๆง‹้€ ๏ผ‰
Raises:
ValueError: ๅฏพๅฟœใ—ใฆใ„ใชใ„ๆ‹กๅผตๅญใฎๅ ดๅˆ
"""
suffix = config_path.suffix.lower()
if suffix in (".yaml", ".yml"):
return _load_yaml(config_path)
if suffix == ".xlsx":
return _load_excel(config_path)
raise ValueError(f"ๅฏพๅฟœใ—ใฆใ„ใชใ„ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐๅฝขๅผใงใ™: {suffix}๏ผˆ.yaml / .xlsx ใฎใฟๆœ‰ๅŠน๏ผ‰")
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# ๅ…ฌ้–‹้–ขๆ•ฐ: Excel ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ็”Ÿๆˆ
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# ใ‚ปใ‚ฏใ‚ทใƒงใƒณไธ€่ฆงใ‚ทใƒผใƒˆใฎใƒ˜ใƒƒใƒ€ใƒผ่ชฌๆ˜Žๆ–‡
_SECTIONS_HEADER_NOTE = (
"section_key๏ผˆๅค‰ๆ›ดไธๅฏ๏ผ‰"
" โ”‚ section_label๏ผˆExcelๅ‡บๅŠ›ๆ™‚ใฎใ‚ทใƒผใƒˆๅ๏ผ‰"
" โ”‚ field_key๏ผˆๅค‰ๆ›ดไธๅฏ๏ผ‰"
)
def create_excel_template(cfg: dict, xlsx_path: Path) -> None:
"""ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ dict ใ‹ใ‚‰็ทจ้›†ใ—ใ‚„ใ™ใ„ Excel ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹ใ€‚
็”Ÿๆˆใ•ใ‚Œใ‚‹ใ‚ทใƒผใƒˆ:
settings โ€ฆ ๅŸบๆœฌ่จญๅฎš๏ผˆimage / output_dir / extract_table๏ผ‰
preprocess โ€ฆ ๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ— ON/OFF
sections โ€ฆ ๅ…จใ‚ปใ‚ฏใ‚ทใƒงใƒณใƒปใƒ•ใ‚ฃใƒผใƒซใƒ‰ใฎไธ€่ฆงใƒ†ใƒผใƒ–ใƒซ
Args:
cfg: load_config() ใงๅ–ๅพ—ใ—ใŸใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐ dict
xlsx_path: ๅ‡บๅŠ›ๅ…ˆ xlsx ใƒ•ใ‚กใ‚คใƒซใƒ‘ใ‚น
"""
wb = openpyxl.Workbook()
# โ”€โ”€ settings ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ws_s = wb.active
ws_s.title = "settings"
ws_s.append(["ใ‚ญใƒผ", "ๅ€ค", "่ชฌๆ˜Ž"])
_header_style(ws_s, 1, "2E75B6")
data_s = [
("image", cfg.get("image", ""), "ๅฏพ่ฑกใƒ•ใ‚กใ‚คใƒซใƒ‘ใ‚น๏ผˆ็”ปๅƒใพใŸใฏ PDF๏ผ‰"),
("output_dir", cfg.get("output_dir", "output"), "Excel ๅ‡บๅŠ›ๅ…ˆใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒช"),
("extract_table", cfg.get("extract_table", True), "ใƒ†ใƒผใƒ–ใƒซ่ช่ญ˜ใ‚’ๅฎŸ่กŒใ™ใ‚‹๏ผˆTRUE/FALSE๏ผ‰"),
]
for i, row in enumerate(data_s, start=2):
ws_s.append(list(row))
_zebra_row(ws_s, i, i % 2 == 0)
_set_column_widths(ws_s, [22, 28, 44])
# โ”€โ”€ preprocess ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ws_p = wb.create_sheet("preprocess")
ws_p.append(["ๅ‰ๅ‡ฆ็†ใ‚นใƒ†ใƒƒใƒ—", "ๆœ‰ๅŠน๏ผˆTRUE/FALSE๏ผ‰", "่ชฌๆ˜Ž"])
_header_style(ws_p, 1, "2E75B6")
default_pre = {"deskew": True, "denoise": True, "enhance_contrast": True, "sharpen": True}
pre_desc = {
"deskew": "ๅ‚พใ่ฃœๆญฃ๏ผˆใ‚นใ‚ญใƒฃใƒณใƒปๆ‰‹ๆŒใกๆ’ฎๅฝฑใฎๅ‚พใใ‚’ Hough ๅค‰ๆ›ใง่‡ชๅ‹•่ฃœๆญฃ๏ผ‰",
"denoise": "ใƒŽใ‚คใ‚บ้™คๅŽป๏ผˆใƒใ‚คใƒฉใƒ†ใƒฉใƒซใƒ•ใ‚ฃใƒซใ‚ฟใ€ใ‚จใƒƒใ‚ธใ‚’ไฟ่ญทใ—ใชใŒใ‚‰ๅนณๆป‘ๅŒ–๏ผ‰",
"enhance_contrast": "ใ‚ณใƒณใƒˆใƒฉใ‚นใƒˆๅผท่ชฟ๏ผˆ็…งๆ˜Žใƒ ใƒฉใซๆœ‰ๅŠนใช CLAHE๏ผ‰",
"sharpen": "ใ‚ทใƒฃใƒผใƒ—ๅŒ–๏ผˆUnsharp Masking ใงใผใ‘ใ‚’่ฃœๆญฃ๏ผ‰",
}
pre_cfg = cfg.get("preprocess", default_pre)
for i, (step, desc) in enumerate(pre_desc.items(), start=2):
ws_p.append([step, pre_cfg.get(step, True), desc])
_zebra_row(ws_p, i, i % 2 == 0)
_set_column_widths(ws_p, [22, 22, 54])
# โ”€โ”€ sections ใ‚ทใƒผใƒˆ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
ws_sec = wb.create_sheet("sections")
ws_sec.append(["section_key", "section_label", "field_key"])
_header_style(ws_sec, 1, "2E75B6")
row_idx = 2
sections = cfg.get("sections", {})
prev_sec = None
for sec_key, sec_cfg in sections.items():
label = sec_cfg.get("label", sec_key)
fields = sec_cfg.get("fields", {})
# ใ‚ปใ‚ฏใ‚ทใƒงใƒณใŒๅค‰ใ‚ใ‚‹ใŸใณใซ่‰ฒใ‚’ๅˆ‡ใ‚Šๆ›ฟใˆใ‚‹
is_odd = (list(sections.keys()).index(sec_key) % 2 == 0)
for field_key in fields:
ws_sec.append([sec_key, label, field_key])
_zebra_row(ws_sec, row_idx, is_odd)
row_idx += 1
_set_column_widths(ws_sec, [18, 22, 22])
# ๆณจ้‡ˆ่กŒ
ws_sec.append([])
ws_sec.append(["โ€ป section_key ใจ field_key ใฏใ‚นใ‚ฏใƒชใƒ—ใƒˆใŒๅ‚็…งใ™ใ‚‹ใ‚ญใƒผๅใงใ™ใ€‚ๅค‰ๆ›ดใ™ใ‚‹ๅ ดๅˆใฏไธ€่‡ดใ‚’็ขบ่ชใ—ใฆใใ ใ•ใ„ใ€‚"])
note_cell = ws_sec.cell(row=row_idx + 2, column=1)
note_cell.font = Font(color="888888", italic=True)
xlsx_path.parent.mkdir(parents=True, exist_ok=True)
wb.save(str(xlsx_path))
print(f"[OK] Excel ใƒ†ใƒณใƒ—ใƒฌใƒผใƒˆ็”Ÿๆˆ: {xlsx_path}")