"""The designed decks, every template and option, as pictures to look at. apps/api/.venv/Scripts/python.exe scripts/slides_baseline.py before apps/api/.venv/Scripts/python.exe scripts/slides_baseline.py after --only geometric Reads two decks' stored outputs from artifacts/slides_review/fixtures/ (real client work, so the folder is ignored by git), draws each in the six templates, with and without pictures, with and without a logo, and writes for each a PPTX, a PDF, and contact sheets of twenty slides. Pictures are fakes - a grid, a centre circle and arrows at the edges, in the shape asked of the model, so a crop shows. No model is called. `slots.json` compares the shape each picture is asked for with the box it is drawn in. """ from __future__ import annotations import argparse import concurrent.futures as cf import io import json import shutil import subprocess import sys import tempfile import threading from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "apps" / "api")) REVIEW = ROOT / "artifacts" / "slides_review" TEMPLATES = ("executive", "panel", "cards", "midnight", "geometric", "editorial", "prestige", "fluid", "leaf", "drop", "sticker", "pulse", "crescent", "mihrab", "agency", "summit", "beacon") DECKS = {"bag": ("arabic_training_bag", "bag.json"), "standalone": ("arabic_slides", "standalone.json")} SOFFICE = r"C:\Program Files\LibreOffice\program\soffice.exe" RATIOS = {"9:16": 9 / 16, "2:3": 2 / 3, "3:4": 3 / 4, "1:1": 1.0, "4:3": 4 / 3, "3:2": 3 / 2, "16:9": 16 / 9} THUMB = (640, 360) GRID = (4, 5) BUILDING = threading.Lock() def fake_picture(ratio: float, label: str, hue=(46, 92, 138)) -> bytes: """A grid, a centre circle and an arrow at each edge: whatever a crop takes shows.""" from PIL import Image, ImageDraw w = 1600 if ratio >= 1 else int(1600 * ratio) h = 1600 if ratio < 1 else int(1600 / ratio) img = Image.new("RGB", (w, h), (232, 238, 244)) d = ImageDraw.Draw(img) for x in range(0, w, 100): d.line([(x, 0), (x, h)], fill=(190, 202, 214), width=2) for y in range(0, h, 100): d.line([(0, y), (w, y)], fill=(190, 202, 214), width=2) r = min(w, h) // 4 d.ellipse([w / 2 - r, h / 2 - r, w / 2 + r, h / 2 + r], fill=hue) d.rectangle([4, 4, w - 5, h - 5], outline=(200, 40, 40), width=10) a = 70 for tip, base in (((w / 2, 12), ((w / 2 - a, 12 + a), (w / 2 + a, 12 + a))), ((w / 2, h - 12), ((w / 2 - a, h - 12 - a), (w / 2 + a, h - 12 - a))), ((12, h / 2), ((12 + a, h / 2 - a), (12 + a, h / 2 + a))), ((w - 12, h / 2), ((w - 12 - a, h / 2 - a), (w - 12 - a, h / 2 + a)))): d.polygon([tip, *base], fill=(200, 40, 40)) d.text((w / 2 - 40, h / 2), label, fill="white") out = io.BytesIO() img.save(out, "JPEG", quality=85) return out.getvalue() def fake_logo() -> bytes: from PIL import Image, ImageDraw img = Image.new("RGBA", (600, 200), (0, 0, 0, 0)) d = ImageDraw.Draw(img) d.rounded_rectangle([4, 4, 596, 196], 30, fill=(180, 60, 60, 255)) d.ellipse([40, 40, 160, 160], fill="white") d.rectangle([200, 80, 560, 120], fill="white") out = io.BytesIO() img.save(out, "PNG") return out.getvalue() def pictures_for(pipeline: str, outputs: dict, template: str) -> dict[str, bytes]: """What a person gets who asks for every picture: the plan's, and the day's infographics.""" from app.agents.arabic_bag.design.imagegen import INFO_LAYOUTS, info_layouts from app.agents.arabic_bag.design.images import pick_by_day from app.agents.slides_design.builder import infographic_candidates from app.agents.slides_design.pictures import aspect, plan from app.agents.slides_design.sources import SOURCES _, slides = SOURCES[pipeline](outputs) out = {s.key: fake_picture(RATIOS[aspect(template, s.kind)], s.key) for s in plan(slides, "all", activities=True)} picks = pick_by_day(infographic_candidates(slides), 99) for pick, layout in zip(picks, info_layouts(picks, template), strict=True): out[pick["key"]] = fake_picture(RATIOS[INFO_LAYOUTS[layout.key].aspect], pick["key"]) return out def slot_report(pptx: bytes, template: str) -> list[dict]: """Each picture on the slides: the box it fills, against the shape asked of the model.""" from app.agents.slides_design.pictures import aspect, boxes from pptx import Presentation rows, seen = [], set() asked = boxes(template) for number, slide in enumerate(Presentation(io.BytesIO(pptx)).slides, 1): pics = [s for s in slide.shapes if s.shape_type == 13 and s.name.startswith("Picture")] for pic in pics: names = [s.name for s in slide.shapes] kind = ("cover" if number == 1 else "session" if any(n.startswith("Big number") for n in names) else "divider") if kind in seen: continue seen.add(kind) w, h = pic.width / 914400, pic.height / 914400 geom = pic._element.spPr.find( "{http://schemas.openxmlformats.org/drawingml/2006/main}prstGeom") ratio = RATIOS[aspect(template, kind)] rows.append({"template": template, "kind": kind, "slide": number, "asked_box": list(asked[kind][:2]), "asked_shape": asked[kind][2], "asked_ratio": aspect(template, kind), "drawn_box": [round(w, 2), round(h, 2)], "drawn_shape": geom.get("prst") if geom is not None else "rect", "cut_share": round(1 - min(ratio, w / h) / max(ratio, w / h), 3)}) return rows def sheets(pdf: Path, out: Path, stem: str) -> int: import fitz from PIL import Image, ImageDraw doc = fitz.open(pdf) cols, rows = GRID per = cols * rows for start in range(0, len(doc), per): sheet = Image.new("RGB", (cols * (THUMB[0] + 8), rows * (THUMB[1] + 8)), "#666666") for i in range(start, min(start + per, len(doc))): page = doc[i] scale = THUMB[0] / page.rect.width pix = page.get_pixmap(matrix=fitz.Matrix(scale, scale), alpha=False) thumb = Image.frombytes("RGB", (pix.width, pix.height), pix.samples) ImageDraw.Draw(thumb).text((6, 4), str(i + 1), fill="red") n = i - start sheet.paste(thumb, ((n % cols) * (THUMB[0] + 8) + 4, (n // cols) * (THUMB[1] + 8) + 4)) sheet.save(out / f"{stem}_{start // per + 1:02d}.jpg", quality=82) return len(doc) def one(job: tuple) -> dict: from app.agents.slides_design.service import design_deck deck, pipeline, outputs, template, pics, logo, stage = job stem = f"{deck}_{template}_{'pics' if pics else 'nopics'}_{'logo' if logo else 'nologo'}" out = REVIEW / stage / deck / template out.mkdir(parents=True, exist_ok=True) # one at a time: icons.png writes its cache file in place, and a second # thread reading it half written stops with UnidentifiedImageError with BUILDING: images = pictures_for(pipeline, outputs, template) if pics else None result = design_deck(pipeline, outputs, template=template, author="أحمد محمد", org_name="جهة تجريبية", logo=fake_logo() if logo else None, make_pdf=False, images=images) pptx = out / f"{stem}.pptx" pptx.write_bytes(result.pptx) with tempfile.TemporaryDirectory() as profile: subprocess.run([SOFFICE, f"-env:UserInstallation=file:///{profile.replace(chr(92), '/')}", "--headless", "--convert-to", "pdf", "--outdir", str(out), str(pptx)], check=True, timeout=900, capture_output=True) pages = sheets(out / f"{stem}.pdf", out, stem) report = result.report print(f"{stem}: {pages} slides, overflow {len(report.get('overflow', []))}", flush=True) return {"file": stem, "deck": deck, "template": template, "pics": pics, "logo": logo, "slides": report.get("slides"), "kinds": report.get("kinds"), "overflow": report.get("overflow"), "missing": report.get("missing"), "slots": slot_report(result.pptx, template) if pics else []} def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("stage", choices=["before", "after"]) ap.add_argument("--only", nargs="*", default=list(TEMPLATES)) ap.add_argument("--workers", type=int, default=4) ap.add_argument("--smoke", action="store_true") args = ap.parse_args() if not Path(SOFFICE).exists() and not shutil.which("soffice"): raise SystemExit("LibreOffice is needed") jobs = [] for deck, (pipeline, name) in DECKS.items(): outputs = json.loads((REVIEW / "fixtures" / name).read_text(encoding="utf-8")) for template in args.only: for pics in (False, True): for logo in (False, True): jobs.append((deck, pipeline, outputs, template, pics, logo, args.stage)) jobs = jobs[:1] + jobs[3:4] if args.smoke else jobs with cf.ThreadPoolExecutor(args.workers) as ex: rows = list(ex.map(one, jobs)) (REVIEW / args.stage).mkdir(parents=True, exist_ok=True) (REVIEW / args.stage / "report.json").write_text( json.dumps(rows, ensure_ascii=False, indent=1), encoding="utf-8") slots = {(s["template"], s["kind"]): s for r in rows for s in r["slots"]} (REVIEW / args.stage / "slots.json").write_text( json.dumps(sorted(slots.values(), key=lambda s: (s["template"], s["kind"])), ensure_ascii=False, indent=1), encoding="utf-8") print("done", len(rows)) return 0 if __name__ == "__main__": raise SystemExit(main())