
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.
Steps to solve machine learning problem
We will use these following applications to deploy our application:
Technical Architecture
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
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'])
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
# 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"]
docker build -t banana-ml/streamlit:latest .
docker run -p 8501:8501 banana-ml/streamlit:latest
docker push banana-ml/streamlist:latest
Create NamespaceSave Namespace
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
---
apiVersion: v1
kind: Service
metadata:
name: streamlit-service
spec:
type: LoadBalancer
selector:
app: streamlit
ports:
- name: streamlit-port
protocol: TCP
port: 8501
targetPort: 8501
---
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: /.*
export KUBECONFIG=/Users/.kube/kubeconfig.yaml
Download Kubeconfig
kubectl apply -f k8s/streamlit.yaml
Healthy Resources
API Rules
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
12 | |
9 | |
7 | |
7 | |
7 | |
6 | |
6 | |
6 | |
6 |