This project fine-tunes a BERT model to classify YouTube comments into sentiment categories (positive, negative, neutral). The model achieves a macro F1-score of 0.82 on an imbalanced dataset of 10,000+ comments. The trained model is deployed as a containerized API for easy integration into applications.
- Dataset: Kaggle YouTube Comments Dataset
- Hugging Face Model: bert-finetuned-youtube_sentiment_analysis
- Docker Image: rahulk98/youtubecomment-sentiment-predictor
- Project Structure
- Model Performance
- API Usage
- Docker Setup
- Local Development
- Example Response
- How It Works
- Training Process
├── Dockerfile # Docker configuration for containerizing the API
├── README.md # Project documentation
├── images/ # Screenshots and images for documentation
│ └── postman_response.jpg # Example API response from Postman
├── notebooks/ # Jupyter notebooks for model training
│ ├── main.ipynb # Training notebook for BERT fine-tuning
│ └── requirements.txt # Python dependencies for training
└── src/ # Source code for the API
├── app.py # Flask API entry point
├── predict.py # Prediction module using the trained model
└── requirements.txt # Python dependencies for the API
The model achieves excellent performance on the YouTube comments dataset:
F1 score (macro): 0.8182257811696632
Confusion matrix:
[[ 161 59 14]
[ 16 375 71]
[ 8 85 1047]]
The model can classify comments into three sentiment categories:
- Neutral
- Positive
- Negative
The API is accessible via HTTP POST requests to the /predict endpoint.
- URL:
http://<host>:5000/predict - Method: POST
- Headers:
Content-Type: application/json - Body:
{ "text": "Your YouTube comment text here" }
{
"sentiment": "positive"
}The response will contain the predicted sentiment, which can be one of:
"positive""negative""neutral"
The API is containerized and available as a Docker image for easy deployment.
docker pull rahulk98/youtubecomment-sentiment-predictordocker run -p 5000:5000 rahulk98/youtubecomment-sentiment-predictorThis will start the API service on port 5000.
If you want to run the API locally without Docker:
-
Clone the repository:
git clone https://github.com/rahulk98/Sentiment-Analysis-with-BERT-Model.git cd Sentiment-Analysis-with-BERT-Model -
Install dependencies:
pip install -r src/requirements.txt
-
Run the API:
cd src python app.py
Below is an example of the API response from Postman:
The API uses a fine-tuned BERT model to predict the sentiment of YouTube comments. The model was trained on a dataset of over 10,000 YouTube comments with manually labeled sentiments.
The prediction process:
- The input text is tokenized using BERT tokenizer
- The tokenized text is passed through the fine-tuned BERT model
- The model outputs probabilities for each sentiment class
- The class with the highest probability is returned as the prediction
The model was fine-tuned using:
- Base model:
bert-base-uncased - Custom weighted loss function to handle class imbalance
- Learning rate: 5e-5
- Training epochs: 5
- Batch size: 16
For more details on the training process, check the notebooks/main.ipynb file.
