triggerflow 0.1.6__py3-none-any.whl → 0.1.8__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,120 @@ 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: bool = True,
19
+ auto_configure: bool = False
20
+ ):
21
+
22
+ # Set the MLflow tracking URI
23
+ if mlflow_uri is None:
24
+ mlflow_uri = os.getenv('MLFLOW_URI', 'https://ngt.cern.ch/models')
25
+ mlflow.set_tracking_uri(mlflow_uri)
26
+ os.environ["MLFLOW_URI"] = mlflow_uri
27
+ print(f"Using MLflow tracking URI: {mlflow_uri}")
28
+
29
+
30
+ # Set the model name
31
+ if model_name is None:
32
+ if os.getenv('MLFLOW_MODEL_NAME'):
33
+ model_name = os.getenv('MLFLOW_MODEL_NAME')
34
+ else:
35
+ model_name = os.getenv('CI_COMMIT_BRANCH', 'Test-Model')
36
+ os.environ["MLFLOW_MODEL_NAME"] = model_name
37
+ print(f"Using model name: {model_name}")
38
+
39
+
40
+ # Set the experiment name
41
+ if experiment_name is None:
42
+ if os.getenv('MLFLOW_EXPERIMENT_NAME'):
43
+ experiment_name = os.getenv('MLFLOW_EXPERIMENT_NAME')
44
+ else:
45
+ experiment_name = os.getenv('CI_COMMIT_BRANCH', 'Test-Training-Torso')
46
+ os.environ["MLFLOW_EXPERIMENT_NAME"] = experiment_name
47
+ print(f"Using experiment name: {experiment_name}")
48
+
49
+
50
+ # Set the run name
51
+ if run_name is None:
52
+ if os.getenv('CI') == 'true':
53
+ if os.getenv('CI_PARENT_PIPELINE_ID'):
54
+ run_name = f"{os.getenv('CI_PARENT_PIPELINE_ID')}-{os.getenv('CI_PIPELINE_ID')}"
55
+ else:
56
+ run_name = f"{os.getenv('CI_PIPELINE_ID')}"
57
+ else:
58
+ import datetime
59
+ run_name = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
60
+ os.environ["MLFLOW_RUN_NAME"] = run_name
61
+ print(f"Using run name: {run_name}")
62
+
63
+
64
+ # Create a new experiment or get the existing one
65
+ if experiment_id is None:
66
+ if os.getenv("MLFLOW_EXPERIMENT_ID"):
67
+ experiment_id = os.getenv("MLFLOW_EXPERIMENT_ID")
68
+ else:
69
+ try:
70
+ experiment_id = mlflow.create_experiment(experiment_name)
71
+ except mlflow.exceptions.MlflowException:
72
+ experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
73
+
74
+ check_experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
75
+ if str(check_experiment_id) != str(experiment_id):
76
+ raise ValueError(f"Provided experiment_id {experiment_id} does not match the ID of experiment_name {experiment_name} ({check_experiment_id})")
77
+
78
+ # if mlflow.get_experiment_by_name(experiment_name).experiment_id is None:
79
+ # experiment_id = mlflow.create_experiment(experiment_name)
80
+ # else:
81
+ # experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
82
+
83
+ mlflow.set_experiment(experiment_id=experiment_id)
84
+ os.environ["MLFLOW_EXPERIMENT_ID"] = experiment_id
85
+ print(f"Using experiment ID: {experiment_id}")
86
+
87
+
88
+ # Start a new MLflow run
89
+ if run_id is None:
90
+ if os.getenv("MLFLOW_RUN_ID"):
91
+ run_id = os.getenv("MLFLOW_RUN_ID")
92
+ else:
93
+ with mlflow.start_run(experiment_id=experiment_id, run_name=run_name) as run:
94
+ run_id = run.info.run_id
95
+
96
+ check_run_info = mlflow.get_run(run_id)
97
+ if str(check_run_info.info.experiment_id) != str(experiment_id):
98
+ raise ValueError(f"Provided run_id {run_id} does not belong to experiment_id {experiment_id} (found {check_run_info.info.experiment_id})")
99
+
100
+ os.environ["MLFLOW_RUN_ID"] = run_id
101
+ print(f"Started run with ID: {run_id}")
102
+
103
+ # Save environment variables to a file for later steps in CI/CD pipelines
104
+ if save_env_file and os.getenv("CI") == "true":
105
+ print(f"Saving MLflow environment variables to {os.getenv('CI_ENV_FILE', 'mlflow.env')}")
106
+ with open(os.getenv('CI_ENV_FILE', 'mlflow.env'), 'a') as f:
107
+ f.write(f"MLFLOW_URI={mlflow_uri}\n")
108
+ f.write(f"MLFLOW_MODEL_NAME={model_name}\n")
109
+ f.write(f"MLFLOW_EXPERIMENT_NAME={experiment_name}\n")
110
+ f.write(f"MLFLOW_RUN_NAME={run_name}\n")
111
+ f.write(f"MLFLOW_EXPERIMENT_ID={experiment_id}\n")
112
+ f.write(f"MLFLOW_RUN_ID={run_id}\n")
113
+
114
+ if auto_configure:
115
+ print("Auto_configure is set to true. Exporting AUTO_CONFIGURE=true")
116
+ f.write(f"AUTO_CONFIGURE=true\n")
117
+
118
+ return experiment_name, run_name, experiment_id, run_id
119
+
120
+ if os.getenv("AUTO_CONFIGURE") == "true":
121
+ print("AUTO_CONFIGURE is true and running in CI environment. Setting up mlflow...")
122
+ setup_mlflow()
123
+ else:
124
+ print("AUTO_CONFIGURE is not set. Skipping mlflow run setup")
125
+
11
126
  class MLflowWrapper(mlflow.pyfunc.PythonModel):
12
127
  """PyFunc wrapper for TriggerModel; backend can be set at runtime."""
13
128
  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.8
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=BJhkVMn7-H4AnjgnpHiprLUqvdXUXWvN4NPdL7anZhQ,8652
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.8.dist-info/METADATA,sha256=_ER4GTO1DCk4dgS_eALiHOeFflnNleuir8Q7_3xwAxM,1942
9
+ triggerflow-0.1.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ triggerflow-0.1.8.dist-info/top_level.txt,sha256=g4M0nqpVPFZcmVmsoLExDtJFLDBK4fzobCIBqo13BEw,12
11
+ triggerflow-0.1.8.dist-info/RECORD,,