Views
No views yet
[ENTITY] token for anonymizing sensitive patient information.1from transformers import AutoTokenizer, AutoModel
2import torch
3import torch.nn.functional as F
4
5# Load model (trust_remote_code=True required for custom model)
6tokenizer = AutoTokenizer.from_pretrained("nikhil061307/contrastive-learning-bert-added-token-v5")
7model = AutoModel.from_pretrained("nikhil061307/contrastive-learning-bert-added-token-v5", trust_remote_code=True)
8
9def get_clinical_embeddings(texts, max_length=256):
10 """Get embeddings for clinical texts with [ENTITY] support."""
11 inputs = tokenizer(
12 texts,
13 padding=True,
14 truncation=True,
15 max_length=max_length,
16 return_tensors='pt'
17 )
18
19 # Use the model's custom encode method
20 with torch.no_grad():
21 embeddings = model.encode(inputs['input_ids'], inputs['attention_mask'])
22
23 return embeddings
24
25# Example with [ENTITY] token for anonymization
26clinical_texts = [
27 "Patient [ENTITY] presents with chest pain and shortness of breath.",
28 "Patient [ENTITY] reports severe headache lasting 3 days.",
29 "Patient [ENTITY] diagnosed with acute myocardial infarction."
30]
31
32embeddings = get_clinical_embeddings(clinical_texts)
33print(f"Embeddings shape: {embeddings.shape}")
34
35# Calculate similarities
36similarity_matrix = torch.mm(embeddings, embeddings.t())
37print(f"Similarity between first two texts: {similarity_matrix[0,1]:.4f}")trust_remote_code=True when loading