This project focuses on classifying Bangla news articles into predefined categories using a fine-tuned BERT-based language model (sagorsarker/bangla-bert-base), Bangla-Electra and also banglabert_small then create a FlaskAPI for most efficient performing model
0
stars
17
commits
Jupyter Notebook
primary language
May 31, 2025
updated
This project focuses on classifying Bangla news articles into predefined categories using a fine-tuned BERT-based language model (sagorsarker/bangla-bert-base), Bangla-Electra and mBert.
The reason for choosing these model is that they performed state of the art in text/nlp classification task which you can check on the huggingface sagorsarker/bangla-bert-base: https://huggingface.co/sagorsarker/bangla-bert-base here you will find that Bangla Bert Base has. outperformed the other two model and Bangla Benchmarks in https://colab.research.google.com/drive/1vltPI81atzRvlALv4eCvEB0KdFoEaCOb#scrollTo=pqsH5gU-QxVf we will also try csebuetnlp/banglabert_small because of its very low number of parameters(13M) just to check how it performs in comparison the other three.
The workflow includes preprocessing raw Bangla text, removing irrelevant tokens, training a transformer model with early stopping, and visualizing word distributions across categories.
The aim is to develop an accurate and robust model that understands Bangla language nuances and effectively categorizes news content for downstream NLP applications.
Here’s a revised version of the "Data Load and Exploration", "Data Cleaning", "Feature Engineering", and "Model Setup and Training" sections. The rewrite focuses on what has been done and what has been achieved, emphasizing your techniques and decisions, not the detailed step-by-step process.
Below is a screenshot of the product's user interface where users can input Bangla news text and instantly see the predicted category:

