added documenation
#10
by
Ananthakrishnan12
- opened
- .gitattributes +0 -1
- .gitignore +1 -6
- About.md +0 -64
- {data_set → Dataset}/transaction_data.csv +1 -1
- LSTM_model.py +0 -62
- README.md +1 -59
- __pycache__/data_preprocessing.cpython-312.pyc +0 -0
- __pycache__/inference.cpython-312.pyc +0 -0
- bert_model.py +134 -0
- config.json +0 -35
- data_preprocessing.py +84 -50
- main.py +0 -57
- model.py +0 -25
- requirements.txt +4 -0
- setup.md +58 -52
.gitattributes
CHANGED
@@ -33,4 +33,3 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
.gitignore
CHANGED
@@ -1,6 +1 @@
|
|
1 |
-
transactify_venv
|
2 |
-
tokenizer.joblib
|
3 |
-
label_encoder.joblib
|
4 |
-
transactify.h5
|
5 |
-
venv
|
6 |
-
.venv
|
|
|
1 |
+
transactify_venv
|
|
|
|
|
|
|
|
|
|
About.md
DELETED
@@ -1,64 +0,0 @@
|
|
1 |
-
Abstract for Transactify......
|
2 |
-
|
3 |
-
Transactify is an LSTM-based model designed to predict the category of online payment transactions from their descriptions.
|
4 |
-
By analyzing textual inputs like "Live concert stream on YouTube" or "Coffee at Starbucks," it classifies transactions into categories such as "Movies & Entertainment" or "Food & Dining."
|
5 |
-
This model helps users track and organize their spending across various sectors, providing better financial insights and budgeting.
|
6 |
-
Transactify is trained on real-world transaction data for improved accuracy and generalization.
|
7 |
-
|
8 |
-
Table of contents....
|
9 |
-
|
10 |
-
1.Data Collection:
|
11 |
-
The dataset consists of 5,000 transaction records generated using ChatGPT, each containing a transaction description and its corresponding category.
|
12 |
-
Example entries include descriptions like "Live concert stream on YouTube" (Movies & Entertainment) and "Coffee at Starbucks" (Food & Dining).
|
13 |
-
These records cover various spending categories such as Lifestyle, Movies & Entertainment, Food & Dining, and others.
|
14 |
-
|
15 |
-
|
16 |
-
2.Data Preprocessing:
|
17 |
-
The preprocessing step involves several natural language processing (NLP) tasks to clean and prepare the text data for model training.
|
18 |
-
These include:
|
19 |
-
Lowercasing all text.
|
20 |
-
Removing digits and punctuation using regular expressions (regex).
|
21 |
-
Tokenizing the cleaned text to convert it into a sequence of tokens.
|
22 |
-
Applying text_to_sequences to transform the tokenized words into numerical sequences.
|
23 |
-
Using pad_sequences to ensure all sequences have the same length for input into the LSTM model.
|
24 |
-
Label encoding the target categories to convert them into numerical labels.
|
25 |
-
After preprocessing, the data is split into training and testing sets to build and validate the model.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
3.Model Building:
|
30 |
-
Embedding Layer: Converts tokenized transaction descriptions into dense vectors, capturing word semantics and relationships.
|
31 |
-
|
32 |
-
LSTM Layer: Learns sequential patterns from the embedded text, helping the model understand the context and relationships between words over time.
|
33 |
-
|
34 |
-
Dropout Layer: Introduces regularization by randomly turning off neurons during training, reducing overfitting and improving the model's generalization.
|
35 |
-
|
36 |
-
Dense Layer with Softmax Activation: Outputs a probability distribution across categories, allowing the model to predict the correct category for each transaction description.
|
37 |
-
|
38 |
-
Model Compilation: Compiled with the Adam optimizer for efficient learning, sparse categorical cross-entropy loss for multi-class classification, and accuracy as the evaluation metric.
|
39 |
-
|
40 |
-
Model Training: The model is trained for 50 epochs with a batch size of 8, using a validation set to monitor performance and adjust during training.
|
41 |
-
|
42 |
-
Saving the Model and Preprocessing Objects:
|
43 |
-
|
44 |
-
The trained model is saved as transactify.h5 for future use.
|
45 |
-
The tokenizer and label encoder used during preprocessing are saved using joblib as tokenizer.joblib and label_encoder.joblib, respectively,
|
46 |
-
ensuring they can be reused for consistent tokenization and label encoding when making predictions on new data.
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
4.Prediction:
|
51 |
-
Once trained, the model is used to predict the category of new transaction descriptions.
|
52 |
-
The output provides the category label, enabling users to classify their spending based on transaction descriptions.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
5.Conclusion:
|
57 |
-
The Transactify model effectively categorizes transaction descriptions using LSTM networks.
|
58 |
-
However, to improve the accuracy and reliability of predictions, a larger and more diverse dataset is necessary.
|
59 |
-
Expanding the dataset will help the model generalize better across various spending behaviors and conditions.
|
60 |
-
This enhancement will lead to more precise predictions, enabling users to gain deeper insights into their spending patterns.
|
61 |
-
Future work should focus on collecting additional data to refine the model's performance and applicability in real-world scenarios.
|
62 |
-
|
63 |
-
|
64 |
-
![Excepted Output:](result.gif)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{data_set → Dataset}/transaction_data.csv
RENAMED
@@ -4998,4 +4998,4 @@ Google Play Music,Online Payment
|
|
4998 |
Yoga class at HealthFit Studio,Lifestyle
|
4999 |
Doctor's appointment payment,Health & Wellness
|
5000 |
New sneakers from Nike,Lifestyle
|
5001 |
-
Breakfast at Denny's,Food & Dining
|
|
|
4998 |
Yoga class at HealthFit Studio,Lifestyle
|
4999 |
Doctor's appointment payment,Health & Wellness
|
5000 |
New sneakers from Nike,Lifestyle
|
5001 |
+
Breakfast at Denny's,Food & Dining
|
LSTM_model.py
DELETED
@@ -1,62 +0,0 @@
|
|
1 |
-
# LSTM_model.py
|
2 |
-
import numpy as np
|
3 |
-
from tensorflow.keras.models import Sequential
|
4 |
-
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
|
5 |
-
from data_preprocessing import preprocess_data, split_data
|
6 |
-
import joblib # To save the tokenizer and label encoder
|
7 |
-
|
8 |
-
# Define the LSTM model
|
9 |
-
def build_lstm_model(vocab_size, embedding_dim=64, max_len=10, lstm_units=128, dropout_rate=0.2, output_units=6):
|
10 |
-
model = Sequential()
|
11 |
-
model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_len))
|
12 |
-
model.add(LSTM(units=lstm_units, return_sequences=False))
|
13 |
-
model.add(Dropout(dropout_rate))
|
14 |
-
model.add(Dense(units=output_units, activation='softmax'))
|
15 |
-
|
16 |
-
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
|
17 |
-
|
18 |
-
return model
|
19 |
-
|
20 |
-
# Main function to execute the training process
|
21 |
-
def main():
|
22 |
-
# Path to your data file
|
23 |
-
data_path = r"E:\transactify\transactify\transactify\transactify\transactify\data_set\transaction_data.csv"
|
24 |
-
|
25 |
-
# Preprocess the data
|
26 |
-
sequences, labels, tokenizer, label_encoder = preprocess_data(data_path)
|
27 |
-
|
28 |
-
# Check if preprocessing succeeded
|
29 |
-
if sequences is not None:
|
30 |
-
print("Data preprocessing successful!")
|
31 |
-
|
32 |
-
# Split the data into training and testing sets
|
33 |
-
X_train, X_test, y_train, y_test = split_data(sequences, labels)
|
34 |
-
print(f"Training data shape: {X_train.shape}, Training labels shape: {y_train.shape}")
|
35 |
-
print(f"Testing data shape: {X_test.shape}, Testing labels shape: {y_test.shape}")
|
36 |
-
|
37 |
-
# Build the LSTM model
|
38 |
-
vocab_size = tokenizer.num_words + 1 # +1 for padding token
|
39 |
-
model = build_lstm_model(vocab_size, max_len=10, output_units=len(label_encoder.classes_))
|
40 |
-
|
41 |
-
# Train the model
|
42 |
-
model.fit(X_train, y_train, epochs=50, batch_size=8, validation_data=(X_test, y_test))
|
43 |
-
|
44 |
-
# Evaluate the model
|
45 |
-
loss, accuracy = model.evaluate(X_test, y_test)
|
46 |
-
print(f"Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}")
|
47 |
-
|
48 |
-
# Save the model
|
49 |
-
model.save('transactify.h5')
|
50 |
-
print("Model saved as 'transactify.h5'")
|
51 |
-
|
52 |
-
# Save the tokenizer and label encoder
|
53 |
-
joblib.dump(tokenizer, 'tokenizer.joblib')
|
54 |
-
joblib.dump(label_encoder, 'label_encoder.joblib')
|
55 |
-
print("Tokenizer and LabelEncoder saved as 'tokenizer.joblib' and 'label_encoder.joblib'")
|
56 |
-
|
57 |
-
else:
|
58 |
-
print("Data preprocessing failed.")
|
59 |
-
|
60 |
-
# Execute the main function
|
61 |
-
if __name__ == "__main__":
|
62 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
@@ -2,62 +2,4 @@
|
|
2 |
license: mit
|
3 |
language:
|
4 |
- en
|
5 |
-
---
|
6 |
-
|
7 |
-
## What is Transactify?
|
8 |
-
Transactify is an LSTM-based model designed to predict the category of online payment transactions from their descriptions.
|
9 |
-
By analyzing textual inputs like "Live concert stream on YouTube" or "Coffee at Starbucks," it classifies transactions into categories such as "Movies & Entertainment" or "Food & Dining."
|
10 |
-
This model helps users track and organize their spending across various sectors, providing better financial insights and budgeting.
|
11 |
-
Transactify is trained on real-world transaction data for improved accuracy and generalization.
|
12 |
-
|
13 |
-
## Table of contents
|
14 |
-
## 1. Data Collection
|
15 |
-
The dataset consists of **5,000 transaction records** generated using ChatGPT, each containing a transaction description and its corresponding category.
|
16 |
-
|
17 |
-
Example entries include:
|
18 |
-
- "Live concert stream on YouTube" (Movies & Entertainment)
|
19 |
-
- "Coffee at Starbucks" (Food & Dining)
|
20 |
-
|
21 |
-
These records cover various spending categories such as **Lifestyle**, **Movies & Entertainment**, **Food & Dining**, and others.
|
22 |
-
|
23 |
-
---
|
24 |
-
|
25 |
-
## 2. Data Preprocessing
|
26 |
-
The preprocessing step involves several natural language processing (NLP) tasks to clean and prepare the text data for model training. These include:
|
27 |
-
|
28 |
-
- Lowercasing all text.
|
29 |
-
- Removing digits and punctuation using regular expressions (regex).
|
30 |
-
- Tokenizing the cleaned text to convert it into a sequence of tokens.
|
31 |
-
- Applying `text_to_sequences` to transform the tokenized words into numerical sequences.
|
32 |
-
- Using `pad_sequences` to ensure all sequences have the same length for input into the LSTM model.
|
33 |
-
- Label encoding the target categories to convert them into numerical labels.
|
34 |
-
|
35 |
-
After preprocessing, the data is split into training and testing sets to build and validate the model.
|
36 |
-
|
37 |
-
---
|
38 |
-
|
39 |
-
## 3. Model Building
|
40 |
-
- **Embedding Layer**: Converts tokenized transaction descriptions into dense vectors, capturing word semantics and relationships.
|
41 |
-
|
42 |
-
- **LSTM Layer**: Learns sequential patterns from the embedded text, helping the model understand the context and relationships between words over time.
|
43 |
-
|
44 |
-
- **Dropout Layer**: Introduces regularization by randomly turning off neurons during training, reducing overfitting and improving the model's generalization.
|
45 |
-
|
46 |
-
- **Dense Layer with Softmax Activation**: Outputs a probability distribution across categories, allowing the model to predict the correct category for each transaction description.
|
47 |
-
|
48 |
-
### Model Compilation
|
49 |
-
- Compiled with the Adam optimizer for efficient learning.
|
50 |
-
- Sparse categorical cross-entropy loss for multi-class classification.
|
51 |
-
- Accuracy as the evaluation metric.
|
52 |
-
|
53 |
-
### Model Training
|
54 |
-
The model is trained for **50 epochs** with a batch size of **8**, using a validation set to monitor performance and adjust during training.
|
55 |
-
|
56 |
-
### Saving the Model and Preprocessing Objects
|
57 |
-
- The trained model is saved as `transactify.h5` for future use.
|
58 |
-
- The tokenizer and label encoder used during preprocessing are saved using joblib as `tokenizer.joblib` and `label_encoder.joblib`, respectively, ensuring they can be reused for consistent tokenization and label encoding when making predictions on new data.
|
59 |
-
|
60 |
-
---
|
61 |
-
|
62 |
-
## 4. Prediction
|
63 |
-
Once trained
|
|
|
2 |
license: mit
|
3 |
language:
|
4 |
- en
|
5 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__pycache__/data_preprocessing.cpython-312.pyc
DELETED
Binary file (3.55 kB)
|
|
__pycache__/inference.cpython-312.pyc
DELETED
Binary file (2.21 kB)
|
|
bert_model.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import Required Libraries
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from torch.utils.data import DataLoader, TensorDataset
|
5 |
+
from transformers import BertModel, AdamW
|
6 |
+
from sklearn.metrics import accuracy_score
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Import functions from the preprocessing module
|
10 |
+
from transactify.data_preprocessing import preprocessing_data, split_data, read_data
|
11 |
+
|
12 |
+
# Define a BERT-based classification model
|
13 |
+
class BertClassifier(nn.Module):
|
14 |
+
def __init__(self, num_labels, dropout_rate=0.3):
|
15 |
+
super(BertClassifier, self).__init__()
|
16 |
+
self.bert = BertModel.from_pretrained("bert-base-uncased")
|
17 |
+
self.dropout = nn.Dropout(dropout_rate)
|
18 |
+
self.classifier = nn.Linear(self.bert.config.hidden_size, num_labels)
|
19 |
+
|
20 |
+
def forward(self, input_ids, attention_mask):
|
21 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
22 |
+
pooled_output = outputs[1] # Pooler output (CLS token)
|
23 |
+
output = self.dropout(pooled_output)
|
24 |
+
logits = self.classifier(output)
|
25 |
+
return logits
|
26 |
+
|
27 |
+
# Training the model
|
28 |
+
# Training the model
|
29 |
+
def train_model(model, train_dataloader, val_dataloader, device, epochs=3, lr=2e-5):
|
30 |
+
optimizer = AdamW(model.parameters(), lr=lr)
|
31 |
+
loss_fn = nn.CrossEntropyLoss()
|
32 |
+
|
33 |
+
for epoch in range(epochs):
|
34 |
+
model.train()
|
35 |
+
total_train_loss = 0
|
36 |
+
for step, batch in enumerate(train_dataloader):
|
37 |
+
b_input_ids, b_input_mask, b_labels = batch
|
38 |
+
|
39 |
+
b_input_ids = b_input_ids.to(device)
|
40 |
+
b_input_mask = b_input_mask.to(device)
|
41 |
+
b_labels = b_labels.to(device).long() # Ensure labels are LongTensor
|
42 |
+
|
43 |
+
model.zero_grad()
|
44 |
+
outputs = model(b_input_ids, b_input_mask)
|
45 |
+
|
46 |
+
loss = loss_fn(outputs, b_labels)
|
47 |
+
total_train_loss += loss.item()
|
48 |
+
loss.backward()
|
49 |
+
optimizer.step()
|
50 |
+
|
51 |
+
avg_train_loss = total_train_loss / len(train_dataloader)
|
52 |
+
print(f"Epoch {epoch+1}, Training Loss: {avg_train_loss}")
|
53 |
+
|
54 |
+
model.eval()
|
55 |
+
total_val_accuracy = 0
|
56 |
+
total_val_loss = 0
|
57 |
+
|
58 |
+
with torch.no_grad():
|
59 |
+
for batch in val_dataloader:
|
60 |
+
b_input_ids, b_input_mask, b_labels = batch
|
61 |
+
b_input_ids = b_input_ids.to(device)
|
62 |
+
b_input_mask = b_input_mask.to(device)
|
63 |
+
b_labels = b_labels.to(device)
|
64 |
+
|
65 |
+
outputs = model(b_input_ids, b_input_mask)
|
66 |
+
loss = loss_fn(outputs, b_labels)
|
67 |
+
total_val_loss += loss.item()
|
68 |
+
|
69 |
+
preds = torch.argmax(outputs, dim=1)
|
70 |
+
total_val_accuracy += (preds == b_labels).sum().item()
|
71 |
+
|
72 |
+
avg_val_accuracy = total_val_accuracy / len(val_dataloader.dataset)
|
73 |
+
avg_val_loss = total_val_loss / len(val_dataloader)
|
74 |
+
print(f"Validation Loss: {avg_val_loss}, Validation Accuracy: {avg_val_accuracy}")
|
75 |
+
|
76 |
+
# Testing the model
|
77 |
+
def test_model(model, test_dataloader, device):
|
78 |
+
model.eval()
|
79 |
+
all_preds = []
|
80 |
+
all_labels = []
|
81 |
+
with torch.no_grad():
|
82 |
+
for batch in test_dataloader:
|
83 |
+
b_input_ids, b_input_mask, b_labels = batch
|
84 |
+
b_input_ids = b_input_ids.to(device)
|
85 |
+
b_input_mask = b_input_mask.to(device)
|
86 |
+
b_labels = b_labels.to(device)
|
87 |
+
|
88 |
+
outputs = model(b_input_ids, b_input_mask)
|
89 |
+
preds = torch.argmax(outputs, dim=1)
|
90 |
+
|
91 |
+
all_preds.append(preds.cpu().numpy())
|
92 |
+
all_labels.append(b_labels.cpu().numpy())
|
93 |
+
|
94 |
+
all_preds = np.concatenate(all_preds)
|
95 |
+
all_labels = np.concatenate(all_labels)
|
96 |
+
accuracy = accuracy_score(all_labels, all_preds)
|
97 |
+
print(f"Test Accuracy: {accuracy}")
|
98 |
+
|
99 |
+
# Main function to train, validate, and test the model
|
100 |
+
def main(data_path, epochs=3, batch_size=16):
|
101 |
+
# Read and preprocess data
|
102 |
+
data = read_data(data_path)
|
103 |
+
if data is None:
|
104 |
+
return
|
105 |
+
|
106 |
+
input_ids, attention_masks, labels, labelencoder = preprocessing_data(data)
|
107 |
+
X_train_ids, X_test_ids, X_train_masks, X_test_masks, y_train, y_test = split_data(input_ids, attention_masks, labels)
|
108 |
+
|
109 |
+
# Determine the number of labels
|
110 |
+
num_labels = len(labelencoder.classes_)
|
111 |
+
|
112 |
+
# Create the model
|
113 |
+
model = BertClassifier(num_labels)
|
114 |
+
|
115 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
116 |
+
model.to(device)
|
117 |
+
|
118 |
+
# Create dataloaders
|
119 |
+
train_dataset = TensorDataset(X_train_ids, X_train_masks, y_train)
|
120 |
+
train_dataloader = DataLoader(train_dataset, batch_size=batch_size)
|
121 |
+
|
122 |
+
val_dataset = TensorDataset(X_test_ids, X_test_masks, y_test)
|
123 |
+
val_dataloader = DataLoader(val_dataset, batch_size=batch_size)
|
124 |
+
|
125 |
+
# Train the model
|
126 |
+
train_model(model, train_dataloader, val_dataloader, device, epochs=epochs)
|
127 |
+
|
128 |
+
# Test the model
|
129 |
+
test_dataloader = DataLoader(val_dataset, batch_size=batch_size)
|
130 |
+
test_model(model, test_dataloader, device)
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
data_path = r"E:\transactify\transactify\Dataset\transaction_data.csv"
|
134 |
+
main(data_path)
|
config.json
DELETED
@@ -1,35 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"model_type": "custom",
|
3 |
-
"architectures": ["LSTM"],
|
4 |
-
"library_name": "tensorflow",
|
5 |
-
"task_specific_params": {
|
6 |
-
"text-classification": {
|
7 |
-
"vocab_size": 500,
|
8 |
-
"embedding_dim": 64,
|
9 |
-
"hidden_size": 64,
|
10 |
-
"num_layers": 2,
|
11 |
-
"dropout_rate": 0.2,
|
12 |
-
"max_sequence_length": 10
|
13 |
-
}
|
14 |
-
},
|
15 |
-
"training_params": {
|
16 |
-
"batch_size": 8,
|
17 |
-
"epochs": 50,
|
18 |
-
"loss_function": "sparse_categorical_crossentropy",
|
19 |
-
"optimizer": "adam",
|
20 |
-
"metrics": ["accuracy"]
|
21 |
-
},
|
22 |
-
"train_data_size": 5000,
|
23 |
-
"id2label": {
|
24 |
-
"0": "Lifestyle",
|
25 |
-
"1": "Movies & Entertainment",
|
26 |
-
"2": "Food & Dining",
|
27 |
-
"3": "Others"
|
28 |
-
},
|
29 |
-
"label2id": {
|
30 |
-
"Lifestyle": 0,
|
31 |
-
"Movies & Entertainment": 1,
|
32 |
-
"Food & Dining": 2,
|
33 |
-
"Others": 3
|
34 |
-
}
|
35 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data_preprocessing.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
#
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
-
|
|
|
|
|
5 |
from sklearn.preprocessing import LabelEncoder
|
6 |
from sklearn.model_selection import train_test_split
|
7 |
-
|
8 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
9 |
|
10 |
# Read the data
|
11 |
def read_data(path):
|
@@ -22,62 +23,95 @@ def read_data(path):
|
|
22 |
print(f"An error occurred: {e}")
|
23 |
return None
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Cleaning the text
|
26 |
def clean_text(text):
|
27 |
-
text = text.lower() #
|
28 |
-
text = re.sub(r"\d+", " ", text) #
|
29 |
-
text = re.sub(r"[^\w\s]", " ", text) #
|
30 |
text = text.strip() # Remove extra spaces
|
31 |
return text
|
32 |
|
33 |
-
#
|
34 |
-
def
|
35 |
-
|
36 |
-
df = read_data(file_path)
|
37 |
-
if df is None:
|
38 |
-
print("Data loading failed.")
|
39 |
-
return None, None, None, None
|
40 |
-
|
41 |
-
# Clean the text
|
42 |
-
df['Transaction Description'] = df['Transaction Description'].apply(clean_text)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
tokenizer.fit_on_texts(df['Transaction Description'])
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
# Split the data into training and testing sets
|
75 |
-
X_train, X_test, y_train, y_test = split_data(sequences, labels)
|
76 |
-
print(f"Training data shape: {X_train.shape}, Training labels shape: {y_train.shape}")
|
77 |
-
print(f"Testing data shape: {X_test.shape}, Testing labels shape: {y_test.shape}")
|
78 |
-
else:
|
79 |
-
print("Data preprocessing failed.")
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
|
|
|
1 |
+
# Import Required Libraries:
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
|
5 |
+
import torch
|
6 |
+
from transformers import BertTokenizer
|
7 |
from sklearn.preprocessing import LabelEncoder
|
8 |
from sklearn.model_selection import train_test_split
|
9 |
+
import re
|
|
|
10 |
|
11 |
# Read the data
|
12 |
def read_data(path):
|
|
|
23 |
print(f"An error occurred: {e}")
|
24 |
return None
|
25 |
|
26 |
+
# Path to your data file
|
27 |
+
data_path = r"E:\transactify\transactify\Dataset\transaction_data.csv"
|
28 |
+
|
29 |
+
# Read the data and check if it was loaded successfully
|
30 |
+
data = read_data(data_path)
|
31 |
+
if data is not None:
|
32 |
+
print("Data loaded successfully:")
|
33 |
+
print(data.head(15))
|
34 |
+
else:
|
35 |
+
print("Data loading failed. Exiting...")
|
36 |
+
exit()
|
37 |
+
|
38 |
# Cleaning the text
|
39 |
def clean_text(text):
|
40 |
+
text = text.lower() # Converting uppercase to lowercase
|
41 |
+
text = re.sub(r"\d+", " ", text) # Removing digits in the text
|
42 |
+
text = re.sub(r"[^\w\s]", " ", text) # Removing punctuations
|
43 |
text = text.strip() # Remove extra spaces
|
44 |
return text
|
45 |
|
46 |
+
# Preprocessing the data
|
47 |
+
def preprocessing_data(df, max_length=20):
|
48 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
input_ids = []
|
51 |
+
attention_masks = []
|
|
|
52 |
|
53 |
+
# Ensure the dataframe has the required columns
|
54 |
+
if "Transaction Description" not in df.columns or "Category" not in df.columns:
|
55 |
+
raise ValueError("The required columns 'Transaction Description' and 'Category' are missing from the dataset.")
|
56 |
|
57 |
+
for description in df["Transaction Description"]:
|
58 |
+
cleaned_text = clean_text(description)
|
59 |
+
|
60 |
+
# Debugging print statements
|
61 |
+
# print(f"Original Description: {description}")
|
62 |
+
# print(f"Cleaned Text: {cleaned_text}")
|
63 |
+
|
64 |
+
# Only tokenize if the cleaned text is not empty
|
65 |
+
if cleaned_text:
|
66 |
+
encoded_dict = tokenizer.encode_plus(
|
67 |
+
cleaned_text,
|
68 |
+
add_special_tokens=True, # Add special tokens for BERT
|
69 |
+
max_length=max_length,
|
70 |
+
pad_to_max_length=True,
|
71 |
+
return_attention_mask=True,
|
72 |
+
return_tensors="pt",
|
73 |
+
truncation=True
|
74 |
+
)
|
75 |
+
|
76 |
+
input_ids.append(encoded_dict['input_ids']) # Append input IDs
|
77 |
+
attention_masks.append(encoded_dict['attention_mask']) # Append attention masks
|
78 |
+
else:
|
79 |
+
print("Cleaned text is empty, skipping...")
|
80 |
|
81 |
+
# Debugging output to check sizes
|
82 |
+
print(f"Total input_ids collected: {len(input_ids)}")
|
83 |
+
print(f"Total attention_masks collected: {len(attention_masks)}")
|
84 |
+
|
85 |
+
if not input_ids:
|
86 |
+
raise ValueError("No input_ids were collected. Check the cleaning process.")
|
87 |
|
88 |
+
# Concatenating the list of tensors to form a single tensor
|
89 |
+
input_ids = torch.cat(input_ids, dim=0)
|
90 |
+
attention_masks = torch.cat(attention_masks, dim=0)
|
91 |
+
|
92 |
+
# Encoding the labels
|
93 |
+
labelencoder = LabelEncoder()
|
94 |
+
labels = labelencoder.fit_transform(df["Category"])
|
95 |
+
labels = torch.tensor(labels, dtype=torch.long) # Convert labels to LongTensor
|
96 |
+
|
97 |
+
return input_ids, attention_masks, labels, labelencoder
|
98 |
|
99 |
+
# Split the data into train and test sets
|
100 |
+
def split_data(input_ids, attention_masks, labels, test_size=0.2, random_state=42):
|
101 |
+
X_train_ids, X_test_ids, y_train, y_test = train_test_split(
|
102 |
+
input_ids, labels, test_size=test_size, random_state=random_state
|
103 |
+
)
|
104 |
+
|
105 |
+
X_train_masks, X_test_masks = train_test_split(
|
106 |
+
attention_masks, test_size=test_size, random_state=random_state
|
107 |
+
)
|
108 |
+
|
109 |
+
return X_train_ids, X_test_ids, X_train_masks, X_test_masks, y_train, y_test
|
110 |
|
111 |
+
# Preprocess the data and split into train and test sets
|
112 |
+
input_ids, attention_masks, labels, labelencoder = preprocessing_data(data)
|
113 |
+
X_train_ids, X_test_ids, X_train_masks, X_test_masks, y_train, y_test = split_data(input_ids, attention_masks, labels)
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
# Output the sizes of the splits for confirmation
|
116 |
+
print(f"Training set size: {X_train_ids.shape[0]}")
|
117 |
+
print(f"Test set size: {X_test_ids.shape[0]}")
|
main.py
DELETED
@@ -1,57 +0,0 @@
|
|
1 |
-
# main.py
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
-
from tensorflow.keras.models import load_model
|
5 |
-
from tensorflow.keras.preprocessing.text import Tokenizer
|
6 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
7 |
-
import joblib
|
8 |
-
import re
|
9 |
-
|
10 |
-
# Function to clean the input text
|
11 |
-
def clean_text(text):
|
12 |
-
text = text.lower()
|
13 |
-
text = re.sub(r"\d+", " ", text)
|
14 |
-
text = re.sub(r"[^\w\s]", " ", text)
|
15 |
-
text = text.strip()
|
16 |
-
return text
|
17 |
-
|
18 |
-
# Load the model, tokenizer, and label encoder
|
19 |
-
def load_resources(model_path, tokenizer_path, label_encoder_path):
|
20 |
-
model = load_model(model_path)
|
21 |
-
tokenizer = joblib.load(tokenizer_path)
|
22 |
-
label_encoder = joblib.load(label_encoder_path)
|
23 |
-
return model, tokenizer, label_encoder
|
24 |
-
|
25 |
-
# Function to make predictions
|
26 |
-
def predict(model, tokenizer, label_encoder, input_text, max_len=50):
|
27 |
-
cleaned_text = clean_text(input_text)
|
28 |
-
sequence = tokenizer.texts_to_sequences([cleaned_text])
|
29 |
-
padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post', truncating='post')
|
30 |
-
|
31 |
-
# Make prediction
|
32 |
-
prediction = model.predict(padded_sequence)
|
33 |
-
predicted_class = np.argmax(prediction, axis=1)
|
34 |
-
|
35 |
-
# Decode the label
|
36 |
-
predicted_label = label_encoder.inverse_transform(predicted_class)
|
37 |
-
|
38 |
-
return predicted_label[0]
|
39 |
-
|
40 |
-
# Main function for running predictions
|
41 |
-
def main():
|
42 |
-
# Paths to your resources
|
43 |
-
model_path = 'transactify.h5' # Update with the correct path if needed
|
44 |
-
tokenizer_path = 'tokenizer.joblib' # Update with the correct path if needed
|
45 |
-
label_encoder_path = 'label_encoder.joblib' # Update with the correct path if needed
|
46 |
-
|
47 |
-
# Load resources
|
48 |
-
model, tokenizer, label_encoder = load_resources(model_path, tokenizer_path, label_encoder_path)
|
49 |
-
|
50 |
-
# Input for prediction
|
51 |
-
input_text = input("Enter a transaction description for prediction: ")
|
52 |
-
predicted_category = predict(model, tokenizer, label_encoder, input_text)
|
53 |
-
print(f"The predicted category is: {predicted_category}")
|
54 |
-
|
55 |
-
# Execute the main function
|
56 |
-
if __name__ == "__main__":
|
57 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.py
DELETED
@@ -1,25 +0,0 @@
|
|
1 |
-
from tensorflow.keras.models import load_model
|
2 |
-
import joblib
|
3 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
4 |
-
import numpy as np
|
5 |
-
import re
|
6 |
-
|
7 |
-
# Load the model, tokenizer, and label encoder
|
8 |
-
model = load_model("transactify.h5")
|
9 |
-
tokenizer = joblib.load("tokenizer.joblib")
|
10 |
-
label_encoder = joblib.load("label_encoder.joblib")
|
11 |
-
|
12 |
-
def clean_text(text):
|
13 |
-
text = text.lower()
|
14 |
-
text = re.sub(r"\d+", "", text)
|
15 |
-
text = re.sub(r"[^\w\s]", "", text)
|
16 |
-
return text.strip()
|
17 |
-
|
18 |
-
def predict(text):
|
19 |
-
cleaned_text = clean_text(text)
|
20 |
-
sequence = tokenizer.texts_to_sequences([cleaned_text])
|
21 |
-
padded_sequence = pad_sequences(sequence, maxlen=100)
|
22 |
-
prediction = model.predict(padded_sequence)
|
23 |
-
predicted_label = np.argmax(prediction, axis=1)
|
24 |
-
category = label_encoder.inverse_transform(predicted_label)
|
25 |
-
return {"category": category[0]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
numpy
|
2 |
pandas
|
3 |
tensorflow
|
|
|
4 |
scikit-learn
|
|
|
|
|
|
|
|
1 |
numpy
|
2 |
pandas
|
3 |
tensorflow
|
4 |
+
transformers
|
5 |
scikit-learn
|
6 |
+
torch
|
7 |
+
torchvision
|
8 |
+
torchaudio
|
setup.md
CHANGED
@@ -1,53 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
1 |
+
## Install Git LFS
|
2 |
+
```
|
3 |
+
brew install git-lfs
|
4 |
+
```
|
5 |
+
or download from https://git-lfs.github.com/
|
6 |
|
7 |
+
## Update global git config
|
8 |
+
```
|
9 |
+
$ git lfs install
|
10 |
+
```
|
11 |
+
|
12 |
+
## Update system git config
|
13 |
+
```
|
14 |
+
$ git lfs install --system
|
15 |
+
```
|
16 |
+
|
17 |
+
## Clone the Repo
|
18 |
+
|
19 |
+
### Entire Clone
|
20 |
+
```
|
21 |
+
git clone https://huggingface.co/webslate/transactify
|
22 |
+
```
|
23 |
+
|
24 |
+
### Light Clone
|
25 |
+
```
|
26 |
+
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/webslate/transactify
|
27 |
+
```
|
28 |
+
|
29 |
+
## For Pushing the Code
|
30 |
+
|
31 |
+
> Refer to https://huggingface.co/blog/password-git-deprecation
|
32 |
+
|
33 |
+
### Set the Remote URL
|
34 |
+
```
|
35 |
+
$: git remote set-url origin https://<user_name>:<token>@huggingface.co/<repo_path>
|
36 |
+
```
|
37 |
+
### Token Creation
|
38 |
+
|
39 |
+
> Go to Settings > Access Tokens > Create new token >
|
40 |
+
Choose Write Tab (3rd one) / go here https://huggingface.co/settings/tokens/new?tokenType=write
|
41 |
+
|
42 |
+
|
43 |
+
## Create Virtual Environment
|
44 |
+
|
45 |
+
```
|
46 |
+
create a Virtual Environment for Transactify project...
|
47 |
+
python -m venv transactify_venv
|
48 |
+
|
49 |
+
To activate environment..
|
50 |
+
go to cmd ..
|
51 |
+
type >> cd transactify_venv
|
52 |
+
>> cd scripts
|
53 |
+
>> activate
|
54 |
+
```
|
55 |
+
## Installing Required Libaries.
|
56 |
+
|
57 |
+
to install required libaries...
|
58 |
+
go to cmd..
|
59 |
+
type >>pip install -r requirements.txt
|