Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 3.0.1 (Unreleased)
## 3.1.0b1 (Unreleased)


## 3.0.0 (2020-08-20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@


class FormRecognizerApiVersion(str, Enum):
"""Form Recognizer service API versions supported by this package"""
"""Form Recognizer API versions supported by this package"""

V2_0 = "v2.0"
#: this is the default version
V2_1_PREVIEW_1 = "2.1-preview.1"
V2_0 = "2.0"


def validate_api_version(api_version):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
prepare_content_result,
prepare_form_result
)
from ._generated.models import AnalyzeOperationResult
from ._api_versions import validate_api_version
from ._helpers import get_content_type, get_authentication_policy, error_map, POLLING_INTERVAL
from ._api_versions import FormRecognizerApiVersion, validate_api_version
from ._helpers import _get_deserialize, get_content_type, get_authentication_policy, error_map, POLLING_INTERVAL
from ._user_agent import USER_AGENT
from ._polling import AnalyzePolling
if TYPE_CHECKING:
Expand Down Expand Up @@ -72,19 +71,22 @@ def __init__(self, endpoint, credential, **kwargs):

authentication_policy = get_authentication_policy(credential)
polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL)
api_version = kwargs.pop('api_version', None)
validate_api_version(api_version)
self.api_version = kwargs.pop('api_version', FormRecognizerApiVersion.V2_1_PREVIEW_1)
validate_api_version(self.api_version)
self._client = FormRecognizer(
endpoint=endpoint,
credential=credential, # type: ignore
api_version=self.api_version,
sdk_moniker=USER_AGENT,
authentication_policy=authentication_policy,
polling_interval=polling_interval,
**kwargs
)
self._deserialize = _get_deserialize()
self._generated_models = self._client.models(self.api_version)

def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_receipt(analyze_result)

@distributed_trace
Expand Down Expand Up @@ -123,24 +125,27 @@ def begin_recognize_receipts(self, receipt, **kwargs):
:dedent: 8
:caption: Recognize US sales receipt fields.
"""

locale = kwargs.pop("locale", None)
polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval)
continuation_token = kwargs.pop("continuation_token", None)
content_type = kwargs.pop("content_type", None)
include_field_elements = kwargs.pop("include_field_elements", False)
if content_type == "application/json":
raise TypeError("Call begin_recognize_receipts_from_url() to analyze a receipt from a URL.")

include_field_elements = kwargs.pop("include_field_elements", False)

cls = kwargs.pop("cls", self._receipt_callback)
polling = LROBasePolling(timeout=polling_interval, **kwargs)
if content_type is None:
content_type = get_content_type(receipt)

if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})

return self._client.begin_analyze_receipt_async( # type: ignore
file_stream=receipt,
content_type=content_type,
include_text_details=include_field_elements,
cls=kwargs.pop("cls", self._receipt_callback),
polling=LROBasePolling(timeout=polling_interval, **kwargs),
cls=cls,
polling=polling,
error_map=error_map,
continuation_token=continuation_token,
**kwargs
Expand Down Expand Up @@ -177,23 +182,26 @@ def begin_recognize_receipts_from_url(self, receipt_url, **kwargs):
:dedent: 8
:caption: Recognize US sales receipt fields from a URL.
"""

locale = kwargs.pop("locale", None)
polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval)
continuation_token = kwargs.pop("continuation_token", None)
include_field_elements = kwargs.pop("include_field_elements", False)

cls = kwargs.pop("cls", self._receipt_callback)
polling = LROBasePolling(timeout=polling_interval, **kwargs)
if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})
return self._client.begin_analyze_receipt_async( # type: ignore
file_stream={"source": receipt_url},
include_text_details=include_field_elements,
cls=kwargs.pop("cls", self._receipt_callback),
polling=LROBasePolling(timeout=polling_interval, **kwargs),
cls=cls,
polling=polling,
error_map=error_map,
continuation_token=continuation_token,
**kwargs
)

def _content_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_content_result(analyze_result)

@distributed_trace
Expand Down Expand Up @@ -325,7 +333,7 @@ def begin_recognize_custom_forms(self, model_id, form, **kwargs):
content_type = get_content_type(form)

def analyze_callback(raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_form_result(analyze_result, model_id)

deserialization_callback = cls if cls else analyze_callback
Expand Down Expand Up @@ -371,7 +379,7 @@ def begin_recognize_custom_forms_from_url(self, model_id, form_url, **kwargs):
include_field_elements = kwargs.pop("include_field_elements", False)

def analyze_callback(raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response)
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_form_result(analyze_result, model_id)

deserialization_callback = cls if cls else analyze_callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
TrainRequest,
TrainSourceFilter,
CopyRequest,
Model,
CopyOperationResult,
CopyAuthorizationResult
)
from ._helpers import error_map, get_authentication_policy, POLLING_INTERVAL, TransportWrapper
from ._helpers import (
error_map, get_authentication_policy, POLLING_INTERVAL, TransportWrapper, _get_deserialize
)
from ._models import (
CustomFormModelInfo,
AccountProperties,
CustomFormModel
CustomFormModel,
)
from ._polling import TrainingPolling, CopyPolling
from ._user_agent import USER_AGENT
from ._form_recognizer_client import FormRecognizerClient
from ._api_versions import validate_api_version
from ._api_versions import FormRecognizerApiVersion, validate_api_version
if TYPE_CHECKING:
from azure.core.credentials import AzureKeyCredential, TokenCredential
from azure.core.pipeline import PipelineResponse
Expand Down Expand Up @@ -85,16 +85,19 @@ def __init__(self, endpoint, credential, **kwargs):
self._credential = credential
authentication_policy = get_authentication_policy(credential)
polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL)
api_version = kwargs.pop('api_version', None)
validate_api_version(api_version)
self.api_version = kwargs.pop('api_version', FormRecognizerApiVersion.V2_1_PREVIEW_1)
validate_api_version(self.api_version)
self._client = FormRecognizer(
endpoint=self._endpoint,
credential=self._credential, # type: ignore
api_version=self.api_version,
sdk_moniker=USER_AGENT,
authentication_policy=authentication_policy,
polling_interval=polling_interval,
**kwargs
)
self._deserialize = _get_deserialize()
self._generated_models = self._client.models(self.api_version)

