oracle-ads 2.12.10rc0__py3-none-any.whl → 2.13.0__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.
Files changed (66) hide show
  1. ads/aqua/__init__.py +2 -1
  2. ads/aqua/app.py +46 -19
  3. ads/aqua/client/__init__.py +3 -0
  4. ads/aqua/client/client.py +799 -0
  5. ads/aqua/common/enums.py +19 -14
  6. ads/aqua/common/errors.py +3 -4
  7. ads/aqua/common/utils.py +2 -2
  8. ads/aqua/constants.py +1 -0
  9. ads/aqua/evaluation/constants.py +7 -7
  10. ads/aqua/evaluation/errors.py +3 -4
  11. ads/aqua/evaluation/evaluation.py +20 -12
  12. ads/aqua/extension/aqua_ws_msg_handler.py +14 -7
  13. ads/aqua/extension/base_handler.py +12 -9
  14. ads/aqua/extension/model_handler.py +29 -1
  15. ads/aqua/extension/models/ws_models.py +5 -6
  16. ads/aqua/finetuning/constants.py +3 -3
  17. ads/aqua/finetuning/entities.py +3 -0
  18. ads/aqua/finetuning/finetuning.py +32 -1
  19. ads/aqua/model/constants.py +7 -7
  20. ads/aqua/model/entities.py +2 -1
  21. ads/aqua/model/enums.py +4 -5
  22. ads/aqua/model/model.py +158 -76
  23. ads/aqua/modeldeployment/deployment.py +22 -10
  24. ads/aqua/modeldeployment/entities.py +3 -1
  25. ads/cli.py +16 -8
  26. ads/common/auth.py +33 -20
  27. ads/common/extended_enum.py +52 -44
  28. ads/llm/__init__.py +11 -8
  29. ads/llm/langchain/plugins/embeddings/__init__.py +4 -0
  30. ads/llm/langchain/plugins/embeddings/oci_data_science_model_deployment_endpoint.py +184 -0
  31. ads/model/artifact_downloader.py +3 -4
  32. ads/model/datascience_model.py +84 -64
  33. ads/model/generic_model.py +3 -3
  34. ads/model/model_metadata.py +17 -11
  35. ads/model/service/oci_datascience_model.py +12 -14
  36. ads/opctl/backend/marketplace/helm_helper.py +13 -14
  37. ads/opctl/cli.py +4 -5
  38. ads/opctl/cmds.py +28 -32
  39. ads/opctl/config/merger.py +8 -11
  40. ads/opctl/config/resolver.py +25 -30
  41. ads/opctl/operator/cli.py +9 -9
  42. ads/opctl/operator/common/backend_factory.py +56 -60
  43. ads/opctl/operator/common/const.py +5 -5
  44. ads/opctl/operator/lowcode/anomaly/const.py +8 -9
  45. ads/opctl/operator/lowcode/common/transformations.py +38 -3
  46. ads/opctl/operator/lowcode/common/utils.py +11 -1
  47. ads/opctl/operator/lowcode/feature_store_marketplace/operator_utils.py +43 -48
  48. ads/opctl/operator/lowcode/forecast/__main__.py +10 -0
  49. ads/opctl/operator/lowcode/forecast/const.py +6 -6
  50. ads/opctl/operator/lowcode/forecast/model/forecast_datasets.py +1 -1
  51. ads/opctl/operator/lowcode/forecast/operator_config.py +31 -0
  52. ads/opctl/operator/lowcode/forecast/schema.yaml +63 -0
  53. ads/opctl/operator/lowcode/forecast/whatifserve/__init__.py +7 -0
  54. ads/opctl/operator/lowcode/forecast/whatifserve/deployment_manager.py +233 -0
  55. ads/opctl/operator/lowcode/forecast/whatifserve/score.py +238 -0
  56. ads/opctl/operator/lowcode/pii/constant.py +6 -7
  57. ads/opctl/operator/lowcode/recommender/constant.py +12 -7
  58. ads/opctl/operator/runtime/marketplace_runtime.py +4 -10
  59. ads/opctl/operator/runtime/runtime.py +4 -6
  60. ads/pipeline/ads_pipeline_run.py +13 -25
  61. ads/pipeline/visualizer/graph_renderer.py +3 -4
  62. {oracle_ads-2.12.10rc0.dist-info → oracle_ads-2.13.0.dist-info}/METADATA +4 -2
  63. {oracle_ads-2.12.10rc0.dist-info → oracle_ads-2.13.0.dist-info}/RECORD +66 -59
  64. {oracle_ads-2.12.10rc0.dist-info → oracle_ads-2.13.0.dist-info}/LICENSE.txt +0 -0
  65. {oracle_ads-2.12.10rc0.dist-info → oracle_ads-2.13.0.dist-info}/WHEEL +0 -0
  66. {oracle_ads-2.12.10rc0.dist-info → oracle_ads-2.13.0.dist-info}/entry_points.txt +0 -0
