Updated on 2023-01-04 GMT+08:00

Calling APIs or SDKs Locally

Content Moderation Software Development Kit (Moderation SDK for short) encapsulates the RESTful APIs provided by Content Moderation to simplify application development. You can add dependencies or download SDKs to call APIs to use Content Moderation.

This section uses Text Moderation as an example to describe how to use the Moderation Python SDK for local development. You can use the SDK by calling API functions.

Prerequisites

Procedure

  1. Install the Python environment and obtain the SDK.
    1. Download Python of a proper version from Python's official website and install it. Python 3.3 or later is recommended. This section uses Python 3.7 as an example.
    2. Download the latest version of PyCharm from PyCharm's official website.
    3. Start the PyCharm development tool and choose File > Settings > Project Interpreter to configure the Python environment.
    4. Select the Python installation path. See Figure 1. After selecting the target Python, click Apply at the bottom of the page to complete the configuration.
      Figure 1 Configuring the python environment using PyCharm

  2. Create a project in PyCharm and click Terminal in the lower left corner. Run the following commands to install the SDK (the SDK supports Python 3 or later):
    Install the SDK using pip commands:
    # Install the core library.
    pip install huaweicloudsdkcore
    
    # Install the moderation service library.
    pip install huaweicloudsdkmoderation

    On PyCharm, choose File > Settings > Project > Python Interpreter. Click + in the upper right corner, search for huaweicloudsdkcore and huaweicloudsdkmoderation respectively, and click Install Package in the lower left corner to install them.

    Figure 2 Installing the Python SDK for Content Moderation using PyCharm
  3. Copy the SDK sample code of Text Moderation to PyCharm as follows:
    # coding: utf-8
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkmoderation.v2.region.moderation_region import ModerationRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkmoderation.v2 import *
    if __name__ == "__main__":
        //Enter your AK/SK.
        ak = "<YOUR AK>"
        sk = "<YOUR SK>"
        credentials = BasicCredentials(ak, sk) \
        client = ModerationClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(ModerationRegion.value_of("ap-southeast-1")) \  
            .build()
    try:
            request = RunTextModerationRequest()
            listItemsbody = [
                TextDetectionItemsReq(
                    text="asdfasdfasdf"   //Enter the text to be detected, for example, asdfasdfasdf.
                )
            ]
            request.body = TextDetectionReq(
                items=listItemsbody
            )
            response = client.run_text_moderation(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
  4. Obtain the AK and SK and replace <YOUR AK> and <YOUR SK> in the sample code with the AK and SK, respectively.

    Log in to the My Credentials page, choose Access Keys in the navigation pane on the left, and click Create Access Key in the right pane.

  5. Run the sample code to obtain the recognition result. You can interpret the review result based on the response parameter description. For details, see Text Moderation Result.
    Figure 3 Example