Spaces:
Sleeping
Sleeping
Update train.py
Browse files
train.py
CHANGED
@@ -14,7 +14,6 @@ from sklearn.preprocessing import StandardScaler
|
|
14 |
from sklearn.compose import make_column_transformer
|
15 |
from sklearn.pipeline import make_pipeline
|
16 |
from sklearn.model_selection import train_test_split
|
17 |
-
from huggingface_hub import HfApi, Repository
|
18 |
|
19 |
# Ignore all warnings
|
20 |
warnings.filterwarnings("ignore")
|
@@ -24,9 +23,6 @@ url = "http://www.ulb.ac.be/di/map/adalpozz/data/creditcard.Rdata"
|
|
24 |
dst_path = "./creditcard.Rdata"
|
25 |
wget.download(url, dst_path)
|
26 |
|
27 |
-
# Define the directory to save the model (same as dataset directory)
|
28 |
-
dataset_dir = os.path.dirname(dst_path)
|
29 |
-
|
30 |
# Load the dataset
|
31 |
parsed_res = rdata.parser.parse_file(dst_path)
|
32 |
res = rdata.conversion.convert(parsed_res)
|
@@ -76,30 +72,8 @@ y_test_pred = model_pipeline.predict(X_test)
|
|
76 |
|
77 |
print("Serializing Model")
|
78 |
|
79 |
-
# Save the model
|
80 |
-
saved_model_path =
|
81 |
joblib.dump(model_pipeline, saved_model_path)
|
82 |
|
83 |
-
print(f"Model Serialized and Saved
|
84 |
-
|
85 |
-
# Get the Hugging Face API token securely from the secret environment variable
|
86 |
-
api_token = os.getenv('FraudDemoWrite') # Access the secret directly using its name
|
87 |
-
|
88 |
-
if not api_token:
|
89 |
-
raise ValueError("Hugging Face API token not found. Ensure 'FraudDemoWrite' is set as a secret in your environment.")
|
90 |
-
|
91 |
-
# Initialize Hugging Face API
|
92 |
-
api = HfApi()
|
93 |
-
|
94 |
-
# Create or use an existing repository on Hugging Face Hub
|
95 |
-
repo_name = "kgauvin603/creditcard-fraud-detection" # Replace with your desired repo name
|
96 |
-
repo_url = api.create_repo(repo_id=repo_name, token=api_token, exist_ok=True)
|
97 |
-
|
98 |
-
# Initialize the repository
|
99 |
-
repo = Repository(local_dir="hf_model_repo", clone_from=repo_url, use_auth_token=api_token)
|
100 |
-
|
101 |
-
# Move the model file to the repository directory
|
102 |
-
os.rename(saved_model_path, os.path.join("hf_model_repo", "model.joblib"))
|
103 |
-
|
104 |
-
# Push the model to the Hugging Face Hub
|
105 |
-
repo.push_to_hub(commit_message="Upload trained model")
|
|
|
14 |
from sklearn.compose import make_column_transformer
|
15 |
from sklearn.pipeline import make_pipeline
|
16 |
from sklearn.model_selection import train_test_split
|
|
|
17 |
|
18 |
# Ignore all warnings
|
19 |
warnings.filterwarnings("ignore")
|
|
|
23 |
dst_path = "./creditcard.Rdata"
|
24 |
wget.download(url, dst_path)
|
25 |
|
|
|
|
|
|
|
26 |
# Load the dataset
|
27 |
parsed_res = rdata.parser.parse_file(dst_path)
|
28 |
res = rdata.conversion.convert(parsed_res)
|
|
|
72 |
|
73 |
print("Serializing Model")
|
74 |
|
75 |
+
# Save the model in the current working directory
|
76 |
+
saved_model_path = "model.joblib"
|
77 |
joblib.dump(model_pipeline, saved_model_path)
|
78 |
|
79 |
+
print(f"Model Serialized and Saved to {saved_model_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|