File size: 1,989 Bytes
ebf8f81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | """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 <html> 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 = ('<!doctype html>\n<html lang="en">\n'
'<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">\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 = '<script src="storyteller.js"></script>'
assert tag in page and "</script" not in engine
b64 = base64.b64encode(open(model, "rb").read()).decode()
single = page.replace(tag, f'<script type="application/octet-stream" id="model-b64">{b64}</script>\n<script>\n{engine}</script>')
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")
|