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
- You have registered an account with Huawei Cloud and completed real-name authentication. Your account cannot be in arrears, frozen, or deregistered.
- You have learned the constraints on Text Moderation.
- You have subscribed to the Text Moderation service.
Procedure
- Install the Python environment and obtain the SDK.
- 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.
- Download the latest version of PyCharm from PyCharm's official website.
- Start the PyCharm development tool and choose to configure the Python environment.
- 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.
- 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
- 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)
- 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.
- 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
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot