-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlflow_tracking.py
More file actions
51 lines (43 loc) · 1.68 KB
/
mlflow_tracking.py
File metadata and controls
51 lines (43 loc) · 1.68 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
45
46
47
48
49
50
51
# Directory: mlflow_tracking.py
import mlflow
import os
import yaml
from urllib.parse import urlparse
def init_mlflow(config_path="config/config.yaml"):
try:
with open(config_path, "r") as f:
config = yaml.safe_load(f)
tracking_uri = config["mlflow"].get("tracking_uri", "./mlruns")
experiment_name = config["mlflow"].get("experiment_name", "Default")
# Validate tracking URI
if tracking_uri.startswith(("http://", "https://")):
parsed = urlparse(tracking_uri)
if not parsed.netloc:
raise ValueError(f"Invalid tracking URI: {tracking_uri}")
else:
# For local paths, ensure directory exists
os.makedirs(os.path.dirname(tracking_uri), exist_ok=True)
mlflow.set_tracking_uri(tracking_uri)
mlflow.set_experiment(experiment_name)
print(f"[MLflow] Tracking initialized: {tracking_uri}")
print(f"[MLflow] Experiment: {experiment_name}")
except Exception as e:
print(f"[ERROR] Failed to initialize MLflow: {e}")
raise
def log_step_metrics(step_name, metrics: dict):
try:
with mlflow.start_run(run_name=step_name):
mlflow.log_params({"step": step_name})
mlflow.log_metrics(metrics)
except Exception as e:
print(f"[ERROR] Failed to log metrics for {step_name}: {e}")
raise
def log_step_artifact(file_path):
try:
if os.path.exists(file_path):
mlflow.log_artifact(file_path)
else:
print(f"[WARNING] Artifact not found: {file_path}")
except Exception as e:
print(f"[ERROR] Failed to log artifact {file_path}: {e}")
raise