123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import json
- import os
- from aliyunsdkcore.acs_exception.exceptions import ClientException
- from aliyunsdkcore.acs_exception.exceptions import ServerException
- from aliyunsdkcore.client import AcsClient
- from aliyunsdkcore.request import CommonRequest
- from common.log import log
- def file_trans(fileLink):
-
- REGION_ID = "cn-beijing"
- PRODUCT = "nls-filetrans"
- DOMAIN = "filetrans.cn-beijing.aliyuncs.com"
- API_VERSION = "2018-08-17"
- POST_REQUEST_ACTION = "SubmitTask"
- GET_REQUEST_ACTION = "GetTaskResult"
-
- KEY_APP_KEY = "appkey"
- KEY_FILE_LINK = "file_link"
- KEY_VERSION = "version"
- KEY_ENABLE_WORDS = "enable_words"
-
- KEY_AUTO_SPLIT = "auto_split"
-
- KEY_TASK = "Task"
- KEY_TASK_ID = "TaskId"
- KEY_STATUS_TEXT = "StatusText"
- KEY_RESULT = "Result"
-
- STATUS_SUCCESS = "SUCCESS"
- STATUS_RUNNING = "RUNNING"
- STATUS_QUEUEING = "QUEUEING"
-
- client = AcsClient(os.getenv('ALIYUN_ACCESS_KEY_ID'), os.getenv('ALIYUN_ACCESS_SECRET'), REGION_ID)
-
- postRequest = CommonRequest()
- postRequest.set_domain(DOMAIN)
- postRequest.set_version(API_VERSION)
- postRequest.set_product(PRODUCT)
- postRequest.set_action_name(POST_REQUEST_ACTION)
- postRequest.set_method('POST')
-
-
-
-
- task = {
- KEY_APP_KEY : os.getenv('ALIYUN_APP_KEY'),
- KEY_FILE_LINK : fileLink,
- KEY_VERSION : "4.0",
- KEY_ENABLE_WORDS : False,
- KEY_AUTO_SPLIT : True,
- "supervise_type":2,
- "speaker_num":2,
- "enable_callback":True,
- "callback_url":"https://toolsapi.gkscrm.com/api/v1/gpt/ali_trans_callback"
- }
- task = json.dumps(task)
- postRequest.add_body_params(KEY_TASK, task)
- taskId = ""
- try:
- postResponse = client.do_action_with_exception(postRequest)
- postResponse = json.loads(postResponse)
- log.info("录音文件识别任务:" + json.dumps(postResponse,indent=4))
- statusText = postResponse[KEY_STATUS_TEXT]
- if statusText == STATUS_SUCCESS:
- taskId = postResponse[KEY_TASK_ID]
- log.info("录音文件识别请求成功响应!任务ID:" + str(taskId))
- return str(taskId)
- else:
- log.error("录音文件识别任务失败!", statusText)
- return False
- except ServerException as e:
- log.error("录音文件识别失败! 服务端异常", e)
- return False
- except ClientException as e:
- log.error("录音文件识别失败! 客户端异常", e)
- return False
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|