none
Consume a web service that returns a model using mrsdeploy for Microsoft R Server 9 RRS feed

  • Question

  •  I am trying to create a prototype of a web service that gives back a model (against the normal usage of web service implementation). In this case the user can generate models based on different input data and test the suitability of the models by further combining with other services.

    This generally worked for me for a simple linear regression model (lm) published as a web service but now has a problem for a gradient boosted tree model (rxBTrees) .

    so the web service creates a new model on each request and shall send it back somehow (just tried to return the model by serializing it to JSON as mrsdeploy function publish service only supports the following I/O data types : numeric, character, vector, matrix, data.frame, logical and integer).

    createmodel <- function(inputdata){
        model <- RevoScaleR::rxBTrees(formula,data=inputdata,learningRate=0.1,nTree=50,maxDepth=5,seed=1234,lossFunction="multinomial")
        ### Serialize the model
         sermod <- jsonlite::serializeJSON(model,digits=2,pretty=FALSE)
         return(sermod)
    }
    
    ## Testing locally: works fine locally
    result <- createmodel(trainingdata) # Trains the model and outputs Json
    
    model2 <- jsonlite::unserializeJSON(result) # Gives back the model locally
    
    Now the problem is to consume this particular function after being published as a web service
    
    remoteLogin( {{SERVER_ADDRESS}})
    
    api <- publishService (
                  "modelService",
    
                   code= createmodel,
    
                   inputs=list(traindata="data.frame"),
    
                   outputs=list(mod="character"),
    
                   v="v0.0.1"
    )
    
    modelApi <- getService ( "modelService" , "v0.0.1")
    
    newresult <- modelApi$createmodel(testingdata)
    
    # ends up with an error (i guess the json is too long)
     #Error in fn(res) : NULLe57ad788-ce57-44cd-a48a-5e023a894dbcFALSE 
    The createmodel function works locally but not while being consuming as a web service; my guess is that the serialized json gets too long and the response is too big. Is there a maximum length of response of mrsdeploy web service? 
    Tuesday, March 28, 2017 3:22 PM

All replies

  • Perhaps, the output parameter "mod" is not being assigned anywhere in the web service, which is why you might be seeing the error.

    One way to do this could be - after remoteLogin(), you can define the "createmodel" function in the remote session. Then you can use pause() to switch back to local session, use "mrsdeploy::createSnapshot()" to get a snapshot of the remote session. This snapshot now contains the createmodel function. You can then use this snapshot in the publishService() function, with code = "mod <- createmodel()". Keep the "mod" in the outputs list (same as now). This should give you the result as expected.


    Monday, April 17, 2017 11:04 PM