@distributed_trace
def begin_training(self, training_files_url, use_training_labels, **kwargs):
Expand Down Expand Up @@ -136,42 +139,66 @@ def begin_training(self, training_files_url, use_training_labels, **kwargs):
:caption: Training a model (without labels) with your custom forms.
"""

def callback(raw_response):
model = self._client._deserialize(Model, raw_response)
def callback_v2_0(raw_response):
model = self._deserialize(self._generated_models.Model, raw_response)
return CustomFormModel._from_generated(model)

def callback_v2_1(raw_response, _, headers): # pylint: disable=unused-argument
model = self._deserialize(self._generated_models.Model, raw_response)
return CustomFormModel._from_generated(model)

cls = kwargs.pop("cls", None)
continuation_token = kwargs.pop("continuation_token", None)
polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval)
deserialization_callback = cls if cls else callback

if continuation_token:
return LROPoller.from_continuation_token(
polling_method=LROBasePolling(timeout=polling_interval, lro_algorithms=[TrainingPolling()], **kwargs),
continuation_token=continuation_token,
client=self._client._client,
deserialization_callback=deserialization_callback

if self.api_version == "2.0":
deserialization_callback = cls if cls else callback_v2_0
if continuation_token:
return LROPoller.from_continuation_token(
polling_method=LROBasePolling(
timeout=polling_interval, lro_algorithms=[TrainingPolling()], **kwargs
),
continuation_token=continuation_token,
client=self._client._client,
deserialization_callback=deserialization_callback
)

response = self._client.train_custom_model_async( # type: ignore
train_request=TrainRequest(
source=training_files_url,
use_label_file=use_training_labels,
source_filter=TrainSourceFilter(
prefix=kwargs.pop("prefix", ""),
include_sub_folders=kwargs.pop("include_subfolders", False),
)
),
cls=lambda pipeline_response, _, response_headers: pipeline_response,
error_map=error_map,
**kwargs
) # type: PipelineResponseType

return LROPoller(
self._client._client,
response,
deserialization_callback,
LROBasePolling(timeout=polling_interval, lro_algorithms=[TrainingPolling()], **kwargs)
)

response = self._client.train_custom_model_async( # type: ignore
deserialization_callback = cls if cls else callback_v2_1
return self._client.begin_train_custom_model_async( # type: ignore
train_request=TrainRequest(
source=training_files_url,
use_label_file=use_training_labels,
source_filter=TrainSourceFilter(
prefix=kwargs.pop("prefix", ""),
include_sub_folders=kwargs.pop("include_subfolders", False),
)
),
),
cls=lambda pipeline_response, _, response_headers: pipeline_response,
cls=deserialization_callback,
continuation_token=continuation_token,
polling=LROBasePolling(timeout=polling_interval, lro_algorithms=[TrainingPolling()], **kwargs),
error_map=error_map,
**kwargs
) # type: PipelineResponseType

return LROPoller(
self._client._client,
response,
deserialization_callback,
LROBasePolling(timeout=polling_interval, lro_algorithms=[TrainingPolling()], **kwargs)
)

@distributed_trace
Expand Down Expand Up @@ -358,7 +385,7 @@ def begin_copy_model(
continuation_token = kwargs.pop("continuation_token", None)

def _copy_callback(raw_response, _, headers): # pylint: disable=unused-argument
copy_result = self._client._deserialize(CopyOperationResult, raw_response)
copy_result = self._deserialize(self._generated_models.CopyOperationResult, raw_response)
return CustomFormModelInfo._from_generated(copy_result, target["modelId"])

return self._client.begin_copy_custom_model( # type: ignore
Expand Down Expand Up @@ -395,6 +422,7 @@ def get_form_recognizer_client(self, **kwargs):
endpoint=self._endpoint,
credential=self._credential,
pipeline=_pipeline,
api_version=self.api_version,
**kwargs
)
# need to share config, but can't pass as a keyword into client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.0.6282, generator: {generator})
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Code generated by Microsoft (R) AutoRest Code Generator (autorest: 3.0.6282, generator: {generator})
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

from typing import TYPE_CHECKING
from typing import Any

from azure.core.configuration import Configuration
from azure.core.pipeline import policies

if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any

from azure.core.credentials import TokenCredential
from ._version import VERSION

VERSION = "unknown"

class FormRecognizerClientConfiguration(Configuration):
"""Configuration for FormRecognizerClient.
Expand Down Expand Up @@ -44,9 +43,8 @@ def __init__(

self.credential = credential
self.endpoint = endpoint
self.credential_scopes = ['https://cognitiveservices.azure.com/.default']
self.credential_scopes.extend(kwargs.pop('credential_scopes', []))
kwargs.setdefault('sdk_moniker', 'ai-formrecognizer/{}'.format(VERSION))
self.credential_scopes = kwargs.pop('credential_scopes', ['https://cognitiveservices.azure.com/.default'])
kwargs.setdefault('sdk_moniker', 'azure-ai-formrecognizer/{}'.format(VERSION))
self._configure(**kwargs)

def _configure(
Expand Down
Loading