Form recognizer custom model - how to use my python parser with the API GET response
Hi,
I'm making a solution who extract pdf content through a Form recognizer Custom model and add the result to a SQL server database.
With the help of the JSON delivered by the Form recognizer UI, i built a python JSON parser who fits to my custom model and successfully add rows to SQL server when i give my function a variable who is the result of the json.load('example.json').
Now, i try to give in argument of my parsing function the "result" generated by the API GET response but failed to make it work. I try many solution but always fall on almost the same error:
[2022-10-02T08:12:40.755Z] System.Private.CoreLib: Exception while executing function: Functions.BlobTrigger1. System.Private.CoreLib: Result: Failure
Exception: TypeError: 'AnalyzeResult' object is not subscriptable
Here is how my parser is working (start of the code):
def insert_json_into_mssql(result_json):
try:
analyzeResult = result_json["analyzeResult"]
documents_list = analyzeResult["documents"]
connection_string = "Driver={ODBC Driver 17 for SQL Server};Server=tcp:xxxxxxserverdev.database.windows.net,1433;Database=nip_facturation_dev;Uid=xxxxxxxx;Pwd=xxxxxxxxxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
mssql_con = pyodbc.connect(connection_string)
mssql_con.setdecoding(pyodbc.SQL_CHAR, encoding='UTF-8')
mssql_con.setencoding('UTF-8')
cursor = mssql_con.cursor()
x = 0
for doc in documents_list :
x = x+1
print("Processing document index "+str(x))
fields = doc["fields"]
if "enseigne" in fields:
enseigneO = fields["enseigne"]
enseigne = enseigneO["content"]
print("enseigne= "+str(enseigne))
else:
enseigne = None
print("enseigne= "+str(enseigne))
and that's how i call the api and get the result:
def main(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob \n"
f"Name: {myblob.name}\n"
f"Blob Size: {myblob.length} bytes")
endpoint = "https://westeurope.api.cognitive.microsoft.com/"
api_key = "xxxxxxxxxxxxxxxxxxxxxxx"
credential = AzureKeyCredential(api_key)
source = myblob.read()
model_id = "my_model"
credential = AzureKeyCredential(api_key)
document_analysis_client = DocumentAnalysisClient(endpoint, credential)
poller = document_analysis_client.begin_analyze_document(model_id, document=source)
result_json = poller.result()
insert_json_into_mssql(result_json)
I know that i miss a step between the API get response and the way i fill the result to my parser.
Ideally, i would like to to be able to read the response without writing the result as a json file in storage blob.
Thanks :)