コード例 #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;

            log.LogInformation("Object detection function: C# HTTP trigger function processed a request.");

            var response = new WebApiResponse();

            response.values = new List <OutputRecord>();

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            var    data        = JsonConvert.DeserializeObject <WebApiRequest>(requestBody);

            // Do some schema validation
            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema. Could not find values array."));
            }

            // Calculate the response for each value.
            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    responseRecord.Data = GetObjects(record.Data.Url).Result;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>();
                    responseRecord.Errors.Add(error);
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return((ActionResult) new OkObjectResult(response));
        }
コード例 #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Custom skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            // Calculate the response for each value.
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    responseRecord.Data = DoWork(record.Data).Result;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return(new OkObjectResult(response));
        }
コード例 #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Custom skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            var storageConnectionString = Environment.GetEnvironmentVariable("StorageContainerString");
            var modelTable  = Environment.GetEnvironmentVariable("ModelTableName");
            var entityTable = Environment.GetEnvironmentVariable("EntityTableName");

            // Calculate the response for each value.
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };

                var rootObject = new OutputRecord.OutputRecordData
                {
                    FormEntitiesv2 = new Dictionary <string, string>()
                };

                try
                {
                    // Read Azure Table and find all entries where mlmodel = "Form"
                    // Read information about the storage account and storage key from App Settings
                    var storageAccount  = CreateStorageAccountFromConnectionString(storageConnectionString);
                    var sourceBlob      = new CloudBlob(new Uri(record.Data.Url), storageAccount.Credentials);
                    var sourceContainer = sourceBlob.Container.Name;
                    // Since we are storing the file into format "container/formtype/attachmenttype/files"
                    var formType       = sourceBlob.Parent.Parent.Prefix.Replace("/", "");
                    var sourceFilePath = sourceBlob.Name;
                    var sourceFileName = sourceFilePath.Replace(sourceBlob.Parent.Prefix, "").Replace(".pdf", "");

                    log.LogInformation("Form Recognizer Skill function: Url : {0}", record.Data.Url);

                    var sortedModel = GetModelInfo(storageConnectionString, modelTable,
                                                   formType).Result;

                    // Loop through all the results once it's sorted by Page Number
                    foreach (var model in sortedModel)
                    {
                        var    folder = sourceBlob.Parent.Prefix;
                        var    file   = sourceFileName + "_" + model.Page.PadLeft(3, '0') + ".jpg";
                        var    blob   = GetBlobReference(folder + file, sourceContainer, storageConnectionString);
                        Stream myBlob = new MemoryStream();
                        blob.DownloadToStreamAsync(myBlob).Wait();
                        myBlob.Position = 0;

                        var entities = AnalyzeForm(model.ModelId, model.EndPoint,
                                                   model.SubscriptionKey, myBlob, rootObject.FormEntitiesv2, entityTable,
                                                   storageConnectionString, file, model.Page, formType, folder, sourceContainer).Result;
                        log.LogInformation("Form Recognizer Skill : C# HTTP output : {0}", responseRecord.Data);
                    }

                    responseRecord.Data = rootObject;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    log.LogInformation("Custom skill: C# Exception : {0}", e.Message);

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return(new OkObjectResult(response));
        }
コード例 #4
0
        public async Task <ActionResult <string> > MergeSharePointMetadatav2()
        {
            System.Diagnostics.Trace.WriteLine("Starting call");

            var response = new WebApiResponse()
            {
                Values = new List <OutputRecord>()
            };

            string requestBody = new StreamReader(Request.Body).ReadToEnd();
            //dynamic data = JsonConvert.DeserializeObject(requestBody);
            var data = JsonConvert.DeserializeObject <CustomSkillApiRequest>(requestBody);

            // Do some schema validation
            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema. Could not find values array."));
            }

            // Calculate the response for each value.
            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }



                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId,
                };


                try
                {
                    System.Diagnostics.Trace.WriteLine("Record Metadata URL Details: {0}", record.Data.Metadataurl);
                    responseRecord.Data = new OutputRecord.OutputRecordData();
                    var metadata = await svc.GetMetadata(new Uri(record.Data.Metadataurl));

                    responseRecord.Data.ACLS     = "";
                    responseRecord.Data.SPWebUrl = metadata.SPWebUrl;
                    responseRecord.Data.CreatedAuthorDisplayName = metadata.CreatedAuthorDisplayName;
                    //responseRecord.Data.DocumentType = metadata.DocumentType;
                    //responseRecord.Data.Region = metadata.Region;
                    //responseRecord.Data.Country = metadata.Country;
                    //responseRecord.Data.AustraliaState = metadata.AustraliaState;
                    //responseRecord.Data.Asset = metadata.Asset;
                    responseRecord.Data.LinkFilename = metadata.LinkFilename;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.

                    System.Diagnostics.Trace.TraceInformation("Something [info] bad happened {0}", e.Message.ToString());
                    System.Diagnostics.Trace.TraceError("Something [error] bad happened {0}", e.Message.ToString());
                    System.Diagnostics.Trace.WriteLine("Something [error] bad happened {0}", e.Message.ToString());
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.InnerException.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.Values.Add(responseRecord);
                }
            }

            return((ActionResult) new OkObjectResult(response));
        }
