# app.py – encoder-only demo for bert-beatrix-2048 # ----------------------------------------------- # launch: python app.py import spaces import torch import gradio as gr from huggingface_hub import snapshot_download from bert_handler import create_handler_from_checkpoint from pathlib import Path # ------------------------------------------------------------------ # 1. Download *once* and load locally ----------------------------- # ------------------------------------------------------------------ LOCAL_CKPT = snapshot_download( repo_id="AbstractPhil/bert-beatrix-2048", revision="main", local_dir="bert-beatrix-2048", local_dir_use_symlinks=False, ) cfg_path = Path(LOCAL_CKPT) / "config.json" with open(cfg_path) as f: cfg = json.load(f) auto_map = cfg.get("auto_map", {}) changed = False for k, v in auto_map.items(): # v looks like "AbstractPhil/bert-beatrix-2048--modeling_hf_nomic_bert.…" if "--" in v: auto_map[k] = PurePosixPath(v.split("--", 1)[1]).as_posix() changed = True if changed: cfg["auto_map"] = auto_map with open(cfg_path, "w") as f: json.dump(cfg, f, indent=2) print("🔧 Patched auto_map → now points to local modules only") # also drop any *previously* imported remote modules in this session for name in list(sys.modules): if name.startswith("transformers_modules.AbstractPhil.bert-beatrix-2048"): del sys.modules[name] # ------------------------------------------------------------------ # 1. normal load via BERTHandler --------------------------------- # ------------------------------------------------------------------ from bert_handler import create_handler_from_checkpoint handler, full_model, tokenizer = create_handler_from_checkpoint(LOCAL_CKPT) full_model = full_model.eval().cuda() # --- pull encoder & embeddings only -------------------------------- encoder = full_model.bert.encoder embeddings = full_model.bert.embeddings emb_ln = full_model.bert.emb_ln emb_drop = full_model.bert.emb_drop # ------------------------------------------------------------------ # 2. Symbolic token list ------------------------------------------ # ------------------------------------------------------------------ SYMBOLIC_ROLES = [ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "