Technical Articles
Run Machine Learning model on SAP Cloud Platform
SAP Cloud Platform is an open set of Software, Platform, and Infrastructure as a Service system. In this article I’ll explain how to deploy your machine learning model on the Cloud Foundry environment ( it contains the SAP Cloud Platform, Cloud Foundry runtime service that enables you to develop new business applications and business services, supporting multiple runtimes, programming languages, libraries, and services. ).
Here I assume that you have a basic understanding of Machine Learning and you have your trial account available on SAP Cloud Platform
The machine learning model which I used in this article is predicting mortality caused by Heart Failure. You can download the complete code from Github ( cloud-platform-ml-deploy-main.zip )
https://github.com/SAP-samples/cloud-platform-ml-deploy
or you can follow all the steps from this article and deploy your own machine learning model.
After downloading from Github, unzip the file. Now open the folder cloud-platform-ml-deploy-main, you will find the following files :
Cloud Foundry usually makes use of additional metadata files in order to understand the app that’s being deployed. There are three rather important metadata files and these are:
- Procfile: Procfile (no extension) tells Cloud Foundry what to do when an app is deployed. Usually, it’s just a command for a piece of code to run, say “python run.py”. Our Procfile would be that simple too – we simply need to tell Cloud Foundry to run ‘server.py’. So, go ahead; open up Procfile and you would see:
web: python app.py
Notice the ‘web:’ in front: it tells Cloud Foundry that the following command should be run in HTTP context.
- runtime.txt: runtime.txt tells Cloud Foundry about what version to run. This is not compulsory for every software; but since we’re using Python, we should explicitly set the runtime version, lest we run into Python issues. If you’re using Python 3.8, you would enter python-3.8.5 here. Take care of the minor versions too.
- requirements.txt: requirements.txt contains the dependencies of the project. For instance, in our own project, we’re using Flask, which is not usually bundled with Python and is thus an external dependency. To let Cloud Foundry know that you need Flask installed on their servers too, you’ll need to make use of requirements.txt.
And the other files in the folder are manifest.yml, app.py, pickle_model.pkl, main.py
Mainfest.yml: This file contains the name of the app, hostname, memory and disk allocation, etc.
app.py: Flask based python HTTP Post API for machine learning.
pickle_model.pkl : For machine learning model
main.py: Although not required for deployment, added if you need to make changes in the ML model.
Deployment
Once all the files are ready, you just need to deploy them on SAP Cloud Platform using Cloud Foundry Command Line Interface (CLI).
Download the latest Command Line Interface (CLI) from the Cloud Foundry web site. Use the following URL:
https://github.com/cloudfoundry/cli/wiki/V7-CLI-Installation-Guide.
Open the command-line interface, navigate to the folder and type in.
cf login -a <URL>
replace <URL> with API Endpoint from your SAP Cloud Platform trial account.
This will ask for your SAP Cloud Platform account credentials. After successful authentication, you are ready to deploy your first machine learning model.
Now you are ready to start the deployment. Using the following command to deploy.
cf push
Once done successfully you will see the details on the command prompt.
Congratulations !! You have successfully deployed your first Machine Learning model.
But one crucial step is still pending, so don’t close this command prompt, you need to test your machine learning model. One way is you can write a fancy front-end application that will consume this service, else you can use any tool/app which provides the functionality to interact with HTTP APIs.
Here we’ll use the Postman app to send JSON requests using the HTTP POST method to our deployed service. Copy the URL from the command prompt provided in the routes section. In our case following is the URL for Postman request.
https://app-timely-puku-lb.cfapps.us10.hana.ondemand.com/
Open the Postman tool, now we will send the input details for prediction under the body Tab. First, select from the drop-down “POST” method and then kindly enter the input details in JSON format and press send :
Hear failure prediction result will appear in the result window, in our case we are returning 0 and 1.
I hope you will find this information useful and will try to deploy your own machine learning model on SAP Cloud Platform. Please give your feedback and suggestion in the comment section.
Thanks for reading!
Thanks for this blog.
It's very well written and to the point !!
I created ML model as suggested in your blog and everything else is same.
I'm getting following error.
UserWarning: Trying to unpickle estimator LogisticRegression from version 0.24.0 when using version 0.22.1.
Thanks for your inputs, Jane.
Regarding this issue please change the scikit-learn version from 0.22 to 0.24 in the requirements.txt file.
Also, let me know that it worked fine after this change 🙂
Regards
Ravi Mittal
Thanks Ravi, Now it's working fine.
Thank you Ravi for your post. Here is one error I am facing in my trail account when I replicate what you have done in this blog. Do you have any idea on this?
API endpoint is https://api.cf.us10.hana.ondemand.com
exit_description: Instance never healthy after 10m0.707s: Failed to make TCP connection to port 8080: connection refused, reason: CRASHED
thank you.
Hello Abe, I never faced this issue. But it seems there is some issue with port configuration. If you can share your Flask based python HTTP Post API code, we can try to figure out the error.
Or any specific changes you did in any of your files for port/host configuration, please share.
Thanks
Ravi Mittal
Hi Ravi,
I am trying to learn to push apps onto the SAP cloud platform. While implementing the steps according to your blog, I encountered the error:
Staging app and tracing logs...
BuildpackCompileFailed - App staging failed in the buildpack compile phase
I am working on windows OS and the available stack is cflinuxfs3. Do you have any idea what should I do ?