SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Make Predictions for House Prices with SAP AI Core

Deploy AI models and set up serving pipelines to scale prediction server.

Overview

🎓 intermediate 20 min. SAP Ai CoreIntermediateMachine Learning

You will learn

  • โœ”How to create deployment server an for AI model
  • โœ”How to set up scaling options for your deployment server
  • โœ”How to swap a deployed AI model with a different new model
Dhrubajyoti Paul D Dhrubajyoti Paul April 1, 2026
Created by July 27, 2022
Contributors

Prerequisites

Prerequisites

  • A BTP global account If you are an SAP Developer or SAP employee, please refer to the following links ( for internal SAP stakeholders only ) - How to create a BTP Account (internal) SAP AI Core If you are an external developer or a customer or a partner kindly refer to this tutorial
  • You have connected code to the AI workflows of SAP AI Core using this tutorial.
  • You have trained a model using SAP AI Core, such as the house price predictor model in this tutorial, or your own model trained in your local system. If you trained your own local model, follow this tutorial to use it with SAP AI Core.
  • You know how to locate artifacts. This is explained in this tutorial.

Steps

Intro

You will create a deployment server for AI models to use in online inferencing. It is possible to change the names of components mentioned in this tutorial, without breaking the functionality, unless stated explicitly.

The deployment server demonstrated in this tutorial can only be used in the backend of your AI project. For security reasons, in your real set up you will not be able to directly make prediction calls from your front end application to the deployment server. Doing so will lead to an inevitable Cross-origin Resource Sharing (CORS) error. As a temporary resolution, please deploy another application between your front end application and this deployment server. This middle application should use the SAP AI Core SDK (python package) to make calls to the deployment server.

Please find downloadable sample notebooks for the tutorials : . Note that these tutorials are for demonstration purposes only and should not be used in production environments. To execute them properly, you’ll need to set up your own S3 bucket or provision services from BTP, including an AI Core with a standard plan for narrow AI and an extended plan for Generative AI Hub. Ensure you input the service keys of these services into the relevant cells of the notebook. Link to notebook


Step 1 Write code for serving engine
โ€”

Create a new directory in your local system named hello-aicore-server.

Create a file named main.py, and paste the following snippet there:

PYTHON[2]
import os
import pickle
import numpy as np
from flask import Flask
from flask import request as call_request

# Creates Flask serving engine
app = Flask(__name__)

model = None

@app.before_first_request
def init():
    """
    Load model else crash, deployment will not start
    """
    global model
    model = pickle.load(open ('/mnt/models/model.pkl','rb')) # All the model files will be read from /mnt/models
    return None

@app.route("/v2/greet", methods=["GET"])
def status():
    global model
    if model is None:
        return "Flask Code: Model was not loaded."
    else:
        return "Model is loaded."

# You may customize the endpoint, but must have the prefix `/v<number>`
@app.route("/v2/predict", methods=["POST"])
def predict():
    """
    Perform an inference on the model created in initialize

    Returns:
        String value price.
    """
    global model
    #
    query = dict(call_request.json)
    input_features = [ # list of values from request call
        query['MedInc'],
        query['HouseAge'],
        query['AveRooms'],
        query['AveBedrms'],
        query['Population'],
        query['AveOccup'],
        query['Latitude'],
        query['Longitude'],
    ]
    # Prediction
    prediction = model.predict(
        np.array([list(map(float, input_features)),]) # (trailing comma) <,> to make batch with 1 observation
    )
    output = str(prediction)
    # Response
    return output

if __name__ == "__main__":
    print("Serving Initializing")
    init()
    print(f'{os.environ["greetingmessage"]}')
    print("Serving Started")
    app.run(host="0.0.0.0", debug=True, port=9001)

Understanding your code

Where should you load your model from?

  • Your code reads files from folder /mnt/models. This folder path is hard-coded in SAP AI Core, and cannot be modified.
  • Later, you will dynamically place your model file in the path /mnt/models.
  • You may place multiple files inside /mnt/models as part of your model. These files may have multiple formats, such as .py or .pickle, however you should not-create sub-directories within it.

Which serving engine to use?

  • Your code uses Flask to create a server, however you may use another python library if you would like to.
  • Your format for prediction REST calls will depend on the implementation of this deployment server.
  • You implement the endpoint /v2/predict to make predictions. You may modify the endpoint name and format, but each endpoint must have the prefix /v&lt;NUMBER&gt;. For example if you want to create endpoint to greet your server, then the endpoint implementation should be /v2/greet or /v1/greet

Create file requirements.txt as shown below.

Text
scikit-learn==0.24.2
joblib==1.0.1
Flask==2.0.1
gunicorn==20.1.0
Step 2 Bundle and publish code to cloud
+
Step 3 Set Compute Resources for Serving - Pre Read
+
Step 4 Create a serving executable
+
Step 5 Select a model to deploy using a configuration
+
Step 6 Start a deployment
+
Step 7 Make a prediction
+
Step 8 Switch the deployed model
+
Step 9 Stop a deployment
+
Step 10 Check Running Resources (optional)
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 10
1. Write code for serving engine 2. Bundle and publish code to cloud 3. Set Compute Resources for Serving - Pre Read 4. Create a serving executable 5. Select a model to deploy using a configuration 6. Start a deployment 7. Make a prediction 8. Switch the deployed model 9. Stop a deployment 10. Check Running Resources (optional)

Learn more →