Search code examples
.netazureartificial-intelligenceazure-form-recognizer

AnalyzeDocumentAsync (Azure Document Intelligence) is throwing a 404 exception


I have the following code:

public async Task TestExtractionDataAsync()
{
    _documentAnalysisClient = new DocumentIntelligenceClient(new Uri(endpoint), new AzureKeyCredential(key));

    var documentUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf");

    var analyzeDocumentContent = new AnalyzeDocumentContent()
{
    UrlSource = documentUri 
};

    var operation = await _documentAnalysisClient.AnalyzeDocumentAsync(WaitUntil.Completed, "Test_6_Jornalera", analyzeDocumentContent);
}

And the method 'AnalyzeDocumentAsync' is throwing a '404 Not Found' exception. The file url is valid. The endpoint and the key are the sames from here: Key and endpoint

And from the ModelId is extracted from here: Models Ids

One thing I noticed is that in the Document Intelligence section in Azure AI my project doesn't appear: DIS section

I created the Document Intelligence project from here: Created in 1 Created in 2

Any ideas of what could be happening? I don't know why the method throws a 404. All the elements, urls and keys are on place.

I tried the documentation step by steps but it seems to be obsolete (Use Document Intelligence models).


Solution

  • Any ideas of what could be happening? I don't know why the method throws a 404. All the elements, urls and keys are on place.

    Here I have created an application to interact with the Azure Form Recognizer service.

    FormRecognizerService.cs:

    using Azure;
    using Azure.AI.FormRecognizer;
    using Azure.AI.FormRecognizer.Models;
    using System;
    using System.Threading.Tasks;
    
    namespace MyFormRecognizerApp
    {
        public class FormRecognizerService
        {
            private readonly FormRecognizerClient _formRecognizerClient;
    
            public FormRecognizerService(string endpoint, string key)
            {
                var credential = new AzureKeyCredential(key);
                _formRecognizerClient = new FormRecognizerClient(new Uri(endpoint), credential);
            }
    
            public async Task AnalyzeDocumentAsync(Uri documentUri, string modelName)
            {
                try
                {
                    // Start the custom form recognition operation
                    RecognizeCustomFormsOperation operation = await _formRecognizerClient.StartRecognizeCustomFormsFromUriAsync(modelName, documentUri);
    
                    // Wait for the operation to complete
                    await operation.WaitForCompletionAsync();
    
                    // Get the response
                    Response<RecognizedFormCollection> response = await operation.WaitForCompletionAsync();
    
                    // Check if the operation was successful
                    if (response.GetRawResponse().Status == 200)
                    {
                        RecognizedFormCollection recognizedForms = response.Value;
                        // Process the recognized forms
                        foreach (RecognizedForm form in recognizedForms)
                        {
                            Console.WriteLine($"Form of type: {form.FormType}");
                            foreach (FormField field in form.Fields.Values)
                            {
                                Console.WriteLine($"INVOICE: {form.Fields["INVOICE"].Value}");
                                Console.WriteLine($"DATE: {form.Fields["DATE"].Value}");
                            }
                        }
                    }
                    else
                    {
                        throw new RequestFailedException("The operation did not complete successfully.");
                    }
                }
                catch (RequestFailedException ex)
                {
                    Console.WriteLine($"Error analyzing document: {ex.Message}");
                }
            }
        }
    }
    

    Program.cs:

    using System;
    using System.Threading.Tasks;
    
    namespace MyFormRecognizerApp
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                string endpoint = "https://your-Document intelligence.cognitiveservices.azure.com/";
                string key = "key";
                string modelName = "modelID";
    
                var service = new FormRecognizerService(endpoint, key);
    
                Uri documentUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf\r\n");
    
                await service.AnalyzeDocumentAsync(documentUri, modelName);
            }
        }
    }
    

    This is my Document intelligence resource. enter image description here

    • Below is my model by taking a sample invoice to test and I have trained.

    enter image description here

    • Here my task is to analyse the document and get the below fields recognise and should be print in the console.

    enter image description here

    enter image description here