SAP Databricks: Create inferences for application integration with SAP Build Apps

Estimated read time 11 min read

Create inferences for application integration with SAP Build Apps

In the earlier parts of the series, we explored the capabilities of SAP Databricks – from running SQL analytics and leveraging AutoML, to building end-to-end data integration with SAC. Now, we’re taking things a step further.

In this post, the focus shifts from model development to real-world application – specifically, how to extend trained and deployed ML models into interactive, user-facing applications.

We’ll walk through how to:

Leverage data products from Unity Catalog and enhance them using notebooksServe a trained ML model for real-time inferenceUse SAP Build Apps (low-code/no-code) to rapidly build a front-end interface that interacts with the model for inference

By integrating SAP Databricks with SAP Build Apps, we extend the powerful capabilities of notebooks into the creation of intelligent applications, enabling machine learning experiences to directly benefit end users. With SAP Build Apps, it’s quick and easy to prototype and bring these ML-powered experiences to life, even without deep frontend development expertise.

 

1. Data Preparation and Feature Engineering

The goal is to train a machine learning model to predict sales order lead times based on inputs such as product, order date, and quantity. In my environment, I accessed the sales order data products from the SAP Databricks Unity Catalog, which was delta-shared from S/4 HANA.

The data was cleaned and preprocessed by addressing missing or inconsistent values, particularly focusing on improving the accuracy of the delivery dates. Relevant features were also added to enhance the dataset, including order lead time, day of the week, and month.

 

Once the data is cleaned up, I saved this back into the Unity Catalog as a delta table. Saving the cleaned and feature engineered table allows you to manage and share datasets across your teams with access control. By storing the processed table here, I could easily access it for model training, and downstream tasks while maintaining version control and consistency.

 

2. Model Training

I used XGBoost to train a model to predict the lead time based on the features. You can use any ML models that suits your use case or take a look at how to run AutoML experiments in Part 3 (link).

 

3. Mosaic AI Model Serving

Mosaic AI Model Serving offers a unified platform to deploy, manage, and run AI/ML models for both real-time and batch predictions. Once deployed, each model is exposed as a REST API that can be easily integrated into web or client applications.

The model being deployed here is a Custom Model, built using Python and trained with standard machine learning libraries or custom code. It is packaged in the MLflow format and can be registered in the SAP Databricks workspace model registry as a Python pyfunc model.

Package your model into a Python class that can be called easily:

# Model wrapper class for XGBoost regression to predict LeadTime
import mlflow.pyfunc
import pandas as pd
from mlflow.models.signature import ModelSignature
from mlflow.types import Schema, ColSpec

class LeadTimeModel(mlflow.pyfunc.PythonModel):
def __init__(self, model_pipeline):
self.model_pipeline = model_pipeline

def predict(self, context, model_input):
# Convert input to pandas DataFrame regardless of input type
if isinstance(model_input, pd.DataFrame):
input_df = model_input
elif isinstance(model_input, dict):
input_df = pd.DataFrame([model_input])
else:
input_df = pd.DataFrame(model_input)
predictions = self.model_pipeline.predict(input_df)
return pd.DataFrame({‘predicted_lead_time’: predictions})

# Define input and output schema
input_feature_names = [
“SalesOrderDayOfWeek”,
“SalesOrderDay”,
“SalesOrderMonth”,
“SalesOrderItemText”,
“OrderQuantity”
]
input_schema = Schema([
ColSpec(“string”, “SalesOrderDayOfWeek”),
ColSpec(“long”, “SalesOrderDay”),
ColSpec(“long”, “SalesOrderMonth”),
ColSpec(“string”, “SalesOrderItemText”),
ColSpec(“long”, “OrderQuantity”)
])
output_schema = Schema([ColSpec(“long”, “predicted_lead_time”)])
signature = ModelSignature(inputs=input_schema, outputs=output_schema)

input_example = pd.DataFrame([{
“SalesOrderDayOfWeek”: “Monday”,
“SalesOrderDay”: 13,
“SalesOrderMonth”: 8,
“SalesOrderItemText”: “Y120 Bike”,
“OrderQuantity”: 5
}])

