remote_server.py
1 import os 2 import random 3 import shutil 4 import sys 5 import tempfile 6 7 from mlflow import ( 8 MlflowClient, 9 active_run, 10 get_artifact_uri, 11 get_tracking_uri, 12 log_artifact, 13 log_artifacts, 14 log_metric, 15 log_param, 16 ) 17 18 if __name__ == "__main__": 19 print(f"Running {sys.argv[0]} with tracking URI {get_tracking_uri()}") 20 log_param("param1", 5) 21 log_metric("foo", 5) 22 log_metric("foo", 6) 23 log_metric("foo", 7) 24 log_metric("random_int", random.randint(0, 100)) 25 run_id = active_run().info.run_id 26 # Get run metadata & data from the tracking server 27 service = MlflowClient() 28 run = service.get_run(run_id) 29 print(f"Metadata & data for run with UUID {run_id}: {run}") 30 local_dir = tempfile.mkdtemp() 31 message = "test artifact written during run {} within artifact URI {}\n".format( 32 active_run().info.run_id, 33 get_artifact_uri(), 34 ) 35 try: 36 file_path = os.path.join(local_dir, "some_output_file.txt") 37 with open(file_path, "w") as handle: 38 handle.write(message) 39 log_artifacts(local_dir, "some_subdir") 40 log_artifact(file_path, "another_dir") 41 finally: 42 shutil.rmtree(local_dir)