module RPA.DocumentAI

class RPA.DocumentAI.DocumentAI.DocumentAI

Wrapper library offering generic keywords for initializing, scanning and retrieving results as fields from documents (PDF, PNG etc.).

Library requires at the minimum rpaframework version 19.0.0.

This is a helper facade for the following libraries:

  • RPA.Cloud.Google (requires rpaframework-google)
  • RPA.DocumentAI.Base64AI
  • RPA.DocumentAI.Nanonets

Where the following steps are required:

  1. Engine initialization: Init Engine
  2. Document scan: Predict
  3. Result retrieval: Get Result

So no matter the engine you’re using, the very same keywords can be used, as only the passed parameters will differ (please check the docs on each library for particularities). Once initialized, you can jump between the engines with Switch Engine. Before scanning documents, you must configure the service first, with a model to scan the files with and an API key for authorizing the access.

See Portal example: https://robocorp.com/portal/robot/robocorp/example-document-ai

Example: Robot Framework

Example: Python

from RPA.DocumentAI import DocumentAI, EngineName lib_docai = DocumentAI() lib_docai.init_engine( EngineName.GOOGLE, vault="document_ai:serviceaccount", region="eu" ) lib_docai.predict( "invoice.pdf", model="df1d166771005ff4", project_id="complete-agency-347912", region="eu" ) print(lib_docai.get_result())







property engine

property engine: Any

method get_result

get_result(extended: bool = False)

Retrieve the result data previously obtained with Predict.

The stored raw result is usually pre-processed with a library specific keyword prior the return.

  • Parameters: extended – Get all the details inside the result data. (main fields only by default)
  • Returns: Usually a list of fields detected in the document.

Example: Robot Framework

Example: Python

result = lib_docai.get_result() for field in result: print(field)

method init_engine

init_engine(name: Union[EngineName, str], secret: Optional[Union[str, Path, Tuple, List, Dict]] = None, vault: Optional[Union[Dict, str]] = None, **kwargs)

Initialize the engine you want to scan documents with.

This is required before being able to run Predict. Once initialized, you don’t need to run this again, simply use Switch Engine to jump between the engines. The final secret value (passed directly with secret or picked up automatically from the Vault with vault) will be split into authorization args and kwargs or just passed as it is to the wrapped library. Keep in mind that some engines are expecting API keys where others tokens or private keys. Any optional keyword argument will be passed further in the wrapped library.

  • name – Name of the engine.
  • secret – Authenticate with a string/file/object secret directly.
  • vault – Specify the Vault storage name and secret key in order to authenticate. (‘name:key’ or {name: key} formats are supported)

How secret resolution works

When vault is passed in, the corresponding Vault is retrieved and the value belonging to specified field is returned as a secret. If a secret is used, then this value is returned as it is if this isn’t a path pointing to the file holding the value to be returned. We’ll be relying on environment variables in the absence of both the secret and vault.

Expected secret value formats:

  • google: <json-service/token> (RPA.Cloud.Google.Init Document AI)
  • base64ai: , (RPA.DocumentAI.Base64AI.Set Authorization)
  • nanonets: (RPA.DocumentAI.Nanonets.Set Authorization)

Example: Robot Framework

Example: Python

from RPA.DocumentAI import DocumentAI from RPA.Robocorp.Vault import Vault lib_docai = DocumentAI() mail_apikey = Vault().get_secret("document_ai")["base64ai"] lib_docai.init_engine("base64ai", secret=mail_apikey)

method predict

predict(location: Union[Path, str], model: Optional[Union[str, List[str]]] = None, **kwargs)

Scan a document with the currently active engine and store the result internally for a later retrieval.

Based on the selected engine, this wraps a chain of libraries until calling a service API in the end, where the passed file is analyzed. Any optional keyword argument will be passed further in the wrapped library. (some engines require mandatory parameters like project ID or region)

  • location – Path to a local file or URL address of a remote one. (not all engines work with URLs)
  • model – Model name(s) to scan with. (some engines guess the model if not specified)

Example: Robot Framework

Example: Python

lib_docai.predict("local/path/to/invoice.png", model="finance/invoice")

property result

property result: Union[Dict[Hashable, Optional[Union[str, int, float, bool, list, dict]]], List[Optional[Union[str, int, float, bool, list, dict]]], str, int, float, bool, list, dict, None, Document]

method switch_engine

switch_engine(name: Union[EngineName, str])

Switch between already initialized engines.

Use this to jump between engines when scanning with multiple of them.

  • Parameters: name – Name of the engine to be set as active. (choose between: google, base64ai, nanonets)

Example: Robot Framework

Example: Python

lib_docai.switch_engine("base64ai") lib_docai.predict("invoice.png")