godlykmathews commited on
Commit
ea2d938
·
verified ·
1 Parent(s): 47cbd59

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +108 -45
README.md CHANGED
@@ -1,75 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # BERT Sentiment Classifier
2
 
3
- A Streamlit app that classifies reviews as **Negative**, **Neutral**, or **Positive** using a fine-tuned BERT model. It displays the predicted sentiment and probabilities for all three classes.
4
 
5
- ## Setup
6
 
7
- The app was checked with Python 3.14 and the package versions in `requirements.txt`.
8
 
9
- ```bash
10
- python3.14 -m venv .venv
11
- source .venv/bin/activate
12
- python -m pip install -r requirements.txt
13
- ```
14
 
15
- `requirements.txt` contains only the app's dependencies. The training notebook installs its own dependencies in its first cell.
16
 
17
- ## Model files
18
 
19
- Run the model-saving cell in [notebooks/train_bert.ipynb](notebooks/train_bert.ipynb). If training in Colab, download the entire saved model directory and extract it locally.
 
 
20
 
21
- The notebook saves a folder named `bert_sentiment_model`. **Rename it to `bert-sentiment-model`** to match the app's current model path, then place it beside `app.py`:
22
 
23
- ```text
24
- bert-sentimentclassifier/
25
- ├── app.py
26
- ├── requirements.txt
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
- The app also accepts `notebooks/bert-sentiment-model/`. Keep all files produced by the model-saving cell. The app loads local files and does not download a model automatically.
 
 
 
 
37
 
38
- Model directories and training outputs are ignored by Git. Model files already committed remain tracked; `.gitignore` does not remove them from Git history.
39
 
40
- ## Run
 
 
 
 
41
 
42
- From the project directory, with the virtual environment activated:
43
-
44
- ```bash
45
- python -m streamlit run app.py
46
  ```
47
 
48
- Open the local URL printed in the terminal, enter a review, and click **Analyze**. The app uses CPU inference and analyzes up to 128 tokens per review.
49
 
50
- ## Training
 
 
 
 
51
 
52
- The notebook fine-tunes [`google-bert/bert-base-uncased`](https://huggingface.co/google-bert/bert-base-uncased) with PyTorch and Hugging Face Transformers on [`Yelp/yelp_review_full`](https://huggingface.co/datasets/Yelp/yelp_review_full).
53
 
54
- | Yelp rating | Sentiment |
55
- |---|---|
56
- | 1–2 stars | Negative |
57
- | 3 stars | Neutral |
58
- | 4–5 stars | Positive |
59
 
60
- Three-star reviews serve as a neutral proxy and can contain mixed opinions.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- The notebook uses 15,000 training reviews, 1,500 validation reviews, and 3,000 test reviews. Training and validation come from the official training split; test reviews come from the official test split. It trains for three epochs and selects the checkpoint with the best validation macro F1.
63
 
64
- ## Results
65
 
66
- Results recorded in the executed notebook on the held-out test sample:
67
 
68
  | Metric | Value |
69
  |---|---:|
70
- | Accuracy | 77.40% |
71
  | Macro precision | 0.7281 |
72
  | Macro recall | 0.7275 |
73
  | Macro F1 | 0.7278 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- The notebook includes the per-class metrics and confusion matrix. Neutral was the weakest class, with an F1 of 0.4996. These results describe the Yelp test sample; class probabilities are not calibrated guarantees of correctness.
 
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)