with mlflow.start_run():
mlflow.pyfunc.log_model(
“lead-time-pred”,
python_model=LeadTimeModel(model_pipeline),
input_example=input_example,
signature=signature,
registered_model_name=”lead-time-prediction”
)

Code run below: 

Track and manage your model lifecycle in the Unity Catalog:

 

 

Create a model serving endpoint

Since the model is registered, you could fill in the endpoint name, select the trained model, and compute type. Take note that the endpoint name cannot be changed after creation. 

 

Test the deployed model endpoint by clicking the ‘Use’ button located at the top-right corner to view the screen below: 

With model serving, users can instantly run inferences on the trained ML models. For example, given a sales order with fields like product and quantity, the model can predict lead time on the spot. This capability allows product and operations teams to call the service live within their workflows. You can easily test the functionality first through a simple query endpoint from the model serving endpoint.

For testing purposes, you can create a personal access token: link

For production use, you can authenticate SAP Databricks with OAuth.

More details on model serving: link 

 

4. Accelerate Development Using SAP Build Apps

SAP Build Apps enables quick app development with a low-code platform, allowing you to create custom applications. In this section, we’ll demonstrate how we can rapidly prototype a user interface to interact with the deployed model.

Set up a destination service in BTP Cockpit, include these additional properties:  

HTML5.DynamicDestination: trueWebIDEEnabled: true

Use the REST API integration to establish a connection with SAP Databricks. Once the destination service is created, you can access it via the SAP Build Apps.

In the Integration tab, specify the relative path and query for the model, then run tests on the API to check your connection.

 

Build the UI with the drag and drop interface.

 

A simple demo below shows the prototype built on SAP Build Apps to perform inference on the model served from SAP Databricks.

 

Conclusion

The application design may vary depending on its complexity and the steps required to make it production-ready. To manage connections and handle additional logic, a backend would be needed. However, this demonstrates how to enhance a data product, serving the ML model on SAP Databricks for inference and connecting it to SAP Build Apps via API, providing a foundation to extend and build your own intelligent applications.

 

 

​ Create inferences for application integration with SAP Build AppsIn the earlier parts of the series, we explored the capabilities of SAP Databricks – from running SQL analytics and leveraging AutoML, to building end-to-end data integration with SAC. Now, we’re taking things a step further.In this post, the focus shifts from model development to real-world application – specifically, how to extend trained and deployed ML models into interactive, user-facing applications.We’ll walk through how to:Leverage data products from Unity Catalog and enhance them using notebooksServe a trained ML model for real-time inferenceUse SAP Build Apps (low-code/no-code) to rapidly build a front-end interface that interacts with the model for inferenceBy integrating SAP Databricks with SAP Build Apps, we extend the powerful capabilities of notebooks into the creation of intelligent applications, enabling machine learning experiences to directly benefit end users. With SAP Build Apps, it’s quick and easy to prototype and bring these ML-powered experiences to life, even without deep frontend development expertise. 1. Data Preparation and Feature EngineeringThe goal is to train a machine learning model to predict sales order lead times based on inputs such as product, order date, and quantity. In my environment, I accessed the sales order data products from the SAP Databricks Unity Catalog, which was delta-shared from S/4 HANA.The data was cleaned and preprocessed by addressing missing or inconsistent values, particularly focusing on improving the accuracy of the delivery dates. Relevant features were also added to enhance the dataset, including order lead time, day of the week, and month. Once the data is cleaned up, I saved this back into the Unity Catalog as a delta table. Saving the cleaned and feature engineered table allows you to manage and share datasets across your teams with access control. By storing the processed table here, I could easily access it for model training, and downstream tasks while maintaining version control and consistency. 2. Model TrainingI used XGBoost to train a model to predict the lead time based on the features. You can use any ML models that suits your use case or take a look at how to run AutoML experiments in Part 3 (link). 3. Mosaic AI Model ServingMosaic AI Model Serving offers a unified platform to deploy, manage, and run AI/ML models for both real-time and batch predictions. Once deployed, each model is exposed as a REST API that can be easily integrated into web or client applications.The model being deployed here is a Custom Model, built using Python and trained with standard machine learning libraries or custom code. It is packaged in the MLflow format and can be registered in the SAP Databricks workspace model registry as a Python pyfunc model.Package your model into a Python class that can be called easily:# Model wrapper class for XGBoost regression to predict LeadTime
import mlflow.pyfunc
import pandas as pd
from mlflow.models.signature import ModelSignature
from mlflow.types import Schema, ColSpec

