"""Build ready-to-use copies of the demo page.
python build.py # uses web/storyteller.bin
Makes:
dist/pocket-storyteller.html one self-contained file (page + engine + model). Double-click to open.
dist/web/ the same demo as three files (index.html, storyteller.js,
storyteller.bin) to put on any web host or copy into another app.
The page source is web/index.html. It is written without the usual wrapper, so this
script adds one.
"""
import base64, os, shutil, sys
model = sys.argv[1] if len(sys.argv) > 1 else "web/storyteller.bin"
page = open("web/index.html", encoding="utf-8").read()
engine = open("web/storyteller.js", encoding="utf-8").read()
HEAD = ('\n\n'
'\n')
os.makedirs("dist/web", exist_ok=True)
# 1. three files to host
urls = 'const MODEL_URLS = ["storyteller.wasm", "storyteller.b64.txt"];'
assert urls in page, "web/index.html no longer has the MODEL_URLS line this script looks for"
open("dist/web/index.html", "w", encoding="utf-8").write(HEAD + page.replace(urls, 'const MODEL_URLS = ["storyteller.bin"];'))
shutil.copyfile("web/storyteller.js", "dist/web/storyteller.js")
shutil.copyfile(model, "dist/web/storyteller.bin")
# 2. one self-contained file: the model is embedded as base64 text, the engine is pasted in
tag = ''
assert tag in page and "{b64}\n')
open("dist/pocket-storyteller.html", "w", encoding="utf-8").write(HEAD + single)
for f in ("dist/pocket-storyteller.html", "dist/web/index.html", "dist/web/storyteller.js", "dist/web/storyteller.bin"):
print(f"{f}: {os.path.getsize(f)/1e6:.2f} MB")