From 9c99580317d803f1a69c7bf07085a67045ba6280 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 24 Sep 2018 13:05:58 -0500 Subject: [PATCH] Fix training corpus format bug for RASA mode. --- BotSharp.RestApi/AgentController.cs | 16 ------- BotSharp.RestApi/BotSharp.RestApi.csproj | 4 +- BotSharp.RestApi/Rasa/StatusController.cs | 6 +++ BotSharp.RestApi/Rasa/TrainController.cs | 58 +++++++++++++++-------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/BotSharp.RestApi/AgentController.cs b/BotSharp.RestApi/AgentController.cs index 50d1fb7a..b8011d48 100644 --- a/BotSharp.RestApi/AgentController.cs +++ b/BotSharp.RestApi/AgentController.cs @@ -94,22 +94,6 @@ namespace BotSharp.RestApi return Ok(agent.Id); } - /// - /// Train agent - /// - /// - /// - /*[HttpGet("{agentId}")] - public string Train([FromRoute] String agentId) - { - string agentDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", agentId); - string dest = Directory.GetDirectories(agentDir).Where(x => x.Contains("model_")).Last(); - var agent = _platform.LoadAgentFromFile(dest); - _platform.Train(new BotTrainOptions { AgentDir = agentDir, Model = dest.Split(Path.DirectorySeparatorChar).Last() }); - - return ""; - }*/ - /// /// Dump agent /// diff --git a/BotSharp.RestApi/BotSharp.RestApi.csproj b/BotSharp.RestApi/BotSharp.RestApi.csproj index 9d9a3edf..c605339c 100644 --- a/BotSharp.RestApi/BotSharp.RestApi.csproj +++ b/BotSharp.RestApi/BotSharp.RestApi.csproj @@ -25,7 +25,7 @@ - BotSharp.RestApi.xml + bin\RASA\netstandard2.0\BotSharp.RestApi.xml TRACE;DEBUG bin\RASA @@ -61,7 +61,7 @@ 1 TRACE;DEBUG - BotSharp.RestApi.xml + bin\DIALOGFLOW\netstandard2.0\BotSharp.RestApi.xml false diff --git a/BotSharp.RestApi/Rasa/StatusController.cs b/BotSharp.RestApi/Rasa/StatusController.cs index 73c80827..78c29317 100644 --- a/BotSharp.RestApi/Rasa/StatusController.cs +++ b/BotSharp.RestApi/Rasa/StatusController.cs @@ -40,6 +40,12 @@ namespace BotSharp.RestApi.Rasa // scan dir, get all models var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects"); + + if (!Directory.Exists(projectPath)) + { + Directory.CreateDirectory(projectPath); + } + var projectDirs = Directory.GetDirectories(projectPath); for(int idx = 0; idx < projectDirs.Length; idx++) { diff --git a/BotSharp.RestApi/Rasa/TrainController.cs b/BotSharp.RestApi/Rasa/TrainController.cs index a0d2785e..8412a139 100644 --- a/BotSharp.RestApi/Rasa/TrainController.cs +++ b/BotSharp.RestApi/Rasa/TrainController.cs @@ -43,33 +43,51 @@ namespace BotSharp.RestApi.Rasa [HttpPost] public async Task> Train([FromQuery] string project, [FromQuery] string model) { - string body = ""; - using (var reader = new StreamReader(Request.Body)) + string agentDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", project); + if (!Directory.Exists(agentDir)) { - body = reader.ReadToEnd(); + Directory.CreateDirectory(agentDir); } - string lang = Regex.Match(body, @"language:.+")?.Value; - if (!String.IsNullOrEmpty(lang)) + if (string.IsNullOrEmpty(model)) { - lang = lang.Substring(11, 2); - } - string data = Regex.Match(body, @"data:([\s\S]*)")?.Value; - if (String.IsNullOrEmpty(data)) - { - data = body; + string dest = Directory.GetDirectories(agentDir).Where(x => x.Contains("model_")).Last(); + var agent = _platform.LoadAgentFromFile(dest); + model = dest.Split(Path.DirectorySeparatorChar).Last(); + await _platform.Train(new BotTrainOptions { AgentDir = agentDir, Model = model }); + + return Ok(new { info = model }); } else { - data = data.Substring(6); + string body = ""; + using (var reader = new StreamReader(Request.Body)) + { + body = reader.ReadToEnd(); + } + + string lang = Regex.Match(body, @"language:.+")?.Value; + if (!String.IsNullOrEmpty(lang)) + { + lang = lang.Substring(11, 2); + } + string data = Regex.Match(body, @"data:([\s\S]*)")?.Value; + if (String.IsNullOrEmpty(data)) + { + data = body; + } + else + { + data = data.Substring(6); + } + + var rasa_nlu_data = JsonConvert.DeserializeObject(data); + rasa_nlu_data.Model = model; + rasa_nlu_data.Project = project; + var trainResult = await Train(rasa_nlu_data, project); + + return trainResult; } - - var rasa_nlu_data = JsonConvert.DeserializeObject(data); - rasa_nlu_data.Model = model; - rasa_nlu_data.Project = project; - var trainResult = await Train(rasa_nlu_data, project); - - return trainResult; } private async Task> Train([FromBody] RasaTrainRequestModel request, [FromQuery] string project) @@ -99,7 +117,7 @@ namespace BotSharp.RestApi.Rasa // in order to unify the process. var fileName = Path.Combine(modelPath, "corpus.json"); - System.IO.File.WriteAllText(fileName, JsonConvert.SerializeObject(request.Corpus, new JsonSerializerSettings + System.IO.File.WriteAllText(fileName, JsonConvert.SerializeObject(request, new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore,