Artificial Intelligence and Machine Learning Blogs
Explore AI and ML blogs. Discover use cases, advancements, and the transformative potential of AI for businesses. Stay informed of trends and applications.
cancel
Showing results for 
Search instead for 
Did you mean: 
ChloeWombatt
Explorer
0 Kudos
231

Prerequisites

Before we begin, make sure you have:

  • An SAP BTP account with access to:
    • SAP AI Core
    • SAP AI Launchpad
  • Python 3.8+
  • TensorFlow 2.x
  • Docker (for containerizing the model)
  • Basic knowledge of machine learning model development

Step 1: Train and Save Your TensorFlow Model

Let’s use a simple classification model trained on the Iris dataset.

python
CopyEdit
import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# Load and split the data
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2)

# Build the model
model = Sequential([
    Dense(10, activation='relu', input_shape=(4,)),
    Dense(3, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10

# Save the model
model.save("iris_model")

Step 2: Create a Docker Image for Your Model

SAP AI Core requires your model to be served as a REST API. Use Flask and TensorFlow Serving or FastAPI.

Here’s a simple Flask app (app.py) to serve the model:

python
CopyEdit

from flask import Flask, request, jsonify
import tensorflow as tf
import numpy as np

model = tf.keras.models.load_model('iris_model')
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    prediction = model.predict(np.array(data['inputs']))
    predicted_class = prediction.argmax(axis=1)
    return jsonify({'predictions': predicted_class.tolist()})

Now create a Dockerfile:
Dockerfile
CopyEdit

FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install flask tensorflow

EXPOSE 5000
CMD ["python", "app.py"]
Build and test your Docker image:
bash
CopyEdit
docker build -t iris-model .
docker run -p 5000:5000 iris-model

Step 3: Register and Deploy the Model to SAP AI Core

a) Push the Image to a Container Registry

Use DockerHub or any registry supported by SAP BTP:

bash
CopyEdit

docker tag iris-model your_dockerhub_user/iris-model
docker push your_dockerhub_user/iris-model

b) Define AI Core YAML Configuration

Create deployment.yaml and scenario.yaml files to define your AI scenario and runtime environment.

deployment.yaml
yaml
CopyEdit
apiVersion: ai.sap.com/v1alpha1
kind: Deployment
metadata:
  name: iris-deployment
spec:
  image: your_dockerhub_user/iris-model
  replicas: 1
  protocol: REST
  port: 5000

scenario.yaml
yaml
CopyEdit
apiVersion: ai.sap.com/v1alpha1
kind: Scenario
metadata:
  name: iris-scenario
spec:
  deployments:
    - name: iris-deployment
      version: v1

Step 4: Deploy Using SAP AI Core CLI

Install the AI Core CLI and authenticate using your BTP credentials.

bash
CopyEdit
aic login
aic scenario apply -f scenario.yaml
aic deployment apply -f deployment.yaml

Once deployed, you’ll receive an endpoint URL to your model.

Step 5: Consume the Model in SAP Applications

Your model can now be consumed via REST calls in:

  • SAP Fiori apps (using JavaScript/Fetch)
  • SAP BTP extensions (Node.js or CAP framework)
  • SAP Integration Suite (using HTTP calls)

Example call using Python:

python
CopyEdit

import requests
response = requests.post(
    'https://your-aicore-endpoint/predict',
    json={'inputs': [[5.1, 3.5, 1.4, 0.2]]}
)

print(response.json())

Benefits of Using SAP BTP for AI Integration

  • Scalability: SAP AI Core handles deployment, autoscaling, and monitoring.
  • Security: BTP ensures enterprise-grade authentication and authorization.
  • Reusability: The same model can be consumed by multiple SAP modules.

Final Thoughts

Bringing your TensorFlow models into SAP BTP opens new doors for intelligent automation. With SAP AI Core and Launchpad, you can operationalize machine learning inside enterprise workflows without reinventing your infrastructure.

Whether you're an SAP ABAP developer learning Python, or a data scientist looking to integrate with enterprise tools, this tutorial bridges the gap.

1 Comment
Labels in this area
Top kudoed authors