Update README.md
Browse files
README.md
CHANGED
|
@@ -50,3 +50,51 @@ predicted_sentiment = predict_sentiment(user_input)
|
|
| 50 |
print(f"Predicted Sentiment: {predicted_sentiment}")
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
print(f"Predicted Sentiment: {predicted_sentiment}")
|
| 51 |
|
| 52 |
|
| 53 |
+
|
| 54 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 55 |
+
from sklearn.preprocessing import LabelEncoder
|
| 56 |
+
import joblib
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def load_model_and_tokenizer(model_name="DineshKumar1329/Sentiment_Analysis"):
|
| 60 |
+
"""Loads the sentiment analysis model and tokenizer from Hugging Face Hub."""
|
| 61 |
+
|
| 62 |
+
# Replace with desired model name if using a different model
|
| 63 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 64 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 65 |
+
|
| 66 |
+
return model, tokenizer
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def clean_text(text):
|
| 70 |
+
"""Converts the input text to lowercase for case-insensitive processing."""
|
| 71 |
+
return text.lower()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def predict_sentiment(user_input, model, tokenizer):
|
| 75 |
+
"""Predicts sentiment for a given user input."""
|
| 76 |
+
|
| 77 |
+
cleaned_text = clean_text(user_input)
|
| 78 |
+
encoded_text = tokenizer(cleaned_text, return_tensors="pt")
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model(**encoded_text)
|
| 82 |
+
logits = outputs.logits
|
| 83 |
+
prediction = torch.argmax(logits, dim=-1).item()
|
| 84 |
+
|
| 85 |
+
if isinstance(model.config.label_list, LabelEncoder):
|
| 86 |
+
prediction = model.config.label_list.inverse_transform([prediction])[0]
|
| 87 |
+
|
| 88 |
+
return prediction
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
model, tokenizer = load_model_and_tokenizer()
|
| 93 |
+
|
| 94 |
+
user_input = input("Enter a sentence: ")
|
| 95 |
+
|
| 96 |
+
predicted_sentiment = predict_sentiment(user_input, model, tokenizer)
|
| 97 |
+
|
| 98 |
+
print(f"Predicted Sentiment: {predicted_sentiment}")
|
| 99 |
+
|
| 100 |
+
|