Fix training corpus format bug for RASA mode.
This commit is contained in:
parent
3de85e39e7
commit
9c99580317
|
|
@ -94,22 +94,6 @@ namespace BotSharp.RestApi
|
|||
return Ok(agent.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Train agent
|
||||
/// </summary>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
/*[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 "";
|
||||
}*/
|
||||
|
||||
/// <summary>
|
||||
/// Dump agent
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RASA|AnyCPU'">
|
||||
<DocumentationFile>BotSharp.RestApi.xml</DocumentationFile>
|
||||
<DocumentationFile>bin\RASA\netstandard2.0\BotSharp.RestApi.xml</DocumentationFile>
|
||||
<DefineConstants>TRACE;DEBUG</DefineConstants>
|
||||
<OutputPath>bin\RASA</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
|
@ -61,7 +61,7 @@
|
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DIALOGFLOW|AnyCPU'">
|
||||
<WarningLevel>1</WarningLevel>
|
||||
<DefineConstants>TRACE;DEBUG</DefineConstants>
|
||||
<DocumentationFile>BotSharp.RestApi.xml</DocumentationFile>
|
||||
<DocumentationFile>bin\DIALOGFLOW\netstandard2.0\BotSharp.RestApi.xml</DocumentationFile>
|
||||
<Optimize>false</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -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++)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,33 +43,51 @@ namespace BotSharp.RestApi.Rasa
|
|||
[HttpPost]
|
||||
public async Task<ActionResult<String>> 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<RasaTrainRequestModel>(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<RasaTrainRequestModel>(data);
|
||||
rasa_nlu_data.Model = model;
|
||||
rasa_nlu_data.Project = project;
|
||||
var trainResult = await Train(rasa_nlu_data, project);
|
||||
|
||||
return trainResult;
|
||||
}
|
||||
|
||||
private async Task<ActionResult<String>> 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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue