generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from databricks-industry-solutions/feature/mod…
…el_serving_endpoint Custom model creation and hosting to Serving Endpoint + optimizations
- Loading branch information
Showing
67 changed files
with
5,878 additions
and
141 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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
Empty file.
This file contains 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,39 @@ | ||
from typing import Iterator | ||
|
||
import pandas as pd | ||
from pyspark.ml.pipeline import Transformer | ||
from pyspark.sql.functions import col, pandas_udf | ||
|
||
from dbx.pixels.modelserving.serving_endpoint_client import MONAILabelClient | ||
|
||
|
||
class MONAILabelTransformer(Transformer): | ||
""" | ||
Transformer class to generate autosegmentations of DICOM files using MONAILabel serving endpoint. | ||
""" | ||
|
||
def __init__(self, endpoint_name="pixels-monai-uc", inputCol="meta"): | ||
self.inputCol = inputCol | ||
self.endpoint_name = endpoint_name | ||
|
||
def _transform(self, df): | ||
@pandas_udf("result string, error string") | ||
def autosegm_monai_udf(iterator: Iterator[pd.Series]) -> Iterator[pd.DataFrame]: | ||
client = MONAILabelClient(self.endpoint_name) | ||
|
||
for s in iterator: | ||
results, errors = [], [] | ||
for series_uid in s: | ||
result, error = client.predict(series_uid) | ||
results.append(result) | ||
errors.append(error) | ||
|
||
yield pd.DataFrame({"result": results, "error": errors}) | ||
|
||
return ( | ||
df.selectExpr(f"{self.inputCol}:['0020000E'].Value[0] as series_uid") | ||
.filter("contains(meta:['00080008'], 'AXIAL')") | ||
.distinct() | ||
.withColumn("segmentation_result", autosegm_monai_udf(col("series_uid"))) | ||
.selectExpr("series_uid", "segmentation_result.*") | ||
) |
This file contains 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,30 @@ | ||
import json | ||
import os | ||
|
||
from mlflow.deployments import get_deploy_client | ||
|
||
|
||
class MONAILabelClient: | ||
def __init__(self, endpoint_name, max_retries=3, request_timeout_sec=300): | ||
os.environ["MLFLOW_HTTP_REQUEST_MAX_RETRIES"] = str(max_retries) | ||
os.environ["MLFLOW_HTTP_REQUEST_TIMEOUT"] = str(request_timeout_sec) | ||
os.environ["MLFLOW_DEPLOYMENT_PREDICT_TIMEOUT"] = str(request_timeout_sec) | ||
|
||
self.client = get_deploy_client("databricks") | ||
self.endpoint = endpoint_name | ||
self.max_retries = max_retries | ||
|
||
def predict(self, series_uid, iteration=0, prev_error=None): | ||
if iteration > self.max_retries: | ||
return ("", str(prev_error)) | ||
|
||
try: | ||
response = self.client.predict( | ||
endpoint=self.endpoint, | ||
inputs={"inputs": {"series_uid": series_uid}}, | ||
) | ||
return (json.loads(response.predictions)["file_path"], "") | ||
except Exception as e: | ||
if "torch.OutOfMemoryError: CUDA out of memory" in str(e): | ||
return ("", str(e)) | ||
return self.predict(series_uid, iteration + 1, prev_error=str(e)) |
Oops, something went wrong.