"""The `tmic shrink` pages under shrink/: one per fleet fixture, and the map of model_type -> pages.""" import json import re import os import subprocess import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path from huggingface_hub import hf_hub_download # Relative and same-frame: the Hub shows the Space in a sandboxed iframe that blocks target="_top". BACK = '
\n' CI_MARK = "" CI_LINE = re.compile(r'\n') OLD_BACK = re.compile(r'\n') PAGE = """ import json, sys, warnings warnings.filterwarnings("ignore") from transformers_mic import shrink_view pin, out = sys.argv[1], sys.argv[2] report = shrink_view.analyze(shrink_view.load(pin)) open(out, "w").write(shrink_view.render(report)) open(out.replace(".html", ".json"), "w").write(json.dumps({"fixture": pin, "findings": shrink_view.verdict(report)})) """ def stale(fixtures, pages): """Pins whose page is missing or was rendered from another revision.""" out = [] for repo, revision in fixtures: record = pages / f"{repo.split('/')[-1]}.json" if not record.exists() or json.loads(record.read_text()).get("fixture") != f"{repo}@{revision}": out.append(f"{repo}@{revision}") return out def render(pins, pages, mic, python=sys.executable, workers=3): """Render pages with a Python whose transformers knows every fixture's model type (tmic from `mic`).""" env = {**os.environ, "HF_HUB_DISABLE_PROGRESS_BARS": "1", "PYTHONPATH": f"{mic}/src"} def one(pin): out = pages / f"{pin.split('/')[-1].split('@')[0]}.html" done = subprocess.run([python, "-c", PAGE, pin, str(out)], env=env, capture_output=True, text=True, timeout=600) return f"{pin}: {done.stderr.strip().splitlines()[-1]}" if done.returncode else "" with ThreadPoolExecutor(workers) as pool: return [line for line in pool.map(one, pins) if line] def ci_line(name, outcome): """One line on the fixture's results on Transformers main, linking the dashboard.""" from results import DAYS, FAILURES, fixture_url now, fails = outcome or (0, 0) state = ( f"failing now on main ({fails} failed checks in {DAYS} days)" if now else f"passing now; {fails} failed checks in {DAYS} days" if fails else f"no failure on main in {DAYS} days" ) return ( f'\n' ) def link_back(pages, outcomes=None): """Put a link to the coverage page, and the fixture's CI results, at the top of every shrink page.""" outcomes = outcomes or {} for page in pages.glob("*.html"): text = CI_LINE.sub("", OLD_BACK.sub("", page.read_text())) text = text.replace("