Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
ipravir
Active Contributor

House Price Prediction.jpg

In the current era, machine learning and artificial intelligence dominate the landscape, with a majority of blogs and innovations centered around these transformative technologies. In today’s business landscape, machine learning (ML) and artificial intelligence (AI) play pivotal roles. ML, a subset of AI, enables systems to learn from data and improve performance without explicit programming. The advantages include efficiency, enhanced decision-making, improved customer experiences, fraud detection, and cost savings. Businesses leverage AI for customer service, cybersecurity, content production, inventory management, and more. Looking ahead, strategic AI adoption is crucial for staying competitive and driving innovation .

This blog delves into constructing a straightforward Linear Regression model for predicting house prices using relevant parameters.

To prepare the model, the hana_ml library has used to establish a connection via SAP HANA Cloud and access relevant tables.

Let’s review each step together.

SAP HANA Cloud Setup and Data Upload

Follow the tutorial below to set up an instance of the SAP HANA Database in the BTP Platform.

https://developers.sap.com/tutorials/hana-cloud-deploying.html

ipravir_0-1709030263887.png

Established a fresh Schema and table named “HouseData” using a CSV file containing house details.

ipravir_0-1709030361074.png

Data Upload in HDB using CSV File : https://help.sap.com/docs/SAP_HANA_PLATFORM/fc5ace7a367c434190a8047881f92ed8/d7a79a58bb5710149ed293c...

VSCode Setup and ML Model Development

Visual Studio code (VS Code) is a powerful python editor that offer auto completion, debugging, and seamless environment switching. It simplifies Python development across different platforms, making it a favorite among developers.

VSCode Download     :           https://code.visualstudio.com/download

Follow the below details and link to setup Jupyter Notebooks in VS Code.

Jupyter Notebook is a proffered choice for python development due to its interactive nature. It allows live exploration, rich documentation combining code and explanations, easy debugging, and widespread adoption in data science and research domain.

https://jupyter.org/

https://code.visualstudio.com/docs/datascience/jupyter-notebooks

Details about the libraries used for creating models and APIs:

  1. HANA_ML                              https://pypi.org/project/hana-ml/
  2. SKLearn                                  https://pypi.org/project/scikit-learn/
  3. Pickle                                     https://wiki.python.org/moin/UsingPickle
  4. FLASK                                    https://pypi.org/project/Flask/
  5. FLASK_RESTFUL                   https://pypi.org/project/Flask-RESTful/
  6. FLASK_CORS                         https://pypi.org/project/Flask-Cors/

Imports of libraries:

ipravir_1-1709030418722.png

Retrieve data from a database table using ConnectionContext, create a linear regression model, and generate a pickle file for future use. The code includes a condition to avoid creating a new model file if one already exists. You can customize the code to adjust the frequency of model file updates based on current database data.

ipravir_2-1709030663150.png

Subsequently, an API built using the Flask library, along with the creation of a model file. The utilization of a request option within the URL facilitated the retrieval of all necessary parameter values. These values are then stored in a payload, which is used to process the model and predict house prices based on various input values.

ipravir_3-1709030681188.png

To address the CORS issue when making requests to the generated API URL from any front-end application, the following approach was employed.

CORS(app, support_credentials=True)

The output following the execution of the API is as follows:

ipravir_4-1709030725335.png

Subsequently, the Business Application Studio application was activated within the BTP platform. The steps for this process are outlined in the following tutorial for the trial plan:

https://developers.sap.com/tutorials/appstudio-onboarding.html#3d3b8693-e86f-4120-8666-25b62797897b

Subsequently, a new development space was established using the “Full-Stack Application Using Productivity Tools” template. Within this space, Python Tools were selected, and additional tools were enabled based on specific requirements.

ipravir_5-1709030747578.png

Given that the API has already been tested and validated with the necessary parameters locally in Visual Studio Code, we can proceed to directly deploy the solution to Cloud Foundry and generate the API.

Provided below are the specifics of the files and the step-by-step process for deploying the solution and creating an API in Cloud Foundry.

ipravir_6-1709030759813.png

In above image below are the file created:

  1. Server.py : This file has same code which has been used in Visual Studio Code.
  2. Runtime.txt :

    The runtime.txt file allows you to explicitly specify the Python version that your application should use.

    ipravir_8-1709030834863.png

    Use below line of syntax to get the installed version of python.

    Remember that this file is particularly useful when deploying Python applications to ensure compatibility with the desired Python version.

    ipravir_9-1709030859134.png

     

  3.  Requirement.txt : 

    The requirements.txt file is essential for tracking and managing dependencies in Python projects. It ensures consistent package versions, simplifies collaboration, and facilitates smooth deployment across different environments.

    To retrieve the versions of all installed libraries, use the following syntax.

    ipravir_10-1709030894830.png

     

  4.  manifest.yml : 

    The manifest.yml file serves as an essential configuration when deploying Python applications to Cloud Foundry. It acts as an application deployment descriptor, containing crucial information such as the app name, path to the application file, and other relevant settings. By using this manifest, you ensure consistency across deployments, facilitate collaboration, and streamline the deployment process on Cloud Foundry.

    ipravir_11-1709030919953.png

    Open the terminal (using Ctrl + Shift + `) in Visual Studio Code (BAS). Navigate to project directory using the CD command. Log in to Cloud Foundry by executing the CF LOGIN command, providing user ID and password when prompted.

    ipravir_0-1709030965032.png

    Next, use the CF PUSH command to deploy the solution to Cloud Foundry as below screen :

    ipravir_1-1709030978422.png

    After a successful process, the application will be accessible in the application section.

    ipravir_2-1709030991288.png

     When the application name is chosen, details like application information, instance details, and the most recent application events become visible.

    ipravir_3-1709031001094.png

    Below is calling of above route with and without using request:

    ipravir_4-1709031012858.png

    SAPUI5 Application using Created ML API

    Utilized the same Development Space to build a basic SAPUI5 application.

 

ipravir_5-1709031050032.png

In this application, the created API is directly invoked when the user clicks the “Predict House Price” button:

ipravir_6-1709031071153.png

Initially, I considered using the API I created via a destination.

Find below post question      :          

https://community.sap.com/t5/technology-q-a/how-to-access-destination-from-bas-which-created-using-p...

Upon integrating the created API into the destination, it successfully returns a 200 status response. However, when attempting to utilize the same API within the application, a CORS error occurs.

After thorough analysis, The flask_cors library was instrumental in resolving the CORS error when directly invoking the API within the application. By incorporating this library, the issue was successfully mitigated.

Build App (Web/Mobile Application) using ML API

Created a web and mobile application using SAP Build, leveraging the same API.

ipravir_7-1709031125516.png

Utilized created API following the tutorial below.

https://developers.sap.com/tutorials/appgyver-connect-publicapi.html

The placeholder details for the GET event of API calling are provided below:

ipravir_8-1709031140458.png

Following the configuration of API details, I applied them to the ‘Predict House Price’ button event, resulting in the display of the predicted house price in an alert as shown below:

ipravir_9-1709031154676.png

Consuming API on ABAP Cloud

Developed an ABAP class using the CL_HTTP_DESTINATION_PROVIDER and CL_WEB_HTTP_CLIENT_MANAGER classes. In this class, ensure that all necessary input parameters are provided as importing parameters. The objective is to retrieve house prices as a response using the GET_TEXT method, following the logic outlined below:

ipravir_10-1709031180100.png

To verify the aforementioned logic, the following tutorial was utilized to establish an HTTP service and invoke the method from the developed class within the handle class:

https://developers.sap.com/tutorials/abap-environment-create-http-service.html

ipravir_11-1709031192076.png

When invoking the aforementioned method with the necessary request parameters, the following output will be presented:

ipravir_12-1709031201439.png

Code logic of handle class:

ipravir_13-1709031212137.png

Consuming API using SAP IOS SDK Frameworks

Developed a compact application using XCode IDE and the SAP iOS SDK Framework to interact with the custom House Price Prediction API.

ipravir_0-1709201675732.png

Utilized the SAPURLSession from SAPFramework libraries to invoke the API with all necessary parameters and displayed the predicted house price value in an alert message, as shown in the screenshot above.

https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/Latest/en-US/Documents/Frameworks/SAPFound...

Question Upon configuring the API within the BTP Cloud Application, it becomes accessible from any web browser or software application. Surprisingly, it doesn’t prompt for cloud credentials. kindly suggest any necessary steps to address this access issue?

Git Links:

Referred Links:

https://developers.sap.com/tutorials/hana-cloud-deploying.html

https://help.sap.com/docs/SAP_HANA_PLATFORM/fc5ace7a367c434190a8047881f92ed8/d7a79a58bb5710149ed293c...

https://code.visualstudio.com/download

https://jupyter.org/

https://code.visualstudio.com/docs/datascience/jupyter-notebooks

https://pypi.org/project/hana-ml/

https://pypi.org/project/scikit-learn/

https://wiki.python.org/moin/UsingPickle

https://pypi.org/project/Flask/

https://pypi.org/project/Flask-RESTful/

https://pypi.org/project/Flask-Cors/

https://en.wikipedia.org/wiki/Linear_regression

https://developers.sap.com/tutorials/appstudio-onboarding.html#3d3b8693-e86f-4120-8666-25b62797897b

https://developers.sap.com/tutorials/appgyver-connect-publicapi.html

https://developers.sap.com/tutorials/abap-environment-create-abap-cloud-project.html

https://blog.sap-press.com/how-to-integrate-a-python-app-with-sap-business-application-studio-for-an...

https://help.sap.com/doc/62a5837b7ce74a92be118efa284c0100/2023_2_QRC/en-US/python_machine_learning_c...

https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/Latest/en-US/Documents/index.html

https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/Latest/en-US/Documents/Frameworks/SAPFound...

Happy Learning 📖 💻

Praveer Kumar Sen

Labels in this area