generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 82
Add XGB & SKL Py handlers with CSV/json support #2906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
smouaa
merged 7 commits into
deepjavalibrary:master
from
smouaa:sklearn-handler-csv-support
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e35c80
Add sklearn handler with CSV support
smouaa 62f5874
sklearn handler with security improvements
smouaa 548d787
Added XGBoost handler for Python engine with support for custom forma…
smouaa 8218240
Added custom formaatter support for SKLearn handler and implemented f…
smouaa 1769295
Added integration tests for SKLearn and XGBoost handlers and bumped P…
smouaa 2fd9054
Added additional, upgraded numpy, and updated handlers to meet expect…
smouaa bc0637a
Merge branch 'master' into sklearn-handler-csv-support
smouaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
| # except in compliance with the License. A copy of the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" | ||
| # BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for | ||
| # the specific language governing permissions and limitations under the License. | ||
|
|
||
| import importlib.util | ||
| import importlib.metadata | ||
|
|
||
|
|
||
| def _is_package_available(pkg_name: str) -> bool: | ||
| """Check if a package is available""" | ||
| package_exists = importlib.util.find_spec(pkg_name) is not None | ||
| if package_exists: | ||
| try: | ||
| importlib.metadata.version(pkg_name) | ||
| except importlib.metadata.PackageNotFoundError: | ||
| package_exists = False | ||
| return package_exists | ||
|
|
||
|
|
||
| # SKLearn model persistance libraries | ||
| _joblib_available = _is_package_available("joblib") | ||
| _cloudpickle_available = _is_package_available("cloudpickle") | ||
| _skops_available = _is_package_available("skops") | ||
|
|
||
| # XGBoost | ||
| _xgboost_available = _is_package_available("xgboost") | ||
|
|
||
|
|
||
| def is_joblib_available() -> bool: | ||
| return _joblib_available | ||
|
|
||
|
|
||
| def is_cloudpickle_available() -> bool: | ||
| return _cloudpickle_available | ||
|
|
||
|
|
||
| def is_skops_available() -> bool: | ||
| return _skops_available | ||
|
|
||
|
|
||
| def is_xgboost_available() -> bool: | ||
| return _xgboost_available | ||
|
|
||
|
|
||
| joblib = None | ||
| if _joblib_available: | ||
| import joblib | ||
|
|
||
| cloudpickle = None | ||
| if _cloudpickle_available: | ||
| import cloudpickle | ||
|
|
||
| skops_io = None | ||
| if _skops_available: | ||
| import skops.io as skops_io | ||
|
|
||
| xgboost = None | ||
| if _xgboost_available: | ||
| import xgboost |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
| # except in compliance with the License. A copy of the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" | ||
| # BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for | ||
| # the specific language governing permissions and limitations under the License. | ||
|
|
||
| import pickle | ||
| import numpy as np | ||
| import os | ||
| from io import StringIO | ||
| from typing import Optional | ||
| from djl_python import Input, Output | ||
| from djl_python.encode_decode import decode | ||
| from djl_python.utils import find_model_file | ||
| from djl_python.service_loader import get_annotated_function | ||
| from djl_python.import_utils import joblib, cloudpickle, skops_io as sio | ||
|
|
||
|
|
||
| class SklearnHandler: | ||
|
|
||
| def __init__(self): | ||
| self.model = None | ||
| self.initialized = False | ||
| self.custom_input_formatter = None | ||
| self.custom_output_formatter = None | ||
| self.custom_predict_formatter = None | ||
|
|
||
| def _get_trusted_types(self, properties: dict): | ||
| trusted_types_str = properties.get("skops_trusted_types", "") | ||
| if not trusted_types_str: | ||
| raise ValueError( | ||
| "option.skops_trusted_types must be set to load skops models. " | ||
| "Example: option.skops_trusted_types='sklearn.ensemble._forest.RandomForestClassifier,numpy.ndarray'" | ||
| ) | ||
| trusted_types = [ | ||
| t.strip() for t in trusted_types_str.split(",") if t.strip() | ||
| ] | ||
| print(f"Using trusted types for skops model loading: {trusted_types}") | ||
|
||
| return trusted_types | ||
|
|
||
| def initialize(self, properties: dict): | ||
| model_dir = properties.get("model_dir") | ||
| model_format = properties.get("model_format", "skops") | ||
|
|
||
| format_extensions = { | ||
| "skops": ["skops"], | ||
| "joblib": ["joblib", "jl"], | ||
| "pickle": ["pkl", "pickle"], | ||
| "cloudpickle": ["pkl", "pickle", "cloudpkl"] | ||
| } | ||
|
|
||
| extensions = format_extensions.get(model_format) | ||
| if not extensions: | ||
| raise ValueError( | ||
| f"Unsupported model format: {model_format}. Supported formats: skops, joblib, pickle, cloudpickle" | ||
| ) | ||
|
|
||
| model_file = find_model_file(model_dir, extensions) | ||
| if not model_file: | ||
| raise FileNotFoundError( | ||
| f"No model file found with format '{model_format}' in {model_dir}" | ||
Lokiiiiii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| if model_format == "skops": | ||
| trusted_types = self._get_trusted_types(properties) | ||
| self.model = sio.load(model_file, trusted=trusted_types) | ||
| else: | ||
| if properties.get("trust_insecure_model_files", | ||
| "false").lower() != "true": | ||
| raise ValueError( | ||
| f"option.trust_insecure_model_files must be set to 'true' to use {model_format} format (only skops is secure by default)" | ||
| ) | ||
|
|
||
| if model_format == "joblib": | ||
| self.model = joblib.load(model_file) | ||
| elif model_format == "pickle": | ||
| with open(model_file, 'rb') as f: | ||
| self.model = pickle.load(f) | ||
| elif model_format == "cloudpickle": | ||
| with open(model_file, 'rb') as f: | ||
| self.model = cloudpickle.load(f) | ||
|
|
||
| self.custom_input_formatter = get_annotated_function( | ||
| model_dir, "is_input_formatter") | ||
| self.custom_output_formatter = get_annotated_function( | ||
| model_dir, "is_output_formatter") | ||
| self.custom_predict_formatter = get_annotated_function( | ||
| model_dir, "is_predict_formatter") | ||
|
|
||
| self.initialized = True | ||
|
|
||
| def inference(self, inputs: Input) -> Output: | ||
| content_type = inputs.get_property("Content-Type") | ||
| accept = inputs.get_property("Accept") or "application/json" | ||
|
|
||
| # Validate accept type (skip validation if custom output formatter is provided) | ||
| if not self.custom_output_formatter: | ||
| supported_accept_types = ["application/json", "text/csv"] | ||
| if not any(supported_type in accept | ||
| for supported_type in supported_accept_types): | ||
| raise ValueError( | ||
| f"Unsupported Accept type: {accept}. Supported types: {supported_accept_types}" | ||
| ) | ||
|
|
||
| # Input processing | ||
| X = None | ||
| if self.custom_input_formatter: | ||
| X = self.custom_input_formatter(inputs) | ||
| elif "text/csv" in content_type: | ||
| X = decode(inputs, content_type, require_csv_headers=False) | ||
| else: | ||
| input_map = decode(inputs, content_type) | ||
| data = input_map.get("inputs") if isinstance(input_map, | ||
| dict) else input_map | ||
| X = np.array(data) | ||
|
|
||
| if X is None or not hasattr(X, 'ndim'): | ||
| raise ValueError( | ||
| f"Input processing failed for content type {content_type}") | ||
|
|
||
| if X.ndim == 1: | ||
| X = X.reshape(1, -1) | ||
|
|
||
| if self.custom_predict_formatter: | ||
| predictions = self.custom_predict_formatter(self.model, X) | ||
| else: | ||
| predictions = self.model.predict(X) | ||
|
|
||
| # Output processing | ||
| if self.custom_output_formatter: | ||
| return self.custom_output_formatter(predictions) | ||
|
|
||
| # Supports CSV/JSON outputs by default | ||
| outputs = Output() | ||
| if "text/csv" in accept: | ||
smouaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| csv_buffer = StringIO() | ||
| np.savetxt(csv_buffer, predictions, fmt='%s', delimiter=',') | ||
| outputs.add(csv_buffer.getvalue().rstrip()) | ||
| outputs.add_property("Content-Type", "text/csv") | ||
| else: | ||
| outputs.add_as_json({"predictions": predictions.tolist()}) | ||
| return outputs | ||
|
|
||
|
|
||
| service = SklearnHandler() | ||
|
|
||
|
|
||
| def handle(inputs: Input) -> Optional[Output]: | ||
| if not service.initialized: | ||
| service.initialize(inputs.get_properties()) | ||
|
|
||
| if inputs.is_empty(): | ||
| return None | ||
|
|
||
| return service.inference(inputs) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.