
Before we begin, make sure you have:
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")
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
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
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
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.
Your model can now be consumed via REST calls in:
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())
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.