Updated on 2025-07-03 GMT+08:00

Calling an API 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. Obtain the SDK sample code for text content moderation.
    1. Log in to the API Explorer and click Python on the Sample Code tab page.
      Figure 3 Sample code

    2. Set the request body parameters event_type (event type) and text (text to be detected).

      For example, enter comment for event_type and enter the text to be detected for text.

      Figure 4 Setting parameters

    3. Copy the sample code to PyCharm.
  4. Obtain the AK and SK and replace <CLOUD_SDK_AK> and <CLOUD_SDK_SK> in the sample code with the AK and SK, respectively.

    Log in to the Access Keys page and create an access key or use an existing access key. The AK and SK are contained in the credentials.csv file.

  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 5 Example