Views
No views yet
transformers library for more custom usage, as shown in the example below.pip install transformers1from transformers import AutoModelForSequenceClassification, XLMRobertaTokenizer
2import torch
3
4# Load tokenizer and model
5tokenizer = XLMRobertaTokenizer.from_pretrained("LocalDoc/language_detection")
6model = AutoModelForSequenceClassification.from_pretrained("LocalDoc/language_detection")
7
8# Prepare text
9text = "Əlqasım oğulları vorzakondu"
10encoded_input = tokenizer(text, return_tensors='pt', truncation=True, max_length=512)
11
12# Prediction
13model.eval()
14with torch.no_grad():
15 outputs = model(**encoded_input)
16
17# Process the outputs
18logits = outputs.logits
19probabilities = torch.nn.functional.softmax(logits, dim=-1)
20predicted_class_index = probabilities.argmax().item()
21labels = ["az", "ar", "bg", "de", "el", "en", "es", "fr", "hi", "it", "ja", "nl", "pl", "pt", "ru", "sw", "th", "tr", "ur", "vi", "zh"]
22predicted_label = labels[predicted_class_index]
23print(f"Predicted Language: {predicted_label}")| Label | Language Code | Language Name |
|---|---|---|
| LABEL_0 | az | Azerbaijani |
| LABEL_1 | ar | Arabic |
| LABEL_2 | bg | Bulgarian |
| LABEL_3 | de | German |
| LABEL_4 | el | Greek |
| LABEL_5 | en | English |
| LABEL_6 | es | Spanish |
| LABEL_7 | fr | French |
| LABEL_8 | hi | Hindi |
| LABEL_9 | it | Italian |
| LABEL_10 | ja | Japanese |
| LABEL_11 | nl | Dutch |
| LABEL_12 | pl | Polish |
| LABEL_13 | pt | Portuguese |
| LABEL_14 | ru | Russian |
| LABEL_15 | sw | Swahili |
| LABEL_16 | th | Thai |
| LABEL_17 | tr | Turkish |
| LABEL_18 | ur | Urdu |
| LABEL_19 | vi | Vietnamese |
| LABEL_20 | zh | Chinese |
Epoch 1: Training Loss: 0.0127, Validation Loss: 0.0174, Accuracy: 0.9966, F1 Score: 0.9966
Epoch 2: Training Loss: 0.0149, Validation Loss: 0.0141, Accuracy: 0.9973, F1 Score: 0.9973
Epoch 3: Training Loss: 0.0001, Validation Loss: 0.0109, Accuracy: 0.9984, F1 Score: 0.9984Loss: 0.0133
Accuracy: 0.9975
F1 Score: 0.9975
Precision: 0.9975
Recall: 0.9975
Evaluation Time: 17.5 seconds
Samples per Second: 599.685
Steps per Second: 9.424