Commit
Β·
83527bc
1
Parent(s):
35827c4
Update model/train.py
Browse filesFixed the file paths to correctly point to `tmp` folder as the original paths are read-only
- model/train.py +85 -76
model/train.py
CHANGED
@@ -1,76 +1,85 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
from pathlib import Path
|
3 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
-
from sklearn.linear_model import LogisticRegression
|
5 |
-
from sklearn.metrics import accuracy_score
|
6 |
-
from sklearn.model_selection import train_test_split
|
7 |
-
import joblib
|
8 |
-
import json
|
9 |
-
import datetime
|
10 |
-
import hashlib
|
11 |
-
|
12 |
-
# Paths
|
13 |
-
BASE_DIR = Path(__file__).resolve().parent
|
14 |
-
DATA_PATH = BASE_DIR.parent / "data" / "combined_dataset.csv"
|
15 |
-
MODEL_PATH = BASE_DIR / "model.pkl"
|
16 |
-
VECTORIZER_PATH = BASE_DIR / "vectorizer.pkl"
|
17 |
-
METADATA_PATH = BASE_DIR / "metadata.json"
|
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 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from pathlib import Path
|
3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
+
from sklearn.linear_model import LogisticRegression
|
5 |
+
from sklearn.metrics import accuracy_score
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
import joblib
|
8 |
+
import json
|
9 |
+
import datetime
|
10 |
+
import hashlib
|
11 |
+
|
12 |
+
# # Paths
|
13 |
+
# BASE_DIR = Path(__file__).resolve().parent
|
14 |
+
# DATA_PATH = BASE_DIR.parent / "data" / "combined_dataset.csv"
|
15 |
+
# MODEL_PATH = BASE_DIR / "model.pkl"
|
16 |
+
# VECTORIZER_PATH = BASE_DIR / "vectorizer.pkl"
|
17 |
+
# METADATA_PATH = BASE_DIR / "metadata.json"
|
18 |
+
|
19 |
+
# Base dir and data location inside /tmp
|
20 |
+
BASE_DIR = Path("/tmp")
|
21 |
+
DATA_PATH = BASE_DIR / "data" / "combined_dataset.csv"
|
22 |
+
|
23 |
+
# Model artifacts also in /tmp (or you can keep these in /app/model if you want to persist them in the container)
|
24 |
+
MODEL_PATH = BASE_DIR / "model.pkl"
|
25 |
+
VECTORIZER_PATH = BASE_DIR / "vectorizer.pkl"
|
26 |
+
METADATA_PATH = BASE_DIR / "metadata.json"
|
27 |
+
|
28 |
+
def hash_file(filepath):
|
29 |
+
content = Path(filepath).read_bytes()
|
30 |
+
return hashlib.md5(content).hexdigest()
|
31 |
+
|
32 |
+
def main():
|
33 |
+
# Load dataset
|
34 |
+
# print('Dataset Loaded')
|
35 |
+
df = pd.read_csv(DATA_PATH)
|
36 |
+
X = df['text']
|
37 |
+
y = df['label']
|
38 |
+
|
39 |
+
# Train-test split
|
40 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.2, random_state=42)
|
41 |
+
|
42 |
+
# Vectorize
|
43 |
+
vectorizer = TfidfVectorizer(stop_words='english', max_features=5000)
|
44 |
+
X_train_vec = vectorizer.fit_transform(X_train)
|
45 |
+
X_test_vec = vectorizer.transform(X_test)
|
46 |
+
|
47 |
+
# print('Train/Test Splits Created')
|
48 |
+
# print('Starting Model Training')
|
49 |
+
|
50 |
+
# Train model
|
51 |
+
model = LogisticRegression(max_iter=1000)
|
52 |
+
model.fit(X_train_vec, y_train)
|
53 |
+
|
54 |
+
# print('Model Training Completed')
|
55 |
+
#print('Model Evaluation Starting!')
|
56 |
+
|
57 |
+
# Evaluate
|
58 |
+
y_pred = model.predict(X_test_vec)
|
59 |
+
acc = accuracy_score(y_test, y_pred)
|
60 |
+
|
61 |
+
# Save model + vectorizer
|
62 |
+
joblib.dump(model, MODEL_PATH)
|
63 |
+
joblib.dump(vectorizer, VECTORIZER_PATH)
|
64 |
+
|
65 |
+
# print('Model Evaluation Done')
|
66 |
+
# print('Model Saved!')
|
67 |
+
|
68 |
+
# Save metadata
|
69 |
+
metadata = {
|
70 |
+
"model_version": f"v1.0",
|
71 |
+
"data_version": hash_file(DATA_PATH),
|
72 |
+
"train_size": len(X_train),
|
73 |
+
"test_size": len(X_test),
|
74 |
+
"test_accuracy": round(acc, 4),
|
75 |
+
"timestamp": datetime.datetime.now().isoformat()
|
76 |
+
}
|
77 |
+
with open(METADATA_PATH, 'w') as f:
|
78 |
+
json.dump(metadata, f, indent=4)
|
79 |
+
|
80 |
+
print(f"β
Model trained and saved.")
|
81 |
+
print(f"π Test Accuracy: {acc:.4f}")
|
82 |
+
print(f"π Metadata saved to {METADATA_PATH}")
|
83 |
+
|
84 |
+
if __name__ == "__main__":
|
85 |
+
main()
|