Views
No views yet
ktr008/sentiment2025-02-25pip install transformers torch scipy1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import numpy as np
3from scipy.special import softmax
4
5model = AutoModelForSequenceClassification.from_pretrained("ktr008/sentiment")
6tokenizer = AutoTokenizer.from_pretrained("ktr008/sentiment")
7
8def predict_sentiment(text):
9 encoded_input = tokenizer(text, return_tensors='pt')
10 output = model(**encoded_input)
11 scores = output[0][0].detach().numpy()
12 scores = softmax(scores)
13
14 labels = ["Negative", "Neutral", "Positive"]
15 ranking = np.argsort(scores)[::-1]
16
17 return {labels[i]: round(float(scores[i]), 4) for i in ranking}
18
19# Example usage
20print(predict_sentiment("I love this product!"))cardiffnlp/twitter-roberta-base-sentiment-latest| Text | Prediction |
|---|---|
"I love this product!" | Positive (98.3%) |
"It's an okay experience." | Neutral (67.4%) |
"I hate this! Never buying again." | Negative (92.1%) |
0.9344@model{ktr008_sentiment_2025,
author = {ktr008},
title = {Fine-Tuned Sentiment Analysis Model},
year = {2025},
publisher = {Hugging Face},
version = {1.0}
}