|
|
|
import numpy as np |
|
import pandas as pd |
|
from tensorflow.keras.models import load_model |
|
from tensorflow.keras.preprocessing.text import Tokenizer |
|
from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
import joblib |
|
import re |
|
|
|
|
|
def clean_text(text): |
|
text = text.lower() |
|
text = re.sub(r"\d+", " ", text) |
|
text = re.sub(r"[^\w\s]", " ", text) |
|
text = text.strip() |
|
return text |
|
|
|
|
|
def load_resources(model_path, tokenizer_path, label_encoder_path): |
|
model = load_model(model_path) |
|
tokenizer = joblib.load(tokenizer_path) |
|
label_encoder = joblib.load(label_encoder_path) |
|
return model, tokenizer, label_encoder |
|
|
|
|
|
def predict(model, tokenizer, label_encoder, input_text, max_len=50): |
|
cleaned_text = clean_text(input_text) |
|
sequence = tokenizer.texts_to_sequences([cleaned_text]) |
|
padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post', truncating='post') |
|
|
|
|
|
prediction = model.predict(padded_sequence) |
|
predicted_class = np.argmax(prediction, axis=1) |
|
|
|
|
|
predicted_label = label_encoder.inverse_transform(predicted_class) |
|
|
|
return predicted_label[0] |
|
|
|
|
|
def main(): |
|
|
|
model_path = 'transactify.h5' |
|
tokenizer_path = 'tokenizer.joblib' |
|
label_encoder_path = 'label_encoder.joblib' |
|
|
|
|
|
model, tokenizer, label_encoder = load_resources(model_path, tokenizer_path, label_encoder_path) |
|
|
|
|
|
input_text = input("Enter a transaction description for prediction: ") |
|
predicted_category = predict(model, tokenizer, label_encoder, input_text) |
|
print(f"The predicted category is: {predicted_category}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|