RPA.DocumentAI.Nanonets

module RPA.Nanonets

class RPA.DocumentAI.Nanonets.Nanonets

Library to support Nanonets service for intelligent document processing (IDP).

Library requires at the minimum rpaframework version 19.0.0.

Service supports identifying fields in the documents, which can be given to the service in multiple different file formats and via URL.

Robot Framework example usage

Python example usage

from RPA.DocumentAI.Nanonets import Nanonets from RPA.Robocorp.Vault import Vault secrets = Vault().get_secret("nanonets-auth") nanolib = Nanonets() nanolib.set_authorization(secrets["apikey"]) result = nanolib.predict_file(file_to_scan, secrets["receipts-model-id"]) fields = nanolib.get_fields_from_prediction_result(result) for field in fields: print(f"Label: {field['label']} Text: {field['ocr_text']}") tables = nanolib.get_tables_from_prediction_result(result) for table in tables: rpatable = Tables().create_table(table["rows"]) for row in table["rows"]: cells = [cell["text"] for cell in row] print(f"ROW: {' | '.join(cells)}")

variable ROBOT_LIBRARY_DOC_FORMAT

ROBOT_LIBRARY_DOC_FORMAT = 'REST'

variable ROBOT_LIBRARY_SCOPE

ROBOT_LIBRARY_SCOPE = 'GLOBAL'

method get_all_models

get_all_models()

Get all available models related to the API key.

  • Returns: object containing available models

Robot Framework example:

Python example:

models = nanolib.get_all_models() for model in models: print(f"model id: {model['model_id']}") print(f"model type: {model['model_type']}")

method get_fields_from_prediction_result

get_fields_from_prediction_result(prediction: Optional[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]])

Helper keyword to get found fields from a prediction result.

For example. see Predict File keyword

  • Parameters: prediction โ€“ prediction result dictionary
  • Returns: list of found fields

method get_tables_from_prediction_result

get_tables_from_prediction_result(prediction: Optional[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]])

Helper keyword to get found tables from a prediction result.

For another example. see Predict File keyword

  • Parameters: prediction โ€“ prediction result dictionary
  • Returns: list of found tables

Robot Framework example:

Python example:

# It is possible to create ``RPA.Tables`` compatible tables from the result tables = nanolib.get_tables_from_prediction_result(result) for table in tables: rpatable = Tables().create_table(table["rows"]) for row in rpatable: print(row)

method ocr_fulltext

ocr_fulltext(filename: str, filepath: str)

OCR fulltext a given file. Returns words and full text.

Filename and filepath needs to be given separately.

Parameters
  • filename โ€“ name of the file
  • filepath โ€“ path of the file
  • Returns: the result in a list format

Robot Framework example:

Python example:

results = nanolib.ocr_fulltext("IMG_8277.jpeg", "./IMG_8277.jpeg") for result in results: print(f"FILENAME: {result['filename']}") for page in result["page_data"]: print(f"Page {page['page']+1}: {page['raw_text']}")

method predict_file

predict_file(filepath: str, model_id: str)

Get prediction result for a file by a given model id.

Parameters
  • filepath โ€“ filepath to the file
  • model_id โ€“ id of the Nanonets model to categorize a file
  • Returns: the result in a list format

Robot Framework example:

Python example:

result = nanolib.predict_file("./docu.pdf", secrets["receipts-model-id"]) fields = nanolib.get_fields_from_prediction_result(result) for field in fields: print(f"Label: {field['label']} Text: {field['ocr_text']}") tables = nanolib.get_tables_from_prediction_result(result) for table in tables: for row in table["rows"]: cells = [cell["text"] for cell in row] print(f"ROW: {' | '.join(cells)}")

method set_authorization

set_authorization(apikey: str)

Set Nanonets request headers with key related to API.

  • Parameters: apikey โ€“ key related to the API

Robot Framework example:

Python example:

secrets = Vault().get_secret("nanonets-auth") nanolib = Nanonets() nanolib.set_authorization(secrets["apikey"])