Upload 7 files
Browse files- README_language_classification.md +130 -0
- config.json +25 -0
- model.safetensors +3 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +56 -0
- vocab.txt +0 -0
README_language_classification.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BERT-Based Language Classification Model
|
| 2 |
+
|
| 3 |
+
This repository contains a fine-tuned BERT-based model for classifying text into different languages. The model is designed to identify the language of a given sentence and has been trained using the Hugging Face Transformers library. It supports post-training dynamic quantization for optimized performance in deployment environments.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Model Details
|
| 8 |
+
|
| 9 |
+
- **Model Name:** BERT Base for Language Classification
|
| 10 |
+
- **Model Architecture:** BERT Base
|
| 11 |
+
- **Task:** Language Identification
|
| 12 |
+
- **Dataset:** Custom Dataset with multilingual text samples
|
| 13 |
+
- **Quantization:** Dynamic Quantization (INT8)
|
| 14 |
+
- **Fine-tuning Framework:** Hugging Face Transformers
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## Usage
|
| 19 |
+
|
| 20 |
+
### Installation
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
pip install transformers torch
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
### Loading the Fine-tuned Model
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
from transformers import pipeline
|
| 30 |
+
|
| 31 |
+
# Load the model and tokenizer from saved directory
|
| 32 |
+
classifier = pipeline("text-classification", model="./saved_model", tokenizer="./saved_model")
|
| 33 |
+
|
| 34 |
+
# Example input
|
| 35 |
+
text = "Bonjour, comment allez-vous?"
|
| 36 |
+
|
| 37 |
+
# Get prediction
|
| 38 |
+
prediction = classifier(text)
|
| 39 |
+
print(f"Prediction: {prediction}")
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## Saving and Testing the Model
|
| 45 |
+
|
| 46 |
+
### Saving
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 50 |
+
|
| 51 |
+
model_checkpoint = "bert-base-uncased" # or your fine-tuned model path
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
| 53 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
|
| 54 |
+
|
| 55 |
+
# Save model and tokenizer
|
| 56 |
+
model.save_pretrained("./saved_model")
|
| 57 |
+
tokenizer.save_pretrained("./saved_model")
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### Testing
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from transformers import pipeline
|
| 64 |
+
|
| 65 |
+
classifier = pipeline("text-classification", model="./saved_model", tokenizer="./saved_model")
|
| 66 |
+
text = "Ceci est un exemple de texte."
|
| 67 |
+
print(classifier(text))
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
## Quantization
|
| 73 |
+
|
| 74 |
+
### Apply Dynamic Quantization
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
import torch
|
| 78 |
+
from transformers import AutoModelForSequenceClassification
|
| 79 |
+
|
| 80 |
+
model = AutoModelForSequenceClassification.from_pretrained("./saved_model")
|
| 81 |
+
|
| 82 |
+
# Apply dynamic quantization
|
| 83 |
+
quantized_model = torch.quantization.quantize_dynamic(
|
| 84 |
+
model, {torch.nn.Linear}, dtype=torch.qint8
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Save quantized model
|
| 88 |
+
quantized_model.save_pretrained("./quantized_model")
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Load and Test Quantized Model
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
from transformers import AutoTokenizer, pipeline
|
| 95 |
+
from transformers import AutoModelForSequenceClassification
|
| 96 |
+
|
| 97 |
+
tokenizer = AutoTokenizer.from_pretrained("./saved_model")
|
| 98 |
+
quantized_model = AutoModelForSequenceClassification.from_pretrained("./quantized_model")
|
| 99 |
+
|
| 100 |
+
classifier = pipeline("text-classification", model=quantized_model, tokenizer=tokenizer)
|
| 101 |
+
text = "Hola, ¿cómo estás?"
|
| 102 |
+
print(classifier(text))
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
## Repository Structure
|
| 108 |
+
|
| 109 |
+
```
|
| 110 |
+
.
|
| 111 |
+
├── saved_model/ # Fine-tuned Model
|
| 112 |
+
├── quantized_model/ # Quantized Model
|
| 113 |
+
├── language-clasification.ipynb
|
| 114 |
+
├── README.md # Documentation
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## Limitations
|
| 120 |
+
|
| 121 |
+
- The model performance may vary for low-resource or underrepresented languages in the training dataset.
|
| 122 |
+
- Quantization may slightly reduce accuracy, but improves inference efficiency.
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## Contributing
|
| 127 |
+
|
| 128 |
+
Feel free to submit issues or pull requests to enhance performance, accuracy, or add new language support.
|
| 129 |
+
|
| 130 |
+
---
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"BertForSequenceClassification"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"gradient_checkpointing": false,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"intermediate_size": 3072,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"max_position_embeddings": 512,
|
| 15 |
+
"model_type": "bert",
|
| 16 |
+
"num_attention_heads": 12,
|
| 17 |
+
"num_hidden_layers": 12,
|
| 18 |
+
"pad_token_id": 0,
|
| 19 |
+
"position_embedding_type": "absolute",
|
| 20 |
+
"torch_dtype": "float16",
|
| 21 |
+
"transformers_version": "4.51.3",
|
| 22 |
+
"type_vocab_size": 2,
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 30522
|
| 25 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2482cf07a839e4f091ada914e740ad10849999df90e38f157b86d5d0c8a710da
|
| 3 |
+
size 218990972
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"added_tokens_decoder": {
|
| 3 |
+
"0": {
|
| 4 |
+
"content": "[PAD]",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false,
|
| 9 |
+
"special": true
|
| 10 |
+
},
|
| 11 |
+
"100": {
|
| 12 |
+
"content": "[UNK]",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false,
|
| 17 |
+
"special": true
|
| 18 |
+
},
|
| 19 |
+
"101": {
|
| 20 |
+
"content": "[CLS]",
|
| 21 |
+
"lstrip": false,
|
| 22 |
+
"normalized": false,
|
| 23 |
+
"rstrip": false,
|
| 24 |
+
"single_word": false,
|
| 25 |
+
"special": true
|
| 26 |
+
},
|
| 27 |
+
"102": {
|
| 28 |
+
"content": "[SEP]",
|
| 29 |
+
"lstrip": false,
|
| 30 |
+
"normalized": false,
|
| 31 |
+
"rstrip": false,
|
| 32 |
+
"single_word": false,
|
| 33 |
+
"special": true
|
| 34 |
+
},
|
| 35 |
+
"103": {
|
| 36 |
+
"content": "[MASK]",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false,
|
| 41 |
+
"special": true
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"clean_up_tokenization_spaces": false,
|
| 45 |
+
"cls_token": "[CLS]",
|
| 46 |
+
"do_lower_case": true,
|
| 47 |
+
"extra_special_tokens": {},
|
| 48 |
+
"mask_token": "[MASK]",
|
| 49 |
+
"model_max_length": 512,
|
| 50 |
+
"pad_token": "[PAD]",
|
| 51 |
+
"sep_token": "[SEP]",
|
| 52 |
+
"strip_accents": null,
|
| 53 |
+
"tokenize_chinese_chars": true,
|
| 54 |
+
"tokenizer_class": "BertTokenizer",
|
| 55 |
+
"unk_token": "[UNK]"
|
| 56 |
+
}
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|