Spaces:
Sleeping
Sleeping
File size: 998 Bytes
21a0de8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # embedding_utils.py
import numpy as np
import torch
import os
def load_glove_embeddings(vocab, glove_path="glove.6B.300d.txt"):
print(f"Loading GloVe embeddings from {glove_path}...")
# Initialize all with random vectors (fallback for words not found in GloVe)
torch.manual_seed(42)
embeddings = {word: torch.randn(300) for word in vocab}
if not os.path.exists(glove_path):
print(f"WARNING: {glove_path} not found. Using random embeddings. Please download GloVe.")
return embeddings
found_count = 0
with open(glove_path, 'r', encoding='utf-8') as f:
for line in f:
values = line.split()
word = values[0]
if word in vocab:
vector = np.asarray(values[1:], dtype='float32')
embeddings[word] = torch.from_numpy(vector)
found_count += 1
print(f"Successfully loaded {found_count}/{len(vocab)} words from GloVe.")
return embeddings |