- What's New
- Product Bulletin
- Service Overview
- Getting Started
- User Guide
- API Reference
-
SDK Reference
- Introduction to Moderation SDK
- Applying for a Service
- Obtaining Authentication Information
- Preparing the Environment
- Using the SDK (Java)
- Using the Python SDK
- Using the .NET SDK
- Using the Go SDK
-
FAQs
- Service Enabling
-
APIs
- How Do I Use Content Moderation APIs?
- What Should I Do When I Fail to Call a Content Moderation API?
- Can I Directly Upload a Word File for Text Moderation?
- What Are the Constraints on Concurrent API Calls?
- What Can I Do if the Request for Calling an API Times Out?
- How Do I Customize a Detection in Text Moderation?
- Can Content Moderation Traverse and Review the Content in an OBS Folder?
- Error Codes
- Monitoring
- Others
- Price Details
- Glossary
- General Reference
Show all
Copied.
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
- You have obtained a HUAWEI ID and completed real-name authentication. Your HUAWEI ID is not 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 File > Settings > Project Interpreter 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