triggerflow 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  # trigger_mlflow.py
2
2
  import mlflow
3
+ import os
3
4
  import mlflow.pyfunc
4
5
  import tempfile
5
6
  from pathlib import Path
@@ -8,6 +9,115 @@ from mlflow.tracking import MlflowClient
8
9
  from .core import TriggerModel
9
10
 
10
11
 
12
+ def setup_mlflow(mlflow_uri: str = None,
13
+ model_name: str = None,
14
+ experiment_name: str = None,
15
+ run_name: str = None,
16
+ experiment_id: str = None,
17
+ run_id: str = None,
18
+ save_env_file=True
19
+ ):
20
+
21
+ # Set the MLflow tracking URI
22
+ if mlflow_uri is None:
23
+ mlflow_uri = os.getenv('MLFLOW_URI', 'https://ngt.cern.ch/models')
24
+ mlflow.set_tracking_uri(mlflow_uri)
25
+ os.environ["MLFLOW_URI"] = mlflow_uri
26
+ print(f"Using MLflow tracking URI: {mlflow_uri}")
27
+
28
+
29
+ # Set the model name
30
+ if model_name is None:
31
+ if os.getenv('MLFLOW_MODEL_NAME'):
32
+ model_name = os.getenv('MLFLOW_MODEL_NAME')
33
+ else:
34
+ model_name = os.getenv('CI_COMMIT_BRANCH', 'Test-Model')
35
+ os.environ["MLFLOW_MODEL_NAME"] = model_name
36
+ print(f"Using model name: {model_name}")
37
+
38
+
39
+ # Set the experiment name
40
+ if experiment_name is None:
41
+ if os.getenv('MLFLOW_EXPERIMENT_NAME'):
42
+ experiment_name = os.getenv('MLFLOW_EXPERIMENT_NAME')
43
+ else:
44
+ experiment_name = os.getenv('CI_COMMIT_BRANCH', 'Test-Training-Torso')
45
+ os.environ["MLFLOW_EXPERIMENT_NAME"] = experiment_name
46
+ print(f"Using experiment name: {experiment_name}")
47
+
48
+
49
+ # Set the run name
50
+ if run_name is None:
51
+ if os.getenv('CI') == 'true':
52
+ if os.getenv('CI_PARENT_PIPELINE_ID'):
53
+ run_name = f"{os.getenv('CI_PARENT_PIPELINE_ID')}-{os.getenv('CI_PIPELINE_ID')}"
54
+ else:
55
+ run_name = f"{os.getenv('CI_PIPELINE_ID')}"
56
+ else:
57
+ import datetime
58
+ run_name = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
59
+ os.environ["MLFLOW_RUN_NAME"] = run_name
60
+ print(f"Using run name: {run_name}")
61
+
62
+
63
+ # Create a new experiment or get the existing one
64
+ if experiment_id is None:
65
+ if os.getenv("MLFLOW_EXPERIMENT_ID"):
66
+ experiment_id = os.getenv("MLFLOW_EXPERIMENT_ID")
67
+ else:
68
+ try:
69
+ experiment_id = mlflow.create_experiment(experiment_name)
70
+ except mlflow.exceptions.MlflowException:
71
+ experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
72
+
73
+ check_experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
74
+ if str(check_experiment_id) != str(experiment_id):
75
+ raise ValueError(f"Provided experiment_id {experiment_id} does not match the ID of experiment_name {experiment_name} ({check_experiment_id})")
76
+
77
+ # if mlflow.get_experiment_by_name(experiment_name).experiment_id is None:
78
+ # experiment_id = mlflow.create_experiment(experiment_name)
79
+ # else:
80
+ # experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
81
+
82
+ mlflow.set_experiment(experiment_id=experiment_id)
83
+ os.environ["MLFLOW_EXPERIMENT_ID"] = experiment_id
84
+ print(f"Using experiment ID: {experiment_id}")
85
+
86
+
87
+ # Start a new MLflow run
88
+ if run_id is None:
89
+ if os.getenv("MLFLOW_RUN_ID"):
90
+ run_id = os.getenv("MLFLOW_RUN_ID")
91
+ else:
92
+ with mlflow.start_run(experiment_id=experiment_id, run_name=run_name) as run:
93
+ run_id = run.info.run_id
94
+
95
+ check_run_info = mlflow.get_run(run_id)
96
+ if str(check_run_info.info.experiment_id) != str(experiment_id):
97
+ raise ValueError(f"Provided run_id {run_id} does not belong to experiment_id {experiment_id} (found {check_run_info.info.experiment_id})")
98
+
99
+ os.environ["MLFLOW_RUN_ID"] = run_id
100
+ print(f"Started run with ID: {run_id}")
101
+
102
+ # Save environment variables to a file for later steps in CI/CD pipelines
103
+ if save_env_file and os.getenv("CI") == "true":
104
+ print(f"Saving MLflow environment variables to {os.getenv('CI_ENV_FILE', 'mlflow.env')}")
105
+ with open(os.getenv('CI_ENV_FILE', 'mlflow.env'), 'a') as f:
106
+ f.write(f"MLFLOW_URI={mlflow_uri}\n")
107
+ f.write(f"MLFLOW_MODEL_NAME={model_name}\n")
108
+ f.write(f"MLFLOW_EXPERIMENT_NAME={experiment_name}\n")
109
+ f.write(f"MLFLOW_RUN_NAME={run_name}\n")
110
+ f.write(f"MLFLOW_EXPERIMENT_ID={experiment_id}\n")
111
+ f.write(f"MLFLOW_RUN_ID={run_id}\n")
112
+
113
+ return experiment_name, run_name, experiment_id, run_id
114
+
115
+ if os.getenv("MLFLOW_TRACKING_PASSWORD") is not None and os.getenv("MLFLOW_TRACKING_USERNAME") is not None and os.getenv('CI') == 'true':
116
+ print("Setup mlflow run")
117
+ setup_mlflow()
118
+ else:
119
+ print("MLFLOW_TRACKING_PASSWORD and MLFLOW_TRACKING_USERNAME not set. Skipping mlflow run setup")
120
+
11
121
  class MLflowWrapper(mlflow.pyfunc.PythonModel):
