How can we easy and fast way to build machine learning applications?
And how can we share the application to other people?
This is the prototype application for today’s goal. The example video is simple image classification web app using machine learning to detect banana ripeness.
Let’s find the solution with SAP BTP Kyma and streamlit!
Overview
We can quickly build machine learning application based on SAP BTP, Kyma runtime and integrate with streamline which is open-source Python library to create custom web apps for machine learning and data science.
Steps to solving ML problem
In general, the following steps are followed to solve a machine learning problem.
In this posting, we will focus on the deployment and monitoring phase using the conceptual architecture and requirements using SAP BTP Kyma and open source app framework – streamline.
Deploy & Monitor
When deploying and monitoring machine learning projects, we can choose prototyping tools from the options listed below. Today, we will use streamlit with SAP BTP kyma runtime.
- Jupyter Notebook
- Streamlit; Simple Edit and Test
- Flask: Backend + Frontend
- Dash: Python dashboard library
- Voila: Visualization for Jupyter Notebook
- …
Instead of using Flask, Streamlit makes building and sharing machine learning apps quick and easy without backend development and http request (Link to streamlit page)
Application
We will use these following applications to deploy our application:
- SAP BTP,Kyma runtime
- Streamlit
- Docker
- Python with VSCode
Architecture
This is technical architecture to build machine learning apps. We will use SAP BTP, Kyma runtime which is fully managed Kubernetes runtime based on the oepn-source project “Kyma”. For the model container, it is assumed that you have already completed training the machine learning model. For application containers, pre-trained machine learning generates predictions.
Prerequisites
- Install Docker Engine
- SAP BTP Kyma
- Pre-trained model
- Check network port accessibility: port 8501
- requirements.txt
tensorflow==2.9.1
streamlit
numpy==1.22.3
pillow==9.2.0
Flask==2.1.1
keras==2.9.0
scipy==1.7.3
matplot
Steps
There are four parts to follow in these steps to build a machine learning app in this tutorial.
- Create application using python
- Build docker
- SAP BTP Kyma runtime
- Upload yaml file
1. Create application using python
We will create python application to get predictions using machine learning model when user uploads an image.
1.1 Import library
import tensorflow as tf
from PIL import Image, ImageOps
import numpy as np
import streamlit as st
st.write('''
# Banana Ripeness Detection
''')
st.write("A Image Classification Web App That Detects the Ripeness Stage of Banana")
file = st.file_uploader("", type=['jpg','png','jpeg'])
1.2 Prediction code using machine learning
Depending on the pre-trained model, we need to load the model and preprocess the image data.
def predict_stage(image_data,model):
size = (224, 224)
image = ImageOps.fit(image_data,size, Image.ANTIALIAS)
image_array = np.array(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
data[0] = normalized_image_array
preds = ""
prediction = model.predict(data)
if np.argmax(prediction)==0:
preds = f"Overripe"
elif np.argmax(prediction)==1:
preds = f"ripe"
else :
preds = f"Unripe"
return preds
2. Build Docker
2.1 Create a Docker container
According to the Dockerfile, docker builds images by reading the instructions. Let’s create Dockerfile as following steps:
# STEP 1: Install Python base image
FROM python:3.9-slim
# Step 2: Add requirements.txt file
COPY requirements.txt /requirements.txt
#
# Step 3: Install required pyhton dependencies from requirements file
RUN pip install -r requirements.txt
# Step 4: Copy source code in the current directory to the container
ADD . /app
COPY app.py ./app.py
COPY ripeness.h5 ./ripeness.h5
# Step 5: Install git
RUN apt-get update && apt-get install -y
build-essential
software-properties-common
git
&& rm -rf /var/lib/apt/lists/*
# If you have git repo, you can clone your code that lives in a remote repo to WORKDIR
# RUN git clone https://github.com/streamlit/streamlit-example.git .
# Step 6: Set working directory to previously added app directory
WORKDIR /app
# # Step 7: Expose the port is running on
EXPOSE 8501
# Step 8: Run the application
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
2.2 Build a Docker image
When you running following command, Dockerfile build docker image. You can change name instead of banana-ml/streamlit:latest
docker build -t banana-ml/streamlit:latest .
2.3 Run the Docker container
We can run the container by executing:
docker run -p 8501:8501 banana-ml/streamlit:latest
2.4 Docker Push
We can push our Docker image into our Docker Hub repository by executing:
docker push banana-ml/streamlist:latest
3. SAP BTP Kyma runtime
3.1 Create namespace from SAP BTP Kyma
In this tutorial, we create namespace with banana-ml from SAP BTP kyma.
3.2 Create yaml file
We will deploy yaml file including Kind:Deployment, Service and APIRule in the same file – k8s/streamlit.yaml .
3.2.1 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: streamlit-deployment
labels:
app: streamlit
spec:
replicas: 1
selector:
matchLabels:
app: streamlit
template:
metadata:
labels:
app: streamlit
spec:
containers:
- name: streamlit
image: banana-ml/streamlit:latest
imagePullPolicy: Always
ports:
- containerPort: 8501
livenessProbe:
httpGet:
path: /healthz
port: 8501
scheme: HTTP
timeoutSeconds: 1
readinessProbe:
httpGet:
path: /healthz
port: 8501
scheme: HTTP
timeoutSeconds: 1
resources:
limits:
cpu: 1
memory: 2Gi
requests:
cpu: 100m
memory: 745Mi
3.2.2 Service
---
apiVersion: v1
kind: Service
metadata:
name: streamlit-service
spec:
type: LoadBalancer
selector:
app: streamlit
ports:
- name: streamlit-port
protocol: TCP
port: 8501
targetPort: 8501
3.2.3 API Rule
API rule allows us for exposing a service externally. We will get URL like https://bananastreamlit-api.c xxxxxxx.kyma.shoot.live.k8s-hana.ondemand.com .
---
apiVersion: gateway.kyma-project.io/v1alpha1
kind: APIRule
metadata:
name: bananastreamlit-api-rule
namespace: banana-ml
labels:
app.kubernetes.io/name: bananastreamlit
spec:
gateway: kyma-gateway.kyma-system.svc.cluster.local
service:
name: streamlit-service
port: 8501
host: bananastreamlit-api
rules:
- accessStrategies:
- config: {}
handler: allow
methods:
- GET
- POST
path: /.*
4. Upload yaml file
4.1 Export kube configuration
We can download from cluster overview using Download Kubeconfig
export KUBECONFIG=/Users/.kube/kubeconfig.yaml
Download Kubeconfig
4.2 Upload yamlfile
kubectl apply -f k8s/streamlit.yaml
4.3 Check deployment
Please wait until the deployment is successful from the Healthy Resources.
4.4 Check and Test with API Rules
You can get the URL to access machine learning application.
Congratulations 👏👏👏
Now we can build our cool machine learning app and we can share app to other people as well✨ I hope this posting is helpful for you!
Reference
- https://blogs.sap.com/2021/03/09/hands-on-tutorial-manage-deploy-machine-learning-in-kubernetes-through-the-sap-business-technology-platform-kyma-runtime/
- https://blogs.sap.com/2021/10/06/hands-on-tutorial-infuse-your-processes-with-machine-learning-through-the-btp-kyma-runtime-sap-integration-suite/
- https://docs.streamlit.io/knowledge-base/tutorials/deploy/kubernetes
- https://blogs.sap.com/2021/01/07/manage-your-first-container-using-kubernetes-in-sap-cloud-platform-kyma-runtime/
- https://developers.sap.com/mission.cp-kyma-full-stack.html
- https://developers.sap.com/mission.cp-kyma-build-app.html
- https://blogs.sap.com/2022/12/04/sap-btp-kyma-kubernetes-how-to-pull-from-private-repository/
- https://github.com/giovannipcarvalho/banana-ripeness-classification/tree/master/data