class LeadTimeModel(mlflow.pyfunc.PythonModel):
def __init__(self, model_pipeline):
self.model_pipeline = model_pipeline

def predict(self, context, model_input):
# Convert input to pandas DataFrame regardless of input type
if isinstance(model_input, pd.DataFrame):
input_df = model_input
elif isinstance(model_input, dict):
input_df = pd.DataFrame([model_input])
else:
input_df = pd.DataFrame(model_input)
predictions = self.model_pipeline.predict(input_df)
return pd.DataFrame({‘predicted_lead_time’: predictions})

# Define input and output schema
input_feature_names = [
“SalesOrderDayOfWeek”,
“SalesOrderDay”,
“SalesOrderMonth”,
“SalesOrderItemText”,
“OrderQuantity”
]
input_schema = Schema([
ColSpec(“string”, “SalesOrderDayOfWeek”),
ColSpec(“long”, “SalesOrderDay”),
ColSpec(“long”, “SalesOrderMonth”),
ColSpec(“string”, “SalesOrderItemText”),
ColSpec(“long”, “OrderQuantity”)
])
output_schema = Schema([ColSpec(“long”, “predicted_lead_time”)])
signature = ModelSignature(inputs=input_schema, outputs=output_schema)

input_example = pd.DataFrame([{
“SalesOrderDayOfWeek”: “Monday”,
“SalesOrderDay”: 13,
“SalesOrderMonth”: 8,
“SalesOrderItemText”: “Y120 Bike”,
“OrderQuantity”: 5
}])

with mlflow.start_run():
mlflow.pyfunc.log_model(
“lead-time-pred”,
python_model=LeadTimeModel(model_pipeline),
input_example=input_example,
signature=signature,
registered_model_name=”lead-time-prediction”
)Code run below:  Track and manage your model lifecycle in the Unity Catalog:  Create a model serving endpointSince the model is registered, you could fill in the endpoint name, select the trained model, and compute type. Take note that the endpoint name cannot be changed after creation.  Test the deployed model endpoint by clicking the ‘Use’ button located at the top-right corner to view the screen below: With model serving, users can instantly run inferences on the trained ML models. For example, given a sales order with fields like product and quantity, the model can predict lead time on the spot. This capability allows product and operations teams to call the service live within their workflows. You can easily test the functionality first through a simple query endpoint from the model serving endpoint.For testing purposes, you can create a personal access token: linkFor production use, you can authenticate SAP Databricks with OAuth.More details on model serving: link  4. Accelerate Development Using SAP Build AppsSAP Build Apps enables quick app development with a low-code platform, allowing you to create custom applications. In this section, we’ll demonstrate how we can rapidly prototype a user interface to interact with the deployed model.Set up a destination service in BTP Cockpit, include these additional properties:  HTML5.DynamicDestination: trueWebIDEEnabled: trueUse the REST API integration to establish a connection with SAP Databricks. Once the destination service is created, you can access it via the SAP Build Apps.In the Integration tab, specify the relative path and query for the model, then run tests on the API to check your connection. Build the UI with the drag and drop interface. A simple demo below shows the prototype built on SAP Build Apps to perform inference on the model served from SAP Databricks. ConclusionThe application design may vary depending on its complexity and the steps required to make it production-ready. To manage connections and handle additional logic, a backend would be needed. However, this demonstrates how to enhance a data product, serving the ML model on SAP Databricks for inference and connecting it to SAP Build Apps via API, providing a foundation to extend and build your own intelligent applications.    Read More Technology Blog Posts by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author