Spaces:
Build error
Build error
Download toxic_detection.py from faizeeb/Toxic_content_detection: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/faizeeb/Toxic_content_detection/resolve/main/toxic_detection.py
- Command line
-
hf download hf://spaces/faizeeb/Toxic_content_detection/toxic_detection.py
-
curl -L -o toxic_detection.py https://huggingface.co/spaces/faizeeb/Toxic_content_detection/resolve/main/toxic_detection.py
1.18 kB
| import pickle | |
| import numpy as np | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.preprocessing.text import Tokenizer | |
| from tensorflow.keras.preprocessing.sequence import pad_sequences | |
| class ToxicClassifier: | |
| def __init__(self): | |
| self.max_features = 13522 | |
| self.max_len = 250 | |
| self.model_path = 'Models/Abusive_Threat_model.hdf5' | |
| self.tokenizer_path = 'Tokenizers/tokenizerOf_ToxicModel.pkl' | |
| self.model = load_model(self.model_path) | |
| weights = load_model(self.model_path).get_weights() | |
| self.model.set_weights(weights) | |
| self.tokenizer = Tokenizer(self.max_features) | |
| with open(self.tokenizer_path, 'rb') as f: | |
| self.tokenizer = pickle.load(f) | |
| def predict(self, text): | |
| tokenized_sentences = self.tokenizer.texts_to_sequences([text]) | |
| train_padding = pad_sequences(tokenized_sentences, self.max_len) | |
| prediction = self.model.predict(train_padding) | |
| id = np.argmax(prediction) | |
| if id == 0: | |
| return "It is normal" | |
| elif id == 1: | |
| return "It is abusive" | |
| elif id == 2: | |
| return "It is a threat" | |