python sdk can't find custom classification model, can find custom extraction model

Sarah Cummings 45 Reputation points
2023-07-12T19:10:32.5233333+00:00

I created both an Azure Custom Classification Model and and Azure Custom Extraction model in Form Recognizer Studio, and I am satisfied with the results. Now, I want to generate these results in python.

The python SDK document_analysis_client.begin_analyze_document() function works great for my extraction model, but for some reason when i switch out the model_id for my classification model, I get:

ResourceNotFoundError: (NotFound) Resource not found.
Code: NotFound
Message: Resource not found.
Inner error: {
    "code": "ModelNotFound",
    "message": "The requested model was not found."
}



I have tried both sdk version 3.2.1 and 3.3.0b1, and get the same error with each. Why can't I find and use my classification model?

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,396 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Sarah Cummings 45 Reputation points
    2023-12-22T15:58:09.01+00:00

    I ended up using custom python code to use my classification model:

    def _post_to_classification_model(pdf_bytes: bytes) -> dict:
        """
        Using configured form recognizer key and model specifications from config,
        post the pdf to the and azure ai classification model for prediction.
        Returns the post response.
        """
    
        FORM_RECOGNIZER_KEY = os.getenv("FORM_RECOGNIZER_KEY")
    
        post_url = (
            ENDPOINT
            + f"/formrecognizer/{API_TYPE}/{FACEPAGE_CLASSIFICATION_MODEL_ID}:analyze?api-version={API_VERSION}"
        )
        params = {"includeTextDetails": True}
    
        headers = {
            # Request headers
            "Content-Type": "application/pdf",
            "Ocp-Apim-Subscription-Key": FORM_RECOGNIZER_KEY,
        }
        logger.debug(f"FORM REC KEY IS: {FORM_RECOGNIZER_KEY}")
        try:
            resp = post(
                url=post_url, data=pdf_bytes, headers=headers, params=params
            )
            if resp.status_code != 202:
                logger.warning(
                    "POST analyze failed:\n%s" % json.dumps(resp.json())
                )
                quit()
            logger.info("POST analyze succeeded:\n%s" % resp.headers)
        except Exception as e:
            logger.warning("POST analyze failed:\n%s" % str(e))
    
        return resp
    
    
    def _get_classification_results(post_response: dict) -> dict:
        """
        Given our response from our post request for classification,
        retrieve the classificaiton results. Returns the get response.
        """
    
        get_url = post_response.headers["operation-location"]
    
        n_tries = 15
        n_try = 0
        wait_sec = 5
        max_wait_sec = 60
        resp_json = None
    
        while n_try < n_tries:
            try:
                resp = get(
                    url=get_url,
                    headers={
                        "Ocp-Apim-Subscription-Key": os.getenv(
                            "FORM_RECOGNIZER_KEY"
                        )
                    },
                )
                resp_json = resp.json()
                if resp.status_code != 200:
                    logger.warning(
                        "GET analyze results failed:\n%s" % json.dumps(resp_json)
                    )
                    break
                status = resp_json["status"]
                if status == "succeeded":
                    logger.info("Analysis succeeded:\n%s" % json.dumps(resp_json))
                    break
                if status == "failed":
                    logger.warning("Analysis failed:\n%s" % json.dumps(resp_json))
                    break
                # Analysis still running. Wait and retry.
                time.sleep(wait_sec)
                n_try += 1
                wait_sec = min(2 * wait_sec, max_wait_sec)
    
            except Exception as e:
                msg = "GET analyze results failed:\n%s" % str(e)
                logger.warning(msg)
                break
    
        return resp_json
    
    0 comments No comments