Update README.md
Browse files
README.md
CHANGED
|
@@ -19,24 +19,46 @@ The sentiment analysis model is trained using a Support Vector Machine (SVM) cla
|
|
| 19 |
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
import joblib
|
|
|
|
|
|
|
|
|
|
| 22 |
model = joblib.load(
|
| 23 |
-
|
| 24 |
)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
user_input = input("Enter a sentence: ")
|
| 27 |
-
cleaned_input = clean_text(user_input)
|
| 28 |
-
input_matrix = tfidf_vectorizer.transform([cleaned_input])
|
| 29 |
-
prediction = model.predict(input_matrix)[0]
|
| 30 |
-
print(f"Predicted Sentiment: {prediction}")
|
| 31 |
-
df_result = pd.DataFrame({'User_Input': [user_input], 'Predicted_Sentiment': [prediction]})
|
| 32 |
-
excel_filename = '/content/output_predictions.xlsx' # Replace with your desired filename
|
| 33 |
-
try:
|
| 34 |
-
# Load existing predictions from the Excel file
|
| 35 |
-
df_existing = pd.read_excel(excel_filename)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except FileNotFoundError:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
import joblib
|
| 22 |
+
from sklearn.preprocessing import LabelEncoder
|
| 23 |
+
|
| 24 |
+
# Download the sentiment analysis model
|
| 25 |
model = joblib.load(
|
| 26 |
+
hf_hub_download("DineshKumar1329/Sentiment_Analysis", "sklearn_model.joblib")
|
| 27 |
)
|
| 28 |
+
|
| 29 |
+
# Load the TF-IDF vectorizer
|
| 30 |
+
tfidf_vectorizer = joblib.load('/content/vectorizer_model.joblib') # Replace with your path
|
| 31 |
+
|
| 32 |
+
def clean_text(text):
|
| 33 |
+
# Implement your text cleaning logic here (e.g., lowercase, remove punctuation, etc.)
|
| 34 |
+
# This example simply lowercases the text
|
| 35 |
+
return text.lower()
|
| 36 |
+
|
| 37 |
+
def predict_sentiment(user_input):
|
| 38 |
+
"""Predicts sentiment for a given user input."""
|
| 39 |
+
cleaned_text = clean_text(user_input)
|
| 40 |
+
input_matrix = tfidf_vectorizer.transform([cleaned_text])
|
| 41 |
+
prediction = model.predict(input_matrix)[0]
|
| 42 |
+
|
| 43 |
+
if isinstance(model.classes_, LabelEncoder):
|
| 44 |
+
prediction = model.classes_.inverse_transform([prediction])[0]
|
| 45 |
+
|
| 46 |
+
return prediction
|
| 47 |
+
|
| 48 |
+
# Get user input
|
| 49 |
user_input = input("Enter a sentence: ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Predict sentiment
|
| 52 |
+
predicted_sentiment = predict_sentiment(user_input)
|
| 53 |
+
|
| 54 |
+
print(f"Predicted Sentiment: {predicted_sentiment}")
|
| 55 |
+
|
| 56 |
+
# Optional: Save predictions (modify paths as needed)
|
| 57 |
+
try:
|
| 58 |
+
df_existing = pd.read_excel('/content/output_predictions.xlsx')
|
| 59 |
except FileNotFoundError:
|
| 60 |
+
df_existing = pd.DataFrame()
|
| 61 |
+
|
| 62 |
+
new_prediction = pd.DataFrame({'User_Input': [user_input], 'Predicted_Sentiment': [predicted_sentiment]})
|
| 63 |
+
df_combined = pd.concat([df_existing, new_prediction], ignore_index=True)
|
| 64 |
+
df_combined.to_excel('/content/output_predictions.xlsx', index=False)
|