예제 #1
0
        public void TestUpdateModelpayload()
        {
            List <Property>      _prop  = new List <Property>();
            List <ModelResponse> _model = new List <ModelResponse>();

            TestCreateModelpayload();
            _model = ModelPayload.Get();
            _prop.Add(new Property {
                key = "width", value = "200px"
            });
            ModelResponse updateRecord = new ModelResponse()
            {
                Id         = "HelloModel",
                Name       = "Hello.pmml",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".png",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "IMAGE",
                Url        = "http://localhost/uploads/data/Hello.png",
                FilePath   = "",
                Loaded     = false,
                Deployed   = false,
                Properties = _prop
            };

            Console.WriteLine(_model[0].Properties);
            Console.WriteLine(updateRecord.Properties);
            ModelResponse updated = ModelPayload.Update(updateRecord);

            Assert.NotEqual(_model[0].Properties, updateRecord.Properties);
        }
예제 #2
0
        public void TestCreateModelpayload()
        {
            List <Property>      _prop  = new List <Property>();
            List <ModelResponse> _model = new List <ModelResponse>();

            _model = ModelPayload.Get();
            ModelResponse newRecord = new ModelResponse()
            {
                Id         = "HelloModel",
                Name       = "Hello.pmml",
                User       = "",
                Created_on = DateTime.Now.ToString(),
                Edited_on  = DateTime.Now.ToString(),
                Extension  = ".png",
                MimeType   = "application/image",
                Size       = 111,
                Type       = "PMML",
                Url        = "http://localhost/uploads/data/Hello.png",
                FilePath   = "",
                Loaded     = false,
                Deployed   = false,
                Properties = _prop
            };

            ModelResponse createdRecord = ModelPayload.Create(newRecord);

            Assert.Equal(newRecord, createdRecord);
        }
예제 #3
0
        public void TestReadModelPayload()
        {
            List <ModelResponse> _model = new List <ModelResponse>();

            TestCreateModelpayload();
            _model = ModelPayload.Get();
            Assert.NotNull(_model);
        }
예제 #4
0
        public IActionResult DeleteInstancesAsync(string id)
        {
            bool   result = false;
            string type   = "";

            //check file type to delete - jupyter
            var codeResponse = CodePayload.Get();

            foreach (var item in codeResponse)
            {
                if (item.Id == id)
                {
                    type = item.Type;
                }
            }

            //check got pmml file
            if (string.IsNullOrEmpty(type))
            {
                var modalResponse = ModelPayload.Get();
                foreach (var item in modalResponse)
                {
                    if (item.Id == id)
                    {
                        type = item.Type;
                    }
                }
            }
            //check for zmk
            if (string.IsNullOrEmpty(type))
            {
                type = "ZMK";
            }

            switch (type)
            {
            case "JUPYTER_NOTEBOOK":
                result = StopJupyter(id);
                InstancePayload.Delete(id);
                break;

            case "PMML":
                result = StopTensorboard(id);
                InstancePayload.Delete(id);
                break;

            case "ZMK":
                result = ZMKDockerCmdHelper.StopZMKInstance(id);
                break;
            }
            var zmkResponse1 = InstancePayload.Get();

            return(Ok(new { user = string.Empty, id = id, type = type, message = "Instance deleted successfully.", Json = JsonConvert.SerializeObject(zmkResponse1) }));
        }
예제 #5
0
        public void TestDeleteModelPayload()
        {
            List <ModelResponse> _model = new List <ModelResponse>();

            TestCreateModelpayload();
            _model = ModelPayload.Get();
            Assert.NotNull(_model);
            //
            bool isDeleted = ModelPayload.Delete("HelloModel");

            Assert.True(isDeleted);
            //
            _model = ModelPayload.Get();
            Assert.True(_model.Count == 0);
        }
예제 #6
0
 public TaskController(IWebHostEnvironment environment, IConfiguration configuration, ILogger <TaskController> log, IPyNNServiceClient srv, IPyAutoMLServiceClient automlSrv)
 {
     _environment       = environment ?? throw new ArgumentNullException(nameof(environment));
     this.Configuration = configuration;
     this.nnclient      = srv;
     this.Logger        = log;
     this.autoMLclient  = automlSrv;
     try
     {
         taskResponse  = TaskPayload.Get();
         modelResponse = ModelPayload.Get();
     }
     catch (Exception ex)
     {
         //ILogger
         string error = ex.Message;
     }
 }
예제 #7
0
        public async Task <IActionResult> GetPmmlAsync(string id)
        {
            string pmmlProps = string.Empty;

            responseData = ModelPayload.Get();
            string jsonStr  = JsonConvert.SerializeObject(responseData, Formatting.Indented);
            string filePath = string.Empty;
            //jsonStr = jsonStr.ToPrettyJsonString();
            var           jsonObj = JsonConvert.DeserializeObject <List <ModelResponse> >(jsonStr);
            ModelResponse _data   = new ModelResponse();

            foreach (var record in responseData)
            {
                if (record.Id.ToString() == id)
                {
                    _data = record;
                }
            }
            try
            {
                pmmlProps = await nnclient.GetPmmlProperties(_data.FilePath);

                //
                string  strjObj1 = JsonConvert.SerializeObject(_data);
                JObject jObj1    = JObject.Parse(strjObj1);
                JObject jObj2    = JObject.Parse(pmmlProps);

                if (!string.IsNullOrEmpty(pmmlProps))
                {
                    jObj1.Merge(jObj2, new JsonMergeSettings
                    {
                        // union array values together to avoid duplicates
                        MergeArrayHandling = MergeArrayHandling.Union
                    });
                }
                //
                return(Json(jObj1));
            }
            catch (Exception ex)
            {
                string _ex = ex.Message;
                return(BadRequest());
            }
        }
예제 #8
0
 public ModelController(IHostingEnvironment environment, IConfiguration configuration, ILogger <ModelController> log, IPyNNServiceClient srv, IPyZMEServiceClient _zmeClient, IZSModelPredictionClient _zsClient, IPyTensorServiceClient tbClientInstance)
 {
     _environment       = environment ?? throw new ArgumentNullException(nameof(environment));
     this.Configuration = configuration;
     this.nnclient      = srv;
     this.Logger        = log;
     this.zmeClient     = _zmeClient;
     this.zsClient      = _zsClient;
     this.tbClient      = tbClientInstance;
     try
     {
         responseData     = ModelPayload.Get();
         dataResponseData = DataPayload.Get();
     }
     catch (Exception ex)
     {
         //ILogger
         string error = ex.Message;
     }
 }
예제 #9
0
    public async Task Execute(IJobExecutionContext context)
    {
        #region Variables
        JobDataMap dataMap      = context.JobDetail.JobDataMap;
        string     filePath     = dataMap.GetString("filePath");
        string     zmodId       = dataMap.GetString("zmodId");
        string     id           = dataMap.GetString("id");
        string     onnxResponse = "";
        #endregion

        Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {id} Model deploy started");

        #region deploy model

        onnxResponse = await OnnxClient.DeployModelAsync(zmodId, filePath);

        #endregion

        ModelResponse record = ModelPayload.Get().Where(i => i.Id == id).FirstOrDefault();
        //
        if (string.IsNullOrEmpty(onnxResponse) || onnxResponse.Contains("Fail@@"))
        {
            record.Deployed     = false;
            record.ReasonFailed = onnxResponse.Replace("Fail@@", "");
            ModelPayload.Update(record);
        }
        else
        {
            MleResponse mle = JsonSerializer.Deserialize <MleResponse>(onnxResponse);
            //add response to ModelResponse
            record.MleResponse = mle;
            record.Deployed    = true;
            ModelPayload.Update(record);
        }

        await Task.FromResult(0);

        Console.WriteLine($">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> {id} Model deploy completed");
    }
예제 #10
0
        public IActionResult Get(bool loaded, bool refresh)
        {
            //
            if (refresh)
            {
                ModelPayload.Clear();
                InitZmodDirectory.ScanModelsDirectory();
                responseData = ModelPayload.Get();
            }
            //
            DefaultContractResolver contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy()
            };
            string jsonStr = JsonConvert.SerializeObject(responseData, new JsonSerializerSettings
            {
                ContractResolver = contractResolver,
                Formatting       = Formatting.Indented
            });
            var jsonObj = JsonConvert.DeserializeObject <List <ModelResponse> >(jsonStr);

            //loaded
            if (loaded)
            {
                List <ModelResponse> loadedModel = new List <ModelResponse>();
                foreach (var record in jsonObj)
                {
                    if (record.Loaded)
                    {
                        loadedModel.Add(record);
                    }
                }
                return(Json(loadedModel));
            }

            return(Json(jsonObj));
        }
예제 #11
0
        public async Task <IActionResult> CreateScoringJobAsync(string id, [FromBody] TrainingRequestParam jsonbody)
        {
            string  filePath = "";
            var     json     = JsonConvert.SerializeObject(jsonbody);
            JObject jo       = JObject.Parse(json);

            /* TODO: validation for req body */
            JObject cronjson = JObject.Parse(jo.ToString());

            filePath = cronjson["filePath"].ToString();

            /*request json for zmk */
            JObject req = JObject.Parse(jo.ToString());

            req.Remove("recurrence");
            req.Remove("cronExpression");
            req.Remove("startDate");
            req.Remove("startTimeH");
            req.Remove("startTimeM");

            /* validate if model exists in the ZMOD directory and loaded. */
            if (ModelPayload.Get().Where(m => m.Id == id).Count() == 1)
            {
                List <TrainingResponse> tresp = new List <TrainingResponse>();

                #region schedule scoring

                /* check if same job is scheduled */
                ISchedulerFactory schfack   = new StdSchedulerFactory();
                IScheduler        scheduler = await schfack.GetScheduler();

                var jobKey = new JobKey(filePath);
                if (await scheduler.CheckExists(jobKey))
                {
                    await scheduler.ResumeJob(jobKey);
                }
                else
                {
                    try
                    {
                        #region create quartz job for training model
                        ITrigger trigger = TriggerBuilder.Create()
                                           .WithIdentity($"Training Model Job-{DateTime.Now}")
                                           .WithCronSchedule(cronjson["cronExpression"].ToString())
                                           .WithPriority(1)
                                           .Build();

                        IJobDetail job = JobBuilder.Create <TrainModelJob>()
                                         .WithIdentity(filePath)
                                         .Build();

                        job.JobDataMap["id"]       = id;
                        job.JobDataMap["filePath"] = filePath;
                        job.JobDataMap["reqBody"]  = req.ToString();
                        job.JobDataMap["baseurl"]  = Configuration["PyServiceLocation:srvurl"];

                        await _scheduler.ScheduleJob(job, trigger);

                        //add to scheduler payload
                        SchedulerResponse schJob = new SchedulerResponse()
                        {
                            CreatedOn      = DateTime.Now.ToString(),
                            CronExpression = cronjson["cronExpression"].ToString(),
                            DateCreated    = DateTime.Now,
                            EditedOn       = DateTime.Now.ToString(),
                            FilePath       = filePath,
                            Id             = id,
                            Name           = id,
                            Type           = "PMML",
                            Url            = "",
                            Recurrence     = cronjson["recurrence"].ToString(),
                            StartDate      = cronjson["startDate"].ToString(),
                            StartTimeH     = (cronjson["startTimeH"].ToString() == null) ? "" : cronjson["startTimeH"].ToString(),
                            StartTimeM     = (cronjson["startTimeM"].ToString() == null) ? "" : cronjson["startTimeM"].ToString(),
                            // ZMKResponse = tresp.ToList<object>()
                        };
                        SchedulerPayload.Create(schJob);
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                #endregion

                return(Ok(new { modelId = id, modelState = "model valid", message = "training job scheduled successfully!" }));
            }
            else
            {
                return(BadRequest(new { modelId = id, modelState = "model not found", message = "model does not exists!" }));
            }
        }
예제 #12
0
        public async Task <IActionResult> PostZSUploadPmmlAsync(string id)
        {
            string  response, modelName, convertedPath = string.Empty;
            JObject jsonResponse = new JObject();
            bool    isExists     = false;

            if (responseData.Count > 0)
            {
                foreach (var record in responseData)
                {
                    if (record.Id.ToString() == id)
                    {
                        try
                        {
                            /* TODO: Add code to call ZMK service to get compatible pmml*/
                            string zmkResponse = await zmeClient.PostConvertPmmlAsync(record.FilePath, record.FilePath.Replace(id, $"Converted_{id}"));

                            if (!string.IsNullOrEmpty(zmkResponse) && !zmkResponse.Contains("Failed"))
                            {
                                JObject jo = JObject.Parse(zmkResponse);
                                convertedPath = jo["filePath"].ToString();
                            }
                            if (string.IsNullOrEmpty(convertedPath))
                            {
                                return(BadRequest(new { message = "Model loading failed.", errorCode = 400, exception = ZMMConstants.ErrorFailed }));
                            }
                            string zsResponse = await zsClient.UploadPmml(convertedPath);

                            //remove file after upload
                            if (System.IO.File.Exists(convertedPath))
                            {
                                System.IO.File.Delete(convertedPath);
                            }
                            if (zsResponse != "Fail" && zsResponse != "FileExists")
                            {
                                //
                                modelName = JObject.Parse(zsResponse)["modelName"].ToString();
                                ModelResponse updateRecord = new ModelResponse()
                                {
                                    Created_on = record.Created_on,
                                    Deployed   = true,
                                    Edited_on  = record.Edited_on,
                                    Extension  = record.Extension,
                                    FilePath   = record.FilePath,
                                    Id         = record.Id,
                                    Loaded     = record.Loaded,
                                    MimeType   = record.MimeType,
                                    ModelName  = modelName,
                                    Name       = record.Name,
                                    Size       = record.Size,
                                    Type       = record.Type,
                                    Url        = record.Url
                                };
                                //
                                ModelPayload.Update(updateRecord);
                                responseData = ModelPayload.Get();
                                response     = @"{ id: '" + record.Id + "', deployed: true}";
                                if (!string.IsNullOrEmpty(response))
                                {
                                    jsonResponse = JObject.Parse(response);
                                }
                                isExists = true;
                            }
                            else
                            {
                                return(BadRequest(new { message = "Model loading failed.", errorCode = 500, exception = "No response from server." }));
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogCritical(ex, ex.StackTrace);
                            return(BadRequest(new { message = "Model loading failed.", errorCode = 400, exception = ex.StackTrace }));
                        }
                    }
                }
            }
            if (!isExists)
            {
                return(NotFound(new { message = "Model loading failed.", errorCode = 404, exception = "No such model." }));
            }
            return(Json(jsonResponse));
        }
예제 #13
0
        public async Task <IActionResult> DeletePmmlFromZementisServer(string id)
        {
            string  response     = string.Empty;
            JObject jsonResponse = new JObject();
            bool    isExists     = false;

            if (responseData.Count > 0)
            {
                foreach (var record in responseData)
                {
                    if (record.Id.ToString() == id)
                    {
                        try
                        {
                            string zsResponse = await zsClient.DeletePmml(record.ModelName);

                            if (zsResponse != "fail")
                            {
                                //
                                ModelResponse updateRecord = new ModelResponse()
                                {
                                    Created_on = record.Created_on,
                                    Deployed   = false,
                                    Edited_on  = record.Edited_on,
                                    Extension  = record.Extension,
                                    FilePath   = record.FilePath,
                                    Id         = record.Id,
                                    Loaded     = record.Loaded,
                                    MimeType   = record.MimeType,
                                    Name       = record.Name,
                                    Size       = record.Size,
                                    Type       = record.Type,
                                    Url        = record.Url
                                };
                                //
                                ModelPayload.Update(updateRecord);
                                responseData = ModelPayload.Get();
                                response     = "{ id: '" + record.Id + "', deployed: false}";
                                if (!string.IsNullOrEmpty(response))
                                {
                                    jsonResponse = JObject.Parse(response);
                                }
                                isExists = true;
                            }
                            else
                            {
                                return(BadRequest(new { message = "Model loading failed.", errorCode = 500, exception = "No response from server." }));
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogCritical(ex, ex.StackTrace);
                            return(BadRequest(new { message = "Model loading failed.", errorCode = 400, exception = ex.Message }));
                        }
                    }
                }
            }
            if (!isExists)
            {
                return(NotFound(new { message = "Model loading failed.", errorCode = 404, exception = "No such model." }));
            }
            return(Json(jsonResponse));
        }
예제 #14
0
        public async Task <IActionResult> Post(List <IFormFile> file)
        {
            #region variables
            List <ModelResponse> _response        = new List <ModelResponse>();
            List <ModelResponse> existingCodeData = new List <ModelResponse>();
            long   size         = file.Sum(f => f.Length);
            string type         = string.Empty;
            bool   IsFileExists = false;
            // full path to file in temp location
            var    filePath    = Path.GetTempFileName();
            string dirFullpath = DirectoryHelper.GetModelDirectoryPath();
            #endregion

            //check if folder path exists...if not then create folder
            if (!Directory.Exists(dirFullpath))
            {
                Directory.CreateDirectory(dirFullpath);
            }

            foreach (var formFile in file)
            {
                if (formFile.Length > 0)
                {
                    //check if the file with the same name exists
                    existingCodeData = ModelPayload.Get();
                    if (existingCodeData.Count > 0)
                    {
                        //
                        foreach (var record in existingCodeData)
                        {
                            if ((record.Name == formFile.FileName) && (record.User == CURRENT_USER))
                            {
                                IsFileExists = true;
                            }
                        }
                    }
                    existingCodeData.Clear();
                    //
                    if (!IsFileExists)
                    {
                        string fileExt = System.IO.Path.GetExtension(formFile.FileName).Substring(1).ToString().ToLower();
                        // upload file start
                        using (var fileStream = new FileStream(Path.Combine(dirFullpath, formFile.FileName), FileMode.Create))
                        {
                            //check file allowed extensions

                            if (!extensions.Contains(fileExt))
                            {
                                return(BadRequest("File type not allowed"));
                            }
                            else
                            {
                                await formFile.CopyToAsync(fileStream);
                            }
                        }

                        if (fileExt.Contains("pmml"))
                        {
                            type = "PMML";
                        }
                        List <Property> _props    = new List <Property>();
                        string          _url      = DirectoryHelper.GetModelUrl(formFile.FileName);
                        string          _filePath = Path.Combine(dirFullpath, formFile.FileName);
                        //
                        ModelResponse newRecord = new ModelResponse()
                        {
                            Created_on = DateTime.Now.ToString(),
                            Deployed   = false,
                            Edited_on  = DateTime.Now.ToString(),
                            Extension  = fileExt,
                            FilePath   = _filePath,
                            Id         = formFile.FileName.Replace($".{fileExt}", ""),
                            Loaded     = false,
                            MimeType   = formFile.ContentType,
                            Name       = formFile.FileName,
                            Size       = formFile.Length,
                            Type       = type,
                            Url        = _url,
                            User       = CURRENT_USER,
                            Properties = _props
                        };
                        //
                        _response.Add(ModelPayload.Create(newRecord));
                    }
                }
            }

            return(Ok(_response));
        }
예제 #15
0
        public async Task <IActionResult> LoadModelAsync(string id)
        {
            string  response     = string.Empty;
            JObject jsonResponse = new JObject();
            bool    isExists     = false;

            if (responseData.Count > 0)
            {
                foreach (var record in responseData)
                {
                    if (record.Id.ToString() == id)
                    {
                        try
                        {
                            string pyResponse = await nnclient.PostLoadModel(record.FilePath);

                            if (!pyResponse.Contains(ZMMConstants.ErrorFailed))
                            {
                                ModelResponse updateRecord = new ModelResponse()
                                {
                                    Created_on = record.Created_on,
                                    Deployed   = record.Deployed,
                                    Edited_on  = record.Edited_on,
                                    Extension  = record.Extension,
                                    FilePath   = record.FilePath,
                                    Id         = record.Id,
                                    Loaded     = true,
                                    MimeType   = record.MimeType,
                                    Name       = record.Name,
                                    Size       = record.Size,
                                    Type       = record.Type,
                                    Url        = record.Url
                                };

                                ModelPayload.Update(updateRecord);
                                responseData = ModelPayload.Get();
                                response     = "{ id: '" + record.Id + "', loaded: true}";
                                if (!string.IsNullOrEmpty(response))
                                {
                                    jsonResponse = JObject.Parse(response);
                                }
                                isExists = true;
                            }
                            else
                            {
                                return(BadRequest(new { message = "Model loading failed.", errorCode = 500, exception = "No response from server." }));
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogCritical(ex, "Model loading failed.");
                            return(BadRequest(new { message = "Model loading failed.", errorCode = 400, exception = ex.Message }));
                        }
                    }
                }
            }
            if (!isExists)
            {
                return(NotFound(new { message = "Model loading failed.", errorCode = 404, exception = "No such model." }));
            }
            return(Json(jsonResponse));
        }