parent
7401c92238
commit
8f00fbc1c3
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -17,6 +17,8 @@ from scripts.physton_prompt.translator.mymemory_translator import MyMemoryTransl
|
|||
from scripts.physton_prompt.translator.niutrans_translator import NiutransTranslator
|
||||
from scripts.physton_prompt.translator.caiyun_translator import CaiyunTranslator
|
||||
from scripts.physton_prompt.translator.volcengine_translator import VolcengineTranslator
|
||||
from scripts.physton_prompt.translator.iflytekV1_translator import IflytekV1Translator
|
||||
from scripts.physton_prompt.translator.iflytekV2_translator import IflytekV2Translator
|
||||
from scripts.physton_prompt.translator.mbart50_translator import MBart50Translator
|
||||
|
||||
caches = {}
|
||||
|
|
@ -108,6 +110,10 @@ def translate(text, from_lang, to_lang, api, api_config=None):
|
|||
translator = CaiyunTranslator()
|
||||
elif api == 'volcengine':
|
||||
translator = VolcengineTranslator()
|
||||
elif api == 'iflytekV1':
|
||||
translator = IflytekV1Translator()
|
||||
elif api == 'iflytekV2':
|
||||
translator = IflytekV2Translator()
|
||||
elif api == 'mbart50':
|
||||
translator = MBart50Translator()
|
||||
elif 'type' in find and find['type'] == 'translators':
|
||||
|
|
|
|||
|
|
@ -0,0 +1,113 @@
|
|||
from scripts.physton_prompt.translator.base_tanslator import BaseTranslator
|
||||
import requests
|
||||
import datetime
|
||||
import hashlib
|
||||
import base64
|
||||
import hmac
|
||||
import json
|
||||
from scripts.physton_prompt.get_lang import get_lang
|
||||
|
||||
|
||||
class IflytekV1Translator(BaseTranslator):
|
||||
def __init__(self):
|
||||
super().__init__('iflytekV1')
|
||||
|
||||
def translate(self, text):
|
||||
if not text:
|
||||
return ''
|
||||
app_id = self.api_config.get('app_id', '')
|
||||
if not app_id:
|
||||
raise Exception(get_lang('is_required', {'0': 'APP ID'}))
|
||||
api_secret = self.api_config.get('api_secret', '')
|
||||
if not api_secret:
|
||||
raise Exception(get_lang('is_required', {'0': 'API Secret'}))
|
||||
api_key = self.api_config.get('api_key', '')
|
||||
if not api_key:
|
||||
raise Exception(get_lang('is_required', {'0': 'API Key'}))
|
||||
|
||||
response = translate(text, From=self.from_lang, To=self.to_lang, APPID=app_id, Secret=api_secret, APIKey=api_key)
|
||||
if response.status_code != 200:
|
||||
raise Exception(get_lang('request_error', {'0': 'iflytekV1'}))
|
||||
if not response.text:
|
||||
raise Exception(get_lang('response_is_empty', {'0': 'iflytekV1'}))
|
||||
result = response.json()
|
||||
if 'code' not in result:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'iflytekV1'}))
|
||||
if result['code'] != 0:
|
||||
raise Exception(result['message'])
|
||||
return result['data']['result']['trans_result']['dst']
|
||||
|
||||
|
||||
def hashlib_256(res):
|
||||
m = hashlib.sha256(bytes(res.encode(encoding='utf-8'))).digest()
|
||||
result = "SHA-256=" + base64.b64encode(m).decode(encoding='utf-8')
|
||||
return result
|
||||
|
||||
def httpdate(dt):
|
||||
"""
|
||||
Return a string representation of a date according to RFC 1123
|
||||
(HTTP/1.1).
|
||||
|
||||
The supplied date must be in UTC.
|
||||
|
||||
"""
|
||||
weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
|
||||
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
|
||||
"Oct", "Nov", "Dec"][dt.month - 1]
|
||||
return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
|
||||
dt.year, dt.hour, dt.minute, dt.second)
|
||||
|
||||
def translate(Text, From, To, APPID, Secret, APIKey, Host="itrans.xfyun.cn"):
|
||||
RequestUri = "/v2/its"
|
||||
url="https://"+Host+RequestUri
|
||||
HttpMethod = "POST"
|
||||
Algorithm = "hmac-sha256"
|
||||
HttpProto = "HTTP/1.1"
|
||||
|
||||
# 设置当前时间
|
||||
curTime_utc = datetime.datetime.utcnow()
|
||||
Date = httpdate(curTime_utc)
|
||||
# 设置业务参数
|
||||
# 语种列表参数值请参照接口文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html
|
||||
BusinessArgs={
|
||||
"from":From,
|
||||
"to": To,
|
||||
}
|
||||
|
||||
content = str(base64.b64encode(Text.encode('utf-8')), 'utf-8')
|
||||
postdata = {
|
||||
"common": {"app_id": APPID},
|
||||
"business": BusinessArgs,
|
||||
"data": {
|
||||
"text": content,
|
||||
}
|
||||
}
|
||||
body = json.dumps(postdata)
|
||||
|
||||
digest = hashlib_256(body)
|
||||
signatureStr = "host: " + Host + "\n"
|
||||
signatureStr += "date: " + Date + "\n"
|
||||
signatureStr += HttpMethod + " " + RequestUri \
|
||||
+ " " + HttpProto + "\n"
|
||||
signatureStr += "digest: " + digest
|
||||
signature = hmac.new(bytes(Secret.encode(encoding='utf-8')),
|
||||
bytes(signatureStr.encode(encoding='utf-8')),
|
||||
digestmod=hashlib.sha256).digest()
|
||||
sign = base64.b64encode(signature).decode(encoding='utf-8')
|
||||
|
||||
authHeader = 'api_key="%s", algorithm="%s", ' \
|
||||
'headers="host date request-line digest", ' \
|
||||
'signature="%s"' \
|
||||
% (APIKey, Algorithm, sign)
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"Method": "POST",
|
||||
"Host": Host,
|
||||
"Date": Date,
|
||||
"Digest": digest,
|
||||
"Authorization": authHeader
|
||||
}
|
||||
|
||||
response = requests.post(url, data=body, headers=headers,timeout=60)
|
||||
return response
|
||||
|
|
@ -0,0 +1,128 @@
|
|||
from scripts.physton_prompt.translator.base_tanslator import BaseTranslator
|
||||
from datetime import datetime
|
||||
from wsgiref.handlers import format_date_time
|
||||
from time import mktime
|
||||
import hashlib
|
||||
import base64
|
||||
import hmac
|
||||
from urllib.parse import urlencode
|
||||
import json
|
||||
import requests
|
||||
from scripts.physton_prompt.get_lang import get_lang
|
||||
|
||||
|
||||
class IflytekV2Translator(BaseTranslator):
|
||||
def __init__(self):
|
||||
super().__init__('iflytekV2')
|
||||
|
||||
def translate(self, text):
|
||||
if not text:
|
||||
return ''
|
||||
app_id = self.api_config.get('app_id', '')
|
||||
if not app_id:
|
||||
raise Exception(get_lang('is_required', {'0': 'APP ID'}))
|
||||
api_secret = self.api_config.get('api_secret', '')
|
||||
if not api_secret:
|
||||
raise Exception(get_lang('is_required', {'0': 'API Secret'}))
|
||||
api_key = self.api_config.get('api_key', '')
|
||||
if not api_key:
|
||||
raise Exception(get_lang('is_required', {'0': 'API Key'}))
|
||||
|
||||
response = translate(text, From=self.from_lang, To=self.to_lang, APPId=app_id, APISecret=api_secret, APIKey=api_key)
|
||||
if response.status_code != 200:
|
||||
raise Exception(get_lang('request_error', {'0': 'iflytekV1'}))
|
||||
if not response.text:
|
||||
raise Exception(get_lang('response_is_empty', {'0': 'iflytekV1'}))
|
||||
result = json.loads(response.content.decode())
|
||||
if 'header' not in result:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'iflytekV1'}))
|
||||
result = json.loads(response.content.decode())
|
||||
if 'code' not in result['header']:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'iflytekV1'}))
|
||||
if result['header']['code'] != 0:
|
||||
raise Exception(result['header']['message'])
|
||||
restul_decode = base64.b64decode(result['payload']['result']['text']).decode()
|
||||
result_json = json.loads(restul_decode)
|
||||
if 'trans_result' not in result_json:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'iflytekV1'}))
|
||||
return result_json['trans_result']['dst']
|
||||
|
||||
class Url:
|
||||
def __init__(self, host, path, schema):
|
||||
self.host = host
|
||||
self.path = path
|
||||
self.schema = schema
|
||||
pass
|
||||
|
||||
# calculate sha256 and encode to base64
|
||||
def sha256base64(data):
|
||||
sha256 = hashlib.sha256()
|
||||
sha256.update(data)
|
||||
digest = base64.b64encode(sha256.digest()).decode(encoding='utf-8')
|
||||
return digest
|
||||
|
||||
def parse_url(requset_url):
|
||||
stidx = requset_url.index("://")
|
||||
host = requset_url[stidx + 3:]
|
||||
schema = requset_url[:stidx + 3]
|
||||
edidx = host.index("/")
|
||||
if edidx <= 0:
|
||||
raise Exception("invalid request url:" + requset_url)
|
||||
path = host[edidx:]
|
||||
host = host[:edidx]
|
||||
u = Url(host, path, schema)
|
||||
return u
|
||||
|
||||
# build websocket auth request url
|
||||
def assemble_ws_auth_url(requset_url, method="POST", api_key="", api_secret=""):
|
||||
u = parse_url(requset_url)
|
||||
host = u.host
|
||||
path = u.path
|
||||
now = datetime.now()
|
||||
date = format_date_time(mktime(now.timetuple()))
|
||||
signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)
|
||||
signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),
|
||||
digestmod=hashlib.sha256).digest()
|
||||
signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
|
||||
authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
|
||||
api_key, "hmac-sha256", "host date request-line", signature_sha)
|
||||
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
|
||||
values = {
|
||||
"host": host,
|
||||
"date": date,
|
||||
"authorization": authorization
|
||||
}
|
||||
|
||||
return requset_url + "?" + urlencode(values)
|
||||
|
||||
def translate(Text, From, To, APPId, APISecret, APIKey, Host="itrans.xf-yun.com"):
|
||||
RequestUri = "/v1/its"
|
||||
url="https://"+Host+RequestUri
|
||||
|
||||
body = {
|
||||
"header": {
|
||||
"app_id": APPId,
|
||||
"status": 3,
|
||||
},
|
||||
"parameter": {
|
||||
"its": {
|
||||
"from": From,
|
||||
"to": To,
|
||||
"result": {}
|
||||
}
|
||||
},
|
||||
"payload": {
|
||||
"input_data": {
|
||||
"encoding": "utf8",
|
||||
"status": 3,
|
||||
"text": base64.b64encode(Text.encode("utf-8")).decode('utf-8')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request_url = assemble_ws_auth_url(url, "POST", APIKey, APISecret)
|
||||
|
||||
headers = {'content-type': "application/json", 'host': 'itrans.xf-yun.com', 'app_id': APPId}
|
||||
# print(request_url)
|
||||
response = requests.post(request_url, data=json.dumps(body), headers=headers)
|
||||
return response
|
||||
|
|
@ -153,6 +153,24 @@ def test_volcengine():
|
|||
print(translate(text, 'en_US', 'zh_TW', 'volcengine', api_config))
|
||||
print(translate(texts, 'en_US', 'zh_TW', 'volcengine', api_config))
|
||||
|
||||
def test_iflytekV1():
|
||||
api_config = {
|
||||
'app_id': os.getenv('IFLYTEK_APP_ID'),
|
||||
'api_secret': os.getenv('IFLYTEK_API_SECRET'),
|
||||
'api_key': os.getenv('IFLYTEK_API_KEY'),
|
||||
}
|
||||
print(translate(text, 'en_US', 'zh_CN', 'iflytekV1', api_config))
|
||||
print(translate(texts, 'en_US', 'zh_CN', 'iflytekV1', api_config))
|
||||
|
||||
def test_iflytekV2():
|
||||
api_config = {
|
||||
'app_id': os.getenv('IFLYTEK_APP_ID'),
|
||||
'api_secret': os.getenv('IFLYTEK_API_SECRET'),
|
||||
'api_key': os.getenv('IFLYTEK_API_KEY'),
|
||||
}
|
||||
print(translate(text, 'en_US', 'zh_CN', 'iflytekV2', api_config))
|
||||
print(translate(texts, 'en_US', 'zh_CN', 'iflytekV2', api_config))
|
||||
|
||||
def test_languages():
|
||||
i18n = get_i18n()
|
||||
languages = []
|
||||
|
|
@ -166,4 +184,4 @@ def test_languages():
|
|||
print(translate(text, 'en_US', lang, 'myMemory_free'))
|
||||
pass
|
||||
|
||||
test_volcengine()
|
||||
test_iflytekV2()
|
||||
|
|
|
|||
Loading…
Reference in New Issue