saskarkhadka's picture
restructure and add telemtry
9766e10
Raw History Blame Contribute Delete
3.61 kB
import tensorflow as tf
import keras
import json
import gc
import pickle
import string
import re
from nltk.corpus import stopwords
from app.core.config import settings
class MLP:
def __init__(self):
self.MAX_SEQUENCE_LENGTH: int = 256
def preprocess(self, text: str) -> str:
nep_stopwrods = stopwords.words("nepali")
text = (
text.replace("\n", " ")
.replace("\r", " ")
.replace("\t", " ")
.replace("–", " ")
.replace("-", " ")
.replace("।", " ")
.replace("—", " ")
.replace("ॽ", " ")
.replace("?", " ")
.replace("ः", "")
.replace(":", " ")
.replace("“", ' ')
.replace("”", ' ')
.replace("‘", " ")
.replace("’", " ")
.replace('"', " ")
.replace("'", " ")
.replace("0", "०")
.replace("1", "१")
.replace("2", "२")
.replace("3", "३")
.replace("4", "४")
.replace("5", "५")
.replace("6", "६")
.replace("7", "७")
.replace("8", "८")
.replace("9", "९")
)
regrex_pattern = re.compile(
pattern="["
"\U0001f600-\U0001f64f"
"\U0001f300-\U0001f5ff"
"\U0001f680-\U0001f6ff"
"\U0001f1e0-\U0001f1ff"
"\U00002500-\U00002bef"
"\U00002702-\U000027b0"
"\U000024c2-\U0001f251"
"\U0001f926-\U0001f937"
"\U00010000-\U0010ffff"
"\u2640-\u2642"
"\u2600-\u2b55"
"\u200d"
"\u23cf"
"\u23e9"
"\u231a"
"\ufe0f"
"\u3030"
"]+",
flags=re.UNICODE,
)
text = regrex_pattern.sub("", text)
pattern = re.compile(r'<.*?>')
text = pattern.sub('', text)
text = re.sub(r'([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})', '', text)
text = re.sub(r"\s+", " ", text)
text = re.sub('[a-zA-Z]', '', text)
text = re.sub(r'\([^)]*\)', '', text)
text = " ".join(text.split())
punctuations = list(string.punctuation)
text = "".join([char for char in text if char not in punctuations and char not in "०१२३४५६७८९"])
text = [word.strip() for word in text.split() if word.strip() not in nep_stopwrods]
text = " ".join(text)
text = " ".join(text.split())
return text
def predict(self, text: str) -> str:
model = keras.models.load_model(settings.MLP_MODEL_PATH)
with open(settings.MLP_TOKENIZER_PATH, encoding='utf-8') as f:
data = json.load(f)
tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(data)
with open(settings.MLP_LABEL_ENCODER, 'rb') as f:
label_encoder = pickle.load(f)
text = self.preprocess(text)
sequence = tokenizer.texts_to_sequences([text])[0]
if len(sequence) > self.MAX_SEQUENCE_LENGTH:
sequence = sequence[:self.MAX_SEQUENCE_LENGTH]
else:
sequence += [0] * (self.MAX_SEQUENCE_LENGTH - len(sequence))
probs = model.predict(tf.convert_to_tensor([sequence], dtype=tf.int32))[0]
pred_label_idx = probs.argmax()
pred = label_encoder.inverse_transform([pred_label_idx])[0]
del model, tokenizer, label_encoder
gc.collect()
return pred