def asr_bgm_aksk(_ak, _sk, url): endpoint = utils.get_endpoint(ais.AisService.IMAGE_SERVICE) _url = 'https://%s/v1.0/bgm/recognition' % endpoint sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk _data = { "url": url, } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = endpoint kreq.uri = "/v1.0/bgm/recognition" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) resp = None status_code = None try: sig.Sign(kreq) # # Here we use the unvertified-ssl-context, Because in FunctionStage # the client CA-validation have some problem, so we must do this. # _context = ssl._create_unverified_context() req = urllib.request.Request(url=_url, data=kreq.body, headers=kreq.headers) r = urllib.request.urlopen(req, context=_context) # # We use HTTPError and URLError,because urllib can't process the 4XX & # 500 error in the single urlopen function. # # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError as e: resp = e.read() status_code = e.code except URLError as e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp.decode('unicode_escape')
def super_resolution_aksk(_ak, _sk, image, scale=3, model="ESPCN"): _url = 'https://%s/v1.0/vision/super-resolution' % ais.AisEndpoint.ENDPOINT sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk _data = {"image": image.decode('utf-8'), "scale": scale, "model": model} kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = ais.AisEndpoint.ENDPOINT kreq.uri = "/v1.0/vision/super-resolution" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) resp = None status_code = None try: sig.Sign(kreq) # # Here we use the unvertified-ssl-context, Because in FunctionStage # the client CA-validation have some problem, so we must do this. # _context = ssl._create_unverified_context() req = urllib.request.Request(url=_url, data=kreq.body, headers=kreq.headers) r = urllib.request.urlopen(req, context=_context) # # We use HTTPError and URLError,because urllib2 can't process the 4XX & # 500 error in the single urlopen function. # # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError as e: resp = e.read() status_code = e.code except URLError as e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp.decode('utf-8')
def video_tagging_aksk(_ak, _sk, url, frame_interval=5, language='zh', threshold=60.0): sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk endpoint = utils.get_endpoint(ais.AisService.MODERATION_SERVICE) status, r = _video_tagging_aksk(endpoint, sig, url, frame_interval, language, threshold) if status != 200: return r submit_result = json.loads(r) job_id = submit_result['result'].get('job_id', '') #print("Process job id is :", job_id) time.sleep(1.0) try: while True: status, r = _get_result_aksk(endpoint, sig, job_id) if status != 200: return r rec_result = json.loads(r) process_status = rec_result["result"].get('status') if process_status == 'failed': return r elif process_status == 'finish': return r # # process_status == 0 || process_status == 1 # else: time.sleep(2.0) continue except Exception: return ''
def long_sentence_aksk(_ak,_sk, data, url=''): sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk status, r = _long_sentence_aksk(sig, data, url) if status != 200: print ('Process long sentence asr failed: summit job failed.') return '' submit_result = json.loads(r) job_id = submit_result['result'].get('job_id', '') print ("Process job id is :", job_id) words = '' time.sleep(1.0) try: while True: status, r = _get_result_aksk(sig, job_id) if status != 200: print ('Process long sentence asr failed: get result failed.') break rec_result = json.loads(r) process_status = rec_result["result"].get('status_code', 1) if process_status == -1: print ('Process long sentence asr failed: get result failed.') break elif process_status == 2: words = rec_result["result"].get('words', '') break # # process_status == 0 || process_status == 1 # else: time.sleep(2.0) continue except Exception: return '' return words
def moderation_video_aksk(_ak, _sk, url, frame_interval=5, categories=['politics', 'terrorism']): endpoint = utils.get_endpoint(ais.AisService.MODERATION_SERVICE) sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk status, r = _moderation_video_aksk(endpoint, sig, url, frame_interval, categories) if status != 200: return r submit_result = json.loads(r) job_id = submit_result['result'].get('job_id', '') #print("Process job id is :", job_id) time.sleep(1.0) try: while True: status, r = _get_result_aksk(endpoint, sig, job_id) if status != 200: return r rec_result = json.loads(r) process_status = rec_result["result"].get('status') if process_status == 'failed': return r elif process_status == 'finish': return r # # process_status == 0 || process_status == 1 # else: time.sleep(2.0) continue except Exception: return ''
def image_tagging_aksk(_ak, _sk, image, url, languzge, limit=-1, threshold=0.0): endpoint = utils.get_endpoint(ais.AisService.IMAGE_SERVICE) _url = 'https://%s/v1.0/image/tagging' % endpoint sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk if sys.version_info.major >= 3: if image != '': image = image.decode("utf-8") _data = { "image": image, "url": url, "language": languzge, "limit": limit, "threshold": threshold } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = endpoint kreq.uri = "/v1.0/image/tagging" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) status_code, resp = utils.request_aksk(sig, kreq, _url) if sys.version_info.major < 3: return resp.decode('unicode-escape').encode('utf-8') else: return resp.decode('unicode_escape')
def image_batch_jobs_aksk(_ak, _sk, urls, categories=['politics', 'terrorism']): sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk status, r = _image_batch_jobs_aksk(sig, urls, categories) if status != 200: return r submit_result = json.loads(r) job_id = submit_result['result'].get('job_id', '') #print("Process job id is :", job_id) time.sleep(1.0) try: while True: status, r = _get_result_aksk(sig, job_id) if status != 200: return r rec_result = json.loads(r) process_status = rec_result["result"].get('status') if process_status == 'failed': return r elif process_status == 'finish': return r # # process_status == 0 || process_status == 1 # else: time.sleep(2.0) continue except Exception: return ''
def moderation_image_aksk(_ak, _sk, image, url, categories=None, threshold=None): endpoint = utils.get_endpoint(ais.AisService.MODERATION_SERVICE) _url = 'https://%s/v1.0/moderation/image' % endpoint sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk if sys.version_info.major >= 3: if image != '': image = image.decode("utf-8") _data = { "image": image, "url": url, "categories": categories, "threshold": threshold, } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = endpoint kreq.uri = "/v1.0/moderation/image" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) status_code, resp = utils.request_aksk(sig, kreq, _url) if sys.version_info.major < 3: return resp else: return resp.decode('utf-8')
def asr_bgm_aksk(_ak, _sk, url): endpoint = utils.get_endpoint(ais.AisService.IMAGE_SERVICE) _url = 'https://%s/v1.0/bgm/recognition' % endpoint sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk _data = { "url": url, } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = endpoint kreq.uri = "/v1.0/bgm/recognition" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) status_code, resp = utils.request_aksk(sig, kreq, _url) if sys.version_info.major < 3: return resp.decode('unicode-escape').encode('utf-8') else: return resp.decode('unicode_escape')
def moderation_text_aksk( _ak, _sk, text, type='content', categories=["ad", "politics", "p**n", "abuse", "contraband", "flood"]): endpoint = utils.get_endpoint(ais.AisService.MODERATION_SERVICE) _url = 'https://%s/v1.0/moderation/text' % endpoint sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk _data = { "categories": categories, # 检测场景 Array politics:涉政 p**n:涉黄 ad:广告 abuse:辱骂 contraband:违禁品 flood:灌水 "items": [{ "text": text, "type": type } # items: 待检测的文本列表 text 待检测文本 type 文本类型 ] } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = endpoint kreq.uri = "/v1.0/moderation/text" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) status_code, resp = utils.request_aksk(sig, kreq, _url) if sys.version_info.major < 3: return resp.decode('unicode-escape').encode('utf-8') else: return resp.decode('unicode_escape')
def image_tagging_aksk(_ak, _sk, image, url, languzge, limit=-1, threshold=0.0): _url = 'https://%s/v1.0/image/tagging' % ais.AisEndpoint.IMAGE_ENDPOINT sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk if image != '': image = image.decode('utf-8') _data = { "image": image, "url": url, "language": languzge, "limit": limit, "threshold": threshold } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = ais.AisEndpoint.IMAGE_ENDPOINT kreq.uri = "/v1.0/image/tagging" kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) resp = None status_code = None try: sig.Sign(kreq) # # Here we use the unvertified-ssl-context, Because in FunctionStage # the client CA-validation have some problem, so we must do this. # _context = ssl._create_unverified_context() req = urllib.request.Request(url=_url, data=kreq.body, headers=kreq.headers) r = urllib.request.urlopen(req, context=_context) # # We use HTTPError and URLError,because urllib can't process the 4XX & # 500 error in the single urlopen function. # # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError as e: resp = e.read() status_code = e.code except URLError as e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp.decode('utf-8')
def tts_aksk(_ak, _sk, text, voice_name='xiaoyan', volume='0', sample_rate='16k', speech_speed='0', pitch_rate='0'): _url = 'https://%s' % (ais.AisEndpoint.ENDPOINT + ais.TtsURI.TTS) sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk _data = { "text": text, "voice_name": voice_name, "volume": volume, "sample_rate": sample_rate, "speech_speed": speech_speed, "pitch_rate": pitch_rate } kreq = signer.HttpRequest() kreq.scheme = "https" kreq.host = ais.AisEndpoint.ENDPOINT kreq.uri = ais.TtsURI.TTS kreq.method = "POST" kreq.headers = {"Content-Type": "application/json"} kreq.body = json.dumps(_data) resp = None status_code = None try: sig.Sign(kreq) # # Here we use the unvertified-ssl-context, Because in FunctionStage # the client CA-validation have some problem, so we must do this. # _context = ssl._create_unverified_context() req = urllib.request.Request(url=_url, data=kreq.body, headers=kreq.headers) r = urllib.request.urlopen(req, context=_context) # # We use HTTPError and URLError,because urllib2 can't process the 4XX & # 500 error in the single urlopen function. # # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests, # there is no this problem. # except HTTPError as e: resp = e.read() status_code = e.code except URLError as e: resp = e.read() status_code = e.code else: status_code = r.code resp = r.read() return resp.decode('utf-8')
def image_antiporn_aksk(_ak, _sk, image_str=None, url=None): sig = signer.Signer() sig.AppKey = _ak sig.AppSecret = _sk print(request_moderation_url_aksk(sig, '/v1.1/moderation/image/anti-p**n', image_str, url)) print(request_moderation_url_aksk(sig, '/v1.0/moderation/image', image_str, url))