Instructions to use godlykmathews/BERT-Sentiment_Classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use godlykmathews/BERT-Sentiment_Classifier with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="godlykmathews/BERT-Sentiment_Classifier")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("godlykmathews/BERT-Sentiment_Classifier", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,75 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# BERT Sentiment Classifier
|
| 2 |
|
| 3 |
-
A
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
##
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
├── notebooks/
|
| 28 |
-
│ └── train_bert.ipynb
|
| 29 |
-
└── bert-sentiment-model/
|
| 30 |
-
├── config.json
|
| 31 |
-
├── model.safetensors
|
| 32 |
-
├── tokenizer.json
|
| 33 |
-
└── tokenizer_config.json
|
| 34 |
-
```
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
```bash
|
| 45 |
-
python -m streamlit run app.py
|
| 46 |
```
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
| 3 stars | Neutral |
|
| 58 |
-
| 4–5 stars | Positive |
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
The
|
| 63 |
|
| 64 |
-
## Results
|
| 65 |
|
| 66 |
-
Results
|
| 67 |
|
| 68 |
| Metric | Value |
|
| 69 |
|---|---:|
|
| 70 |
-
| Accuracy |
|
| 71 |
| Macro precision | 0.7281 |
|
| 72 |
| Macro recall | 0.7275 |
|
| 73 |
| Macro F1 | 0.7278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-classification
|
| 6 |
+
base_model: google-bert/bert-base-uncased
|
| 7 |
+
datasets:
|
| 8 |
+
- Yelp/yelp_review_full
|
| 9 |
+
tags:
|
| 10 |
+
- bert
|
| 11 |
+
- sentiment-analysis
|
| 12 |
+
- pytorch
|
| 13 |
+
- text-classification
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
+
- precision
|
| 17 |
+
- recall
|
| 18 |
+
- f1
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
# BERT Sentiment Classifier
|
| 22 |
|
| 23 |
+
A fine-tuned BERT model for classifying English reviews as **Negative**, **Neutral**, or **Positive**.
|
| 24 |
|
| 25 |
+
The model was fine-tuned from [google-bert/bert-base-uncased](https://huggingface.co/google-bert/bert-base-uncased) using a subset of [Yelp Review Full](https://huggingface.co/datasets/Yelp/yelp_review_full).
|
| 26 |
|
| 27 |
+
## Labels
|
| 28 |
|
| 29 |
+
| Class ID | Sentiment | Original Yelp rating |
|
| 30 |
+
|---|---|---|
|
| 31 |
+
| 0 | Negative | 1–2 stars |
|
| 32 |
+
| 1 | Neutral | 3 stars |
|
| 33 |
+
| 2 | Positive | 4–5 stars |
|
| 34 |
|
| 35 |
+
Three-star reviews serve as a neutral proxy and may contain mixed opinions.
|
| 36 |
|
| 37 |
+
## Usage
|
| 38 |
|
| 39 |
+
```python
|
| 40 |
+
import html
|
| 41 |
+
import re
|
| 42 |
|
| 43 |
+
from transformers import pipeline
|
| 44 |
|
| 45 |
+
classifier = pipeline(
|
| 46 |
+
"text-classification",
|
| 47 |
+
model="godlykmathews/BERT-Sentiment_Classifier",
|
| 48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
def clean_text(text):
|
| 51 |
+
text = html.unescape(text)
|
| 52 |
+
text = re.sub(r"<br\s*/?>", " ", text, flags=re.IGNORECASE)
|
| 53 |
+
text = text.replace("\\n", " ").replace("\\t", " ")
|
| 54 |
+
return re.sub(r"\s+", " ", text).strip()
|
| 55 |
|
| 56 |
+
review = "The food was wonderful and the service was excellent!"
|
| 57 |
|
| 58 |
+
result = classifier(
|
| 59 |
+
clean_text(review),
|
| 60 |
+
truncation=True,
|
| 61 |
+
max_length=128,
|
| 62 |
+
)
|
| 63 |
|
| 64 |
+
print(result)
|
|
|
|
|
|
|
|
|
|
| 65 |
```
|
| 66 |
|
| 67 |
+
## Training Data
|
| 68 |
|
| 69 |
+
| Split | Reviews |
|
| 70 |
+
|---|---:|
|
| 71 |
+
| Training | 15,000 |
|
| 72 |
+
| Validation | 1,500 |
|
| 73 |
+
| Test | 3,000 |
|
| 74 |
|
| 75 |
+
Training and validation samples were selected from Yelp’s official training split. Test samples were selected from its official test split.
|
| 76 |
|
| 77 |
+
Sampling was stratified with a random seed of 42. Empty reviews, duplicate normalized text, and conflicting duplicate labels were removed from the sampled pools. Duplicate text shared between the development and test samples was excluded.
|
| 78 |
+
|
| 79 |
+
Preprocessing decoded HTML entities, normalized line breaks, and collapsed whitespace. Negation, punctuation, and stopwords were retained.
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
## Training Configuration
|
| 82 |
+
|
| 83 |
+
| Setting | Value |
|
| 84 |
+
|---|---|
|
| 85 |
+
| Framework | PyTorch |
|
| 86 |
+
| Transformers version | 5.12.1 |
|
| 87 |
+
| Optimizer | AdamW |
|
| 88 |
+
| Learning rate | 2e-5 |
|
| 89 |
+
| Epochs | 3 |
|
| 90 |
+
| Batch size | 4 |
|
| 91 |
+
| Gradient accumulation steps | 4 |
|
| 92 |
+
| Effective batch size | 16 on one device |
|
| 93 |
+
| Maximum sequence length | 128 tokens |
|
| 94 |
+
| Weight decay | 0.01 |
|
| 95 |
+
| Warmup | Approximately 10% of update steps |
|
| 96 |
+
| Random seed | 42 |
|
| 97 |
|
| 98 |
+
The BERT encoder and classification layer were both fine-tuned. Validation ran after each epoch, and the checkpoint with the highest validation macro F1 was selected for final evaluation.
|
| 99 |
|
| 100 |
+
## Evaluation Results
|
| 101 |
|
| 102 |
+
Results on the held-out sample of 3,000 Yelp reviews:
|
| 103 |
|
| 104 |
| Metric | Value |
|
| 105 |
|---|---:|
|
| 106 |
+
| Accuracy | 0.7740 |
|
| 107 |
| Macro precision | 0.7281 |
|
| 108 |
| Macro recall | 0.7275 |
|
| 109 |
| Macro F1 | 0.7278 |
|
| 110 |
+
| Weighted F1 | 0.7734 |
|
| 111 |
+
|
| 112 |
+
### Per-class Results
|
| 113 |
+
|
| 114 |
+
| Sentiment | Precision | Recall | F1 | Support |
|
| 115 |
+
|---|---:|---:|---:|---:|
|
| 116 |
+
| Negative | 0.8280 | 0.8425 | 0.8352 | 1,200 |
|
| 117 |
+
| Neutral | 0.5042 | 0.4950 | 0.4996 | 600 |
|
| 118 |
+
| Positive | 0.8521 | 0.8450 | 0.8485 | 1,200 |
|
| 119 |
+
|
| 120 |
+
Macro averages weight each class equally. Neutral classification was the weakest area in this evaluation.
|
| 121 |
+
|
| 122 |
+
## Intended Uses
|
| 123 |
+
|
| 124 |
+
- Educational sentiment-analysis projects.
|
| 125 |
+
- Classifying English review text.
|
| 126 |
+
- Demonstrations through a Streamlit interface.
|
| 127 |
+
|
| 128 |
+
## Limitations
|
| 129 |
+
|
| 130 |
+
- Neutral labels are derived from three-star ratings rather than independent neutral-sentiment annotations.
|
| 131 |
+
- Performance on Yelp does not establish performance on movie reviews, other product domains, or other languages.
|
| 132 |
+
- Reviews longer than 128 tokens are truncated.
|
| 133 |
+
- Sarcasm, mixed opinions, and ambiguous language may produce incorrect predictions.
|
| 134 |
+
- Output probabilities are not calibrated guarantees of correctness.
|
| 135 |
+
|
| 136 |
+
## Project
|
| 137 |
|
| 138 |
+
[Source code, training notebook, and Streamlit app](https://github.com/godlykmathews/bert-sentiment-classifier)
|