Issues with form recognizer API

Cloud DevOps 1 Reputation point
2022-06-09T20:33:06.43+00:00

Recently MS retired the version of the form recognizer service we have been using.
Have been unable to post new pdf’s. Wondering if anyone can see any issues with the code:

##### Python Form Recognizer Async Analyze v3.0#############

import json
import time
from requests import get, post
import pandas as pd
import objectpath
import numpy as np
import http.client, urllib.request, urllib.parse, urllib.error, base64

def Analyze_pdf(source,model_id):

Endpoint URL
endpoint = “https://companyformrecognizer.cognitiveservices.azure.com/”
model_id = ‘model1’
apim_key = ‘apikey1’
post_url = endpoint + “formrecognizer/documentModels/%s:analyze” % model_id
parms = {
“urlSource”: “string”
}
headers = {
‘Content-Type’: ‘application/pdf’,
‘Ocp-Apim-Subscription-Key’: apim_key,
}
with open(source, “rb”) as f:
data_bytes = f.read()
try:
resp = post(url=post_url, data=data_bytes, headers=headers, params=params)
if resp.status_code !=202:
print(“POST analyze failed:\n%s” % json.dumps(resp.json()))
quit()
get_url = resp.headers[“operation-location”]
n_tries = 15
n_try = 0
wait_sec = 5
max_wait_sec = 60
while n_try < n_tries:
try:
resp = get(url=get_url,headers={“Ocp-Apim-Subscription-Key”: apim_key})
resp_json = resp.json()
status = resp_json[“status”]
if status == “succeeded”:
return resp_json
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)
print(msg)
quit()
print(“Analyze operation did not complete within the allocated time.”)
except Exception as e:
print(“POST analyze failed:\n%s” % str(e))
quit()

def Read_json(path, encoding=None):
if not encoding:
encoding = “utf8”
with open(path, encoding=encoding) as f:
data = json.load(f)
f.close()
return data

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
23,971 questions
{count} votes