@@ -1,13 +1,13 @@
1
1
  #!/usr/bin/env python
2
2
 
3
- # Copyright (c) 2023, 2024 Oracle and/or its affiliates.
3
+ # Copyright (c) 2023, 2025 Oracle and/or its affiliates.
4
4
  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
5
 
6
- from ads.common.extended_enum import ExtendedEnumMeta
6
+ from ads.common.extended_enum import ExtendedEnum
7
7
  from ads.opctl.operator.lowcode.common.const import DataColumns
8
8
 
9
9
 
10
- class SupportedModels(str, metaclass=ExtendedEnumMeta):
10
+ class SupportedModels(ExtendedEnum):
11
11
  """Supported forecast models."""
12
12
 
13
13
  Prophet = "prophet"
@@ -19,7 +19,7 @@ class SupportedModels(str, metaclass=ExtendedEnumMeta):
19
19
  # Auto = "auto"
20
20
 
21
21
 
22
- class SpeedAccuracyMode(str, metaclass=ExtendedEnumMeta):
22
+ class SpeedAccuracyMode(ExtendedEnum):
23
23
  """
24
24
  Enum representing different modes based on time taken and accuracy for explainability.
25
25
  """
@@ -35,7 +35,7 @@ class SpeedAccuracyMode(str, metaclass=ExtendedEnumMeta):
35
35
  ratio[AUTOMLX] = 0 # constant
36
36
 
37
37
 
38
- class SupportedMetrics(str, metaclass=ExtendedEnumMeta):
38
+ class SupportedMetrics(ExtendedEnum):
39
39
  """Supported forecast metrics."""
40
40
 
41
41
  MAPE = "MAPE"
@@ -62,7 +62,7 @@ class SupportedMetrics(str, metaclass=ExtendedEnumMeta):
62
62
  ELAPSED_TIME = "Elapsed Time"
63
63
 
64
64
 
65
- class ForecastOutputColumns(str, metaclass=ExtendedEnumMeta):
65
+ class ForecastOutputColumns(ExtendedEnum):
66
66
  """The column names for the forecast.csv output file"""
67
67
 
68
68
  DATE = "Date"
@@ -167,7 +167,7 @@ class ForecastDatasets:
167
167
  self.historical_data.data,
168
168
  self.additional_data.data,
169
169
  ],
170
- axis=1,
170
+ axis=1
171
171
  )
172
172
 
173
173
  def get_data_by_series(self, include_horizon=True):
@@ -18,6 +18,35 @@ from ads.opctl.operator.lowcode.common.utils import find_output_dirname
18
18
 
19
19
  from .const import SpeedAccuracyMode, SupportedMetrics, SupportedModels
20
20
 
21
+ @dataclass
22
+ class AutoScaling(DataClassSerializable):
23
+ """Class representing simple autoscaling policy"""
24
+ minimum_instance: int = 1
25
+ maximum_instance: int = None
26
+ cool_down_in_seconds: int = 600
27
+ scale_in_threshold: int = 10
28
+ scale_out_threshold: int = 80
29
+ scaling_metric: str = "CPU_UTILIZATION"
30
+
31
+ @dataclass(repr=True)
32
+ class ModelDeploymentServer(DataClassSerializable):
33
+ """Class representing model deployment server specification for whatif-analysis."""
34
+ display_name: str = None
35
+ initial_shape: str = None
36
+ description: str = None
37
+ log_group: str = None
38
+ log_id: str = None
39
+ auto_scaling: AutoScaling = field(default_factory=AutoScaling)
40
+
41
+
42
+ @dataclass(repr=True)
43
+ class WhatIfAnalysis(DataClassSerializable):
44
+ """Class representing operator specification for whatif-analysis."""
45
+ model_display_name: str = None
46
+ compartment_id: str = None
47
+ project_id: str = None
48
+ model_deployment: ModelDeploymentServer = field(default_factory=ModelDeploymentServer)
49
+
21
50
 
22
51
  @dataclass(repr=True)
23
52
  class TestData(InputData):
@@ -90,12 +119,14 @@ class ForecastOperatorSpec(DataClassSerializable):
90
119
  confidence_interval_width: float = None
91
120
  metric: str = None
92
121
  tuning: Tuning = field(default_factory=Tuning)
122
+ what_if_analysis: WhatIfAnalysis = field(default_factory=WhatIfAnalysis)
93
123
 
94
124
  def __post_init__(self):
95
125
  """Adjusts the specification details."""
96
126
  self.output_directory = self.output_directory or OutputDirectory(
97
127
  url=find_output_dirname(self.output_directory)
98
128
  )
129
+ self.generate_model_pickle = True if self.generate_model_pickle or self.what_if_analysis else False
99
130
  self.metric = (self.metric or "").lower() or SupportedMetrics.SMAPE.lower()
100
131
  self.model = self.model or SupportedModels.Prophet
101
132
  self.confidence_interval_width = self.confidence_interval_width or 0.80
@@ -353,6 +353,69 @@ spec:
353
353
  meta:
354
354
  description: "Report file generation can be enabled using this flag. Defaults to true."
355
355
 
356
+ what_if_analysis:
357
+ type: dict
358
+ required: false
359
+ schema:
360
+ model_deployment:
361
+ type: dict
362
+ required: false
363
+ meta: "If model_deployment id is not specified, a new model deployment is created; otherwise, the model is linked to the specified model deployment."
364
+ schema:
365
+ id:
366
+ type: string
367
+ required: false
368
+ display_name:
369
+ type: string
370
+ required: false
371
+ initial_shape:
372
+ type: string
373
+ required: false
374
+ description:
375
+ type: string
376
+ required: false
377
+ log_group:
378
+ type: string
379
+ required: true
380
+ log_id:
381
+ type: string
382
+ required: true
383
+ auto_scaling:
384
+ type: dict
385
+ required: false
386
+ schema:
387
+ minimum_instance:
388
+ type: integer
389
+ required: true
390
+ maximum_instance:
391
+ type: integer
392
+ required: true
393
+ scale_in_threshold:
394
+ type: integer
395
+ required: true
396
+ scale_out_threshold:
397
+ type: integer
398
+ required: true
399
+ scaling_metric:
400
+ type: string
401
+ required: true
402
+ cool_down_in_seconds:
403
+ type: integer
404
+ required: true
405
+ model_display_name:
406
+ type: string
407
+ required: true
408
+ project_id:
409
+ type: string
410
+ required: false
411
+ meta: "If not provided, The project OCID from config.PROJECT_OCID is used"
412
+ compartment_id:
413
+ type: string
414
+ required: false
415
+ meta: "If not provided, The compartment OCID from config.NB_SESSION_COMPARTMENT_OCID is used."
416
+ meta:
417
+ description: "When enabled, the models are saved to the model catalog. Defaults to false."
418
+
356
419
  generate_metrics:
357
420
  type: boolean
