rafaelm47labs
commited on
Commit
•
9439693
1
Parent(s):
9aa3671
Update README.md
Browse files
README.md
CHANGED
@@ -46,9 +46,69 @@ Columns: idTask,task content 1,idTag,tag.
|
|
46 |
|
47 |
## Example of Use
|
48 |
|
|
|
49 |
|
|
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
## Finetune Hyperparameters
|
|
|
46 |
|
47 |
## Example of Use
|
48 |
|
49 |
+
### Pipeline
|
50 |
|
51 |
+
```{python}
|
52 |
|
53 |
+
import torch
|
54 |
+
from transformers import AutoTokenizer, BertForSequenceClassification,TextClassificationPipeline
|
55 |
+
|
56 |
+
|
57 |
+
review_text = 'los vehiculos que esten esperando pasajaeros deberan estar apagados para reducir emisiones'
|
58 |
+
path = "M47Labs/spanish_news_classification_headlines"
|
59 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
60 |
+
model = BertForSequenceClassification.from_pretrained(path)
|
61 |
+
|
62 |
+
|
63 |
+
nlp = TextClassificationPipeline(task = "text-classification",
|
64 |
+
model = model,
|
65 |
+
tokenizer = tokenizer)
|
66 |
+
|
67 |
+
print(nlp(review_text))
|
68 |
+
|
69 |
+
```
|
70 |
+
```[{'label': 'medio_ambiente', 'score': 0.5648820996284485}]```
|
71 |
+
|
72 |
+
### Pytorch
|
73 |
+
|
74 |
+
```{python}
|
75 |
+
|
76 |
+
model_name = 'M47Labs/spanish_news_classification_headlines'
|
77 |
+
MAX_LEN = 32
|
78 |
+
|
79 |
+
|
80 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
81 |
+
|
82 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
83 |
+
|
84 |
+
texto = "las emisiones estan bajando, debido a las medidas ambientales tomadas por el gobierno"
|
85 |
+
|
86 |
+
|
87 |
+
encoded_review = tokenizer.encode_plus(
|
88 |
+
texto,
|
89 |
+
max_length=MAX_LEN,
|
90 |
+
add_special_tokens=True,
|
91 |
+
#return_token_type_ids=False,
|
92 |
+
pad_to_max_length=True,
|
93 |
+
return_attention_mask=True,
|
94 |
+
return_tensors='pt',
|
95 |
+
)
|
96 |
+
|
97 |
+
input_ids = encoded_review['input_ids']#.to(device)
|
98 |
+
attention_mask = encoded_review['attention_mask']#.to(device)
|
99 |
+
output = model(input_ids, attention_mask)
|
100 |
+
|
101 |
+
_, prediction = torch.max(output['logits'], dim=1)
|
102 |
+
print(f'Review text: {texto}')
|
103 |
+
|
104 |
+
print(f'Sentiment : {model.config.id2label[prediction.detach().cpu().numpy()[0]]}')
|
105 |
+
|
106 |
+
```
|
107 |
+
```Review text: las emisiones estan bajando, debido a las medidas ambientales tomadas por el gobierno
|
108 |
+
Sentiment : medio_ambiente```
|
109 |
+
|
110 |
+
|
111 |
+
A more in depth example on how to use the model can be found in this colab notebook: https://colab.research.google.com/drive/1XsKea6oMyEckye2FePW_XN7Rf8v41Cw_?usp=sharing
|
112 |
|
113 |
|
114 |
## Finetune Hyperparameters
|