Created
March 15, 2020 12:10
-
-
Save aribornstein/7eecf911506d89f9cfef4abbc51dfbea to your computer and use it in GitHub Desktop.
Python Form Recognizer Async Layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########### 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