How to Use
!pip install huggingface_hub datasets
from huggingface_hub import hf_hub_download
import joblib
model = joblib.load(
hf_hub_download("faizalnf1800/sentiment-naivebayes", "sklearn_model.joblib")
)
# only load pickle files from sources you trust
# read more about it here https://skops.readthedocs.io/en/stable/persistence.html
from datasets import load_dataset
dataset = load_dataset("faizalnf1800/gpt-generated-review-product")
dataset2 = load_dataset("yelp_review_full")
import pandas as pd
# Assuming your dataset has columns 'label' and 'text'
df_training = pd.DataFrame({'text': dataset['train']['text'], 'label': dataset['train']['label']})
# Combine data from 'dataset2' into df_training
df_training = pd.concat([df_training, pd.DataFrame({'text': dataset2['train']['text'], 'label': dataset2['train']['label']})], ignore_index=True)
# initialize vectorizer and fit it with training data
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
vectorizer.fit(df_training['text'])
# Example new data for sentiment analysis
new_data = [
"I bought this product, and it turned out to be a huge disappointment. It malfunctioned within days, and the customer service was unhelpful. I regret wasting my money on it.",
"This product is a disaster. It's poorly made, and it broke after minimal use. I tried reaching out to customer support, but their response was unprofessional. Stay away from this one.",
"I'm absolutely thrilled with this product! It's a game-changer in every sense. The quality is outstanding, and it has improved my daily life significantly. I highly recommend it to anyone.",
"I can't believe how amazing this product is. It's exceeded all my expectations. The performance is top-notch, and it's worth every penny. I couldn't be happier with my purchase.",
"This product does the job adequately. It's not exceptional, but it's not terrible either. It's a reasonable choice for the price.",
"I have mixed feelings about this product. It has some good features, but it also has some drawbacks. It's neither a regret nor a delight.",
"The product is average. It neither impresses nor disappoints. It's a middle-of-the-road option for those looking for something basic.",
"This product is okay. It doesn't stand out, but it's not terrible either. It's a decent choice if you have modest expectations."
]
# Convert the new data using the same TfidfVectorizer
X_new = vectorizer.transform(new_data)
# Predict sentiments
predictions = model.predict(X_new)
def map_to_0_1(sentiment):
return sentiment / 4
# Print the predictions
for i, sentiment in enumerate(predictions):
sentiment_0_1 = map_to_0_1(sentiment)
if sentiment > 2:
print(f"Text: '{new_data[i]}' - Sentiment: Positive - Score: {sentiment_0_1:.2f}")
elif sentiment == 2:
print(f"Text: '{new_data[i]}' - Sentiment: Neutral - Score: {sentiment_0_1:.2f}")
else:
print(f"Text: '{new_data[i]}' - Sentiment: Negative - Score: {sentiment_0_1:.2f}")