The dataset was inspected and cleaned by discarding irrelevant columns such as reporter, which contained a large number of null values and had no influence on the classification task. Only the title, content, and category fields were retained for modeling.
An initial analysis confirmed that the dataset is balanced across four distinct news categories. Additionally, content strings were analyzed to detect and eliminate repetitive, irrelevant phrases like “ছবি: সংগৃহীত” and “ফাইল ছবি”, which often appear at the beginning of articles but offer no contextual value for classification.
To enhance the quality of input data, punctuation and Bangla stopwords were removed from the content. Stopwords were merged from multiple sources to ensure comprehensive coverage. This significantly reduced noise and preserved only meaningful tokens, especially important when dealing with a token limit of 512.
Additional low-value terms (e.g., সাল, মিনিট) that were domain-irrelevant were identified and excluded to further refine the dataset. This targeted cleaning approach ensured higher-quality inputs for the model and reduced the risk of truncating informative tokens during tokenization.
The data was restructured to create a new processed text field that combines the cleaned and filtered content. Once finalized, the original unprocessed content field was removed to optimize memory usage and keep the dataset concise.
The cleaned dataset was prepared for training by tokenizing the text using the sagorsarker/bangla-bert-base model. The label space was mapped, and the model architecture was adapted for multi-class classification with four output categories.
The model was fine-tuned using a robust training loop that incorporates early stopping to prevent overfitting. A linear learning rate scheduler was employed to stabilize learning and optimize convergence. This approach resulted in a well-generalized model capable of accurately classifying Bangla news content across all categories.
The models is trained for up to num_epochs= 10 , but all of these stopped earlier as their validation loss does not improve significantly for a number of patience consecutive epochs. This helps prevent overfitting and saves training time.
threshold, the model's state is saved and the patience_counter is reset.patience_counter is incremented.patience_counter exceeds the allowed patience, training is stopped early.This strategy ensures efficient training and prevents the model from overfitting to the training data.
In this step, we load the trained Bangla BERT model and tokenizer to evaluate it on the test data. This setup is crucial for making predictions and analyzing the model's performance using various metrics.
"sagorsarker/bangla-bert-base", a pretrained Bangla BERT model.4 corresponding to the categories: sports, international, entertainment, national.GPU if available, otherwise defaults to CPU.label2id: A dictionary mapping category names to numerical IDs.id2label: The inverse mapping to convert prediction IDs back to readable category names.This mapping is necessary for both training and interpreting model predictions.
banglabert_tokenizer directory.banglabert_category_model.pt).model.load_state_dict(torch.load("banglabert_category_model.pt", map_location=device))
After training and saving the best model, we now evaluate its performance on the test dataset. This step helps assess how well the model generalizes to unseen data.
Here's a clear comparison of all three Bangla news classification models i have tested:
| Model | Accuracy | Macro F1 | Weighted F1 |
|---|---|---|---|
| BanglaBERT-Small ✅ | 0.9723 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.9647 | 0.96 | 0.96 |
| Bangla-Electra | 0.9496 | 0.95 | 0.95 |
| Model | Sports | International | Entertainment | National |
|---|---|---|---|---|
| BanglaBERT-Small ✅ | 0.99 | 0.96 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.99 | 0.95 | 0.96 | 0.96 |
| Bangla-Electra | 0.97 | 0.95 | 0.93 | 0.95 |
argmax.with torch.no_grad():
for batch in test_loader:
...
preds = torch.argmax(outputs.logits, dim=1)
...
The BanglaBERT-based text classification model achieved an impressive accuracy of 96.47% on the test dataset.
sports, international, entertainment, national) have high precision, recall, and F1-scores, indicating that the model performs well across all classes.sports category (F1-score: 0.99), and consistently strong results on the other categories as well.This suggests that the model generalizes well and is highly effective for multilingual news classification in Bangla.
The confusion matrix provides a detailed breakdown of the model’s classification performance across the four news categories:
entertainment (7) and national (7).international (5), national (2), or sports (1).entertainment (7).Overall, the model performs exceptionally well with minimal confusion, confirming its ability to distinguish between Bangla news categories with high precision.
All the four predictions were correct.
The csebuetnlp/banglabert_small model was fine-tuned on a Bangla news classification task with four categories: sports, international, entertainment, and national. After training and applying early stopping, the model's performance was evaluated on a held-out test set.
Overall Accuracy: 0.9723
| Model | Accuracy | Macro F1 | Weighted F1 |
|---|---|---|---|
| BanglaBERT-Small ✅ | 0.9723 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.9647 | 0.96 | 0.96 |
| Bangla-Electra | 0.9496 | 0.95 | 0.95 |
| Category | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Sports | 0.99 | 0.99 | 0.99 | 297 |
| International | 0.99 | 0.94 | 0.96 | 298 |
| Entertainment | 0.96 | 0.98 | 0.97 | 298 |
| National | 0.95 | 0.98 | 0.97 | 298 |
| Macro Avg | 0.97 | 0.97 | 0.97 | 1191 |
| Weighted Avg | 0.97 | 0.97 | 0.97 | 1191 |
Hence Banglabert-small performs better than others in metrics evaluation and with its small size of parameters. SO, we will use this model fro seployment using FastApi
17 commits
Jupyter Notebook
99.6%
This project focuses on classifying Bangla news articles into predefined categories using a fine-tuned BERT-based language model (sagorsarker/bangla-bert-base), Bangla-Electra and also banglabert_small then create a FlaskAPI for most efficient performing model
0
stars
17
commits
Jupyter Notebook
primary language
May 31, 2025
updated
This project focuses on classifying Bangla news articles into predefined categories using a fine-tuned BERT-based language model (sagorsarker/bangla-bert-base), Bangla-Electra and mBert.
The reason for choosing these model is that they performed state of the art in text/nlp classification task which you can check on the huggingface sagorsarker/bangla-bert-base: https://huggingface.co/sagorsarker/bangla-bert-base here you will find that Bangla Bert Base has. outperformed the other two model and Bangla Benchmarks in https://colab.research.google.com/drive/1vltPI81atzRvlALv4eCvEB0KdFoEaCOb#scrollTo=pqsH5gU-QxVf we will also try csebuetnlp/banglabert_small because of its very low number of parameters(13M) just to check how it performs in comparison the other three.
The workflow includes preprocessing raw Bangla text, removing irrelevant tokens, training a transformer model with early stopping, and visualizing word distributions across categories.
The aim is to develop an accurate and robust model that understands Bangla language nuances and effectively categorizes news content for downstream NLP applications.
Here’s a revised version of the "Data Load and Exploration", "Data Cleaning", "Feature Engineering", and "Model Setup and Training" sections. The rewrite focuses on what has been done and what has been achieved, emphasizing your techniques and decisions, not the detailed step-by-step process.
Below is a screenshot of the product's user interface where users can input Bangla news text and instantly see the predicted category:

The dataset was inspected and cleaned by discarding irrelevant columns such as reporter, which contained a large number of null values and had no influence on the classification task. Only the title, content, and category fields were retained for modeling.
An initial analysis confirmed that the dataset is balanced across four distinct news categories. Additionally, content strings were analyzed to detect and eliminate repetitive, irrelevant phrases like “ছবি: সংগৃহীত” and “ফাইল ছবি”, which often appear at the beginning of articles but offer no contextual value for classification.
To enhance the quality of input data, punctuation and Bangla stopwords were removed from the content. Stopwords were merged from multiple sources to ensure comprehensive coverage. This significantly reduced noise and preserved only meaningful tokens, especially important when dealing with a token limit of 512.
Additional low-value terms (e.g., সাল, মিনিট) that were domain-irrelevant were identified and excluded to further refine the dataset. This targeted cleaning approach ensured higher-quality inputs for the model and reduced the risk of truncating informative tokens during tokenization.
The data was restructured to create a new processed text field that combines the cleaned and filtered content. Once finalized, the original unprocessed content field was removed to optimize memory usage and keep the dataset concise.
The cleaned dataset was prepared for training by tokenizing the text using the sagorsarker/bangla-bert-base model. The label space was mapped, and the model architecture was adapted for multi-class classification with four output categories.
The model was fine-tuned using a robust training loop that incorporates early stopping to prevent overfitting. A linear learning rate scheduler was employed to stabilize learning and optimize convergence. This approach resulted in a well-generalized model capable of accurately classifying Bangla news content across all categories.
The models is trained for up to num_epochs= 10 , but all of these stopped earlier as their validation loss does not improve significantly for a number of patience consecutive epochs. This helps prevent overfitting and saves training time.
threshold, the model's state is saved and the patience_counter is reset.patience_counter is incremented.patience_counter exceeds the allowed patience, training is stopped early.This strategy ensures efficient training and prevents the model from overfitting to the training data.
In this step, we load the trained Bangla BERT model and tokenizer to evaluate it on the test data. This setup is crucial for making predictions and analyzing the model's performance using various metrics.
"sagorsarker/bangla-bert-base", a pretrained Bangla BERT model.4 corresponding to the categories: sports, international, entertainment, national.GPU if available, otherwise defaults to CPU.label2id: A dictionary mapping category names to numerical IDs.id2label: The inverse mapping to convert prediction IDs back to readable category names.This mapping is necessary for both training and interpreting model predictions.
banglabert_tokenizer directory.banglabert_category_model.pt).model.load_state_dict(torch.load("banglabert_category_model.pt", map_location=device))
After training and saving the best model, we now evaluate its performance on the test dataset. This step helps assess how well the model generalizes to unseen data.
Here's a clear comparison of all three Bangla news classification models i have tested:
| Model | Accuracy | Macro F1 | Weighted F1 |
|---|---|---|---|
| BanglaBERT-Small ✅ | 0.9723 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.9647 | 0.96 | 0.96 |
| Bangla-Electra | 0.9496 | 0.95 | 0.95 |
| Model | Sports | International | Entertainment | National |
|---|---|---|---|---|
| BanglaBERT-Small ✅ | 0.99 | 0.96 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.99 | 0.95 | 0.96 | 0.96 |
| Bangla-Electra | 0.97 | 0.95 | 0.93 | 0.95 |
argmax.with torch.no_grad():
for batch in test_loader:
...
preds = torch.argmax(outputs.logits, dim=1)
...
The BanglaBERT-based text classification model achieved an impressive accuracy of 96.47% on the test dataset.
sports, international, entertainment, national) have high precision, recall, and F1-scores, indicating that the model performs well across all classes.sports category (F1-score: 0.99), and consistently strong results on the other categories as well.This suggests that the model generalizes well and is highly effective for multilingual news classification in Bangla.
The confusion matrix provides a detailed breakdown of the model’s classification performance across the four news categories:
entertainment (7) and national (7).international (5), national (2), or sports (1).entertainment (7).Overall, the model performs exceptionally well with minimal confusion, confirming its ability to distinguish between Bangla news categories with high precision.
All the four predictions were correct.
The csebuetnlp/banglabert_small model was fine-tuned on a Bangla news classification task with four categories: sports, international, entertainment, and national. After training and applying early stopping, the model's performance was evaluated on a held-out test set.
Overall Accuracy: 0.9723
| Model | Accuracy | Macro F1 | Weighted F1 |
|---|---|---|---|
| BanglaBERT-Small ✅ | 0.9723 | 0.97 | 0.97 |
| BanglaBERT-Base | 0.9647 | 0.96 | 0.96 |
| Bangla-Electra | 0.9496 | 0.95 | 0.95 |
| Category | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Sports | 0.99 | 0.99 | 0.99 | 297 |
| International | 0.99 | 0.94 | 0.96 | 298 |
| Entertainment | 0.96 | 0.98 | 0.97 | 298 |
| National | 0.95 | 0.98 | 0.97 | 298 |
| Macro Avg | 0.97 | 0.97 | 0.97 | 1191 |
| Weighted Avg | 0.97 | 0.97 | 0.97 | 1191 |
Hence Banglabert-small performs better than others in metrics evaluation and with its small size of parameters. SO, we will use this model fro seployment using FastApi
17 commits
Jupyter Notebook
99.6%