コード例 #5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Custom Search function: C# HTTP trigger function processed a request.");

            var response = new WebApiResponse
            {
                Values = new List <OutputRecord>()
            };

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            var    data        = JsonConvert.DeserializeObject <WebApiRequest>(requestBody);

            log.LogInformation("Custom Search function: Request is validated." + data);
            // Do some schema validation
            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema. Could not find values array."));
            }
            log.LogInformation("Custom Search function: Starting the record .");
            // Calculate the response for each value.
            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };

                try
                {
                    responseRecord.Data = GetEntityMetadata(record.Data.Name, log).Result;
                    log.LogInformation("Custom Search function: Response record." + responseRecord.Data.ToString());
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };

                    log.LogError("Custom Search function: Error message." + error.Message);
                }
                finally
                {
                    response.Values.Add(responseRecord);
                }
            }

            return((ActionResult) new OkObjectResult(response));
        }
コード例 #6
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;

            log.LogInformation("Custom skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            // Calculate the response for each value.
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    responseRecord.Data = GetPosition(record.Data).Result;


                    if (responseRecord.Data != null && responseRecord.Data.Results != null && responseRecord.Data.Results.Count > 0)
                    {
                        var firstPoint = responseRecord.Data.Results[0];

                        if (firstPoint.Position != null)
                        {
                            responseRecord.Data.MainGeoPoint = new OutputRecord.EdmGeographPoint(
                                Convert.ToDouble(firstPoint.Position.lat),
                                Convert.ToDouble(firstPoint.Position.lon));
                        }
                    }
                }
                catch (Exception e)
                {
                    log.LogInformation($"{skillName}: Error {e.Message}.");

                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return(new OkObjectResult(response));
        }
