File size: 2,612 Bytes
7d29d04
 
 
938aac8
7d29d04
 
 
 
938aac8
 
08073b4
 
 
 
 
 
 
 
 
938aac8
9cc3c83
938aac8
08073b4
938aac8
9cc3c83
938aac8
08073b4
22b416a
f5476e4
22b416a
e1834a6
22b416a
 
 
e1834a6
83c6f72
 
7d29d04
 
 
6b5feee
 
 
 
7d29d04
 
 
 
 
 
 
 
208a846
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b4b15
 
208a846
 
 
 
 
 
 
a6b4b15
208a846
7d29d04
 
 
 
 
 
 
 
 
2f25700
7d29d04
 
 
0f81abe
b8bc2fc
a6b4b15
 
7d29d04
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
language:
- vi
license: apache-2.0
library_name: sentence-transformers
tags:
- cross-encoder
- rerank
datasets:
- unicamp-dl/mmarco
model-index:
- name: VietnameseCrossEncoder
  results:
  - task:
      type: text-classification
    dataset:
      name: MMarco (Vi-Dev)
      type: unicamp-dl/mmarco
    metrics:
    - type: NDCG
      value: 0
      name: NDCG@10
      verified: false
    - type: MRR
      value: 0
      name: MRR@10
      verified: false

widget:
- source_sentence: "That is a happy person"
  sentences:
    - "That is a happy dog"
    - "That is a very happy person"
    - "Today is a sunny day"
  example_title: "Happy"

pipeline_tag: sentence-similarity
---

## Installation
  - Install `pyvi` to word segment:

	- `pip install pyvi`
      
 -  Install `sentence-transformers` (recommend):
	
	- `pip install sentence-transformers`

 -  Install `transformers` (optional):

	- `pip install transformers`

## Pre-processing

```python
from pyvi import ViTokenizer

query = "UIT là gì?"
sentences = [
    "UIT là Trường Đại học Công nghệ Thông tin (ĐH CNTT), Đại học Quốc gia Thành phố Hồ Chí Minh (ĐHQG-HCM)",
    "Mô hình rerank — còn được gọi là cross-encoder — là một loại mô hình mà, khi được cung cấp một cặp truy vấn và tài liệu, sẽ đưa ra một điểm tương đồng.",
    "Việt Nam, quốc hiệu là Cộng hòa xã hội chủ nghĩa Việt Nam, là một quốc gia xã hội chủ nghĩa nằm ở cực Đông của bán đảo Đông Dương thuộc khu vực Đông Nam Á"
]

tokenized_query = ViTokenizer.tokenize(query)
tokenized_sentences = [ViTokenizer.tokenize(sent) for sent in sentences]

tokenized_pairs = [[tokenized_query, sent] for sent in tokenized_sentences]
```

## Usage with sentence-transformers

```python
from sentence_transformers import CrossEncoder
model = CrossEncoder('itdainb/vietnamese-cross-encoder', max_length=256)
scores = model.predict(tokenized_pairs)
```

## Usage with transformers

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained('itdainb/vietnamese-cross-encoder')
tokenizer = AutoTokenizer.from_pretrained('itdainb/vietnamese-cross-encoder')

features = tokenizer(tokenized_pairs, padding=True, truncation="longest_first", return_tensors="pt", max_length=256)

model.eval()
with torch.no_grad():
    model_predictions = model(**features, return_dict=True)
    logits = model_predictions.logits

    scores = [score[0] for score in logits]
    print(scores)
```