Exemple #1
0
        public static async Task <HttpResponseMessage> WebApiSkill([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                // Get request body
                var jsonRequest = await req.Content.ReadAsStringAsync();

                var docs = JsonConvert.DeserializeObject <WebApiSkillRequest>(jsonRequest);

                WebApiSkillResponse response = new WebApiSkillResponse();

                HttpClient httpClient = new HttpClient();

                foreach (var inRecord in docs.values)
                {
                    var outRecord = new WebApiResponseRecord()
                    {
                        recordId = inRecord.recordId
                    };

                    string name = inRecord.data["name"] as string;
                    log.Info($"Creating Search Document:{name}");
                    string blobUrl = ((string)inRecord.data["url"]) + inRecord.data["querystring"] as string;

                    try
                    {
                        log.Info($"Downloading Document:{blobUrl}");
                        var aa = await httpClient.GetAsync(blobUrl);

                        aa.EnsureSuccessStatusCode();
                        using (var stream = await aa.Content.ReadAsStreamAsync())
                        {
                            log.Info($"Processing Document...");
                            var annotations = await ProcessDocument(stream);

                            log.Info($"Creating Search Document...");
                            var searchDocument = CreateSearchDocument(name, annotations);
                            log.Info($"Document complete");

                            outRecord.data["metadata"] = searchDocument.Metadata;
                            outRecord.data["text"]     = searchDocument.Text;
                            outRecord.data["entities"] = searchDocument.LinkedEntities;
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error(e.ToString());
                        outRecord.errors.Add("Error processing the Document: " + e.ToString());
                    }
                    response.values.Add(outRecord);
                }

                return(req.CreateResponse(HttpStatusCode.OK, response));
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                return(req.CreateResponse(HttpStatusCode.BadRequest, "Error: " + ex.ToString()));
            }
        }
        public static async Task ProcessRecord(WebApiRequestRecord record, IOcrClient ocrClient, int idx)
        {
            WebApiResponseRecord waRecord = new WebApiResponseRecord();

            record.Data.TryGetValue("formUrl", out object imgFile);
            record.Data.TryGetValue("formSasToken", out object sasToken);
            string imgFileWithSaS = imgFile.ToString() + sasToken.ToString();
            string fileType       = imgFile.ToString().Substring(imgFile.ToString().LastIndexOf("."));
            string localFile      = Path.GetTempPath() + "\\" + "temp_" + idx.ToString() + fileType;

            using (var client = new WebClient())
            {
                client.DownloadFile(imgFileWithSaS, localFile);
            }

            // Process image
            // You could also call ProcessDocumentAsync or any other processing method declared below
            var resultUrls = await ProcessImageAsync(ocrClient, localFile);

            //Get results - the first doc is a docx, second is a text file
            using (var client = new WebClient())
            {
                waRecord.Data.Add("content", client.DownloadString(resultUrls[1].ToString()));
            }

            File.Delete(Path.GetTempPath() + "\\" + "temp_" + idx.ToString() + fileType);

            waRecord.RecordId = record.RecordId;

            bag.Add(waRecord);
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log, ExecutionContext executionContext)
        {
            log.Info("C# HTTP trigger function processed a request.");
            string skillName = executionContext.FunctionName;

            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{skillName} - Invalid request record array."));
            }
            dynamic obj = requestRecords.First().Data.First().Value;

            string val = await MakeRequest(obj);

            ContentModerator     mod    = JsonConvert.DeserializeObject <ContentModerator>(val);
            WebApiResponseRecord output = new WebApiResponseRecord();

            output.RecordId    = requestRecords.First().RecordId;
            output.Data["PII"] = mod.PII;
            WebApiSkillResponse resp = new WebApiSkillResponse();

            resp.Values = new List <WebApiResponseRecord>();
            resp.Values.Add(output);
            return(req.CreateResponse(HttpStatusCode.OK, resp));
        }
        public async Task <WebApiSkillResponse> ProcessInvoicesRecordsAsync(IEnumerable <WebApiRequestRecord> requestRecords)
        {
            WebApiSkillResponse response = new WebApiSkillResponse();

            foreach (WebApiRequestRecord inRecord in requestRecords)
            {
                WebApiResponseRecord outRecord = new WebApiResponseRecord()
                {
                    RecordId = inRecord.RecordId
                };

                try
                {
                    outRecord = await ProcessInvoiceRecord(inRecord, outRecord);
                }

                catch (Exception e)
                {
                    var erorMessage = $"{ServiceConstants.FormAnalyzerServiceName} - Error processing the request record: {e.ToString() }";
                    outRecord.Errors.Add(new WebApiErrorWarningContract()
                    {
                        Message = erorMessage
                    });

                    _log.LogError(erorMessage);
                }
                response.Values.Add(outRecord);
            }

            return(response);
        }
