Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
datasets:
|
4 |
+
- TucanoBR/GigaVerbo-Text-Filter
|
5 |
+
language:
|
6 |
+
- pt
|
7 |
+
metrics:
|
8 |
+
- accuracy
|
9 |
+
library_name: xgboost
|
10 |
+
tags:
|
11 |
+
- text-quality
|
12 |
+
- portuguese
|
13 |
+
---
|
14 |
+
# XGBClassifier-text-filter
|
15 |
+
|
16 |
+
XGBClassifier-text-filter is a text-quality filter built on top of the [`xgboost`](https://xgboost.readthedocs.io/en/stable/) library. It uses the embeddings generated by [sentence-transformers/LaBSE](https://huggingface.co/sentence-transformers/LaBSE) as a feature vector.
|
17 |
+
|
18 |
+
This repository has the [source code](https://github.com/Nkluge-correa/Tucano) used to train this model.
|
19 |
+
|
20 |
+
## Usage
|
21 |
+
|
22 |
+
Here's an example of how to use the XGBClassifier-text-filter:
|
23 |
+
|
24 |
+
```python
|
25 |
+
from transformers import AutoTokenizer, AutoModel
|
26 |
+
from xgboost import XGBClassifier
|
27 |
+
import torch.nn.functional as F
|
28 |
+
import torch
|
29 |
+
|
30 |
+
def mean_pooling(model_output, attention_mask):
|
31 |
+
token_embeddings = model_output[0]
|
32 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
33 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
34 |
+
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/LaBSE")
|
36 |
+
embedding_model = AutoModel.from_pretrained("sentence-transformers/LaBSE")
|
37 |
+
device = ("cuda" if torch.cuda.is_available() else "cpu")
|
38 |
+
embedding_model.to(device)
|
39 |
+
|
40 |
+
bst = XGBClassifier({'device': device})
|
41 |
+
bst.load_model('/path/to/XGBClassifier-text-classifier.json')
|
42 |
+
|
43 |
+
def score_text(text, model):
|
44 |
+
|
45 |
+
encoded_input = tokenizer(text, padding=True, truncation=True, return_tensors='pt').to(device)
|
46 |
+
|
47 |
+
with torch.no_grad():
|
48 |
+
model_output = embedding_model(**encoded_input)
|
49 |
+
|
50 |
+
sentence_embedding = mean_pooling(model_output, encoded_input['attention_mask'])
|
51 |
+
|
52 |
+
embedding = F.normalize(sentence_embedding, p=2, dim=1).numpy()
|
53 |
+
score = model.predict(embedding)[0]
|
54 |
+
|
55 |
+
return score
|
56 |
+
|
57 |
+
score_text("Os tucanos são aves que correspondem à família Ramphastidae, vivem nas florestas tropicais da América Central e América do Sul. A família inclui cinco gêneros e mais de quarenta espécies diferentes. Possuem bicos notavelmente grandes e coloridos, que possuem a função de termorregulação para as muitas espécies que passam muito tempo na copa da floresta exposta ao sol tropical quente.", bst)
|
58 |
+
```
|
59 |
+
|
60 |
+
## Cite as 🤗
|
61 |
+
|
62 |
+
```latex
|
63 |
+
@misc{correa24tucano,
|
64 |
+
title = {{Tucano: Advancing Neural Text Generation for Portuguese}},
|
65 |
+
author = {Corr{\^e}a, Nicholas Kluge and Sen, Aniket and Falk, Sophia and Fatimah, Shiza},
|
66 |
+
journal={arXiv preprint arXiv:xxxx.xxxxx},
|
67 |
+
year={2024}
|
68 |
+
}
|
69 |
+
```
|
70 |
+
|
71 |
+
## Aknowlegments
|
72 |
+
|
73 |
+
We gratefully acknowledge the granted access to the [Marvin cluster](https://www.hpc.uni-bonn.de/en/systems/marvin) hosted by [University of Bonn](https://www.uni-bonn.de/en) along with the support provided by its High Performance Computing \& Analytics Lab.
|
74 |
+
|
75 |
+
## License
|
76 |
+
|
77 |
+
XGBClassifier-text-filter is licensed under the Apache License, Version 2.0. For more details, see the [LICENSE](./LICENSE) file.
|