Skip to Content

Discover how to implement SAP Leonardo Machine Learning Functional Service in ABAP environment

The below tutorial explains the steps to implement SAP Leonardo Machine Learning Functional Service  using SAPUI5

https://www.sap.com/india/developer/tutorials/ml-fs-sapui5-img-classification.html

 

You can explore various Machine Learning API from  SAP Leonardo Machine Learning   Foundation using link below

https://api.sap.com/shell/discover/contentpackage/SAPLeonardoMLFunctionalServices?section=ARTIFACTS

 

Since  SAP Leonardo Machine Learning Functional Service are implemented as REST API, the same can be called from ABAP environment too. This blog post will explain the same.

Before I go into details of the implementation let me explain the use case.

We will be building a simple ABAP application to ask user for an image  input and pass it to REST API using  class CL_REST_HTTP_CLIENT. The Image Classification Service API will return us the classification of images with probabilities.

In my program I tested it using different types of images and the results were quite accurate.

Input to ABAP program

Output

 

 

 

 

 

It can bee seen the Machine Learning API was able to predict the image with good accuracy.

Now coming to the actual steps to implement the same

Step1:Get the API key 

Go to below URL and get your API key. You will have to register to get the API ket

https://api.sap.com/shell/discover/contentpackage/SAPLeonardoMLFunctionalServices/api/image_classification_api

The link to get API key is on top left corner

Step2: Create a ABAP report program

Copy the code below as a new report program.

*&---------------------------------------------------------------------*
*& Report ZML_IMAGE_CLASSIF
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zml_image_classif.

PARAMETERS: p_fname TYPE rlgrap-filename.


TYPES: BEGIN OF ty_filetab,
         value TYPE x,
       END OF ty_filetab.

DATA: lv_url            TYPE string,
      lt_filetable      TYPE filetable,
      lv_rc             TYPE i,
      lv_file_name      TYPE string,
      lt_file           TYPE STANDARD TABLE OF ty_filetab,
      lv_file_content   TYPE xstring,
      lv_file_form_data TYPE string.



AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.



  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title = 'Choose a file'
    CHANGING
      file_table   = lt_filetable
      rc           = lv_rc.

  lv_file_name = lt_filetable[ 1 ]-filename.
  p_fname = lv_file_name.

AT SELECTION-SCREEN.


*Covert file to binary format
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename   = lv_file_name
      filetype   = 'BIN'
    IMPORTING
      filelength = DATA(lv_input_len)
    CHANGING
      data_tab   = lt_file.


*convert file to XSTRING
  CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    EXPORTING
      input_length = lv_input_len
    IMPORTING
      buffer       = lv_file_content
    TABLES
      binary_tab   = lt_file.

************************Initailize HTTP resquest creation************************************

  lv_url = 'https://sandbox.api.sap.com/ml/imageclassifier/inference_sync'.


  cl_http_client=>create_by_url( EXPORTING url = lv_url
                                 IMPORTING client = DATA(lo_http_client) ).

  lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.

  lo_http_client->request->set_method( if_http_request=>co_request_method_post ).


***********************************Request Headers*********************************************

* get data in JSON format
  lo_http_client->request->set_header_field( name = 'Accept'
                                   value = 'application/json' ).

* Set API key for image classification API
  lo_http_client->request->set_header_field( name = 'APIKey'
                                 value = 'Your Own API Key' ).

  DATA(lo_rest_client) = NEW cl_rest_http_client( lo_http_client ).

  DATA(lo_request) = lo_rest_client->if_rest_client~create_request_entity( ).

  lo_request->set_content_type( if_rest_media_type=>gc_multipart_form_data ).

  DATA(lo_post_file) = NEW cl_rest_multipart_form_data( lo_request ).

* send Image file to API
  lo_post_file->set_file( iv_name = 'files'  "This is the name of fied which API expects: See API documentation
                          iv_filename = lv_file_name
                          iv_type = 'image/jpeg'
                          iv_data = lv_file_content ).

  lo_post_file->if_rest_entity_provider~write_to( lo_request ).

  lo_rest_client->if_rest_resource~post( lo_request ).

*******************get Response******************************************
  DATA(lo_response) = lo_rest_client->if_rest_client~get_response_entity( ).

  DATA(lv_response) = lo_response->get_string_data( ).

  cl_demo_output=>display( lv_response ).


*

Step 3: Replace  your own API key in the copied code.

Find the below code and replace it with your own API Key

* Set API key for image classification API
  lo_http_client->request->set_header_field( name = 'APIKey'
                                 value = 'Your Own API Key' ).

 

Step 4: Run and test your code.

Congratulations you have complete all steps and now you can test your program using different types of images.

To report this post you need to login first.

7 Comments

You must be Logged on to comment or reply to a post.

    1. Ajay Rawat Post author

      Thanks

      Machine learning has huge potential for SAP customer who have huge data in their ERP system

      It was natural for SAP to come up with API’s that can be called from anywhere

      (0) 
  1. Jacques Nomssi

    Hello Ayat,

    thanks for sharing. I currently get the error message

    <H2><b>500 SSL Peer Certificate Untrusted</b></H2><hr>

    SSL handshake with sandbox.api.sap.com:443 failed: SSSLERR_PEER_CERT_UNTRUSTED (‑102)<br><br><br><pre>The peer's X.509 Certificate (chain) is untrusted

    Please let me know if you have a hint.

    best regards,

    JNN

    (0) 

Leave a Reply