DineshKumar1329 commited on
Commit
ebe41b8
·
verified ·
1 Parent(s): 212e319

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +22 -73
README.md CHANGED
@@ -19,82 +19,31 @@ The sentiment analysis model is trained using a Support Vector Machine (SVM) cla
19
 
20
  # Usage :
21
 
22
- from huggingface_hub import hf_hub_download
23
- import joblib
24
- from sklearn.preprocessing import LabelEncoder
25
-
26
- model = joblib.load(
27
- hf_hub_download("DineshKumar1329/Sentiment_Analysis", "sklearn_model.joblib")
28
- )
29
-
30
- tfidf_vectorizer = joblib.load('/content/vectorizer_model.joblib') # Replace with your path
31
-
32
- def clean_text(text):
33
- return text.lower()
34
-
35
- def predict_sentiment(user_input):
36
- """Predicts sentiment for a given user input."""
37
- cleaned_text = clean_text(user_input)
38
- input_matrix = tfidf_vectorizer.transform([cleaned_text])
39
- prediction = model.predict(input_matrix)[0]
40
-
41
- if isinstance(model.classes_, LabelEncoder):
42
- prediction = model.classes_.inverse_transform([prediction])[0]
43
-
44
- return prediction
45
-
46
- user_input = input("Enter a sentence: ")
47
-
48
- predicted_sentiment = predict_sentiment(user_input)
49
-
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
 
 
19
 
20
  # Usage :
21
 
22
+ <!-- from huggingface_hub import hf_hub_download
23
+ import joblib
24
+ from sklearn.preprocessing import LabelEncoder
25
+
26
+ model = joblib.load(
27
+ hf_hub_download("DineshKumar1329/Sentiment_Analysis", "sklearn_model.joblib")
28
+ )
29
+
30
+ tfidf_vectorizer = joblib.load('/content/vectorizer_model.joblib') # Replace with your path
31
+
32
+ def clean_text(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  return text.lower()
34
+
35
+ def predict_sentiment(user_input):
 
36
  """Predicts sentiment for a given user input."""
 
37
  cleaned_text = clean_text(user_input)
38
+ input_matrix = tfidf_vectorizer.transform([cleaned_text])
39
+ prediction = model.predict(input_matrix)[0]
40
+ if isinstance(model.classes_, LabelEncoder):
41
+ prediction = model.classes_.inverse_transform([prediction])[0]
 
 
 
 
 
 
42
  return prediction
43
+
44
+ user_input = input("Enter a sentence: ")
45
+ predicted_sentiment = predict_sentiment(user_input)
46
+ print(f"Predicted Sentiment: {predicted_sentiment}")
47
+ -->
 
 
 
 
 
48
 
49