Exemple #5
0
        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));
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        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 async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log, ExecutionContext executionContext)
        {
            log.Info("C# HTTP trigger function processed a request.");
            string skillName = executionContext.FunctionName;

            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{skillName} - Invalid request record array."));
            }
            dynamic obj = requestRecords.First().Data.First().Value;

            if (obj.Length > 1024)
            {
                obj = obj.Substring(0, 1024);
            }

            string val = await MakeRequest(obj);

            ContentModerator mod = JsonConvert.DeserializeObject <ContentModerator>(val);

            bool requiresModeration = false;

            //Jon Dobrzeniecki helped with the code below, since may 2019 the CM API isn't returning the PII section if there is no data for it
            if (mod.PII != null)
            {
                if (mod.PII.Email.Length > 0)
                {
                    requiresModeration = true;
                }
                if (mod.PII.Address.Length > 0)
                {
                    requiresModeration = true;
                }
                if (mod.PII.IPA.Length > 0)
                {
                    requiresModeration = true;
                }
                if (mod.PII.Phone.Length > 0)
                {
                    requiresModeration = true;
                }
            }
            WebApiResponseRecord output = new WebApiResponseRecord();

            output.RecordId     = requestRecords.First().RecordId;
            output.Data["text"] = requiresModeration;

            WebApiSkillResponse resp = new WebApiSkillResponse();

            resp.Values = new List <WebApiResponseRecord>();
            resp.Values.Add(output);
            return(req.CreateResponse(HttpStatusCode.OK, resp));
        }
Exemple #10
0
        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);
        }
Exemple #11
0
        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 static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log, ExecutionContext executionContext)
        {
            try
            {
                log.Info("C# HTTP trigger function processed a request.");

                string skillName = executionContext.FunctionName;

                IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);
                if (requestRecords == null)
                {
                    return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{skillName} - Invalid request record array."));
                }
                log.Info($"Content Moderator : {requestRecords.ToString()}");
                dynamic obj = requestRecords.First().Data.First().Value;
                log.Info($"Content Moderator : {obj.ToString()}");

                string val = await MakeRequest(obj);

                ContentModerator     mod    = JsonConvert.DeserializeObject <ContentModerator>(val);
                WebApiResponseRecord output = new WebApiResponseRecord();
                output.RecordId = requestRecords.First().RecordId;
                if (mod.PII.Email.Length > 0)
                {
                    output.Data["PII"] = mod.PII.Email.FirstOrDefault().Detected;
                }
                else
                {
                    output.Data["PII"] = null;
                }
                WebApiSkillResponse resp = new WebApiSkillResponse();
                resp.Values = new List <WebApiResponseRecord>();
                resp.Values.Add(output);
                return(req.CreateResponse(HttpStatusCode.OK, resp));
            }
            catch (System.Exception ex)
            {
                log.Info(ex.StackTrace);
            }
            return(null);
        }
        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));
        }
Exemple #14
0
        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 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));
        }
