How to get the results of a custom form-classification model with Java.

Erik N 0 Reputation points
2023-04-07T17:26:20.78+00:00

I am trying to call out to a custom classification model in Azure form recognizer in Java with no success. Is there a Java sample available yet for the custom form classification preview, or an idea of when the API may be ready for full use? When I use the form-recognizer studio, I am pointed to the message below.
""" The code sample for Custom Classification Model operation will be coming soon. """ I have tried utilizing the API 3.0 documentation, but it appears to be more suited for custom document models and not the document classification models. Ideally, I was hoping there would be a similar approach to the code found here: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AnalyzeTaxW2.java Thanks in advance for the help.

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
2,104 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 53,966 Reputation points Moderator
    2023-04-09T09:36:26.4366667+00:00

    Hello @Erik N
    Thanks for reaching out to us. For Custom Classification Model, the JAVA SDK has not been released yet, so there is no working example for it. I will forward this feedback to product team and request the code sample to come soon. For API, could you please try if below code sample works or not? 1.Classify document with document classifier.

    // // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
    import java.net.URI;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class JavaSample 
    {
        public static void main(String[] args) 
        {
            HttpClient httpclient = HttpClients.createDefault();
    
            try
            {
                URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/formrecognizer/documentClassifiers/{classifierId}:analyze?api-version=2023-02-28-preview");
    
                builder.setParameter("pages", "{string}");
                builder.setParameter("locale", "{string}");
                builder.setParameter("stringIndexType", "textElements");
    
                URI uri = builder.build();
                HttpPost request = new HttpPost(uri);
                request.setHeader("Content-Type", "application/json");
                request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}");
    
    
                // Request body
                StringEntity reqEntity = new StringEntity("{body}");
                request.setEntity(reqEntity);
    
                HttpResponse response = httpclient.execute(request);
                HttpEntity entity = response.getEntity();
    
                if (entity != null) 
                {
                    System.out.println(EntityUtils.toString(entity));
                }
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    
    

    2.Gets the result of document classifier.

    // // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
    import java.net.URI;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class JavaSample 
    {
        public static void main(String[] args) 
        {
            HttpClient httpclient = HttpClients.createDefault();
    
            try
            {
                URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/formrecognizer/documentClassifiers/{classifierId}/analyzeResults/{resultId}?api-version=2023-02-28-preview");
    
    
                URI uri = builder.build();
                HttpGet request = new HttpGet(uri);
                request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}");
    
    
                // Request body
                StringEntity reqEntity = new StringEntity("{body}");
                request.setEntity(reqEntity);
    
                HttpResponse response = httpclient.execute(request);
                HttpEntity entity = response.getEntity();
    
                if (entity != null) 
                {
                    System.out.println(EntityUtils.toString(entity));
                }
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    
    

    I hope this helps. Regards, Yutong -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.