CodeIsAbstract commited on
Commit
9b1e4dc
·
verified ·
1 Parent(s): 80c9d14

Upload modal_notebook_options.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. modal_notebook_options.md +23 -7
modal_notebook_options.md CHANGED
@@ -93,15 +93,31 @@ print("pushed to https://huggingface.co/CodeIsAbstract/sanskrit-sandhi-boundary-
93
 
94
  ## Cell 5 (optional) — quick sanity check on a phrase
95
 
 
 
 
 
96
  ```python
97
  import os, torch
98
- from huggingface_hub import hf_hub_download
99
- hf_hub_download("CodeIsAbstract/sanskrit-sandhi-boundaries-v2", "decode_best.py",
100
- repo_type="dataset", local_dir=".")
101
- from decode_best import load_model, predict
102
-
103
- model, delay = load_model("checkpoints/delayed_model_save_fixed_delay2", torch.device("cuda"))
104
- print(predict(model, "satyamevajayatenAnftaM", delay=delay))
 
 
 
 
 
 
 
 
 
 
 
 
105
  ```
106
 
107
  ---
 
93
 
94
  ## Cell 5 (optional) — quick sanity check on a phrase
95
 
96
+ `decode_best.py` needs the full local Sanskrit stack (sanskrit_parser, vidyut,
97
+ SanskritTokenizer) that only exists on your Mac — don't import it here.
98
+ This lightweight check only needs what Cell 2 already downloaded:
99
+
100
  ```python
101
  import os, torch
102
+ from model import HybridTimeScaleConfig
103
+ from train_split_model import BinaryBoundaryModel, char_to_idx, PAD_ID
104
+ from safetensors.torch import load_file
105
+
106
+ ckpt = "checkpoints/delayed_model_save_fixed_delay2"
107
+ cfg = HybridTimeScaleConfig.from_pretrained(ckpt) # config.json written by the trainer
108
+ model = BinaryBoundaryModel(cfg)
109
+ model.load_state_dict(load_file(os.path.join(ckpt, "model.safetensors")), strict=False)
110
+ model.eval().to(torch.device("cuda"))
111
+
112
+ text = "satyamevajayatenAnftaM"
113
+ ids = torch.tensor([[char_to_idx.get(c, PAD_ID) for c in text] + [PAD_ID]*2],
114
+ dtype=torch.long, device="cuda")
115
+ mask = torch.ones(1, len(text)+2, dtype=torch.long, device="cuda")
116
+ with torch.no_grad():
117
+ p = torch.sigmoid(model(input_ids=ids, attention_mask=mask)["logits"].squeeze(0)).cpu()
118
+
119
+ print("boundary probs:", [f"{p[i]:.2f}" for i in range(len(text))])
120
+ print("predicted split:", "".join(c + (" " if p[i] > 0.5 else "") for i, c in enumerate(text)))
121
  ```
122
 
123
  ---