Exemple #16
0
        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 <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log, ILogger logger, ExecutionContext executionContext)
        {
            WebApiResponseRecord output = new WebApiResponseRecord();
            string skillName            = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{skillName} - Invalid request record array."));
            }
            try
            {
                List <string> customer = new List <string>();
                logger.LogInformation("Hello World");
                Conversation conversation = new Conversation();
                string       content      = (string)requestRecords.First().Data["content"];
                conversation.Turns = new List <Turn>();

                string[] lines = content.Split(new char[] { '\n' });
                int      i     = 0;
                foreach (string line in lines)
                {
                    if (i == 0)
                    {
                        i++;
                        continue;
                    }
                    string[] regexed = SplitCSV(line);
                    //string[] cols = line.Split(new char[] { ',' });
                    if (regexed.Length < 3)
                    {
                        continue;
                    }
                    conversation.Turns.Add(new Turn(regexed[2], regexed[1], Convert.ToInt32(regexed[0])));
                    if (regexed[1] == "customer")
                    {
                        customer.Add(regexed[2]);
                    }
                    i++;
                }
                conversation.Count = conversation.Turns.Count;

                output.RecordId             = requestRecords.First().RecordId;
                output.Data["conversation"] = conversation;
                output.Data["customer"]     = customer;
                WebApiSkillResponse resp = new WebApiSkillResponse();
                resp.Values = new List <WebApiResponseRecord>();
                resp.Values.Add(output);
                log.Info($"Successful Run  ");
                return(req.CreateResponse(HttpStatusCode.OK, resp));
            }
            catch (Exception ex)
            {
                log.Info($"Error:  {ex.Message}");
                log.Info(ex.StackTrace);
                output.RecordId = requestRecords.First().RecordId;
                output.Errors   = new List <WebApiErrorWarningContract>();
                output.Errors.Add(new WebApiErrorWarningContract()
                {
                    Message = ex.Message
                });

                WebApiSkillResponse resp = new WebApiSkillResponse();
                resp.Values = new List <WebApiResponseRecord>();
                resp.Values.Add(output);
                return(req.CreateResponse(HttpStatusCode.OK, resp));
            }
        }
Exemple #18
0
        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 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 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}"));
            }
        }
        private async Task <WebApiResponseRecord> ProcessInvoiceRecord(WebApiRequestRecord webApiRequestRecord,
                                                                       WebApiResponseRecord webApiResponseRecord)
        {
            var formUrl = webApiRequestRecord.Data["formUrl"] as string;

            _log.LogInformation($"{ServiceConstants.FormAnalyzerServiceName} - Got form URL: {formUrl}");

            var analysisResult = await ProcessInvoiceDocumentContent(formUrl);

            webApiResponseRecord.Data = new Dictionary <string, object>();
            var invoiceData = new InvoiceData();

            if (analysisResult.documentResults != null)
            {
                var documents = analysisResult.documentResults;
                foreach (var documentResult in documents)
                {
                    var documentFields = documentResult.fields;
                    if (documentFields != null)
                    {
                        if (documentFields.Charges != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.Charges.fieldName, documentFields.Charges.text);
                            invoiceData.Charges = documentFields.Charges.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'Charges' for the form with URL: {formUrl}");
                        }
                        if (documentFields.ForCompany != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.ForCompany.fieldName, documentFields.ForCompany.text);
                            invoiceData.ForCompany = documentFields.ForCompany.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'ForCompany' for the form with URL: {formUrl}");
                        }
                        if (documentFields.FromCompany != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.FromCompany.fieldName, documentFields.FromCompany.text);
                            invoiceData.FromCompany = documentFields.FromCompany.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'FromCompany' for the form with URL: {formUrl}");
                        }
                        if (documentFields.InvoiceDate != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.InvoiceDate.fieldName, documentFields.InvoiceDate.text);
                            invoiceData.InvoiceDate = documentFields.InvoiceDate.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceDate' for the form with URL: {formUrl}");
                        }
                        if (documentFields.InvoiceDueDate != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.InvoiceDueDate.fieldName, documentFields.InvoiceDueDate.text);
                            invoiceData.InvoiceDueDate = documentFields.InvoiceDueDate.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceDueDate' for the form with URL: {formUrl}");
                        }
                        if (documentFields.InvoiceNumber != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.InvoiceNumber.fieldName, documentFields.InvoiceNumber.text);
                            invoiceData.InvoiceNumber = documentFields.InvoiceNumber.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceNumber' for the form with URL: {formUrl}");
                        }
                        if (documentFields.VatID != null)
                        {
                            webApiResponseRecord.Data.Add(documentFields.VatID.fieldName, documentFields.VatID.text);
                            invoiceData.VatID = documentFields.VatID.text;
                        }
                        else
                        {
                            _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'VatID' for the form with URL: {formUrl}");
                        }
                    }
                    else
                    {
                        _log.LogError($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get any fields from the form with URL: {formUrl}");
                    }
                }
            }
            else
            {
                _log.LogError($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get any document results from the form with URL: {formUrl}");
            }

            await _dataService.AddAsync(invoiceData);

            return(webApiResponseRecord);
        }
        private static async Task ExtractEntityData(AsyncPageable <AnalyzeHealthcareEntitiesResultCollection> pages, WebApiResponseRecord outRecord)
        {
            // Based on our input, there should only be one page per pages, and one document per page, but to guarantuee success we collect
            // all output into these two collections.
            var entities  = new List <Object>();
            var relations = new List <Object>();

            await foreach (AnalyzeHealthcareEntitiesResultCollection page in pages)
            {
                foreach (AnalyzeHealthcareEntitiesResult document in page)
                {
                    if (!document.HasError)
                    {
                        entities.AddRange(document.Entities);
                        relations.AddRange(document.EntityRelations);
                    }
                    else
                    {
                        outRecord.Errors.Add(new WebApiErrorWarningContract {
                            Message = $"TextAnalyticsForHealth Error: {document.Error.ErrorCode}. Error Message: {document.Error.Message}"
                        });
                    }

                    if (document.Warnings.Count > 0)
                    {
                        foreach (TextAnalyticsWarning w in document.Warnings)
                        {
                            outRecord.Warnings.Add(new WebApiErrorWarningContract
                            {
                                Message = $"TextAnalyticsForHealth Warning: {w.WarningCode}. Error Message: {w.Message}"
                            });
                        }
                    }
                }
            }
            outRecord.Data[$"entities"]  = entities;
            outRecord.Data[$"relations"] = relations;
        }
