Views
No views yet
1import torch, re, unicodedata
2from transformers import AutoModel, AutoTokenizer
3
4model_name = "cnmoro/LexicalEmbed-Base"
5
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
8model.eval()
9
10def preprocess(text):
11 text = unicodedata.normalize('NFD', text)
12 text = ''.join(c for c in text if unicodedata.category(c) != 'Mn')
13 text = re.sub(r'[^\w\s]+', ' ', text.lower())
14 return re.sub(r'\s+', ' ', text).strip()
15
16texts = ["hello world", "hel wor"]
17texts = [ preprocess(s) for s in texts ]
18inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
19
20with torch.no_grad():
21 embeddings = model(**inputs)
22
23cosine_sim = torch.nn.functional.cosine_similarity(embeddings[0], embeddings[1], dim=0)
24print(f"Cosine Similarity: {cosine_sim.item()}") # 0.8966174125671387