Download serve.py from Einsia/LitGraph: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/datasets/Einsia/LitGraph/resolve/main/serve.py
- Command line
-
hf download hf://datasets/Einsia/LitGraph/serve.py
-
curl -L -o serve.py https://huggingface.co/datasets/Einsia/LitGraph/resolve/main/serve.py
2.44 kB
| """Verify and run a downloaded LitGraph snapshot; Python 3.10+ standard library.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| from pathlib import Path | |
| import sqlite3 | |
| import sys | |
| import tempfile | |
| ROOT = Path(__file__).resolve().parent | |
| def digest(path): | |
| h = hashlib.sha256() | |
| with path.open('rb') as stream: | |
| for block in iter(lambda: stream.read(8 * 1024 * 1024), b''): | |
| h.update(block) | |
| return h.hexdigest() | |
| def prepare(work_dir): | |
| manifest = json.loads((ROOT / 'manifest.json').read_text()) | |
| for name in ('snapshot/library.db', 'snapshot/api_index.db'): | |
| if digest(ROOT / name) != manifest['files'][name]['sha256']: | |
| raise ValueError('Checksum mismatch: ' + name) | |
| sys.path.insert(0, str(ROOT / 'api')) | |
| from library_api_index import fingerprint | |
| db = ROOT / 'snapshot/library.db' | |
| canonical = ROOT / 'snapshot/api_index.db' | |
| work_dir.mkdir(parents=True, exist_ok=True) | |
| # A download changes timestamps. Rebind only a verified local index copy. | |
| fd, temp_name = tempfile.mkstemp(prefix='index-', suffix='.db', dir=work_dir) | |
| os.close(fd) | |
| temporary = Path(temp_name) | |
| try: | |
| with sqlite3.connect(canonical.as_uri() + '?mode=ro', uri=True) as source: | |
| with sqlite3.connect(temporary) as dest: | |
| source.backup(dest) | |
| dest.execute("UPDATE metadata SET value=? WHERE key='source'", (json.dumps(fingerprint(db)),)) | |
| dest.commit() | |
| target = work_dir / 'api_index.db' | |
| os.replace(temporary, target) | |
| finally: | |
| temporary.unlink(missing_ok=True) | |
| return db, target | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--host', default='127.0.0.1') | |
| parser.add_argument('--port', type=int, default=8895) | |
| parser.add_argument('--work-dir', type=Path, default=ROOT / '.runtime') | |
| parser.add_argument('--verify-only', action='store_true') | |
| args = parser.parse_args() | |
| db, index = prepare(args.work_dir.resolve()) | |
| if args.verify_only: | |
| from library_api import Library | |
| print(json.dumps(Library(db, index).stats(), indent=2)) | |
| return | |
| from library_api import main as serve | |
| sys.argv = ['library_api.py', 'serve', '--db', str(db), '--index', str(index), | |
| '--public', '--host', args.host, '--port', str(args.port)] | |
| serve() | |
| if __name__ == '__main__': | |
| main() | |