import pickle from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences class FakeContentClassifier: def __init__(self, model_path, tokenizer_path, max_features=10000, max_len=250): # Load the Keras model self.model = load_model(model_path) self.weights = load_model(model_path).get_weights() self.model.set_weights(self.weights) self.tokenizer = Tokenizer(max_features) self.max_len = max_len # Load the saved tokenizer with open(tokenizer_path, 'rb') as f: self.tokenizer = pickle.load(f) def predict(self, text): # Tokenize the input text tokenized_sentences = self.tokenizer.texts_to_sequences([text]) train_padding = pad_sequences(tokenized_sentences, self.max_len) # Make a prediction with the model prediction = self.model.predict(train_padding) # Return the prediction as a string if prediction > 0.5: return "The sentence is classified as fake." else: return "The sentence is classified as real."