Skip to content

Instantly share code, notes, and snippets.

@aribornstein
Created March 15, 2020 12:10
Show Gist options
  • Save aribornstein/7eecf911506d89f9cfef4abbc51dfbea to your computer and use it in GitHub Desktop.
Save aribornstein/7eecf911506d89f9cfef4abbc51dfbea to your computer and use it in GitHub Desktop.
Python Form Recognizer Async Layout
########### Python Form Recognizer Async Analyze #############
import json
import time
from requests import get, post
# Endpoint URL
endpoint = r"<endpoint>"
apim_key = "<subsription key>"
model_id = "<model_id>"
post_url = endpoint + "/formrecognizer/v2.0-preview/custom/models/%s/analyze" % model_id
source = r"<file path>"
params = {
"includeTextDetails": True
}
headers = {
# Request headers
'Content-Type': '<file type>',
'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()
print("POST analyze succeeded:\n%s" % resp.headers)
get_url = resp.headers["operation-location"]
except Exception as e:
print("POST analyze failed:\n%s" % str(e))
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment