# app.py – encoder-only demo + pool-and-test prototype # ---------------------------------------------------- # launch: python app.py # UI: http://localhost:7860 import json, re, sys, math from pathlib import Path, PurePosixPath import torch, torch.nn.functional as F import gradio as gr, spaces from huggingface_hub import snapshot_download from bert_handler import create_handler_from_checkpoint # ------------------------------------------------------------------ # 0. One-time patch of auto_map in config.json # ------------------------------------------------------------------ REPO_ID = "AbstractPhil/bert-beatrix-2048" LOCAL_CKPT = "bert-beatrix-2048" snapshot_download( repo_id=REPO_ID, revision="main", local_dir=LOCAL_CKPT, local_dir_use_symlinks=False, ) cfg_path = Path(LOCAL_CKPT) / "config.json" cfg = json.loads(cfg_path.read_text()) auto_map = cfg.get("auto_map", {}) changed = False for k, v in auto_map.items(): if "--" in v: # strip “repo--” auto_map[k] = PurePosixPath(v.split("--", 1)[1]).as_posix() changed = True if changed: cfg_path.write_text(json.dumps(cfg, indent=2)) print("🛠️ Patched config.json → auto_map points to local modules") # ------------------------------------------------------------------ # 1. Load model + tokenizer with BERTHandler # ------------------------------------------------------------------ handler, full_model, tokenizer = create_handler_from_checkpoint(LOCAL_CKPT) full_model = full_model.eval().cuda() # pull encoder & embedding stack encoder = full_model.bert.encoder embeddings = full_model.bert.embeddings emb_weight = embeddings.word_embeddings.weight # <- correct tensor emb_ln = full_model.bert.emb_ln emb_drop = full_model.bert.emb_drop # ------------------------------------------------------------------ # 2. Symbolic roles # ------------------------------------------------------------------ SYMBOLIC_ROLES = [ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "