This model is a Vietnamese adaptation of CLIP text encoder, trained on Vietnamese data.
1from transformers import AutoTokenizer
2from huggingface_hub import hf_hub_download
3import torch
4import torch.nn as nn
5import torch.nn.functional as F
6
7class PhoCLIPTextModel(nn.Module):
8 def __init__(self):
9 super().__init__()
10 # Load text encoder
11 self.text_encoder = AutoModel.from_pretrained("kienhoang123/ViCLIP")
12
13 # Load text projection head
14 state_dict = torch.load(hf_hub_download(repo_id="kienhoang123/ViCLIP", filename="model.pt"))
15 self.load_state_dict(state_dict)
16
17 def forward(self, input_ids, attention_mask=None):
18 # Get text embeddings
19 text_outputs = self.text_encoder(
20 input_ids=input_ids,
21 attention_mask=attention_mask,
22 return_dict=True
23 )
24 text_cls = text_outputs.last_hidden_state[:, 0, :]
25 text_proj = self.text_proj(text_cls)
26 return text_proj
27
28# Load tokenizer
29tokenizer = AutoTokenizer.from_pretrained("kienhoang123/ViCLIP")
30
31# Encode text
32text = "This is an example Vietnamese text"
33inputs = tokenizer(text, return_tensors="pt", padding="max_length", max_length=77, truncation=True)
34
35model = PhoCLIPTextModel()
36model.eval()
37
38with torch.no_grad():
39 embedding = model(inputs.input_ids, inputs.attention_mask)
40 normalized_embedding = F.normalize(embedding, p=2, dim=-1)