faizeeb commited on
Commit
216e078
·
1 Parent(s): 2411456

Create FakeContentClassifier.py

Browse files
Files changed (1) hide show
  1. FakeContentClassifier.py +33 -0
FakeContentClassifier.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing.text import Tokenizer
4
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
5
+
6
+ class FakeContentClassifier:
7
+ def __init__(self, max_features=10000, max_len=250):
8
+ # Load the Keras model
9
+ self.model = load_model("./Models/Abusive_Threat_model.hdf5")
10
+ self.weights = load_model("./Models/Abusive_Threat_model.hdf5").get_weights()
11
+ self.model.set_weights(self.weights)
12
+
13
+ self.tokenizer = Tokenizer(max_features)
14
+ self.max_len = max_len
15
+
16
+ # Load the saved tokenizer
17
+ with open("Tokenizers/tokenizer.pkl", 'rb') as f:
18
+ self.tokenizer = pickle.load(f)
19
+
20
+
21
+ def predict(self, text):
22
+ # Tokenize the input text
23
+ tokenized_sentences = self.tokenizer.texts_to_sequences([text])
24
+ train_padding = pad_sequences(tokenized_sentences, self.max_len)
25
+
26
+ # Make a prediction with the model
27
+ prediction = self.model.predict(train_padding)
28
+
29
+ # Return the prediction as a string
30
+ if prediction > 0.5:
31
+ return "The sentence is classified as fake."
32
+ else:
33
+ return "The sentence is classified as real."