Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions backend/retrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@

import argparse
import os
feature
import pickle
from collections import Counter

import shutil
import sys
from datetime import datetime
main

import pandas as pd
import joblib
Expand All @@ -57,31 +55,6 @@ def backup_existing_files():
backup_dir = os.path.join("backups", timestamp)
files_to_backup = [MODEL_PATH, VECTORIZER_PATH, LABEL_ENCODER_PATH]
existing = [f for f in files_to_backup if os.path.exists(f)]

feature
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(labels)

# Hold out a test split when there's enough data per class to stratify;
# otherwise train on everything and skip the report.
counts = Counter(y)
can_split = len(samples) >= 5 and min(counts.values()) >= 2
if can_split:
X_train, X_test, y_train, y_test = train_test_split(
texts, y, test_size=0.2, random_state=42, stratify=y
)
else:
X_train, y_train = texts, y
X_test, y_test = [], []
print("Not enough samples per class for a held-out test split; "
"training on all available data.")

# Fit TF-IDF vectorizer with max_features=5000 to match model
print("\nFitting TfidfVectorizer (max_features=5000)...")
vectorizer = TfidfVectorizer(max_features=5000)
X_train_vec = vectorizer.fit_transform(X_train)
print(f"Vocabulary size: {len(vectorizer.vocabulary_)}")

if not existing:
print("No existing model files found to back up (first-time training).")
return
Expand All @@ -100,7 +73,6 @@ def load_dataset(path):
df = pd.read_csv(path)
if "text" not in df.columns and "message" in df.columns:
df.rename(columns={"message": "text"}, inplace=True)
main

if "text" not in df.columns or "label" not in df.columns:
print("Dataset CSV must have 'text' (or 'message') and 'label' columns.")
Expand Down Expand Up @@ -138,15 +110,19 @@ def load_feedback(path):


def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
default_feedback = os.path.join(script_dir, "output", "feedback_store.csv")
default_dataset = os.path.join(script_dir, "dataset.csv")

parser = argparse.ArgumentParser(description="Retrain spam detection model with feedback data")
parser.add_argument(
"--dataset",
default=os.environ.get("DATASET_PATH", "dataset.csv"),
default=os.environ.get("DATASET_PATH", default_dataset),
help="Path to original training dataset CSV (default: dataset.csv or $DATASET_PATH)",
)
parser.add_argument(
"--feedback",
default="feedback_store.csv",
default=default_feedback,
help="Path to feedback CSV collected from /feedback endpoint",
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions backend/seeders/adminSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const seedAdminUser = async () => {
name: 'Admin'
});

conso;e.log('Admin user created successfully');
cnsole.log(`Email: ${email}`);
console.log('Admin user created successfully');
console.log(`Email: ${email}`);
console.log(`Password: ${password}`);
}
}catch(error){
Expand Down
2 changes: 1 addition & 1 deletion backend/spam_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def load_data():

# Try feedback_store.csv in backend directory
base_dir = os.path.dirname(__file__)
feedback_path = os.path.join(base_dir, "feedback_store.csv")
feedback_path = os.path.join(base_dir, "output", "feedback_store.csv")
if os.path.isfile(feedback_path):
try:
with open(feedback_path, newline="", encoding="utf-8") as f:
Expand Down
Loading