This project uses a fine-tuned DistilBERT transformer model to perform sentiment analysis on movie reviews. It classifies text into two categories: Positive and Negative.
The model is fine-tuned on the IMDb movie review dataset using PyTorch and Hugging Face Transformers.
| Property | Details |
|---|---|
| Model Architecture | DistilBERT |
| Base Model | DistilBERT |
| Task | Binary Sentiment Classification |
| Framework | PyTorch |
| Library | Hugging Face Transformers |
| Dataset | IMDb Movie Reviews |
| Number of Classes | 2 (Positive, Negative) |
| Evaluation Accuracy | Approximately 82.8% |
Note: The reported evaluation accuracy was obtained using the sample evaluation setup above. It should not be interpreted as a full-dataset benchmark.
Install the required libraries:
pip install torch transformers
Load the model and tokenizer:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = "Swalpita123/imdb-sentiment-distilbert"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
text = "This movie was absolutely fantastic!"
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=256
)
model.eval()
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=1)
predicted_id = torch.argmax(probabilities, dim=1).item()
sentiment = "POSITIVE" if predicted_id == 1 else "NEGATIVE"
print("Sentiment:", sentiment)
print("Confidence:", round(probabilities[0][predicted_id].item() * 100, 2), "%")
Input: "This movie was excellent. The acting and storyline were amazing."
Expected output:
Sentiment: POSITIVE
Swalpita Ray Nayak
Hugging Face: https://huggingface.co/Swalpita123
GitHub: https://github.com/Swalpita123/AI-Sentiment-Analysis
checkpoints/checkpoint-125/optimizer.pt 536 MBcheckpoints/checkpoint-125/model.safetensors 268 MBmodel.safetensors 268 MBcheckpoints/checkpoint-125/rng_state.pth 14.5 kBcheckpoints/checkpoint-125/training_args.bin 5.2 kBtraining_args.bin 5.2 kBcheckpoints/checkpoint-125/scheduler.pt 1.5 kB