content_audit_bd.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import ssl
  4. import sys
  5. import json
  6. import base64
  7. # 保证兼容python2以及python3
  8. IS_PY3 = sys.version_info.major == 3
  9. if IS_PY3:
  10. from urllib.request import urlopen
  11. from urllib.request import Request
  12. from urllib.error import URLError
  13. from urllib.parse import urlencode
  14. from urllib.parse import quote_plus
  15. else:
  16. import urllib2
  17. from urllib import quote_plus
  18. from urllib2 import urlopen
  19. from urllib2 import Request
  20. from urllib2 import URLError
  21. from urllib import urlencode
  22. # 防止https证书校验不正确
  23. ssl._create_default_https_context = ssl._create_unverified_context
  24. API_KEY = 'XuDpl9wYzpKAi1DqH5wv4zx4'
  25. SECRET_KEY = 'dqL2gDMgf6HsbMcxBgpTt0lUzOEldRZm'
  26. IMAGE_CENSOR = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined"
  27. TEXT_CENSOR = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined"
  28. """ TOKEN start """
  29. TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
  30. """
  31. 获取token
  32. """
  33. def fetch_token():
  34. params = {'grant_type': 'client_credentials',
  35. 'client_id': API_KEY,
  36. 'client_secret': SECRET_KEY}
  37. post_data = urlencode(params)
  38. if (IS_PY3):
  39. post_data = post_data.encode('utf-8')
  40. req = Request(TOKEN_URL, post_data)
  41. try:
  42. f = urlopen(req, timeout=5)
  43. result_str = f.read()
  44. except URLError as err:
  45. print(err)
  46. if (IS_PY3):
  47. result_str = result_str.decode()
  48. result = json.loads(result_str)
  49. if ('access_token' in result.keys() and 'scope' in result.keys()):
  50. if not 'brain_all_scope' in result['scope'].split(' '):
  51. print('please ensure has check the ability')
  52. exit()
  53. return result['access_token']
  54. else:
  55. print('please overwrite the correct API_KEY and SECRET_KEY')
  56. exit()
  57. """
  58. 读取文件
  59. """
  60. def read_file(image_path):
  61. f = None
  62. try:
  63. f = open(image_path, 'rb')
  64. return f.read()
  65. except:
  66. print('read image file fail')
  67. return None
  68. finally:
  69. if f:
  70. f.close()
  71. """
  72. 调用远程服务
  73. """
  74. def request(url, data):
  75. req = Request(url, data.encode('utf-8'))
  76. has_error = False
  77. try:
  78. f = urlopen(req)
  79. result_str = f.read()
  80. if (IS_PY3):
  81. result_str = result_str.decode()
  82. return result_str
  83. except URLError as err:
  84. print(err)
  85. if __name__ == '__main__':
  86. # 获取access token
  87. token = fetch_token()
  88. # 拼接图像审核url
  89. image_url = IMAGE_CENSOR + "?access_token=" + token
  90. # 拼接文本审核url
  91. text_url = TEXT_CENSOR + "?access_token=" + token
  92. file_content = read_file('image_normal.jpeg')
  93. result = request(image_url, urlencode(
  94. {'image': file_content}))
  95. print("----- 正常图调用结果 -----")
  96. print(result)
  97. file_content = read_file('image_advertise.jpeg')
  98. result = request(image_url, urlencode(
  99. {'image': base64.b64encode(file_content)}))
  100. print("----- 广告图调用结果 -----")
  101. print(result)
  102. text = "我们要热爱祖国热爱党"
  103. result = request(text_url, urlencode({'text': text}))
  104. print("----- 正常文本调用结果 -----")
  105. print(result)
  106. text = "我要爆粗口啦:百度AI真他妈好用"
  107. result = request(text_url, urlencode({'text': text}))
  108. print("----- 粗俗文本调用结果 -----")
  109. print(result)