12
122
  """PyFunc wrapper for TriggerModel; backend can be set at runtime."""
13
123
  def load_context(self, context):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: triggerflow
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: Utilities for ML models targeting hardware triggers
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -1,11 +1,11 @@
1
1
  triggerflow/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  triggerflow/core.py,sha256=MRr2M2_9kvANx8ZW0wBnS0f6L3uAQBDPQle9NXsPbvs,20639
3
- triggerflow/mlflow_wrapper.py,sha256=2z-278Z1gvoEdVsNw6M1S7UoDw14Vlr2FCJWBVMLwSg,3889
3
+ triggerflow/mlflow_wrapper.py,sha256=86dyMJJh5sckN-8qTvBfzZIFKbxrMrj8NOzwm3dKzUg,8503
4
4
  triggerflow/templates/makefile,sha256=VL39isTUBewrs8zTSDzdP6LLln7zpGoCZnLadpMu7CA,808
5
5
  triggerflow/templates/makefile_version,sha256=Tmu0tyAopJbiBQVMMOa6l2Cz5GkEn20mwgzIi0CfhyM,338
6
6
  triggerflow/templates/model_template.cpp,sha256=eGwY5ca_HgjoIvqorOBPSJspP0wngpjJheq3meb48r4,1616
7
7
  triggerflow/templates/scales.h,sha256=5bq6lVF36SRQKE2zg9RpBG6K5orpPlnJ8g125nbtFow,365
8
- triggerflow-0.1.6.dist-info/METADATA,sha256=_EsPMT8F3BTu1hA9Ff0oxisqB2qeqpgZdIqf_k_PLIQ,1942
9
- triggerflow-0.1.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
- triggerflow-0.1.6.dist-info/top_level.txt,sha256=g4M0nqpVPFZcmVmsoLExDtJFLDBK4fzobCIBqo13BEw,12
11
- triggerflow-0.1.6.dist-info/RECORD,,
8
+ triggerflow-0.1.7.dist-info/METADATA,sha256=fxAaPnQbPcs4mPJV7-dl5q8uotlWDpKQ0a5P6RRWKpw,1942
9
+ triggerflow-0.1.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ triggerflow-0.1.7.dist-info/top_level.txt,sha256=g4M0nqpVPFZcmVmsoLExDtJFLDBK4fzobCIBqo13BEw,12
11
+ triggerflow-0.1.7.dist-info/RECORD,,