# Updating the app to use only the encoder from the model, ensuring symbolic support import spaces from bert_handler import create_handler_from_checkpoint import torch import gradio as gr import re from pathlib import Path from huggingface_hub import snapshot_download # Load checkpoint using BERTHandler (loads tokenizer and full model) checkpoint_path = snapshot_download( repo_id="AbstractPhil/bert-beatrix-2048", revision="main", local_dir="bert-beatrix-2048", local_dir_use_symlinks=False ) handler, model, tokenizer = create_handler_from_checkpoint(checkpoint_path) model = model.eval().cuda() # Extract encoder only (NomicBertModel -> encoder) encoder = model.bert.encoder embeddings = model.bert.embeddings emb_ln = model.bert.emb_ln emb_drop = model.bert.emb_drop @spaces.GPU def encode_and_predict(text: str, selected_roles: list[str]): with torch.no_grad(): inputs = tokenizer(text, return_tensors="pt").to("cuda") input_ids = inputs.input_ids attention_mask = inputs.attention_mask # Run embedding + encoder pipeline x = embeddings(input_ids) x = emb_ln(x) x = emb_drop(x) encoded = encoder(x, attention_mask=attention_mask.bool()) symbolic_ids = [tokenizer.convert_tokens_to_ids(tok) for tok in selected_roles] symbolic_mask = torch.isin(input_ids, torch.tensor(symbolic_ids, device=input_ids.device)) masked_tokens = [tokenizer.convert_ids_to_tokens([tid])[0] for tid in input_ids[0] if tid in symbolic_ids] role_reprs = encoded[symbolic_mask].mean(dim=0) if symbolic_mask.any() else torch.zeros_like(encoded[0, 0]) return { "Symbolic Tokens": masked_tokens, "Embedding Norm": f"{role_reprs.norm().item():.4f}", "Symbolic Token Count": symbolic_mask.sum().item(), } symbolic_roles = [ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "