From 27f41c923c8142a2e71d390fc73e8c6baa52a734 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 13 Apr 2022 22:10:01 +0200 Subject: Izmenjena metoda sa slanje modela na treniranje. Omoguceno slanje poruka kada se model istrenira i kada se epoch-a zavrsi.(BackEnd) --- backend/api/api/Controllers/ModelController.cs | 38 ++++++++++++++++++++---- backend/api/api/Models/Model.cs | 2 +- backend/api/api/Services/ChatHub.cs | 2 ++ backend/api/api/Services/IMlConnectionService.cs | 2 +- backend/api/api/Services/MlConnectionService.cs | 10 +++---- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 1f98ed26..5a4f8b65 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -3,6 +3,7 @@ using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using Microsoft.Net.Http.Headers; using System.Net.Http.Headers; @@ -20,10 +21,13 @@ namespace api.Controllers private readonly IExperimentService _experimentService; private IJwtToken jwtToken; private readonly IMlConnectionService _mlConnectionService; + private readonly IUserService _userService; + private readonly IHubContext _ichat; - public ModelController(IMlConnectionService mlService, IModelService modelService, IMlConnectionService mlConnectionService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration,IJwtToken token,IExperimentService experiment) + public ModelController(IMlConnectionService mlService, IModelService modelService, IMlConnectionService mlConnectionService, IDatasetService datasetService, IFileService fileService, IConfiguration configuration,IJwtToken token,IExperimentService experiment,IUserService user,IHubContext ichat) { + _mlService = mlService; _modelService = modelService; _datasetService = datasetService; @@ -31,6 +35,8 @@ namespace api.Controllers _experimentService = experiment; jwtToken = token; _mlConnectionService = mlConnectionService; + _userService= user; + _ichat= ichat; } public string getUserId() @@ -70,7 +76,7 @@ namespace api.Controllers [HttpPost("trainModel")] [Authorize(Roles = "User,Guest")] - public async Task> Test([FromBody] TrainModelObject trainModelObject) + public async Task> TrainModel([FromBody] TrainModelObject trainModelObject) { string experimentId = trainModelObject.ExperimentId; string modelId = trainModelObject.ModelId; @@ -84,10 +90,28 @@ namespace api.Controllers var dataset = _datasetService.GetOneDataset(experiment.datasetId); var filepath = _fileService.GetFilePath(dataset.fileId, uploaderId); var model = _modelService.GetOneModel(modelId); - _mlService.TrainModel(model,experiment,filepath);//To do Obavestiti korisnika kada se model istrenira + _mlService.TrainModel(model,experiment,filepath,dataset,uploaderId);//To do Obavestiti korisnika kada se model istrenira return Ok(); } + [HttpPost("epoch")] + public async Task> Epoch([FromBody] Epoch info) + { + + var model=_modelService.GetOneModel(info.ModelId); + var user = _userService.GetUserByUsername(model.username); + + if (ChatHub.CheckUser(user._id)) + await _ichat.Clients.Client(ChatHub.Users[user._id]).SendAsync("NotifyEpoch",info.ModelId,info.Stat); + + return Ok(); + } + + + + + + // GET: api//mymodels [HttpGet("mymodels")] [Authorize(Roles = "User")] @@ -166,8 +190,7 @@ namespace api.Controllers if (existingModel != null && !overwrite) return NotFound($"Model with name = {model.name} exisits"); else - { - model.isTrained = false; + { //_modelService.Create(model); //return Ok(); if (existingModel == null) @@ -230,4 +253,9 @@ namespace api.Controllers public string ExperimentId { get; set; } } + public class Epoch + { + public string ModelId { get; set; } + public string Stat { get; set; } + } } diff --git a/backend/api/api/Models/Model.cs b/backend/api/api/Models/Model.cs index 72f989a6..72ee093b 100644 --- a/backend/api/api/Models/Model.cs +++ b/backend/api/api/Models/Model.cs @@ -36,7 +36,7 @@ namespace api.Models public string[] metrics { get; set; } public int epochs { get; set; } - public bool isTrained { get; set; } + //public bool isTrained { get; set; } //public NullValues[] nullValues { get; set; } } } diff --git a/backend/api/api/Services/ChatHub.cs b/backend/api/api/Services/ChatHub.cs index efa13a3b..72febce1 100644 --- a/backend/api/api/Services/ChatHub.cs +++ b/backend/api/api/Services/ChatHub.cs @@ -20,6 +20,8 @@ namespace api.Services public override async Task OnConnectedAsync() { string token=Context.GetHttpContext().Request.Query["access_token"]; + if (token == null) + return; string id=_tokenService.TokenToId(token); Users.Add(id,Context.ConnectionId); //await SendDirect(id, "poruka"); diff --git a/backend/api/api/Services/IMlConnectionService.cs b/backend/api/api/Services/IMlConnectionService.cs index 9a9a81f4..d161bf49 100644 --- a/backend/api/api/Services/IMlConnectionService.cs +++ b/backend/api/api/Services/IMlConnectionService.cs @@ -7,7 +7,7 @@ namespace api.Services { Task SendModelAsync(object model, object dataset); Task PreProcess(Dataset dataset, string filePath,string id); - Task TrainModel(Model model, Experiment experiment, string filePath); + Task TrainModel(Model model, Experiment experiment, string filePath, Dataset dataset, string id); //Task PreProcess(Dataset dataset, byte[] file, string filename); } } \ No newline at end of file diff --git a/backend/api/api/Services/MlConnectionService.cs b/backend/api/api/Services/MlConnectionService.cs index d3a98042..fa5ee2d0 100644 --- a/backend/api/api/Services/MlConnectionService.cs +++ b/backend/api/api/Services/MlConnectionService.cs @@ -32,19 +32,19 @@ namespace api.Services var result = await this.client.ExecuteAsync(request); return result.Content; //Response od ML microservisa } - public async Task TrainModel(Model model, Experiment experiment, string filePath) + public async Task TrainModel(Model model, Experiment experiment, string filePath,Dataset dataset,string id) { var request = new RestRequest("train", Method.Post); request.AddParameter("model", JsonConvert.SerializeObject(model)); request.AddParameter("experiment", JsonConvert.SerializeObject(experiment)); + request.AddParameter("dataset", JsonConvert.SerializeObject(dataset)); //request.AddFile("file", file,filename); request.AddFile("file", filePath); request.AddHeader("Content-Type", "multipart/form-data"); var result = await this.client.ExecuteAsync(request); - Model newModel = JsonConvert.DeserializeObject(result.Content); - newModel.isTrained = true; - _modelService.Update(newModel._id, newModel); + if (ChatHub.CheckUser(id)) + await _ichat.Clients.Client(ChatHub.Users[id]).SendAsync("NotifyModel", "Trained model with name " +model.name ); return; @@ -62,7 +62,7 @@ namespace api.Services newDataset.isPreProcess = true; _datasetService.Update(newDataset); if(ChatHub.CheckUser(id)) - await _ichat.Clients.Client(ChatHub.Users[id]).SendAsync("Notify", "Preprocessed dataset with name "+newDataset.name); + await _ichat.Clients.Client(ChatHub.Users[id]).SendAsync("NotifyDataset", "Preprocessed dataset with name "+newDataset.name); return; } -- cgit v1.2.3