358
421
  required: false
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) 2023, 2024 Oracle and/or its affiliates.
4
+ # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
+
6
+
7
+ from .deployment_manager import ModelDeploymentManager
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env python
2
+ import json
3
+ # Copyright (c) 2023, 2024 Oracle and/or its affiliates.
4
+ # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
+
6
+ import os
7
+ import pickle
8
+ import shutil
9
+ import sys
10
+ import tempfile
11
+ import oci
12
+
13
+ import pandas as pd
14
+ import cloudpickle
15
+
16
+ from ads.opctl import logger
17
+ from ads.common.model_export_util import prepare_generic_model
18
+ from ads.opctl.operator.lowcode.common.utils import write_data, write_simple_json
19
+ from ads.opctl.operator.lowcode.common.utils import default_signer
20
+ from ..model.forecast_datasets import AdditionalData
21
+ from ..operator_config import ForecastOperatorSpec
22
+
23
+ from oci.data_science import DataScienceClient, DataScienceClientCompositeOperations
24
+
25
+ from oci.data_science.models import ModelConfigurationDetails, InstanceConfiguration, \
26
+ FixedSizeScalingPolicy, CategoryLogDetails, LogDetails, \
27
+ SingleModelDeploymentConfigurationDetails, CreateModelDeploymentDetails
28
+ from ads.common.object_storage_details import ObjectStorageDetails
29
+
30
+
31
+ class ModelDeploymentManager:
32
+ def __init__(self, spec: ForecastOperatorSpec, additional_data: AdditionalData, previous_model_version=None):
33
+ self.spec = spec
34
+ self.model_name = spec.model
35
+ self.horizon = spec.horizon
36
+ self.additional_data = additional_data.get_dict_by_series()
37
+ self.model_obj = {}
38
+ self.display_name = spec.what_if_analysis.model_display_name
39
+ self.project_id = spec.what_if_analysis.project_id if spec.what_if_analysis.project_id \
40
+ else os.environ.get('PROJECT_OCID')
41
+ self.compartment_id = spec.what_if_analysis.compartment_id if spec.what_if_analysis.compartment_id \
42
+ else os.environ.get('NB_SESSION_COMPARTMENT_OCID')
43
+ if self.project_id is None or self.compartment_id is None:
44
+ raise ValueError("Either project_id or compartment_id cannot be None.")
45
+ self.path_to_artifact = f"{self.spec.output_directory.url}/artifacts/"
46
+ self.pickle_file_path = f"{self.spec.output_directory.url}/model.pkl"
47
+ self.model_version = previous_model_version + 1 if previous_model_version else 1
48
+ self.catalog_id = None
49
+ self.test_mode = os.environ.get("TEST_MODE", False)
50
+ self.deployment_info = {}
51
+
52
+ def _sanity_test(self):
53
+ """
54
+ Function perform sanity test for saved artifact
55
+ """
56
+ org_sys_path = sys.path[:]
57
+ try:
58
+ sys.path.insert(0, f"{self.path_to_artifact}")
59
+ from score import load_model, predict
60
+ _ = load_model()
61
+
62
+ # Write additional data to tmp file and perform sanity check
63
+ with tempfile.NamedTemporaryFile(suffix='.csv') as temp_file:
64
+ one_series = next(iter(self.additional_data))
65
+ sample_prediction_data = self.additional_data[one_series].tail(self.horizon)
66
+ sample_prediction_data[self.spec.target_category_columns[0]] = one_series
67
+ date_col_name = self.spec.datetime_column.name
68
+ date_col_format = self.spec.datetime_column.format
69
+ sample_prediction_data[date_col_name] = sample_prediction_data[date_col_name].dt.strftime(
70
+ date_col_format)
71
+ sample_prediction_data.to_csv(temp_file.name, index=False)
72
+ input_data = {"additional_data": {"url": temp_file.name}}
73
+ prediction_test = predict(input_data, _)
74
+ logger.info(f"prediction test completed with result :{prediction_test}")
75
+ except Exception as e:
76
+ logger.error(f"An error occurred during the sanity test: {e}")
77
+ raise
78
+ finally:
79
+ sys.path = org_sys_path
80
+
81
+ def _copy_score_file(self):
82
+ """
83
+ Copies the score.py to the artifact_path.
84
+ """
85
+ try:
86
+ current_dir = os.path.dirname(os.path.abspath(__file__))
87
+ score_file = os.path.join(current_dir, "score.py")
88
+ destination_file = os.path.join(self.path_to_artifact, os.path.basename(score_file))
89
+ shutil.copy2(score_file, destination_file)
90
+ logger.info(f"score.py copied successfully to {self.path_to_artifact}")
91
+ except Exception as e:
92
+ logger.warn(f"Error copying file: {e}")
93
+ raise e
94
+
95
+ def save_to_catalog(self):
96
+ """Save the model to a model catalog"""
97
+ with open(self.pickle_file_path, 'rb') as file:
98
+ self.model_obj = pickle.load(file)
99
+
100
+ if not os.path.exists(self.path_to_artifact):
101
+ os.mkdir(self.path_to_artifact)
102
+
103
+ artifact_dict = {"spec": self.spec.to_dict(), "models": self.model_obj}
104
+ with open(f"{self.path_to_artifact}/models.pickle", "wb") as f:
105
+ cloudpickle.dump(artifact_dict, f)
106
+ artifact = prepare_generic_model(
107
+ self.path_to_artifact,
108
+ function_artifacts=False,
109
+ force_overwrite=True,
110
+ data_science_env=True)
111
+
112
+ self._copy_score_file()
113
+ self._sanity_test()
114
+
115
+ if isinstance(self.model_obj, dict):
116
+ series = self.model_obj.keys()
117
+ else:
118
+ series = self.additional_data.keys()
119
+ description = f"The object contains {len(series)} {self.model_name} models"
120
+
121
+ if not self.test_mode:
122
+ catalog_entry = artifact.save(
123
+ display_name=self.display_name,
124
+ compartment_id=self.compartment_id,
125
+ project_id=self.project_id,
126
+ description=description)
127
+ self.catalog_id = catalog_entry.id
128
+
129
+ logger.info(f"Saved {self.model_name} version-v{self.model_version} to model catalog"
130
+ f" with model ocid : {self.catalog_id}")
131
+
132
+ self.deployment_info = {"model_ocid": self.catalog_id, "series": list(series)}
133
+
134
+ def create_deployment(self):
135
+ """Create a model deployment serving"""
136
+
137
+ # create new model deployment
138
+ initial_shape = self.spec.what_if_analysis.model_deployment.initial_shape
139
+ name = self.spec.what_if_analysis.model_deployment.display_name
140
+ description = self.spec.what_if_analysis.model_deployment.description
141
+ auto_scaling_config = self.spec.what_if_analysis.model_deployment.auto_scaling
142
+
143
+ # if auto_scaling_config is defined
144
+ if auto_scaling_config:
145
+ scaling_policy = oci.data_science.models.AutoScalingPolicy(
146
+ policy_type="AUTOSCALING",
147
+ auto_scaling_policies=[
148
+ oci.data_science.models.ThresholdBasedAutoScalingPolicyDetails(
149
+ auto_scaling_policy_type="THRESHOLD",
150
+ rules=[
151
+ oci.data_science.models.PredefinedMetricExpressionRule(
152
+ metric_expression_rule_type="PREDEFINED_EXPRESSION",
153
+ metric_type=auto_scaling_config.scaling_metric,
154
+ scale_in_configuration=oci.data_science.models.PredefinedExpressionThresholdScalingConfiguration(
155
+ scaling_configuration_type="THRESHOLD",
156
+ threshold=auto_scaling_config.scale_in_threshold
157
+ ),
158
+ scale_out_configuration=oci.data_science.models.PredefinedExpressionThresholdScalingConfiguration(
159
+ scaling_configuration_type="THRESHOLD",
160
+ threshold=auto_scaling_config.scale_out_threshold
161
+ )
162
+ )],
163
+ maximum_instance_count=auto_scaling_config.maximum_instance,
164
+ minimum_instance_count=auto_scaling_config.minimum_instance,
165
+ initial_instance_count=auto_scaling_config.minimum_instance)],
166
+ cool_down_in_seconds=auto_scaling_config.cool_down_in_seconds,
167
+ is_enabled=True)
168
+ logger.info(f"Using autoscaling {auto_scaling_config.scaling_metric} for creating MD")
169
+ else:
170
+ scaling_policy = FixedSizeScalingPolicy(instance_count=1)
171
+ logger.info("Using fixed size policy for creating MD")
172
+
173
+ model_configuration_details_object = ModelConfigurationDetails(
174
+ model_id=self.catalog_id,
175
+ instance_configuration=InstanceConfiguration(
176
+ instance_shape_name=initial_shape),
177
+ scaling_policy=scaling_policy,
178
+ bandwidth_mbps=20)
179
+
180
+ single_model_config = SingleModelDeploymentConfigurationDetails(
181
+ deployment_type='SINGLE_MODEL',
182
+ model_configuration_details=model_configuration_details_object
183
+ )
184
+
185
+ log_group = self.spec.what_if_analysis.model_deployment.log_group
186
+ log_id = self.spec.what_if_analysis.model_deployment.log_id
187
+
188
+ logs_configuration_details_object = CategoryLogDetails(
189
+ access=LogDetails(log_group_id=log_group,
190
+ log_id=log_id),
191
+ predict=LogDetails(log_group_id=log_group,
192
+ log_id=log_id))
193
+
194
+ model_deploy_configuration = CreateModelDeploymentDetails(
195
+ display_name=name,
196
+ description=description,
197
+ project_id=self.project_id,
198
+ compartment_id=self.compartment_id,
199
+ model_deployment_configuration_details=single_model_config,
200
+ category_log_details=logs_configuration_details_object)
201
+
202
+ if not self.test_mode:
203
+ auth = oci.auth.signers.get_resource_principals_signer()
204
+ data_science = DataScienceClient({}, signer=auth)
205
+ data_science_composite = DataScienceClientCompositeOperations(data_science)
206
+ model_deployment = data_science_composite.create_model_deployment_and_wait_for_state(
207
+ model_deploy_configuration,
208
+ wait_for_states=[
209
+ "SUCCEEDED", "FAILED"])
210
+ self.deployment_info['work_request'] = model_deployment.data.id
211
+ logger.info(f"deployment metadata :{model_deployment.data}")
212
+ md = data_science.get_model_deployment(model_deployment_id=model_deployment.data.resources[0].identifier)
213
+ self.deployment_info['model_deployment_ocid'] = md.data.id
214
+ endpoint_url = md.data.model_deployment_url
215
+ self.deployment_info['model_deployment_endpoint'] = f"{endpoint_url}/predict"
216
+
217
+ def save_deployment_info(self):
218
+ output_dir = self.spec.output_directory.url
219
+ if ObjectStorageDetails.is_oci_path(output_dir):
220
+ storage_options = default_signer()
221
+ else:
222
+ storage_options = {}
223
+ write_data(
224
+ data=pd.DataFrame.from_dict(self.deployment_info),
225
+ filename=os.path.join(output_dir, "deployment_info.json"),
226
+ format="json",
227
+ storage_options=storage_options,
228
+ index=False,
229
+ indent=4,
230
+ orient="records"
231
+ )
232
+ write_simple_json(self.deployment_info, os.path.join(output_dir, "deployment_info.json"))
233
+ logger.info(f"Saved deployment info to {output_dir}")
@@ -0,0 +1,238 @@
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) 2023, 2024 Oracle and/or its affiliates.
4
+ # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5
+
6
+ import json
7
+ import os
8
+ from joblib import load
9
+ import pandas as pd
10
+ import numpy as np
11
+ from functools import lru_cache
12
+ import logging
13
+ import ads
14
+ from ads.opctl.operator.lowcode.common.utils import load_data
15
+ from ads.opctl.operator.common.operator_config import InputData
16
+ from ads.opctl.operator.lowcode.forecast.const import SupportedModels
17
+
18
+ ads.set_auth("resource_principal")
19
+
20
+ logging.basicConfig(format='%(name)s - %(levelname)s - %(message)s', level=logging.INFO)
21
+ logger_pred = logging.getLogger('model-prediction')
22
+ logger_pred.setLevel(logging.INFO)
23
+ logger_feat = logging.getLogger('input-features')
24
+ logger_feat.setLevel(logging.INFO)
25
+
26
+ """
27
+ Inference script. This script is used for prediction by scoring server when schema is known.
28
+ """
29
+
30
+
31
+ @lru_cache(maxsize=10)
32
+ def load_model():
33
+ """
34
+ Loads model from the serialized format
35
+
36
+ Returns
37
+ -------
38
+ model: a model instance on which predict API can be invoked
39
+ """
40
+ model_dir = os.path.dirname(os.path.realpath(__file__))
41
+ contents = os.listdir(model_dir)
42
+ model_file_name = "models.pickle"
43
+ if model_file_name in contents:
44
+ with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), model_file_name), "rb") as file:
45
+ model = load(file)
46
+ else:
47
+ raise Exception('{0} is not found in model directory {1}'.format(model_file_name, model_dir))
48
+ return model
49
+
50
+
51
+ @lru_cache(maxsize=1)
52
+ def fetch_data_type_from_schema(
53
+ input_schema_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "input_schema.json")):
54
+ """
55
+ Returns data type information fetch from input_schema.json.
56
+
57
+ Parameters
58
+ ----------
59
+ input_schema_path: path of input schema.
60
+
61
+ Returns
62
+ -------
63
+ data_type: data type fetch from input_schema.json.
64
+
65
+ """
66
+ data_type = {}
67
+ if os.path.exists(input_schema_path):
68
+ schema = json.load(open(input_schema_path))
69
+ for col in schema['schema']:
70
+ data_type[col['name']] = col['dtype']
71
+ else:
72
+ print(
73
+ "input_schema has to be passed in in order to recover the same data type. pass `X_sample` in `ads.model.framework.sklearn_model.SklearnModel.prepare` function to generate the input_schema. Otherwise, the data type might be changed after serialization/deserialization.")
74
+ return data_type
75
+
76
+
77
+ def deserialize(data, input_schema_path):
78
+ """
79
+ Deserialize json serialization data to data in original type when sent to predict.
80
+
81
+ Parameters
82
+ ----------
83
+ data: serialized input data.
84
+ input_schema_path: path of input schema.
85
+
86
+ Returns
87
+ -------
88
+ data: deserialized input data.
89
+
90
+ """
91
+
92
+ # Add further data deserialization if needed
93
+ return data
94
+
95
+
96
+ def pre_inference(data, input_schema_path):
97
+ """
98
+ Preprocess data
99
+
100
+ Parameters
101
+ ----------
102
+ data: Data format as expected by the predict API of the core estimator.
103
+ input_schema_path: path of input schema.
104
+
105
+ Returns
106
+ -------
107
+ data: Data format after any processing.
108
+
109
+ """
110
+ return deserialize(data, input_schema_path)
111
+
112
+
113
+ def post_inference(yhat):
114
+ """
115
+ Post-process the model results
116
+
117
+ Parameters
118
+ ----------
119
+ yhat: Data format after calling model.predict.
120
+
121
+ Returns
122
+ -------
123
+ yhat: Data format after any processing.
124
+
125
+ """
126
+ if isinstance(yhat, pd.core.frame.DataFrame):
127
+ yhat = yhat.values
128
+ if isinstance(yhat, np.ndarray):
129
+ yhat = yhat.tolist()
130
+ return yhat
131
+
132
+
133
+ def get_forecast(future_df, model_name, series_id, model_object, date_col, target_column, target_cat_col, horizon):
134
+ date_col_name = date_col["name"]
135
+ date_col_format = date_col["format"]
136
+ future_df[target_cat_col] = future_df[target_cat_col].astype("str")
137
+ future_df[date_col_name] = pd.to_datetime(
138
+ future_df[date_col_name], format=date_col_format
139
+ )
140
+ if model_name == SupportedModels.AutoTS:
141
+ series_id_col = "Series"
142
+ full_data_indexed = future_df.rename(columns={target_cat_col: series_id_col})
143
+ additional_regressors = list(
144
+ set(full_data_indexed.columns) - {target_column, series_id_col, date_col_name}
145
+ )
146
+ future_reg = full_data_indexed.reset_index().pivot(
147
+ index=date_col_name,
148
+ columns=series_id_col,
149
+ values=additional_regressors,
150
+ )
151
+ pred_obj = model_object.predict(future_regressor=future_reg)
152
+ return pred_obj.forecast[series_id].tolist()
153
+ elif model_name == SupportedModels.Prophet and series_id in model_object:
154
+ model = model_object[series_id]
155
+ processed = future_df.rename(columns={date_col_name: 'ds', target_column: 'y'})
156
+ forecast = model.predict(processed)
157
+ return forecast['yhat'].tolist()
158
+ elif model_name == SupportedModels.NeuralProphet and series_id in model_object:
159
+ model = model_object[series_id]
160
+ model.restore_trainer()
161
+ accepted_regressors = list(model.config_regressors.regressors.keys())
162
+ data = future_df.rename(columns={date_col_name: 'ds', target_column: 'y'})
163
+ future = data[accepted_regressors + ["ds"]].reset_index(drop=True)
164
+ future["y"] = None
165
+ forecast = model.predict(future)
166
+ return forecast['yhat1'].tolist()
167
+ elif model_name == SupportedModels.Arima and series_id in model_object:
168
+ model = model_object[series_id]
169
+ future_df = future_df.set_index(date_col_name)
170
+ x_pred = future_df.drop(target_cat_col, axis=1)
171
+ yhat, conf_int = model.predict(
172
+ n_periods=horizon,
173
+ X=x_pred,
174
+ return_conf_int=True
175
+ )
176
+ yhat_clean = pd.DataFrame(yhat, index=yhat.index, columns=["yhat"])
177
+ return yhat_clean['yhat'].tolist()
178
+ elif model_name == SupportedModels.AutoMLX and series_id in model_object:
179
+ # automlx model
180
+ model = model_object[series_id]
181
+ x_pred = future_df.drop(target_cat_col, axis=1)
182
+ x_pred = x_pred.set_index(date_col_name)
183
+ forecast = model.forecast(
184
+ X=x_pred,
185
+ periods=horizon
186
+ )
187
+ return forecast[target_column].tolist()
188
+ else:
189
+ raise Exception(f"Invalid model object type: {type(model_object).__name__}.")
190
+
191
+
192
+ def predict(data, model=load_model()) -> dict:
193
+ """
194
+ Returns prediction given the model and data to predict
195
+
196
+ Parameters
197
+ ----------
198
+ model: Model instance returned by load_model API
199
+ data: Data format as expected by the predict API of the core estimator. For eg. in case of sckit models it could be numpy array/List of list/Panda DataFrame
200
+
201
+ Returns
202
+ -------
203
+ predictions: Output from scoring server
204
+ Format: { 'prediction': output from `model.predict` method }
205
+
206
+ """
207
+ assert model is not None, "Model is not loaded"
208
+
209
+ models = model["models"]
210
+ specs = model["spec"]
211
+ horizon = specs["horizon"]
212
+ model_name = specs["model"]
213
+ date_col = specs["datetime_column"]
214
+ target_column = specs["target_column"]
215
+ target_category_column = specs["target_category_columns"][0]
216
+
217
+ try:
218
+ input_data = InputData(**data["additional_data"])
219
+ except TypeError as e:
220
+ raise ValueError(f"Validation error: {e}")
221
+ additional_data = load_data(input_data)
222
+
223
+ unique_values = additional_data[target_category_column].unique()
224
+ forecasts = {}
225
+ for key in unique_values:
226
+ try:
227
+ s_id = str(key)
228
+ filtered = additional_data[additional_data[target_category_column] == key]
229
+ future = filtered.tail(horizon)
230
+ forecast = get_forecast(future, model_name, s_id, models, date_col,
231
+ target_column, target_category_column, horizon)
232
+ forecasts[s_id] = json.dumps(forecast)
233
+ except Exception as e:
234
+ raise RuntimeError(
235
+ f"An error occurred during prediction: {e}."
236
+ f" Please ensure the input data matches the format and structure of the data used during training.")
237
+
238
+ return {'prediction': json.dumps(forecasts)}