OvisOCR3-Arabic-workdir / code /common /render_config.py
aziz9788's picture
Initial commit
cf8c8d4
Raw History Blame Contribute Delete
1.6 kB
"""Fill folder placeholders in an upstream training config and write a ready-to-run copy.
Configs in architectures/*/ use placeholders instead of machine-specific paths:
${OCR_WORK_DIR} ${OCR_MODELS_DIR} ${OCR_UPSTREAM_DIR} ${OCR_PREPARED_DIR} ${OCR_RUNS_DIR}
They are replaced with absolute paths from common/paths.py (which honours the same environment variables).
python -m common.render_config architectures/02_fd_rl/fdrl_sft.yaml data/runs/configs/fdrl_sft.yaml
Prints the path of the written file, so scripts can capture it.
"""
import argparse
import re
from pathlib import Path
from .paths import MODELS_DIR, PREPARED_DIR, RUNS_DIR, UPSTREAM_DIR, WORK_DIR
VALUES = {
"OCR_WORK_DIR": WORK_DIR, "OCR_MODELS_DIR": MODELS_DIR, "OCR_UPSTREAM_DIR": UPSTREAM_DIR,
"OCR_PREPARED_DIR": PREPARED_DIR, "OCR_RUNS_DIR": RUNS_DIR,
}
PLACEHOLDER = re.compile(r"\$\{(OCR_[A-Z_]+)\}")
def render(text: str) -> str:
def sub(m):
if m.group(1) not in VALUES:
raise SystemExit(f"unknown placeholder ${{{m.group(1)}}}")
return VALUES[m.group(1)].resolve().as_posix()
return PLACEHOLDER.sub(sub, text)
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("template", type=Path)
ap.add_argument("out", type=Path)
a = ap.parse_args(argv)
a.out.parent.mkdir(parents=True, exist_ok=True)
a.out.write_text(render(a.template.read_text(encoding="utf-8")), encoding="utf-8")
print(a.out.resolve())
if __name__ == "__main__":
main()