1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4# Load the fine-tuned model from Hugging Face Hub
5model = AutoModelForSequenceClassification.from_pretrained("your-hf-username/imdb-distilbert")
6tokenizer = AutoTokenizer.from_pretrained("your-hf-username/imdb-distilbert")
7
8def predict_sentiment(review):
9 inputs = tokenizer(review, return_tensors="pt", truncation=True, padding=True, max_length=256)
10 with torch.no_grad():
11 logits = model(**inputs).logits
12 prediction = torch.argmax(logits, dim=1).item()
13 return "Positive" if prediction == 1 else "Negative"
14
15# Example Usage
16print(predict_sentiment("This movie was absolutely fantastic!"))
17print(predict_sentiment("The acting was terrible, and the story made no sense."))
1@article{salonen2025imdb-distilbert,
2 title={Fine-tuned DistilBERT for Sentiment Analysis on IMDB Reviews},
3 author={Nikke Salonen},
4 year={2025}
5}
For questions or issues, contact
nikke.salonen@gmail.com.