transactify / prediction.py
ananthakrishnan
tech: build LSTM model
02b8f3f
raw
history blame
2.11 kB
# prediction.py
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
# Function to clean the input text
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
# Load the model, tokenizer, and label encoder
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
# Function to make predictions
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')
# Make prediction
prediction = model.predict(padded_sequence)
predicted_class = np.argmax(prediction, axis=1)
# Decode the label
predicted_label = label_encoder.inverse_transform(predicted_class)
return predicted_label[0]
# Main function for running predictions
def main():
# Paths to your resources
model_path = 'transactify.h5' # Update with the correct path if needed
tokenizer_path = 'tokenizer.joblib' # Update with the correct path if needed
label_encoder_path = 'label_encoder.joblib' # Update with the correct path if needed
# Load resources
model, tokenizer, label_encoder = load_resources(model_path, tokenizer_path, label_encoder_path)
# Input for prediction
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}")
# Execute the main function
if __name__ == "__main__":
main()