Download apps/api/scripts/make_design_previews.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/make_design_previews.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/scripts/make_design_previews.py
-
curl -L -o make_design_previews.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/make_design_previews.py
3.26 kB
| """Rebuild the template previews the design screen shows (dev tool). | |
| PYTHONPATH=<folder with pymupdf> python scripts/make_design_previews.py | |
| Renders the stored test bag in every template with its default colours, | |
| converts it with LibreOffice and saves three pages side by side - the cover, | |
| the first session divider and the first page of scientific material - to | |
| apps/web/public/design-previews/<template>.png. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import sys | |
| import unicodedata | |
| import tempfile | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(ROOT)) | |
| import pymupdf # noqa: E402 - dev-only, not a runtime dependency | |
| from app.agents.arabic_bag.context import using # noqa: E402 | |
| from app.agents.arabic_bag.design import generate_designed_word, theme_from_brand # noqa: E402 | |
| from app.agents.arabic_bag.design.catalog import TEMPLATES # noqa: E402 | |
| from app.agents.arabic_bag.design.service import to_pdf # noqa: E402 | |
| FIXTURE = ROOT / "tests" / "fixtures" / "bag_run_98f70313.json" | |
| OUT = ROOT.parents[1] / "apps" / "web" / "public" / "design-previews" | |
| def main() -> None: | |
| data = json.loads(FIXTURE.read_text(encoding="utf-8")) | |
| stored = lambda k: (data[k].get("value") if isinstance(data[k], dict) else data[k]) or [] # noqa: E731 | |
| OUT.mkdir(parents=True, exist_ok=True) | |
| with tempfile.TemporaryDirectory() as tmp: | |
| for t in TEMPLATES: | |
| docx = Path(tmp) / f"{t['key']}.docx" | |
| theme = theme_from_brand({"family": t["key"], "author": "اسم المدرب"}) | |
| with using(data["bag_ar.blueprint"]): | |
| generate_designed_word( | |
| str(docx), theme=theme, preamble=data["bag_ar.preamble"], | |
| intro=data["bag_ar.intro"], content=json.loads(json.dumps(stored("bag_ar.content"))), | |
| activities=json.loads(json.dumps(stored("bag_ar.activities"))), | |
| quizzes=stored("bag_ar.quiz"), references=stored("bag_ar.references"), | |
| auto_images=True) | |
| pdf = to_pdf(str(docx)) | |
| doc = pymupdf.open(pdf) | |
| # LibreOffice writes Arabic as presentation forms; fold them back | |
| texts = [unicodedata.normalize("NFKC", p.get_text()) for p in doc] | |
| divider = next(i for i, x in enumerate(texts) if i > 1 and "الجلسة التدريبية" in x | |
| and len(x.split()) < 25) | |
| material = next(i for i, x in enumerate(texts) if "مادة علمية" in x and i > divider) | |
| W, H, gap = 595, 842, 24 | |
| sheet = pymupdf.open() | |
| page = sheet.new_page(width=3 * W + 4 * gap, height=H + 2 * gap) | |
| page.draw_rect(page.rect, color=None, fill=(0.95, 0.95, 0.96)) | |
| for k, n in enumerate((0, divider, material)): | |
| r = pymupdf.Rect(gap + k * (W + gap), gap, gap + k * (W + gap) + W, gap + H) | |
| page.show_pdf_page(r, doc, n) | |
| page.draw_rect(r, color=(0.8, 0.8, 0.82), width=1) | |
| page.get_pixmap(dpi=55).save(str(OUT / f"{t['key']}.png")) | |
| doc.close() | |
| sheet.close() | |
| print(t["key"], "->", OUT / f"{t['key']}.png") | |
| if __name__ == "__main__": | |
| main() | |