Upload char_encoder.py with huggingface_hub
Browse files- char_encoder.py +23 -10
char_encoder.py
CHANGED
|
@@ -191,21 +191,34 @@ class CharEncoder(nn.Module):
|
|
| 191 |
from huggingface_hub import snapshot_download
|
| 192 |
local_dir = snapshot_download(
|
| 193 |
pretrained_dir_or_repo,
|
| 194 |
-
allow_patterns=["config.json", "prefix_encoder.pt"],
|
| 195 |
)
|
| 196 |
except (ImportError, ValueError):
|
| 197 |
local_dir = pretrained_dir_or_repo
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
config_path = os.path.join(local_dir, "config.json")
|
| 200 |
-
if
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
| 209 |
weights_path = os.path.join(local_dir, "prefix_encoder.pt")
|
| 210 |
if not os.path.isfile(weights_path):
|
| 211 |
raise FileNotFoundError(f"prefix_encoder.pt not found in {local_dir}")
|
|
|
|
| 191 |
from huggingface_hub import snapshot_download
|
| 192 |
local_dir = snapshot_download(
|
| 193 |
pretrained_dir_or_repo,
|
| 194 |
+
allow_patterns=["config.json", "prefix_encoder.pt", "char_vocab.json"],
|
| 195 |
)
|
| 196 |
except (ImportError, ValueError):
|
| 197 |
local_dir = pretrained_dir_or_repo
|
| 198 |
|
| 199 |
+
# Vocab size comes from char_vocab.json (the ground truth), not config.json.
|
| 200 |
+
# config.json can drift if it was hand-edited.
|
| 201 |
+
vocab_size = None
|
| 202 |
+
embedding_dim = EMBEDDING_DIM
|
| 203 |
+
vocab_path = os.path.join(local_dir, "char_vocab.json")
|
| 204 |
+
if os.path.isfile(vocab_path):
|
| 205 |
+
with open(vocab_path, encoding="utf-8") as f:
|
| 206 |
+
data = json.load(f)
|
| 207 |
+
if isinstance(data, dict) and "itos" in data:
|
| 208 |
+
vocab_size = len(data["itos"])
|
| 209 |
+
|
| 210 |
config_path = os.path.join(local_dir, "config.json")
|
| 211 |
+
if os.path.isfile(config_path):
|
| 212 |
+
with open(config_path, encoding="utf-8") as f:
|
| 213 |
+
cfg = json.load(f)
|
| 214 |
+
if vocab_size is None:
|
| 215 |
+
vocab_size = cfg.get("vocab_size", 45)
|
| 216 |
+
embedding_dim = cfg.get("embedding_dim", EMBEDDING_DIM)
|
| 217 |
+
|
| 218 |
+
if vocab_size is None:
|
| 219 |
+
raise FileNotFoundError(f"Cannot determine vocab_size from {local_dir}")
|
| 220 |
+
|
| 221 |
+
model = cls(vocab_size=vocab_size, embedding_dim=embedding_dim)
|
| 222 |
weights_path = os.path.join(local_dir, "prefix_encoder.pt")
|
| 223 |
if not os.path.isfile(weights_path):
|
| 224 |
raise FileNotFoundError(f"prefix_encoder.pt not found in {local_dir}")
|