Update README.md
Browse files
README.md
CHANGED
|
@@ -53,4 +53,60 @@ tags:
|
|
| 53 |
- Hakha_Chin
|
| 54 |
- Kabyle
|
| 55 |
- Sakha
|
| 56 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
- Hakha_Chin
|
| 54 |
- Kabyle
|
| 55 |
- Sakha
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
### Overview
|
| 60 |
+
This model supports the detection of **45** languages, and it's fine-tuned using **multilingual-e5-base** model on the **common-language** dataset.
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
### Download the model
|
| 64 |
+
```python
|
| 65 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 66 |
+
tokenizer = AutoTokenizer.from_pretrained('Mike0307/multilingual-e5-language-detection')
|
| 67 |
+
model = AutoModelForSequenceClassification.from_pretrained('Mike0307/multilingual-e5-language-detection', num_labels=45)
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
### Example of language detection
|
| 71 |
+
```python
|
| 72 |
+
import torch
|
| 73 |
+
|
| 74 |
+
languages = [
|
| 75 |
+
"Arabic", "Basque", "Breton", "Catalan", "Chinese_China", "Chinese_Hongkong",
|
| 76 |
+
"Chinese_Taiwan", "Chuvash", "Czech", "Dhivehi", "Dutch", "English",
|
| 77 |
+
"Esperanto", "Estonian", "French", "Frisian", "Georgian", "German", "Greek",
|
| 78 |
+
"Hakha_Chin", "Indonesian", "Interlingua", "Italian", "Japanese", "Kabyle",
|
| 79 |
+
"Kinyarwanda", "Kyrgyz", "Latvian", "Maltese", "Mongolian", "Persian", "Polish",
|
| 80 |
+
"Portuguese", "Romanian", "Romansh_Sursilvan", "Russian", "Sakha", "Slovenian",
|
| 81 |
+
"Spanish", "Swedish", "Tamil", "Tatar", "Turkish", "Ukranian", "Welsh"
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
def predict(text, model, tokenizer, device = torch.device('cpu')):
|
| 85 |
+
model.eval()
|
| 86 |
+
tokenized = tokenizer(text, padding='max_length', truncation=True, max_length=128, return_tensors="pt")
|
| 87 |
+
input_ids = tokenized['input_ids']
|
| 88 |
+
attention_mask = tokenized['attention_mask']
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
input_ids = input_ids.to(device)
|
| 91 |
+
attention_mask = attention_mask.to(device)
|
| 92 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 93 |
+
logits = outputs.logits
|
| 94 |
+
probabilities = torch.nn.functional.softmax(logits, dim=1)
|
| 95 |
+
return probabilities
|
| 96 |
+
|
| 97 |
+
def get_topk(probabilities, languages, k=3):
|
| 98 |
+
topk_prob, topk_indices = torch.topk(probabilities, k)
|
| 99 |
+
topk_prob = topk_prob.cpu().numpy()[0].tolist()
|
| 100 |
+
topk_indices = topk_indices.cpu().numpy()[0].tolist()
|
| 101 |
+
topk_labels = [languages[index] for index in topk_indices]
|
| 102 |
+
return topk_prob, topk_labels
|
| 103 |
+
|
| 104 |
+
text = "你的測試句子"
|
| 105 |
+
probabilities = predict(text, model, tokenizer)
|
| 106 |
+
topk_prob, topk_labels = get_topk(probabilities, languages)
|
| 107 |
+
print(topk_prob, topk_labels)
|
| 108 |
+
|
| 109 |
+
# [0.999620258808, 0.00025940246996469, 2.7690215574693e-05]
|
| 110 |
+
# ['Chinese_Taiwan', 'Chinese_Hongkong', 'Chinese_China']
|
| 111 |
+
```
|
| 112 |
+
|