コード例 #7
0
        public static IActionResult RunHocrGenerator([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;

            log.LogInformation($"{skillName}: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    log.LogInformation($"{skillName}: List was received {record.Data["ocrImageMetadataList"]}.");

                    List <OcrImageMetadata> imageMetadataList = JsonConvert.DeserializeObject <List <OcrImageMetadata> >(JsonConvert.SerializeObject(record.Data["ocrImageMetadataList"]));

                    List <HocrPage> pages = new List <HocrPage>();

                    for (int i = 0; i < imageMetadataList.Count; i++)
                    {
                        pages.Add(new HocrPage(imageMetadataList[i], i));
                    }

                    HocrDocument hocrDocument = new HocrDocument(pages);
                    responseRecord.Data["hocrDocument"] = hocrDocument;
                }
                catch (Exception e)
                {
                    log.LogInformation($"{skillName}: Error {e.Message}.");

                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }
            return(new OkObjectResult(response));
        }
コード例 #8
0
        public static async Task <IActionResult> RunImageStore(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;

            log.LogInformation($"{skillName}: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }

            string blobStorageConnectionString = Environment.GetEnvironmentVariable("BlobStorageAccountConnectionString", EnvironmentVariableTarget.Process);
            string blobContainerName           = Environment.GetEnvironmentVariable("BlobContainerName", EnvironmentVariableTarget.Process);

            if (String.IsNullOrEmpty(blobStorageConnectionString) || String.IsNullOrEmpty(blobContainerName))
            {
                return(new BadRequestObjectResult($"{skillName} - Information for the blob storage account is missing"));
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobStorageConnectionString);
            var blobClient       = storageAccount.CreateCloudBlobClient();
            var libraryContainer = blobClient.GetContainerReference(blobContainerName);

            // Calculate the response for each value.
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    var blockBlob = libraryContainer.GetBlockBlobReference(Guid.NewGuid().ToString());
                    if (!await blockBlob.ExistsAsync())
                    {
                        using (var stream = new MemoryStream(Convert.FromBase64String(record.Data["imageData"].ToString())))
                        {
                            await blockBlob.UploadFromStreamAsync(stream);

                            blockBlob.Properties.ContentType = "image/jpg";
                            await blockBlob.SetPropertiesAsync();
                        }
                    }

                    log.LogInformation($"{skillName}: Saving image to {blockBlob.Uri.ToString()}.");

                    responseRecord.Data["imageStoreUri"] = blockBlob.Uri.ToString();
                }
                catch (Exception e)
                {
                    log.LogInformation($"{skillName}: Error {e.Message}.");

                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return(new OkObjectResult(response));
        }
コード例 #9
0
ファイル: DataProcessor.cs プロジェクト: dem108/msrpa
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Data Processor skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            var storageConnectionString = Environment.GetEnvironmentVariable("StorageContainerString");
            var entityTable             = Environment.GetEnvironmentVariable("EntityTableName");
            var cosmosUri       = Environment.GetEnvironmentVariable("CosmosUri");
            var cosmosKey       = Environment.GetEnvironmentVariable("CosmosKey");
            var cosmosDbId      = Environment.GetEnvironmentVariable("CosmosDbId");
            var cosmosContainer = Environment.GetEnvironmentVariable("CosmosContainer");

            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };

                var rootObject = new OutputRecord.OutputRecordData
                {
                    FormDoc = new List <FormDoc>()
                };

                try
                {
                    log.LogInformation("Data Processor skill: Process record : {0}", record.Data.Url);

                    var storageAccount  = CreateStorageAccountFromConnectionString(storageConnectionString);
                    var sourceBlob      = new CloudBlob(new Uri(record.Data.Url), storageAccount.Credentials);
                    var sourceContainer = sourceBlob.Container.Name;
                    var sourceFilePath  = sourceBlob.Name;
                    var sourceFileName  = sourceFilePath.Replace(sourceBlob.Parent.Prefix, "").Replace(".pdf", "");

                    var json    = string.Empty;
                    var formDoc = await ProcessData(storageConnectionString, entityTable,
                                                    sourceFileName, sourceContainer, log);

                    rootObject.FormDoc = formDoc;

                    json = JsonConvert.SerializeObject(formDoc, Formatting.Indented);
                    log.LogInformation("Json Value : " + json);

                    WriteToCosmos(formDoc, cosmosUri, cosmosDbId,
                                  cosmosContainer, cosmosKey);

                    rootObject.FormDocJson = json;
                    responseRecord.Data    = rootObject;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    log.LogInformation("Data Processor skill: C# Exception : {0}", e.Message);

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            log.LogInformation($"Completed");

            return(new OkObjectResult(response));
        }
コード例 #10
0
        public static IActionResult RunHeaderExtraction([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;

            log.LogInformation($"{skillName}: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord();
                responseRecord.RecordId = record.RecordId;

                try
                {
                    var fileLocation = string.Format("{0}?{1}", record.Data["path"], record.Data["token"].ToString().StartsWith("?") ? record.Data["token"].ToString().Substring(1) : record.Data["token"]);
                    using (WebClient webConnection = new WebClient())
                    {
                        using (var mStream = new MemoryStream(webConnection.DownloadData(fileLocation)))
                        {
                            if (record.Data["contentType"].ToString() == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
                            {
                                using (var doc = WordprocessingDocument.Open(mStream, false))
                                {
                                    var paragraphs = doc.MainDocumentPart.Document.Body
                                                     .OfType <Paragraph>()
                                                     .Where(p => p.ParagraphProperties != null &&
                                                            p.ParagraphProperties.ParagraphStyleId != null &&
                                                            p.ParagraphProperties.ParagraphStyleId.Val.Value.Contains("Heading")).ToList();

                                    //var allStyles = DocumentFormat.OpenXml.Wordprocessing.
                                    responseRecord.Data["headings"] = paragraphs.Select(a => a.InnerText).ToList();
                                }
                            }
                            responseRecord.Data["headings"] = null;
                        }
                    }
                }
                catch (Exception e)
                {
                    log.LogInformation($"{skillName}: Error {e.Message}");

                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }
            return(new OkObjectResult(response));
        }