Commit
·
0ee6042
1
Parent(s):
fb5d1a8
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Transformation spoken text to written text
|
2 |
+
|
3 |
+

|
4 |
+
|
5 |
+
```python
|
6 |
+
import torch
|
7 |
+
import model_handling
|
8 |
+
from data_handling import DataCollatorForNormSeq2Seq
|
9 |
+
from model_handling import EncoderDecoderSpokenNorm
|
10 |
+
import os
|
11 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
12 |
+
```
|
13 |
+
|
14 |
+
# Init tokenizer and model
|
15 |
+
|
16 |
+
|
17 |
+
```python
|
18 |
+
tokenizer = model_handling.init_tokenizer()
|
19 |
+
model = EncoderDecoderSpokenNorm.from_pretrained('nguyenvulebinh/spoken-norm', cache_dir=model_handling.cache_dir)
|
20 |
+
data_collator = DataCollatorForNormSeq2Seq(tokenizer)
|
21 |
+
```
|
22 |
+
|
23 |
+
# Infer sample
|
24 |
+
|
25 |
+
|
26 |
+
```python
|
27 |
+
bias_list = ['scotland', 'covid', 'delta', 'beta']
|
28 |
+
input_str = 'ngày hai tám tháng tư cô vít bùng phát ở sờ cốt lờn chiếm tám mươi phần trăm là biến chủng đen ta và bê ta'
|
29 |
+
```
|
30 |
+
|
31 |
+
|
32 |
+
```python
|
33 |
+
inputs = tokenizer([input_str])
|
34 |
+
input_ids = inputs['input_ids']
|
35 |
+
attention_mask = inputs['attention_mask']
|
36 |
+
if len(bias_list) > 0:
|
37 |
+
bias = data_collator.encode_list_string(bias_list)
|
38 |
+
bias_input_ids = bias['input_ids']
|
39 |
+
bias_attention_mask = bias['attention_mask']
|
40 |
+
else:
|
41 |
+
bias_input_ids = None
|
42 |
+
bias_attention_mask = None
|
43 |
+
|
44 |
+
inputs = {
|
45 |
+
"input_ids": torch.tensor(input_ids),
|
46 |
+
"attention_mask": torch.tensor(attention_mask),
|
47 |
+
"bias_input_ids": bias_input_ids,
|
48 |
+
"bias_attention_mask": bias_attention_mask,
|
49 |
+
}
|
50 |
+
```
|
51 |
+
|
52 |
+
## Format input text **with** bias phrases
|
53 |
+
|
54 |
+
|
55 |
+
```python
|
56 |
+
outputs = model.generate(**inputs, output_attentions=True, num_beams=1, num_return_sequences=1)
|
57 |
+
|
58 |
+
for output in outputs.cpu().detach().numpy().tolist():
|
59 |
+
# print('\n', tokenizer.decode(output, skip_special_tokens=True).split(), '\n')
|
60 |
+
print(tokenizer.sp_model.DecodePieces(tokenizer.decode(output, skip_special_tokens=True).split()))
|
61 |
+
```
|
62 |
+
|
63 |
+
28/4 covid bùng phát ở scotland chiếm 80 % là biến chủng delta và beta
|
64 |
+
|
65 |
+
|
66 |
+
## Format input text **without** bias phrases
|
67 |
+
|
68 |
+
|
69 |
+
```python
|
70 |
+
outputs = model.generate(**{
|
71 |
+
"input_ids": torch.tensor(input_ids),
|
72 |
+
"attention_mask": torch.tensor(attention_mask),
|
73 |
+
"bias_input_ids": None,
|
74 |
+
"bias_attention_mask": None,
|
75 |
+
}, output_attentions=True, num_beams=1, num_return_sequences=1)
|
76 |
+
|
77 |
+
for output in outputs.cpu().detach().numpy().tolist():
|
78 |
+
# print('\n', tokenizer.decode(output, skip_special_tokens=True).split(), '\n')
|
79 |
+
print(tokenizer.sp_model.DecodePieces(tokenizer.decode(output, skip_special_tokens=True).split()))
|
80 |
+
```
|
81 |
+
|
82 |
+
28/4 cô vít bùng phát ở sờ cốt lờn chiếm 80 % là biến chủng đen ta và bê ta
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
```python
|
87 |
+
|
88 |
+
```
|