SDK Calling Example

There are two service authentication modes: This section uses Image Moderation as an example to describe how to use AK/SK-based and token-based authentication. The corresponding URI is POST /v1.0/moderation/image.

Using SDK in AK/SK-based Authentication Mode

  1. Set app_key and app_secret in the moderation_image_aksk_demo.py file. The sample code is as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    if __name__ == '__main__':
        # Services currently support North China-Beijing(cn-north-4)
        init_global_env('cn-north-4')
        #
        # access moderation image,post data by ak,sk
        #
        app_key = '*************'
        app_secret = '************'
    
  2. Modify the local path or URL path of the image in the moderation_image_aksk_demo.py file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace data/moderation-terrorism.jpg in encode_to_base64 with the path of the target image. The sample code is as follows:
      1
      2
      3
          # call interface use the local file
          result = moderation_image_aksk(app_key, app_secret, encode_to_base64('data/moderation-terrorism.jpg'), '', ['politics','terrorism'],'')
          print(result)
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of demo_data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      demo_data_url = 'https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg'
          # call interface use the url
          result = moderation_image_aksk(app_key, app_secret, "", demo_data_url, ['politics','terrorism'], '')
          print(result)
      
  3. Execute the moderation_image_aksk_demo.py. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    4
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    
    Process finished with exit code 0
    

Using SDK in Token-based Authentication Mode

  1. In the moderation_image_token_demo.py file, set the registered username and password. The username of a non-IAM login user is the same as the account name of an IAM login user. The sample code is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    if __name__ == '__main__':
    
        # Services currently support North China-Beijing(cn-north-4)
        init_global_env('cn-north-4')
        #
        # access moderation image,post data by token
        #
        user_name = '******'
        password = '******'
        account_name = '******'  # the same as user_name in commonly use
     
        token = get_token(user_name, password, account_name)
    
  2. Modify the local path or URL path of the image in the moderation_image_token_demo.py file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace data/moderation-terrorism.jpg in encode_to_base64 with the path of the target image. The sample code is as follows:
      1
      2
      3
          # call interface use the local file
          result = moderation_image(token, encode_to_base64('data/moderation-terrorism.jpg'), '', ['porn'], '')
          print(result)
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of demo_data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      demo_data_url = 'https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg'
          # call interface use the url (token, image, url, threshold=0.95, scene=None)
          result = moderation_image(token, "", demo_data_url, ['porn'], '')
          print(result)
      
  3. Execute the moderation_image_token_demo.py file. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    4
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    {"result":{"detail":{"porn":[{"confidence":1.0,"label":"normal"},{"confidence":0.0,"label":"porn"},{"confidence":0.0,"label":"sexy"}]},"suggestion":"pass","category_suggestions":{"porn":"pass"}}}
    
    Process finished with exit code 0