Spaces:
Runtime error
Runtime error
Download fake_content_classifier.py from faizeeb/Fake_content_Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/spaces/faizeeb/Fake_content_Detection/resolve/main/fake_content_classifier.py
- Command line
-
hf download hf://spaces/faizeeb/Fake_content_Detection/fake_content_classifier.py
-
curl -L -o fake_content_classifier.py https://huggingface.co/spaces/faizeeb/Fake_content_Detection/resolve/main/fake_content_classifier.py
1.2 kB
| 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." | |