public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Admin, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); log.LogInformation("Comment Analysis function triggered"); var db = TailwindContext.CreateNew(config.GetConnectionString("Tailwind")); int id = 0; int.TryParse(req.Query["CommentId"], out id); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string bodyid = data ?? data?.CommentId; if (!string.IsNullOrEmpty(bodyid)) { int.TryParse(bodyid, out id); } // using the comment Id, get the database entries var comment = db.Comments.FirstOrDefault(c => c.Id == id); var analysis = db.CommentAnalyses.FirstOrDefault(ca => ca.Id == id); // Get analysis of the text var analyser = new TextAnalyser(config["analyticsSubscriptionKey"], config["translatorSubscriptionKey"], config["region"]); var result = analyser.GetTextAnalysis(id, comment.CommentText); var isnew = analysis == null; if (isnew) { analysis = new CommentAnalysis() { Id = id }; } // Update the database analysis.Language = result.Language; analysis.Sentiment = result.Sentiment; analysis.EnglishTranslation = result.EnglishTranslation; analysis.Keywords = result.Keywords; if (isnew) { db.Add(analysis); } db.SaveChanges(); return((ActionResult) new OkObjectResult(result)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Admin, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); log.LogInformation("Get Product Image Tags function triggered"); var db = TailwindContext.CreateNew(config.GetConnectionString("Tailwind")); int id = 0; int.TryParse(req.Query["ProductId"], out id); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string bodyid = data ?? data?.CommentId; if (!string.IsNullOrEmpty(bodyid)) { int.TryParse(bodyid, out id); } // using the product Id, get the database entry var product = db.Products.FirstOrDefault(c => c.Id == id); // Get analysis of the text var analyser = new CustomVisionAnalyser(config["customVisionSubscriptionKey"], new Guid(config["customVisionProjectId"])); var tags = analyser.GetImageTagPredictions(product.ImageUrl); log.LogInformation("Tag predictions: {Tags}", tags); // get the confidence tag var bestTag = (tags.OrderByDescending(t => t.Confidence).FirstOrDefault()); if (tags.Any(t => t.Tag != bestTag.Tag && bestTag.Confidence - t.Confidence < 30)) { // take a bit off for having another tag so close bestTag.Confidence -= Math.Min(bestTag.Confidence, 20); log.LogInformation("Two tags were close together so we're reducing the confidence"); } product.Tags = bestTag.Tag; product.AutoTagConfidence = bestTag.Confidence; db.SaveChanges(); return((ActionResult) new OkObjectResult(tags)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Admin, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); log.LogInformation("Return Handwriting function triggered"); var db = TailwindContext.CreateNew(config.GetConnectionString("Tailwind")); int id = 0; int.TryParse(req.Query["ReturnId"], out id); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string bodyid = data ?? data?.CommentId; if (!string.IsNullOrEmpty(bodyid)) { int.TryParse(bodyid, out id); } // using the return Id, get the database entry var rtn = db.Returns.FirstOrDefault(c => c.Id == id); // Get analysis of the text var analyser = new HandwritingAnalyser(config["visionSubscriptionKey"], config["region"]); var readText = analyser.GetHandwritingAnalysis(rtn.ReturnImageUrl).Result; rtn.ReturnNotes = readText; db.SaveChanges(); return((ActionResult) new OkObjectResult(readText)); }