npedrazzini commited on
Commit
ebd9c90
·
verified ·
1 Parent(s): bf5fa66

Update continuous_time_embedding.py

Browse files
Files changed (1) hide show
  1. continuous_time_embedding.py +10 -8
continuous_time_embedding.py CHANGED
@@ -1,7 +1,7 @@
1
  """
2
  Companion module for loading the continuous-time NewsBERT model.
3
 
4
- This model is not a plain AutoModelForMaskedLM. It wraps a full
5
  fine-tuned BERT with a continuous sinusoidal (Fourier) time embedding
6
  injected at the input layer. You need this file to load and query it.
7
 
@@ -9,6 +9,7 @@ Usage:
9
  from continuous_time_embedding import load_continuous_time_model
10
 
11
  tokenizer, model = load_continuous_time_model("npedrazzini/NewsBERT_1800-1920-Temporal")
 
12
  """
13
 
14
  import os
@@ -18,7 +19,7 @@ import torch.nn as nn
18
  from transformers import AutoTokenizer, AutoModelForMaskedLM
19
  from huggingface_hub import snapshot_download
20
 
21
- BASE_MODEL_ID = "TextMachineProject/NewsBERT_1800-1920"
22
 
23
  MIN_YEAR = 1800.0
24
  MAX_YEAR = 1920.0
@@ -101,9 +102,6 @@ class ContinuousTimeBertForMLM(nn.Module):
101
 
102
 
103
  def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
104
- """Convenience loader. Works with either a local checkpoint directory or
105
- a Hugging Face Hub repo id (downloads it locally first).
106
- """
107
  if os.path.isdir(repo_id_or_path):
108
  local_dir = repo_id_or_path
109
  else:
@@ -112,12 +110,16 @@ def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
112
  tokenizer = None
113
  for candidate in (os.path.join(local_dir, "full_model"), local_dir):
114
  try:
115
- tokenizer = AutoTokenizer.from_pretrained(candidate)
116
- break
117
  except Exception:
118
  continue
 
 
 
 
 
119
  if tokenizer is None:
120
- print(f"[load_continuous_time_model] No tokenizer found in {repo_id_or_path}, "
121
  f"falling back to base model tokenizer: {BASE_MODEL_ID}")
122
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
123
 
 
1
  """
2
  Companion module for loading the continuous-time NewsBERT model.
3
 
4
+ This model is NOT a plain AutoModelForMaskedLM -- it wraps a full
5
  fine-tuned BERT with a continuous sinusoidal (Fourier) time embedding
6
  injected at the input layer. You need this file to load and query it.
7
 
 
9
  from continuous_time_embedding import load_continuous_time_model
10
 
11
  tokenizer, model = load_continuous_time_model("npedrazzini/NewsBERT_1800-1920-Temporal")
12
+
13
  """
14
 
15
  import os
 
19
  from transformers import AutoTokenizer, AutoModelForMaskedLM
20
  from huggingface_hub import snapshot_download
21
 
22
+ BASE_MODEL_ID = "TextMachineProject/NewsBERT_1800-1920"
23
 
24
  MIN_YEAR = 1800.0
25
  MAX_YEAR = 1920.0
 
102
 
103
 
104
  def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
 
 
 
105
  if os.path.isdir(repo_id_or_path):
106
  local_dir = repo_id_or_path
107
  else:
 
110
  tokenizer = None
111
  for candidate in (os.path.join(local_dir, "full_model"), local_dir):
112
  try:
113
+ candidate_tokenizer = AutoTokenizer.from_pretrained(candidate)
 
114
  except Exception:
115
  continue
116
+ if len(candidate_tokenizer) < 1000:
117
+ continue
118
+ tokenizer = candidate_tokenizer
119
+ break
120
+
121
  if tokenizer is None:
122
+ print(f"[load_continuous_time_model] No valid tokenizer found in {repo_id_or_path}, "
123
  f"falling back to base model tokenizer: {BASE_MODEL_ID}")
124
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
125