SAPMLModelManager

public class SAPMLModelManager

The SAPMLModelManager is a convenience class for distributing new versions of Core ML models to the device and handling the local compilation and persistence of the Core ML models. The Core ML models are distributed from SAP Mobile Services as Client Resource files. The Core ML models are distributed remotely allowing the app to use the updated versions of the models, without requiring a re-distribution of the app itself. It is also possible to distribute the initial version of the model within the app and have subsequent versions distributed remotely. SAPMLModelManager provides APIs for performing operations like download, update, delete & list of Core ML models stored on File System.

SAPMLModelManager follows singleton design pattern. So to invoke SAPMLModelManager APIs use SAPMLModelManager.shared instance.

Example use

//Configure `SAPMLModelManager` by setting the `sapUrlSession` and `cpmsSettingsParameters` properties which will be used for downloading the Core ML Models from SAP Mobile Services
   SAPMLModelManager.shared.sapUrlSession = //set the fully configured SAPURLSession
   SAPMLModelManager.shared.cpmsSettingsParameters = //set the cpmsSettingsParameters
   SAPMLModelManager.shared.delegate = MyMLModelManagerDelegate() //set the delegate that will receive a callback on download completion
//Specify the name of the Core ML model that needs to be downloaded from Mobile Services using SAPMLModel
   let helloCPMS = SAPMLModel(named:"HelloCPMSModel" , localURL: nil)
//Trigger download of the model
   SAPMLModelManager.shared.download(model: helloCPMS)
//Handle completion callback received on SAPMLModelManagerDelegate
func sapMLModelManager(_ manager: SAPMLModelManager, model: SAPMLModel?, didCompleteWithError error: SAPMLModelManagerError?) {
    if let err = error {
        switch err {
        case .compilationError(let compilationError):
            // Handle model compilation error
        case .downloadUnderway:
            // Handle model compilation error
        case .other(let otherError):
            // Handle otehr errors
        case .server(let serverError):
            // Handle server error
        case .cpmsSettingsMissing:
            // Handle CPMS Settings missing error
        case .modelNotFound:
            // Handle model not found error

        }
    }
    else {
        // Model downloaded/updated successfully
    }
}
//Access the downloaded and compiled ML model
    do {
       try SAPMLModelManager.shared.mlModel(for: helloCPMS)
    }
    catch _ {
       //Error Handling
    }

Modifications required in AppDelegate

//The SAPMLModelManager downloads the latest version of the Core ML model file that is available as a Client Resource on Mobile Services. It is recommended that the download call is hooked into the AppDelegate `applicationWillEnterForeground` lifecycle method so that the app gets to use the latest version.
    func applicationWillEnterForeground(_: UIApplication) {
        let helloCPMS = SAPMLModel(named:"HelloCPMSModel" , localURL: nil)
        SAPMLModelManager.shared.download(model: helloCPMS)
    }
//As the `SAPMLModelManager` performs download of Core ML models in the background, the handleEventsForBackgroundURLSession event must be forwarded to the SDK to handle
completion of tasks when the application is launched or resumed in the background
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
    AppDelegateDispatcher.application(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
}

Example - Starting off with a local Core ML model within the app, updated subsequently with remotely distributed models.

//The Core ML model located at `localURL` will be available using the `mlModel(..)` function.
   let helloCPMS = SAPMLModel(named:"HelloCPMSModel" , localURL: Bundle.main.url(forResource: "myModel", withExtension: "mlmodelc"))
   SAPMLModelManager.shared.download(model: helloCPMS)
   try? SAPMLModelManager.shared.mlModel(for: helloCPMS) //returns local model

 //If a new version of the Core ML model is uploaded on Mobile Services Client Resources, it will be downloaded and compiled.
   let helloCPMS = SAPMLModel(named:"HelloCPMSModel" , localURL: Bundle.main.url(forResource: "myModel", withExtension: "mlmodelc"))
   SAPMLModelManager.shared.download(model: helloCPMS)
   try? SAPMLModelManager.shared.mlModel(for: helloCPMS) //returns remote model
  • SAPMLModelManager is a singleton, making it easier to keep track of downloaed models across the app. Use SAPMLModelManager.shared instance to invoke SAPMLModelManager APIs.

    Declaration

    Swift

    public static let shared: SAPMLModelManager
  • Delegate to get events from SAPMLModelManager after model download has completed

    Declaration

    Swift

    public var delegate: SAPMLModelManagerDelegate?
  • cpmsSettingsParameters indicates the Cloud Platform Mobile Services application that hosts the remote Core ML models

    Declaration

    Swift

    public var cpmsSettingsParameters: SAPcpmsSettingsParameters?
  • This sapUrlSession will be used to download the ML models

    Declaration

    Swift

    public var sapUrlSession: SAPURLSession? { get set }
  • Return Void Instructs SAPMLModelManager to download the given SAPMLModel. If for the given SAPMLModel localURL argument is given then the model can be immediately used. Even if the model is not uploaded in Mobile Services Client Resources, the model can be used through given localURL. Calling download again on the same SAPMLModel checks for an updated version of the model file in CPMS’s Client Resources. The model gets updated only if CPMS Client Resources has a updated model version file. The model is downloaded in the background so download happens even if the app is not currently active.

    • model: Represents Model which has to be downloaded with SAPModelManager

    Declaration

    Swift

    public func download(model: SAPMLModel)
  • Return Void Deletes the model from SAPMLModelManager.

    • model: Represents Model which has to be deleted from SAPMLModelManager Throws an error if the model is not downloaded previously with SAPMLModelManager

    Declaration

    Swift

    public func delete(model: SAPMLModel) throws
  • Get a list of all the succesfully downloaded models with SAPMLModelManager

    Declaration

    Swift

    public func listDownloadedModels() -> [SAPMLModel]

    Return Value

    an array of SAPMLModel objects.

  • Gets the compiled Core ML model object of type MLModel for the specified SAPMLModel

    • for: Represents the downloaded SAPMLModel for which the compiled MLModel object is required

    Throws

    an error object of type SAPMLModelManagerError describing the problem

    Declaration

    Swift

    public func mlModel(for model: SAPMLModel) throws -> MLModel

    Return Value

    the MLModel object