vyasmax9 commited on
Commit
2e2eaac
·
verified ·
1 Parent(s): 52c40dd

Upload folder using huggingface_hub

Browse files
data/engine_data.csv ADDED
The diff for this file is too large to render. See raw diff
 
data/test.csv ADDED
The diff for this file is too large to render. See raw diff
 
data/train.csv ADDED
The diff for this file is too large to render. See raw diff
 
hosting/hosting.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import login, upload_folder
2
+ import os
3
+
4
+ # ==========================================
5
+ # HF TOKEN
6
+ # ==========================================
7
+
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+
10
+ if not HF_TOKEN:
11
+ raise Exception("HF_TOKEN not found")
12
+
13
+ HF_TOKEN = HF_TOKEN.strip()
14
+
15
+ print("HF Token Loaded Successfully")
16
+
17
+ # ==========================================
18
+ # LOGIN
19
+ # ==========================================
20
+
21
+ login(token=HF_TOKEN)
22
+
23
+ # ==========================================
24
+ # UPLOAD TO HUGGING FACE SPACE
25
+ # ==========================================
26
+
27
+ upload_folder(
28
+ repo_id="vyasmax9/predictive-maintenance",
29
+ folder_path="Predictive_Maintenance",
30
+ repo_type="space",
31
+ token=HF_TOKEN
32
+ )
33
+
34
+ print("Deployment Uploaded Successfully")
model_building/data_register.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import (
2
+ HfApi,
3
+ create_repo,
4
+ login
5
+ )
6
+
7
+ from huggingface_hub.utils import RepositoryNotFoundError
8
+
9
+ import os
10
+
11
+ # ==========================================
12
+ # CONFIGURATION
13
+ # ==========================================
14
+
15
+ DATA_PATH = os.path.join(
16
+ os.getcwd(),
17
+ "Predictive_Maintenance",
18
+ "data"
19
+ )
20
+
21
+ repo_id = "vyasmax9/predictive-maintenance-engine"
22
+ repo_type = "dataset"
23
+
24
+ # ==========================================
25
+ # TOKEN CHECK
26
+ # ==========================================
27
+
28
+ TOKEN = os.getenv("HF_TOKEN")
29
+
30
+ if not TOKEN:
31
+ raise Exception("HF_TOKEN not found")
32
+
33
+ # Remove hidden spaces/newlines
34
+ TOKEN = TOKEN.strip()
35
+
36
+ print("Token Found:", TOKEN is not None)
37
+ print("Token Length:", len(TOKEN))
38
+ print("Token Prefix:", TOKEN[:3])
39
+
40
+ # ==========================================
41
+ # LOGIN TO HUGGING FACE
42
+ # ==========================================
43
+
44
+ login(token=TOKEN)
45
+
46
+ api = HfApi()
47
+
48
+ # ==========================================
49
+ # DEBUG INFORMATION
50
+ # ==========================================
51
+
52
+ print("Current Directory:", os.getcwd())
53
+ print("Data Path:", DATA_PATH)
54
+ print("Path Exists:", os.path.exists(DATA_PATH))
55
+
56
+ # ==========================================
57
+ # CHECK DATA FOLDER
58
+ # ==========================================
59
+
60
+ if not os.path.exists(DATA_PATH):
61
+ raise Exception(
62
+ f"Path not found: {DATA_PATH}"
63
+ )
64
+
65
+ # ==========================================
66
+ # CREATE DATASET REPOSITORY
67
+ # ==========================================
68
+
69
+ try:
70
+
71
+ api.repo_info(
72
+ repo_id=repo_id,
73
+ repo_type=repo_type
74
+ )
75
+
76
+ print(
77
+ f"Dataset Repo '{repo_id}' already exists."
78
+ )
79
+
80
+ except RepositoryNotFoundError:
81
+
82
+ print(
83
+ "Creating Dataset Repository..."
84
+ )
85
+
86
+ create_repo(
87
+ repo_id=repo_id,
88
+ repo_type=repo_type,
89
+ private=False,
90
+ token=TOKEN
91
+ )
92
+
93
+ print(
94
+ "Dataset Repository Created"
95
+ )
96
+
97
+ except Exception as e:
98
+
99
+ print(
100
+ f"Repository Check Error: {e}"
101
+ )
102
+ raise
103
+
104
+ # ==========================================
105
+ # UPLOAD DATA FOLDER
106
+ # ==========================================
107
+
108
+ try:
109
+
110
+ api.upload_folder(
111
+ folder_path=DATA_PATH,
112
+ repo_id=repo_id,
113
+ repo_type=repo_type
114
+ )
115
+
116
+ print(
117
+ "Folder Uploaded Successfully"
118
+ )
119
+
120
+ except Exception as e:
121
+
122
+ print(
123
+ f"Upload Error: {e}"
124
+ )
125
+ raise
126
+
127
+ # ==========================================
128
+ # VERIFY FILES
129
+ # ==========================================
130
+
131
+ try:
132
+
133
+ files = api.list_repo_files(
134
+ repo_id=repo_id,
135
+ repo_type=repo_type
136
+ )
137
+
138
+ print("\nFiles Uploaded:")
139
+ print(files)
140
+
141
+ except Exception as e:
142
+
143
+ print(
144
+ f"Verification Error: {e}"
145
+ )
146
+ raise
147
+
148
+ print("\nDataset Registration Completed Successfully")
model_building/model_experiment.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import joblib
4
+ import pandas as pd
5
+ from datasets import load_dataset
6
+ from sklearn.ensemble import RandomForestClassifier
7
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report, confusion_matrix
8
+ from huggingface_hub import HfApi, create_repo
9
+
10
+ # ==========================================
11
+ # 1. ENVIRONMENT SETUP & REPOSITORY CONFIG
12
+ # ==========================================
13
+ TOKEN = os.getenv("HF_TOKEN")
14
+ DATASET_REPO = "vyasmax9/predictive-maintenance-engine"
15
+ MODEL_REPO = "vyasmax9/predictive-maintenance-model"
16
+ api = HfApi(token=TOKEN)
17
+
18
+ MODEL_PATH = os.path.join(os.getcwd(), "Predictive_Maintenance", "models")
19
+ os.makedirs(MODEL_PATH, exist_ok=True)
20
+
21
+ # ==========================================
22
+ # 2. LOAD TRAIN / TEST DATA SPLITS FROM HUB
23
+ # ==========================================
24
+ print("--- Loading train and test CSV files from Hugging Face Hub ---")
25
+ train_df = load_dataset(DATASET_REPO, data_files="train.csv", split="train").to_pandas()
26
+ test_df = load_dataset(DATASET_REPO, data_files="test.csv", split="train").to_pandas()
27
+
28
+ TARGET = "Engine_Condition"
29
+ X_train = train_df.drop(columns=[TARGET])
30
+ y_train = train_df[TARGET]
31
+ X_test = test_df.drop(columns=[TARGET])
32
+ y_test = test_df[TARGET]
33
+
34
+ # ==========================================
35
+ # 3. INITIALIZE CHAMPION WITH BEST PARAMETERS
36
+ # ==========================================
37
+ print("\n--- Training Champion Random Forest Classifier with Optimized Parameters ---")
38
+
39
+ # Replace these key-value pairs with the exact values printed by your Random Forest cell
40
+ best_params = {
41
+ "n_estimators": 200,
42
+ "max_depth": None,
43
+ "min_samples_split": 2,
44
+ "class_weight": "balanced",
45
+ "random_state": 42,
46
+ "n_jobs": -1
47
+ }
48
+
49
+ best_model = RandomForestClassifier(**best_params)
50
+ best_model.fit(X_train, y_train)
51
+
52
+ # ==========================================
53
+ # 4. FINAL PRODUCTION EVALUATION MATRICES
54
+ # ==========================================
55
+ preds = best_model.predict(X_test)
56
+
57
+ final_metrics = {
58
+ "Accuracy": round(accuracy_score(y_test, preds), 4),
59
+ "Precision": round(precision_score(y_test, preds, average='weighted'), 4),
60
+ "Recall": round(recall_score(y_test, preds, average='weighted'), 4),
61
+ "F1-Score": round(f1_score(y_test, preds, average='weighted'), 4)
62
+ }
63
+
64
+ print("\nFinal Optimized Model Metrics:")
65
+ for metric_name, value in final_metrics.items():
66
+ print(f" - {metric_name}: {value}")
67
+
68
+ print("\nClassification Report:")
69
+ print(classification_report(y_test, preds))
70
+
71
+ print("Confusion Matrix Array:")
72
+ print(confusion_matrix(y_test, preds))
73
+
74
+ # ==========================================
75
+ # 5. LOCAL FILE SERIALIZATION
76
+ # ==========================================
77
+ # Export parameters dictionary
78
+ params_path = os.path.join(MODEL_PATH, "best_params.json")
79
+ with open(params_path, "w") as file:
80
+ json.dump(best_params, file, indent=4)
81
+
82
+ # Export evaluation metrics table
83
+ metrics_path = os.path.join(MODEL_PATH, "final_metrics.json")
84
+ with open(metrics_path, "w") as file:
85
+ json.dump(final_metrics, file, indent=4)
86
+
87
+ # Export feature importance scores
88
+ if hasattr(best_model, "feature_importances_"):
89
+ importance_df = pd.DataFrame({
90
+ "Feature": X_train.columns,
91
+ "Importance": best_model.feature_importances_
92
+ }).sort_values(by="Importance", ascending=False)
93
+
94
+ importance_path = os.path.join(MODEL_PATH, "feature_importance.csv")
95
+ importance_df.to_csv(importance_path, index=False)
96
+
97
+ # Save the final binary weight file
98
+ model_file = os.path.join(MODEL_PATH, "best_model.pkl")
99
+ joblib.dump(best_model, model_file)
100
+ print("\n[SUCCESS] Local serialization of production artifacts completed.")
101
+
102
+ # ==========================================
103
+ # 6. REGISTRATION TO HUGGING FACE MODEL HUB
104
+ # ==========================================
105
+ print("\n--- Registering Production Model to Hugging Face Model Hub ---")
106
+ try:
107
+ create_repo(repo_id=MODEL_REPO, repo_type="model", token=TOKEN, private=False, exist_ok=True)
108
+
109
+ upload_list = [model_file, params_path, metrics_path]
110
+ if 'importance_path' in locals():
111
+ upload_list.append(importance_path)
112
+
113
+ for file_to_upload in upload_list:
114
+ api.upload_file(
115
+ path_or_fileobj=file_to_upload,
116
+ path_in_repo=os.path.basename(file_to_upload),
117
+ repo_id=MODEL_REPO,
118
+ repo_type="model"
119
+ )
120
+ print("\n[SUCCESS] Production Model Hub Repository fully synchronized on Hugging Face!")
121
+ except Exception as e:
122
+ print(f"\n[ERROR] Problem encountered during repository file push: {e}")
model_building/model_registration.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import mlflow
3
+
4
+ from huggingface_hub import (
5
+
6
+ HfApi,
7
+
8
+ create_repo
9
+
10
+ )
11
+
12
+ from huggingface_hub.utils import (
13
+
14
+ RepositoryNotFoundError
15
+
16
+ )
17
+
18
+ # =====================================
19
+ # Configure MLflow
20
+ # =====================================
21
+
22
+ mlflow.set_tracking_uri(
23
+ "file:./mlruns"
24
+ )
25
+
26
+ mlflow.set_experiment(
27
+ "predictive-maintenance"
28
+ )
29
+
30
+ api = HfApi()
31
+
32
+ # =====================================
33
+ # Paths
34
+ # =====================================
35
+
36
+ MODEL_PATH = "Predictive_Maintenance/models/best_model.pkl"
37
+
38
+ repo_id = "vyasmax9/predictive-maintenance-model"
39
+
40
+ # =====================================
41
+ # Verify Model Exists
42
+ # =====================================
43
+
44
+ if not os.path.exists(
45
+ MODEL_PATH
46
+ ):
47
+
48
+ raise FileNotFoundError(
49
+
50
+ f"Model not found: {MODEL_PATH}"
51
+
52
+ )
53
+
54
+ # =====================================
55
+ # Start Tracking
56
+ # =====================================
57
+
58
+ with mlflow.start_run():
59
+
60
+ mlflow.log_param(
61
+
62
+ "model_name",
63
+
64
+ "best_model.pkl"
65
+
66
+ )
67
+
68
+ mlflow.log_artifact(
69
+
70
+ MODEL_PATH,
71
+
72
+ artifact_path="model"
73
+
74
+ )
75
+
76
+ # =====================================
77
+ # Create Repo If Needed
78
+ # =====================================
79
+
80
+ try:
81
+
82
+ api.repo_info(
83
+
84
+ repo_id=repo_id,
85
+
86
+ repo_type="model"
87
+
88
+ )
89
+
90
+ print(
91
+ "Model Repository Exists"
92
+ )
93
+
94
+ except RepositoryNotFoundError:
95
+
96
+ create_repo(
97
+
98
+ repo_id=repo_id,
99
+
100
+ repo_type="model",
101
+
102
+ private=False
103
+
104
+ )
105
+
106
+ print(
107
+ "Created New Model Repository"
108
+ )
109
+
110
+ # =====================================
111
+ # Upload Model
112
+ # =====================================
113
+
114
+ api.upload_file(
115
+
116
+ path_or_fileobj=MODEL_PATH,
117
+
118
+ path_in_repo="best_model.pkl",
119
+
120
+ repo_id=repo_id,
121
+
122
+ repo_type="model"
123
+
124
+ )
125
+
126
+ print(
127
+ "Model Uploaded Successfully"
128
+ )
model_building/prep.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi
2
+ import os
3
+ import pandas as pd
4
+ from sklearn.model_selection import train_test_split
5
+
6
+ # ==========================================
7
+ # HF TOKEN
8
+ # ==========================================
9
+
10
+ TOKEN = os.getenv("HF_TOKEN")
11
+
12
+ if not TOKEN:
13
+ raise Exception("HF_TOKEN not found")
14
+
15
+ TOKEN = TOKEN.strip()
16
+
17
+ print("HF Token Loaded Successfully")
18
+
19
+ # ==========================================
20
+ # HF API
21
+ # ==========================================
22
+
23
+ api = HfApi()
24
+
25
+ repo_id = "vyasmax9/predictive-maintenance-engine"
26
+
27
+ # ==========================================
28
+ # PATHS
29
+ # ==========================================
30
+
31
+ save_path = os.path.join(
32
+ os.getcwd(),
33
+ "Predictive_Maintenance",
34
+ "data"
35
+ )
36
+
37
+ os.makedirs(
38
+ save_path,
39
+ exist_ok=True
40
+ )
41
+
42
+ dataset_path = os.path.join(
43
+ save_path,
44
+ "engine_data.csv"
45
+ )
46
+
47
+ print("Dataset Path:", dataset_path)
48
+ print("Dataset Exists:", os.path.exists(dataset_path))
49
+
50
+ if not os.path.exists(dataset_path):
51
+ raise FileNotFoundError(
52
+ f"Dataset not found: {dataset_path}"
53
+ )
54
+
55
+ # ==========================================
56
+ # LOAD DATASET
57
+ # ==========================================
58
+
59
+ df = pd.read_csv(dataset_path)
60
+
61
+ print("Dataset Loaded Successfully")
62
+ print("Shape:", df.shape)
63
+
64
+ # ==========================================
65
+ # TRAIN TEST SPLIT
66
+ # ==========================================
67
+
68
+ train_df, test_df = train_test_split(
69
+ df,
70
+ test_size=0.2,
71
+ random_state=42
72
+ )
73
+
74
+ print("Train Shape:", train_df.shape)
75
+ print("Test Shape:", test_df.shape)
76
+
77
+ # ==========================================
78
+ # SAVE FILES
79
+ # ==========================================
80
+
81
+ train_path = os.path.join(
82
+ save_path,
83
+ "train.csv"
84
+ )
85
+
86
+ test_path = os.path.join(
87
+ save_path,
88
+ "test.csv"
89
+ )
90
+
91
+ train_df.to_csv(
92
+ train_path,
93
+ index=False
94
+ )
95
+
96
+ test_df.to_csv(
97
+ test_path,
98
+ index=False
99
+ )
100
+
101
+ print("Train CSV Saved")
102
+ print("Test CSV Saved")
103
+
104
+ # ==========================================
105
+ # UPLOAD TRAIN FILE
106
+ # ==========================================
107
+
108
+ api.upload_file(
109
+ path_or_fileobj=train_path,
110
+ path_in_repo="train.csv",
111
+ repo_id=repo_id,
112
+ repo_type="dataset",
113
+ token=TOKEN
114
+ )
115
+
116
+ print("train.csv uploaded")
117
+
118
+ # ==========================================
119
+ # UPLOAD TEST FILE
120
+ # ==========================================
121
+
122
+ api.upload_file(
123
+ path_or_fileobj=test_path,
124
+ path_in_repo="test.csv",
125
+ repo_id=repo_id,
126
+ repo_type="dataset",
127
+ token=TOKEN
128
+ )
129
+
130
+ print("test.csv uploaded")
131
+
132
+ # ==========================================
133
+ # VERIFY FILES
134
+ # ==========================================
135
+
136
+ files = api.list_repo_files(
137
+ repo_id=repo_id,
138
+ repo_type="dataset",
139
+ token=TOKEN
140
+ )
141
+
142
+ print("\nFiles Uploaded:")
143
+ print(files)
144
+
145
+ print("\nData Preparation Completed")
models/best_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e140b71071c3ace1d0883330bc4b6896e604a6aa528e7b61718354e3ee3c0ef
3
+ size 100147273
models/best_params.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "n_estimators": 200,
3
+ "max_depth": null,
4
+ "min_samples_split": 2,
5
+ "class_weight": "balanced",
6
+ "random_state": 42,
7
+ "n_jobs": -1
8
+ }
models/feature_importance.csv ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Feature,Importance
2
+ Engine_RPM,0.22753045540374342
3
+ Fuel_Pressure,0.16503340519834353
4
+ Lub_Oil_Temperature,0.15893902772340662
5
+ Lub_Oil_Pressure,0.15304628244540297
6
+ Coolant_Temperature,0.14923144007738604
7
+ Coolant_Pressure,0.14621938915171748
models/final_metrics.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "Accuracy": 0.6496,
3
+ "Precision": 0.6311,
4
+ "Recall": 0.6496,
5
+ "F1-Score": 0.6291
6
+ }
models/model_comparison_matrix.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ ,Accuracy,Precision,Recall,F1-Score
2
+ Random Forest,0.6496032761709751,0.6688388216128218,0.6496032761709751,0.6549160477054105
3
+ XGBoost,0.6429485538776555,0.6727409853036613,0.6429485538776555,0.6490870197278636
4
+ Gradient Boosting,0.6647043767596621,0.6484145866975868,0.6647043767596621,0.6427105885077777
5
+ AdaBoost,0.6567699001791656,0.6405334577085224,0.6567699001791656,0.6394045447165251
models/model_specs.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "model_algorithm": "Random Forest",
3
+ "f1_score": 0.6549160477054105,
4
+ "accuracy": 0.6496032761709751
5
+ }
requirements.txt CHANGED
@@ -7,4 +7,4 @@ mlflow
7
  huggingface_hub
8
  datasets
9
  matplotlib
10
- seaborn
 
7
  huggingface_hub
8
  datasets
9
  matplotlib
10
+ seaborn