Spaces:
Runtime error
Runtime error
Update 2.py
Browse files7 unmodified lines8910111213141516171819202122232411121362 unmodified lines76777893798081822 unmodified lines8586871028889909192931089495110969798998 unmodified lines1081091101251111121131147 unmodified lines
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Set the cache directory pathcache_dir = '/app/cache'
# Create the directory if it doesn't existif not os.path.exists(cache_dir): os.makedirs(cache_dir)
# Set the environment variableos.environ['HF_HOME'] = cache_dir
# Verify the environment variable is setprint(f"HF_HOME is set to: {os.environ['HF_HOME']}")
class Config: E, H, N, C, B, M, S, V, W, L, D = 512, 32, 1024, 256, 128, 20000, 2048, 1e5, 4000, 2e-4, .15
class MyDataset(Dataset):62 unmodified lines s[os.path.basename(r).replace('.', '_')].extend(parse_xml(os.path.join(r, file))) return DM(dict(s))
def create_embeddings_and_sentences(folder_path, model_name="pile-of-law/legalbert-large-1.7M-1"):def create_embeddings_and_sentences(folder_path, model_name="pile-of-law/legalbert-large-1.7M-1", max_length=512): t, m, embeddings, ds = AutoTokenizer.from_pretrained(model_name), AutoModel.from_pretrained(model_name), [], [] for r, d, f in os.walk(folder_path): for file in f:2 unmodified lines for e in root.iter(): if e.text: text = e.text.strip() i = t(text, return_tensors="pt", truncation=True, padding=True) i = t(text, return_tensors="pt", truncation=True, padding=True, max_length=max_length) with torch.no_grad(): embeddings.append(m(**i).last_hidden_state.mean(dim=1).numpy()) ds.append(text) return np.vstack(embeddings), ds
def query_vector_similarity(query, embeddings, ds, model_name="pile-of-law/legalbert-large-1.7M-2"):def query_vector_similarity(query, embeddings, ds, model_name="pile-of-law/legalbert-large-1.7M-2", max_length=512): t, m = AutoTokenizer.from_pretrained(model_name), AutoModel.from_pretrained(model_name) i = t(query, return_tensors="pt", truncation=True, padding=True) i = t(query, return_tensors="pt", truncation=True, padding=True, max_length=max_length) with torch.no_grad(): qe = m(**i).last_hidden_state.mean(dim=1).numpy() return [ds[i] for i in cosine_similarity(qe, embeddings)[0].argsort()[-5:][::-1]]8 unmodified lines return []
def main(): folder_path, model = 'data', create_model_from_folder('data') folder_path, model = 'data', create_model_from_folder('Xml_Data') logging.info(f"Created dynamic PyTorch model with sections: {list(model.s.keys())}") embeddings, ds = create_embeddings_and_sentences(folder_path) accelerator, optimizer, criterion, num_epochs = Accelerator(), torch.optim.Adam(model.parameters(), lr=0.001), nn.CrossEntropyLoss(), 102.py+5-19