Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

mlflow_handler.py 3.4 KB

You have to be logged in to leave a comment. Sign In
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  1. import os
  2. import logging
  3. import mlflow
  4. import tarfile
  5. import sagemaker
  6. import mlflow
  7. from mlflow.tracking import MlflowClient
  8. from mlflow.tracking.artifact_utils import _download_artifact_from_uri
  9. class MLflowHandler:
  10. def __init__(self, tracking_uri, model_name, model_version):
  11. mlflow.set_registry_uri(tracking_uri)
  12. mlflow.set_tracking_uri(tracking_uri)
  13. self.client = MlflowClient()
  14. self.model_name = model_name
  15. self.model_version = model_version
  16. self.model_stage = None
  17. logging.info('MLFLOW HANDLER LOADED!!!')
  18. def _download_model_version_files(self):
  19. """
  20. download model version files to a local tmp folder.
  21. """
  22. model_version = self.client.get_model_version(name=self.model_name, version=self.model_version)
  23. artifact_uri = model_version.source
  24. return _download_artifact_from_uri(artifact_uri)
  25. @staticmethod
  26. def _make_tar_gz_file(output_filename, source_dir):
  27. """
  28. create a tar.gz from a directory.
  29. """
  30. with tarfile.open(output_filename, "w:gz") as tar:
  31. for f in os.listdir(source_dir):
  32. tar.add(os.path.join(source_dir, f), arcname=f)
  33. def prepare_sagemaker_model(self):
  34. """
  35. create and upload a tar.gz to S3 from a chosen MLflow model version.
  36. """
  37. try:
  38. sagemaker_session = sagemaker.Session()
  39. bucket = 'kvasir-segmentation' # you can specify other bucket name here
  40. prefix = f'mlflow_model/{self.model_name}-{self.model_version}'
  41. tmp_file = '/tmp/model.tar.gz'
  42. model_local_path = self._download_model_version_files()
  43. self._make_tar_gz_file(tmp_file, model_local_path)
  44. tar_gz_s3_location = sagemaker_session.upload_data(path=tmp_file, bucket=bucket, key_prefix=prefix)
  45. logging.info(f'model.tar.gz upload to {tar_gz_s3_location}')
  46. return tar_gz_s3_location
  47. except Exception as e:
  48. logging.error(e)
  49. def transition_model_version_stage(self, stage):
  50. """
  51. Transitions a model version to input stage.
  52. Transitions other model versions to Archived if they were in Staging or Production.
  53. """
  54. try:
  55. for model in self.client.search_model_versions(f"name='{self.model_name}'"):
  56. if model.current_stage in ['Staging', 'Production']:
  57. self.client.transition_model_version_stage(
  58. name=model.name,
  59. version=model.version,
  60. stage="Archived"
  61. )
  62. logging.info(f'Transitioning {model.name}/{model.version} to Archived')
  63. self.client.transition_model_version_stage(
  64. name=self.model_name,
  65. version=self.model_version,
  66. stage=stage
  67. )
  68. logging.info(f'Model transitioned to {stage}')
  69. except Exception as e:
  70. logging.error(e)
  71. def deploy_model(image_uri, role):
  72. model_uri = f'modesls:{self.model_name}/{self.model_version}'
  73. #deploy model
  74. mlflow.sagemaker.deploy(
  75. mode='create',
  76. app_name='Image-segmentation',
  77. model_uri=model_uri,
  78. image_url=image_uri,
  79. execution_role_arn=role,
  80. instance_type='ml.m5.xlarge',
  81. instance_count=1,
  82. region_name='us-east-2'
  83. )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...