public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { try { log.Info("[FaceIdentify] started"); string name = ""; // 1. Parameter Validations if (key == null) { return(new BadRequestObjectResult($"[FaceIdentify][Error] TranslatorText KEY is missing in Environment Variable.")); } string requestBody = new StreamReader(req.Body).ReadToEnd(); log.Info($"[FaceIdentify] Request Data:{requestBody}"); dynamic data = JsonConvert.DeserializeObject(requestBody); var recordId = data?.values?.First?.recordId?.Value as string; var imagedata = data?.values?.First?.data?.image?.data.Value as string; log.Info($"[FaceIdentify] recordId:{recordId}, imagedata:{imagedata}"); byte[] bytedata = System.Convert.FromBase64String(imagedata); Stream s = new MemoryStream(bytedata); var faceID = detectFace(s).Result; log.Info($"[FaceIdentify] faceID:{faceID}"); if (faceID != null) { var personID = identifyFace(faceID).Result; log.Info($"[FaceIdentify] personID:{personID}"); if (personID != null) { name = getPerson(personID).Result; log.Info($"[FaceIdentify] name:{name}"); } } // 3. Build response JSON WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.recordId = recordId; responseRecord.data = new Dictionary <string, object>(); responseRecord.data.Add("name", name); // Put together response. WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); log.Info($"[FaceIdentify] Complate."); return((ActionResult) new OkObjectResult(response)); } catch (Exception e) { log.Error($"[FaceIdentify] Exception: {e.Message}"); return(new BadRequestObjectResult($"[FaceIdentify] Exception: {e.Message}")); } }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log) { string recordId = null; string originalText = null; List <string> productList = new List <string>(); List <string> termList = new List <string>(); // This is a hard list you can check in your documents List <string> techProducts = new List <string> { "Microsoft", "XBOX", "Windows", "Windows 10", "Windows 8", "Windows 8.1", "Office 365", "Dynamics 365", "Azure", "Cortana", "Microsoft Edge" }; List <string> techTerms = new List <string> { "CRM", "More Personal Computing", "MPC", "AI", "Artificial Intelligence", "Machine Learning", "Deep Learning" }; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); // Validation if (data?.values == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } // Retrieve the values from json Body recordId = data?.values?.First?.recordId?.Value as string; originalText = (data?.values?.First?.data?.text?.Value as string).ToLower(); // Search values termList = techTerms.Where(w => originalText.Contains(w.ToLower())).ToList <string>(); productList = techProducts.Where(w => originalText.Contains(w.ToLower())).ToList <string>(); if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.recordId = recordId; responseRecord.data.Add("termList", termList); responseRecord.data.Add("productList", productList); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return((ActionResult) new OkObjectResult(response)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = new StreamReader(req.Body).ReadToEnd(); log.LogInformation($"Translate function receivied a request. Request body: {requestBody}"); var request = JsonConvert.DeserializeObject <TranslateRequest>(requestBody); // Validation if (request?.Values == null) { return(new BadRequestObjectResult("Could not find values array")); } if (request.Values.Any() == false || string.IsNullOrWhiteSpace(request.Values.First().Data?.Text) == true) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult("Could not find valid records in values array")); } var valueToTranslate = request.Values.First(); if (valueToTranslate.RecordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } log.LogInformation($"Translate function translating '{valueToTranslate.Data.Text}' from {valueToTranslate.Data.Language} to English."); var translatedText = valueToTranslate.Data.Language != "en" ? await TranslateText(valueToTranslate.Data.Text) : valueToTranslate.Data.Text; log.LogInformation($"Translate function translation '{translatedText}'."); // Put together response. var responseRecord = new WebApiResponseRecord { Data = new Dictionary <string, object> { { "text", translatedText } }, RecordId = valueToTranslate.RecordId }; var response = new WebApiEnricherResponse { Values = new List <WebApiResponseRecord> { responseRecord } }; log.LogInformation($"Translate function output '{responseRecord}'."); return(new OkObjectResult(response)); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); string recordId = null; string originalText = null; string originalLanguage = null; string translatedText = null; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); // Validation if (data?.values == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } recordId = data?.values?.First?.recordId?.Value as string; originalText = data?.values?.First?.data?.text?.Value as string; originalLanguage = data?.values?.First?.data?.language?.Value as string; if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } if (!originalLanguage.Contains("en")) { translatedText = TranslateText(originalText, "en").Result; } else { translatedText = originalText; } // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.recordId = recordId; responseRecord.data.Add("text", translatedText); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return((ActionResult) new OkObjectResult(response)); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string recordId = null; string originalText = null; string toLanguage = null; string translatedText = null; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); // Validation if (data?.values == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } foreach (var rec in data.values) { } recordId = data?.values?.First?.recordId?.Value as string; originalText = data?.values?.First?.data?.text?.Value as string; if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } List <string> SortedWordFrequency = SortTextByFrequency(originalText).Result; // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.recordId = recordId; responseRecord.data.Add("text", string.Join(" ", SortedWordFrequency)); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); var r = (ActionResult) new OkObjectResult(response); log.LogInformation(r.ToString()); return(r); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext) { log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); string imageUrl = data?.url; // Check if imageUrl is not empty if (string.IsNullOrWhiteSpace(imageUrl)) { return(new BadRequestObjectResult("Please pass an image URL in the request body")); } else { // Get SAS Access to private containers CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("ConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(Environment.GetEnvironmentVariable("ContainerName")); // Get ImageUrl Uri uri = new Uri(imageUrl); // This code part is part is adjust for forms, find file path & name after container name string[] parts = uri.LocalPath.Split(Environment.GetEnvironmentVariable("ContainerName") + "/"); string filename = parts[parts.Length - 1]; imageUrl = GetBlobSasUri(container, filename, null); // Retrieve Classification of the images string formTemplate = await MakeCustomVisionRequestByUrl(imageUrl); // Key-Value Extraction via OCR Output var outputResult = await MakeOCRRequestByUrl(imageUrl, formTemplate, executionContext); // Put together response as JSON output WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.dataSource = imageUrl; responseRecord.data.Add("KeyValues", outputResult); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return((ActionResult) new OkObjectResult(response)); } }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { using var inputStream = new StreamReader(req.Body); var requestBody = await inputStream.ReadToEndAsync(); var data = JToken.Parse(requestBody); // Validation if (data == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data["values"]?.FirstOrDefault() == null) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } var recordId = data["values"].First()["recordId"]?.ToString(); if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } // Creates the response. var responseRecord = new WebApiResponseRecord(recordId); var response = new WebApiEnricherResponse(responseRecord); var imageCaption = data["values"].First()["data"]?["imageCaption"]?.FirstOrDefault()?["captions"]?.FirstOrDefault(); var description = imageCaption?["text"]?.ToString(); var confidence = double.Parse(imageCaption?["confidence"]?.ToString().Replace(",", ".") ?? "0", CultureInfo.InvariantCulture); responseRecord.Data.Add("description", description); responseRecord.Data.Add("confidence", confidence); return(new OkObjectResult(response)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string recordId = null; string contentType = null; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); recordId = data?.values?.First?.recordId?.Value as string; contentType = data?.values?.First?.contentType?.Value as string; if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } var fileDescription = MimeTypes.FirstOrDefault(f => f.Mime == contentType); // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord { data = new Dictionary <string, object>(), recordId = recordId }; responseRecord.data.Add("dataType", fileDescription == null ? "Unkown File Type" : fileDescription.Name); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return((ActionResult) new OkObjectResult(response)); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { string recordId = null; string textInput = null; List <string> termList = new List <string>(); // Stop word list List <string> stopwords = new List <string>() { "a", "able", "about", "above", "according", "accordingly", "across", "actually", "after", "afterwards", "again", "against", "ain't", "all", "allow", "allows", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "amongst", "an", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren't", "around", "as", "a's", "aside", "ask", "asking", "associated", "at", "available", "away", "awfully", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "by", "came", "can", "cannot", "cant", "can't", "cause", "causes", "certain", "certainly", "changes", "clearly", "c'mon", "co", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn't", "course", "c's", "currently", "definitely", "described", "despite", "did", "didn't", "different", "do", "does", "doesn't", "doing", "done", "don't", "down", "downwards", "during", "each", "edu", "eg", "eight", "either", "else", "elsewhere", "enough", "entirely", "especially", "et", "etc", "even", "ever", "every", "everybody", "everyone", "everything", "everywhere", "ex", "exactly", "example", "except", "far", "few", "fifth", "first", "five", "followed", "following", "follows", "for", "former", "formerly", "forth", "four", "from", "further", "furthermore", "get", "gets", "getting", "given", "gives", "go", "goes", "going", "gone", "got", "gotten", "greetings", "had", "hadn't", "happens", "hardly", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "hello", "help", "hence", "her", "here", "hereafter", "hereby", "herein", "here's", "hereupon", "hers", "herself", "he's", "hi", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "how's", "i", "i'd", "ie", "if", "ignored", "i'll", "i'm", "immediate", "in", "inasmuch", "inc", "indeed", "indicate", "indicated", "indicates", "inner", "insofar", "instead", "into", "inward", "is", "isn't", "it", "it'd", "it'll", "its", "it's", "itself", "i've", "just", "keep", "keeps", "kept", "know", "known", "knows", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "let's", "like", "liked", "likely", "little", "look", "looking", "looks", "ltd", "mainly", "many", "may", "maybe", "me", "mean", "meanwhile", "merely", "might", "more", "moreover", "most", "mostly", "much", "must", "mustn't", "my", "myself", "name", "namely", "nd", "near", "nearly", "necessary", "need", "needs", "neither", "never", "nevertheless", "new", "next", "nine", "no", "nobody", "non", "none", "noone", "nor", "normally", "not", "nothing", "novel", "now", "nowhere", "obviously", "of", "off", "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones", "only", "onto", "or", "other", "others", "otherwise", "ought", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "particular", "particularly", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provides", "que", "quite", "qv", "rather", "rd", "re", "really", "reasonably", "regarding", "regardless", "regards", "relatively", "respectively", "right", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "since", "six", "so", "some", "somebody", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "take", "taken", "tell", "tends", "th", "than", "thank", "thanks", "thanx", "that", "thats", "that's", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "theres", "there's", "thereupon", "these", "they", "they'd", "they'll", "they're", "they've", "think", "third", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "t's", "twice", "two", "un", "under", "unfortunately", "unless", "unlikely", "until", "unto", "up", "upon", "us", "use", "used", "useful", "uses", "using", "usually", "value", "various", "very", "via", "viz", "vs", "want", "wants", "was", "wasn't", "way", "we", "we'd", "welcome", "well", "we'll", "went", "were", "we're", "weren't", "we've", "what", "whatever", "what's", "when", "whence", "whenever", "when's", "where", "whereafter", "whereas", "whereby", "wherein", "where's", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "who's", "whose", "why", "why's", "will", "willing", "wish", "with", "within", "without", "wonder", "won't", "would", "wouldn't", "yes", "yet", "you", "you'd", "you'll", "your", "you're", "yours", "yourself", "yourselves", "you've", "zero" }; // This is a hard list you can check in your documents List <string> customwords = new List <string> { "marie", "curie", "curies", "pierre", "irène ", "irene", "new", "time", "also", "one", "name", "work", "first", "became", "began", "work", "working", "must", "left", "could", "never", "life", "died", "found", "set", "still", "awarded", "worked", "used", "called", "rosalind", "franklin" }; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); // Validation if (data?.values == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } // Retrieve the values from json Body recordId = data?.values?.First?.recordId?.Value as string; textInput = (data?.values?.First?.data?.text?.Value as string).ToLower(); // Lower case all letters textInput = textInput.ToLower(); // Remove Digits \d- // Remove punctiations \p{P} // Remove new line characters \t|\n|\r textInput = Regex.Replace(textInput, @"[\d-\p{P}\t|\n|\r]", string.Empty); // Replace multiple whitespaces with a single one textInput = Regex.Replace(textInput, @"\s+", " "); // Tokenize all words string[] tokenizedText = textInput.Split(' '); // Put all words in a dictionary and group them for Term Frequency var frequency = tokenizedText.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count()).OrderByDescending(i => i.Value).ToDictionary(x => x.Key, x => x.Value); // Remove Stop words and custom stop words var cleanTokens = frequency.Keys.Except(stopwords); cleanTokens = cleanTokens.Except(customwords); if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.recordId = recordId; responseRecord.data.Add("termList", cleanTokens.Take(40)); WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return((ActionResult) new OkObjectResult(response)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); log.LogInformation($"Summarize function receivied a request. Request body: {requestBody}"); var request = JsonConvert.DeserializeObject <CustomSkillRequest>(requestBody); // Validation if (request?.Values == null) { return(new BadRequestObjectResult("Could not find values array")); } if (request.Values.Any() == false || request.Values.First().Data == null) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult("Could not find valid records in values array")); } var valueToSummarize = request.Values.First(); if (valueToSummarize.RecordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } string textToSummarize = valueToSummarize.Data.text; if (string.IsNullOrWhiteSpace(textToSummarize)) { return(new BadRequestObjectResult("Text to summarize is required.")); } log.LogInformation($"Summarize function creating summary text for '{textToSummarize}'."); var summaryText = await SummarizeText(textToSummarize); log.LogInformation($"Summarize function summary: '{summaryText}'."); // Put together response. var responseRecord = new WebApiResponseRecord { Data = new Dictionary <string, object> { { "summaryText", summaryText } }, RecordId = valueToSummarize.RecordId }; var response = new WebApiEnricherResponse { Values = new List <WebApiResponseRecord> { responseRecord } }; log.LogInformation($"Summary function output '{responseRecord}'."); return(new OkObjectResult(response)); }
public static IActionResult Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log) { try { string recordId = null; string originalText = null; string translatedText = null; log.Info("[Translate] Function started."); // 1. Parameter Validations if (key == null) { return(new BadRequestObjectResult($"[Translate][Error] TranslatorText KEY is missing in Environment Variable.")); } string requestBody = new StreamReader(req.Body).ReadToEnd(); log.Info($"[Translate] Request Data:{requestBody}"); dynamic data = JsonConvert.DeserializeObject(requestBody); if (data?.values == null) { return(new BadRequestObjectResult("[Translate] Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult("[Translate] Could not find valid records in values array")); } recordId = data?.values?.First?.recordId?.Value as string; if (recordId == null) { return(new BadRequestObjectResult("[Translate] recordId cannot be null")); } originalText = data?.values?.First?.data?.text?.Value as string; if (originalText == null) { return(new BadRequestObjectResult("[Translate] text cannot be null")); } // 1.1. Text clearnup for TextSplit task originalText = originalText.Replace("\n", "").Trim(); log.Info($"[Translate] OriginalText:{originalText}"); // 2. Call Translator Text translatedText = TranslateText(originalText).Result; log.Info($"[Translate] TranslatedData: id:{recordId} - text:{translatedText}"); // 3. Build response JSON WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.recordId = recordId; responseRecord.data = new Dictionary <string, object>(); responseRecord.data.Add("text", translatedText); // Put together response. WebApiEnricherResponse response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); log.Info($"[Translate] Complate."); return((ActionResult) new OkObjectResult(response)); } catch (Exception e) { log.Error($"[Translate] Exception: {e.Message}"); return(new BadRequestObjectResult($"[Translate] Exception: {e.Message}")); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("DetectAnomalies function received a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var request = JsonConvert.DeserializeObject <DetectAnomalyRequest>(requestBody); log.LogInformation($"DetectAnomalies function received body: {requestBody}."); log.LogInformation($"DetectAnomalies function received body: {requestBody}."); if (request?.Values == null) { return(new BadRequestObjectResult("Could not find values array")); } if (request.Values.Any() == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult("Could not find valid records in values array")); } var document = request.Values.First(); if (document.RecordId == null) { return(new BadRequestObjectResult("RecordId cannot be null")); } log.LogInformation($"DetectAnomalies function processing record {document.RecordId}."); var result = await Detect(document.Data); // Put together response. var responseRecord = new WebApiResponseRecord { RecordId = document.RecordId }; var dataRecords = new Dictionary <string, object> { { "anomalyResult", result }, { "isAnomaly", result.IsAnomaly }, { "isPositiveAnomaly", result.IsPositiveAnomaly }, { "isNegativeAnomaly", result.IsNegativeAnomaly }, { "expectedValue", result.ExpectedValue }, { "upperMargin", result.UpperMargin }, { "lowerMargin", result.LowerMargin } }; responseRecord.Data = dataRecords; var response = new WebApiEnricherResponse { Values = new List <WebApiResponseRecord> { responseRecord } }; log.LogInformation($"Response for {document.RecordId } is: {JsonConvert.SerializeObject(response)}"); return(new OkObjectResult(response)); }
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req, FunctionContext executionContext) { string recordId = null; string originalText = null; var log = executionContext.GetLogger("HttpTriggerTopTen"); string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation("==>>>>REQUEST BODY [" + requestBody + "]"); var responseError = req.CreateResponse(HttpStatusCode.InternalServerError); responseError.Headers.Add("Content-Type", "application/json; charset=utf-8"); // Validation if (data?.values == null) { responseError.WriteString(JsonConvert.SerializeObject(( new WebApiResponseRecord() { errors = new List <WebApiResponseError>() { new WebApiResponseError() { message = "Could not find values array" } } } ))); return(responseError); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { responseError.WriteString(JsonConvert.SerializeObject(( new WebApiResponseRecord() { errors = new List <WebApiResponseError>() { new WebApiResponseError() { message = "Could not find valid records in values array" } } } ))); return(responseError); } WebApiEnricherResponse responseApi = new WebApiEnricherResponse(); responseApi.values = new List <WebApiResponseRecord>(); foreach (var record in data?.values) { recordId = record.recordId?.Value as string; originalText = record.data?.text?.Value as string; if (recordId == null) { responseError.WriteString(JsonConvert.SerializeObject(( new WebApiResponseRecord() { errors = new List <WebApiResponseError>() { new WebApiResponseError() { message = "recordId cannot be null" } } }))); return(responseError); } // log input log.LogInformation("==>>>>REQUEST text [" + originalText + "]"); // Put together response. WebApiResponseRecord responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.data.Add("words", get_top_ten_words(originalText)); //new Dictionary<string, object>(); responseRecord.data.Add("topten", string.Join(",", get_top_ten_words(originalText))); responseRecord.recordId = recordId; responseRecord.warnings = new List <WebApiResponseWarning>() { new WebApiResponseWarning() { message = string.Join(",", get_top_ten_words(originalText)) } }; //responseRecord.data.Add("text", get_top_ten_words(originalText)); responseApi.values.Add(responseRecord); } var response = req.CreateResponse(HttpStatusCode.OK); response.Headers.Add("Content-Type", "application/json; charset=utf-8"); log.LogInformation("==>>>>RESPONSE " + JsonConvert.SerializeObject(responseApi)); response.WriteString(JsonConvert.SerializeObject(responseApi)); //response.WriteString(JsonConvert.SerializeObject(new { values = new { data = new { words = get_top_ten_words(originalText) } } })); return(response); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("AnalyzeForm function received a request."); string modelId = req.Query["modelId"]; if (string.IsNullOrWhiteSpace(modelId)) { return(new BadRequestObjectResult("The Form Recognizer ModelId must be passed in the query string.")); } log.LogInformation($"AnalyzeForm function using Form Recognizer model {modelId}."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var request = JsonConvert.DeserializeObject <AnalyzeFormRequest>(requestBody); log.LogInformation($"AnalyzeForm function received body: {requestBody}."); if (request?.Values == null) { return(new BadRequestObjectResult("Could not find values array")); } if (request.Values.Any() == false || string.IsNullOrWhiteSpace(request.Values.First().Data?.StorageUri) == true) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult("Could not find valid records in values array")); } var document = request.Values.First(); if (document.RecordId == null) { return(new BadRequestObjectResult("RecordId cannot be null")); } log.LogInformation($"AnalyzeForm function processing record {document.RecordId}."); log.LogInformation($"AnalyzeForm function is retrieving the document '{document.Data.StorageUri}'."); var formBytes = await GetDocumentFromStorage(document.Data); log.LogInformation($"AnalyzeForm function analyzing '{document.Data.StorageUri}' using model ID {modelId}."); var analyzedForm = await AnalyzeForm(document.Data, modelId, formBytes); log.LogInformation($"AnalyzeForm function completed analyzing document '{document.Data.StorageUri}'."); log.LogInformation($"Analyzed form result: {JsonConvert.SerializeObject(analyzedForm)}"); var form = JsonConvert.DeserializeObject <FormRecognizerResponse>(analyzedForm); // Put together response. var responseRecord = new WebApiResponseRecord { RecordId = document.RecordId }; var page = form.Pages.First(); var dataRecords = new Dictionary <string, object> { { "formHeight", page.Height }, { "formWidth", page.Width } }; var keyValuePairs = new List <string>(); foreach (var kvp in page.KeyValuePairs) { keyValuePairs.Add($"{kvp.Key.First().Text}: {string.Join(" ", kvp.Value.Select(v => v.Text).ToList())}"); } dataRecords.Add("formKeyValuePairs", keyValuePairs); var columns = new List <string>(); foreach (var column in page.Tables.First().Columns) { columns.Add($"{column.Header.First().Text}: {string.Join(" ", column.Entries.First().Select(v => v.Text).ToList())}"); } dataRecords.Add("formColumns", columns); responseRecord.Data = dataRecords; var response = new WebApiEnricherResponse { Values = new List <WebApiResponseRecord> { responseRecord } }; log.LogInformation($"Response for {document.Data.StorageUri} is: {JsonConvert.SerializeObject(response)}"); return(new OkObjectResult(response)); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext context) { log.Info("Function processing request..."); var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var mapServiceBaseAddress = config["MapServiceBaseUrl"]; var mapServiceKey = config["MapServiceKey"]; // TODO: Move this to static to avoid socket exhaustion. var client = new HttpClient(); client.BaseAddress = new Uri(mapServiceBaseAddress); client.DefaultRequestHeaders.Accept.Clear(); // Call map service var mapService = new AzureMapService(client, mapServiceKey); // var mapService = new MockedMapService(); var requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); // Validation if (data?.values == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data?.values.HasValues == false || data?.values.First.HasValues == false) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } var recordId = data?.values?.First?.recordId?.Value as string; var locations = data?.values?.First?.data?.locations; var geoPoints = new List <GeoPoint>(); foreach (var location in locations) { var coordinates = await mapService.GetCoordinates(JsonConvert.SerializeObject(location)); geoPoints.Add(coordinates); } // Create Response var responseRecord = new WebApiResponseRecord(); responseRecord.data = new Dictionary <string, object>(); responseRecord.recordId = recordId; responseRecord.data.Add("lon-lat", geoPoints); var response = new WebApiEnricherResponse(); response.values = new List <WebApiResponseRecord>(); response.values.Add(responseRecord); return(new OkObjectResult(response)); }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { using var inputStream = new StreamReader(req.Body); var requestBody = await inputStream.ReadToEndAsync(); var data = JToken.Parse(requestBody); // Validation if (data == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data["values"]?.FirstOrDefault() == null) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } var recordId = data["values"].First()["recordId"]?.ToString(); if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } var base64image = data["values"].First()["data"]?["image"]?["data"]?.ToString(); if (base64image == null) { return(new BadRequestObjectResult("image data cannot be null")); } // Creates the response. var responseRecord = new WebApiResponseRecord(recordId); var response = new WebApiEnricherResponse(responseRecord); var people = new List <string>(); var faceClient = new FaceClient(new ApiKeyServiceClientCredentials(settings.FaceSubscriptionKey), httpClient, false) { Endpoint = $"https://{settings.Region}.api.cognitive.microsoft.com" }; var buffer = System.Convert.FromBase64String(base64image); using var stream = new MemoryStream(buffer); var faces = await faceClient.Face.DetectWithStreamAsync(stream); if (faces.Any()) { var personGroups = await faceClient.PersonGroup.ListAsync(); var identifyPersonGroupId = (personGroups?.FirstOrDefault(p => p.Name.ToLower() == "default" || p.UserData.ToLower() == "default") ?? personGroups?.FirstOrDefault())?.PersonGroupId; if (identifyPersonGroupId != null) { var faceIds = faces.Select(face => face.FaceId.Value).ToList(); var faceIdentificationResult = await faceClient.Face.IdentifyAsync(faceIds, identifyPersonGroupId); foreach (var face in faces) { var candidate = faceIdentificationResult?.FirstOrDefault(r => r.FaceId == face.FaceId)?.Candidates.FirstOrDefault(); if (candidate != null) { // Gets the person name. var person = await faceClient.PersonGroupPerson.GetAsync(identifyPersonGroupId, candidate.PersonId); people.Add(person.Name); } } } } responseRecord.Data.Add("people", people); return(new OkObjectResult(response)); }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { using var inputStream = new StreamReader(req.Body); var requestBody = await inputStream.ReadToEndAsync(); var data = JToken.Parse(requestBody); // Validation if (data == null) { return(new BadRequestObjectResult(" Could not find values array")); } if (data["values"]?.FirstOrDefault() == null) { // It could not find a record, then return empty values array. return(new BadRequestObjectResult(" Could not find valid records in values array")); } var recordId = data["values"].First()["recordId"]?.ToString(); if (recordId == null) { return(new BadRequestObjectResult("recordId cannot be null")); } // Creates the response. var responseRecord = new WebApiResponseRecord(recordId); var response = new WebApiEnricherResponse(responseRecord); double? latitude = null, longitude = null; DateTime?takenAt = null; Address address = null; var uri = data["values"].First()["data"]?["uri"]?.ToString(); var imageContent = await httpClient.GetByteArrayAsync(uri); using var image = new MagickImage(imageContent, 0, imageContent.Length); // Retrieve the exif information. var profile = image.GetExifProfile(); var exifLatitude = profile?.Values.FirstOrDefault(v => v.Tag == ExifTag.GPSLatitude).GetValue() as Rational[]; var exifLatitudeRef = profile?.Values.FirstOrDefault(v => v.Tag == ExifTag.GPSLatitudeRef).GetValue() as string ?? "N"; var exifLongitude = profile?.Values.FirstOrDefault(v => v.Tag == ExifTag.GPSLongitude).GetValue() as Rational[]; var exifLongitudeRef = profile?.Values.FirstOrDefault(v => v.Tag == ExifTag.GPSLongitudeRef).GetValue() as string ?? "E"; latitude = ToDecimalDegrees(exifLatitude, exifLatitudeRef == "N" ? 1 : -1); longitude = ToDecimalDegrees(exifLongitude, exifLongitudeRef == "E" ? 1 : -1); // Perform a reverse geocoding of the address. address = await GetAddressAsync(latitude, longitude); var takenAtRef = (profile?.Values.FirstOrDefault(v => v.Tag == ExifTag.DateTime) ?? profile.Values.FirstOrDefault(v => v.Tag == ExifTag.DateTimeOriginal)).GetValue() as string; if (!string.IsNullOrWhiteSpace(takenAtRef) && DateTime.TryParseExact(takenAtRef, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result)) { takenAt = result; } responseRecord.Data.Add("location", new Location { Position = EdmGeographyPoint.Create(latitude, longitude), Address = address }); responseRecord.Data.Add("takenAt", takenAt); return(new OkObjectResult(response)); }