license: mit
language:
- en
metrics:
- accuracy
library_name: sklearn
pipeline_tag: text-classification
tags:
- code
Model Training
The sentiment analysis model is trained using a Support Vector Machine (SVM) classifier with a linear kernel. The cleaned text data is transformed into a bag-of-words representation using the CountVectorizer. The trained model is saved as Sentiment_classifier_model.joblib
, and the corresponding TF-IDF vectorizer is saved as vectorizer_model.joblib
.
- from huggingface_hub import hf_hub_download
- import joblib
- from sklearn.preprocessing import LabelEncoder
Download the sentiment analysis model
- model = joblib.load( hf_hub_download("DineshKumar1329/Sentiment_Analysis", "sklearn_model.joblib") )
Load the TF-IDF vectorizer
tfidf_vectorizer = joblib.load('/content/vectorizer_model.joblib') # Replace with your path
def clean_text(text):
Implement your text cleaning logic here (e.g., lowercase, remove punctuation, etc.)
This example simply lowercases the text
return text.lower()
def predict_sentiment(user_input): """Predicts sentiment for a given user input.""" cleaned_text = clean_text(user_input) input_matrix = tfidf_vectorizer.transform([cleaned_text]) prediction = model.predict(input_matrix)[0]
if isinstance(model.classes_, LabelEncoder): prediction = model.classes_.inverse_transform([prediction])[0]
return prediction
Get user input
user_input = input("Enter a sentence: ")
Predict sentiment
predicted_sentiment = predict_sentiment(user_input)
print(f"Predicted Sentiment: {predicted_sentiment}")
Optional: Save predictions (modify paths as needed)
try: df_existing = pd.read_excel('/content/output_predictions.xlsx') except FileNotFoundError: df_existing = pd.DataFrame()
new_prediction = pd.DataFrame({'User_Input': [user_input], 'Predicted_Sentiment': [predicted_sentiment]}) df_combined = pd.concat([df_existing, new_prediction], ignore_index=True) df_combined.to_excel('/content/output_predictions.xlsx', index=False)