File size: 2,108 Bytes
54eddc8
cd72b8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# main.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()