import subprocess, os, sys, urllib.request def sh(cmd): r = subprocess.run(cmd, shell=True, capture_output=True, text=True) if r.returncode != 0: print("CMD FAIL:", cmd, "\n", r.stderr[:800]) sys.exit(1) return r.stdout os.environ["GIT_LFS_SKIP_SMUDGE"] = "1" os.chdir("/tmp") sh("git clone -q https://user:%s@huggingface.co/spaces/bep40/vai-avatar vai 2>&1" % os.environ["HF_TOKEN"]) os.chdir("/tmp/vai") def load(name): return urllib.request.urlopen( "https://huggingface.co/datasets/bep40/vaistudio-data/resolve/main/" + name ).read().decode("utf-8") def apply_edits(path, edits): src = open(path, encoding="utf-8").read() for i, (old, new) in enumerate(edits): cnt = src.count(old) if cnt == 0: print("EDIT %d MISS in %s" % (i, path)) print(" old[:120]:", repr(old[:120])) sys.exit(2) if cnt > 1: print("EDIT %d AMBIG (%d) in %s" % (i, cnt, path)) sys.exit(3) src = src.replace(old, new, 1) print("EDIT %d OK in %s" % (i, path)) open(path, "w", encoding="utf-8").write(src) # ---- entry_ai_image.ts edits ---- ns = {} exec(compile(load("p_detect.py"), "p_detect.py", "exec"), ns) apply_edits("entry_ai_image.ts", ns["EDITS"]) ns = {} exec(compile(load("p_album.py"), "p_album.py", "exec"), ns) apply_edits("entry_ai_image.ts", ns["EDITS"]) ns = {} exec(compile(load("p_promo.py"), "p_promo.py", "exec"), ns) apply_edits("entry_ai_image.ts", ns["EDITS"]) print("=== entry_ai_image.ts patched, lines now:", len(open("entry_ai_image.ts").read().splitlines())) # ---- compile check with bun ---- print("=== downloading bun ===") sh("curl -sL https://github.com/oven-sh/bun/releases/latest/download/bun-linux-x64.zip -o /tmp/bun.zip") sh("unzip -q -o /tmp/bun.zip -d /tmp/bunbin") BUN = "/tmp/bunbin/bun-linux-x64/bun" print(sh("%s --version" % BUN).strip()) sh("%s install" % BUN) print("=== compiling entry_ai_image.ts ===") r = subprocess.run("%s build entry_ai_image.ts --outfile /tmp/eai.out 2>&1" % BUN, shell=True, capture_output=True, text=True) print(r.stdout[-3000:]) print("STDERR:", r.stderr[-3000:]) if r.returncode != 0: print("COMPILE FAIL") sys.exit(4) print("COMPILE OK")