-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
44 lines (37 loc) · 1.17 KB
/
Copy pathtrain_model.py
File metadata and controls
44 lines (37 loc) · 1.17 KB
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
# train_model.py
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
import joblib
import logging
from dotenv import load_dotenv
import os
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("train_model.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
MODEL_FILE = 'models/scam_detector_model.pkl'
def train_model(data_path='scam_dataset.csv', model_path=MODEL_FILE):
try:
data = pd.read_csv(data_path)
X = data['url']
y = data['label']
pipeline = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', RandomForestClassifier(n_estimators=100, random_state=42))
])
pipeline.fit(X, y)
os.makedirs(os.path.dirname(model_path), exist_ok=True)
joblib.dump(pipeline, model_path)
logger.info("Model trained and saved.")
except Exception as e:
logger.error(f"Error training model: {e}")
if __name__ == "__main__":
train_model()