Exemple #23
0
        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)
        {
            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 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));
        }
Exemple #26
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log, ILogger logger, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"{skillName} - Invalid request record array."));
            }
            WebApiSkillResponse resp = new WebApiSkillResponse();

            resp.Values = new List <WebApiResponseRecord>();
            foreach (WebApiRequestRecord reqRec in requestRecords)
            {
                double lat                  = Convert.ToDouble(reqRec.Data["latitude"]);
                double lng                  = Convert.ToDouble(reqRec.Data["longitude"]);
                double review               = Convert.ToDouble(reqRec.Data["reviews_rating"]);
                string language             = (string)reqRec.Data["language"];
                WebApiResponseRecord output = new WebApiResponseRecord();
                try
                {
                    if (review > 5)
                    {
                        review = review / 2;
                    }

                    Address addr = await reverseGeocode(lat, lng, logger);

                    if (addr != null)
                    {
                        output.Data["state"]   = addr.adminDistrict;
                        output.Data["country"] = addr.countryRegion;
                    }
                    else
                    {
                        output.Data["state"]   = "UNKNOWN";
                        output.Data["country"] = "UNKNOWN";
                    }

                    output.RecordId = reqRec.RecordId;

                    output.Data["reviews_rating"] = review;


                    resp.Values.Add(output);
                }
                catch (System.Exception ex)
                {
                    log.Info($"EXCEPTION !!!! {ex.Message}");
                    log.Info(ex.StackTrace);
                    output.RecordId = reqRec.RecordId;
                    //output.Errors = new List<WebApiErrorWarningContract>();
                    //output.Errors.Add(new WebApiErrorWarningContract() { Message = ex.Message });
                    output.Data["state"]          = "UNKNOWN";
                    output.Data["country"]        = "UNKNOWN";
                    output.Data["reviews_rating"] = review;

                    resp.Values.Add(output);

                    return(req.CreateResponse(HttpStatusCode.OK, resp));
                }
            }



            log.Info($"Successful Run  returning {resp.Values.Count} records");
            return(req.CreateResponse(HttpStatusCode.OK, resp));
        }
        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));
        }