Download code/build.py from Redhanuman/pocket-storyteller: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/build.py
- Command line
-
hf download hf://Redhanuman/pocket-storyteller/code/build.py
-
curl -L -o build.py https://huggingface.co/Redhanuman/pocket-storyteller/resolve/main/code/build.py
1.99 kB
| """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") | |