mirror of
https://github.com/Physton/sd-webui-prompt-all-in-one.git
synced 2026-05-01 03:31:41 +00:00
Fix the issue #80
Former-commit-id: 002b06d87fde4f71a6fa9576ff8922393c18ae63
This commit is contained in:
@@ -11,7 +11,6 @@ packages = {
|
||||
"boto3": "boto3",
|
||||
"aliyunsdkcore": "aliyun-python-sdk-core",
|
||||
"aliyunsdkalimt": "aliyun-python-sdk-alimt",
|
||||
"tencentcloud": "tencentcloud-sdk-python",
|
||||
}
|
||||
|
||||
for package_name in packages:
|
||||
|
||||
65
scripts/sign_tencent.py
Normal file
65
scripts/sign_tencent.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import hashlib, hmac, json, time
|
||||
from datetime import datetime
|
||||
|
||||
def sign_tencent(secret_id, secret_key, regin, params, action="TextTranslate", version = "2018-03-21"):
|
||||
host = 'tmt.tencentcloudapi.com'
|
||||
endpoint = "https://" + host
|
||||
action = "TextTranslate"
|
||||
|
||||
service = "tmt"
|
||||
algorithm = "TC3-HMAC-SHA256"
|
||||
timestamp = int(time.time())
|
||||
date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
|
||||
|
||||
# ************* 步骤 1:拼接规范请求串 *************
|
||||
http_request_method = "POST"
|
||||
canonical_uri = "/"
|
||||
canonical_querystring = ""
|
||||
ct = "application/json; charset=utf-8"
|
||||
payload = json.dumps(params)
|
||||
canonical_headers = "content-type:%s\nhost:%s\nx-tc-action:%s\n" % (ct, host, action.lower())
|
||||
signed_headers = "content-type;host;x-tc-action"
|
||||
hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
||||
canonical_request = (http_request_method + "\n" +
|
||||
canonical_uri + "\n" +
|
||||
canonical_querystring + "\n" +
|
||||
canonical_headers + "\n" +
|
||||
signed_headers + "\n" +
|
||||
hashed_request_payload)
|
||||
|
||||
# ************* 步骤 2:拼接待签名字符串 *************
|
||||
credential_scope = date + "/" + service + "/" + "tc3_request"
|
||||
hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
|
||||
string_to_sign = (algorithm + "\n" +
|
||||
str(timestamp) + "\n" +
|
||||
credential_scope + "\n" +
|
||||
hashed_canonical_request)
|
||||
|
||||
|
||||
# ************* 步骤 3:计算签名 *************
|
||||
# 计算签名摘要函数
|
||||
def sign(key, msg):
|
||||
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
|
||||
secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
|
||||
secret_service = sign(secret_date, service)
|
||||
secret_signing = sign(secret_service, "tc3_request")
|
||||
signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
|
||||
|
||||
# ************* 步骤 4:拼接 Authorization *************
|
||||
authorization = (algorithm + " " +
|
||||
"Credential=" + secret_id + "/" + credential_scope + ", " +
|
||||
"SignedHeaders=" + signed_headers + ", " +
|
||||
"Signature=" + signature)
|
||||
|
||||
return {
|
||||
"url": endpoint,
|
||||
"headers": {
|
||||
"Authorization": authorization,
|
||||
"Content-Type": ct,
|
||||
"Host": host,
|
||||
"X-TC-Action": action,
|
||||
"X-TC-Timestamp": str(timestamp),
|
||||
"X-TC-Version": version,
|
||||
"X-TC-Region": regin,
|
||||
},
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
from scripts.get_translate_apis import get_translate_apis
|
||||
from scripts.sign_tencent import sign_tencent
|
||||
import hashlib
|
||||
import os
|
||||
import requests
|
||||
@@ -224,24 +225,20 @@ def translate_tencent(text, from_lang, to_lang, api_config):
|
||||
if not region:
|
||||
raise Exception("region is required")
|
||||
|
||||
from tencentcloud.tmt.v20180321 import models
|
||||
from tencentcloud.common import credential
|
||||
from tencentcloud.tmt.v20180321 import tmt_client
|
||||
|
||||
request = models.TextTranslateRequest()
|
||||
request.SourceText = text
|
||||
request.Source = from_lang
|
||||
request.Target = to_lang
|
||||
request.ProjectId = 0
|
||||
cred = credential.Credential(secret_id, secret_key)
|
||||
client = tmt_client.TmtClient(cred, region)
|
||||
response = client.TextTranslate(request)
|
||||
result = json.loads(response.to_json_string())
|
||||
if 'Error' in result:
|
||||
raise Exception(result['Error']['Message'])
|
||||
if 'TargetText' not in result:
|
||||
params = {
|
||||
'SourceText': text,
|
||||
'Source': from_lang,
|
||||
'Target': to_lang,
|
||||
'ProjectId': 0
|
||||
}
|
||||
res = sign_tencent(secret_id, secret_key, region, params)
|
||||
response = requests.post(res['url'], json=params, timeout=10, headers=res['headers'])
|
||||
result = response.json()
|
||||
if 'Response' not in result:
|
||||
raise Exception("No response from Tencent")
|
||||
return result['TargetText']
|
||||
if 'TargetText' not in result['Response']:
|
||||
raise Exception("No response from Tencent")
|
||||
return result['Response']['TargetText']
|
||||
|
||||
|
||||
def translate(text, from_lang, to_lang, api, api_config = {}):
|
||||
|
||||
Reference in New Issue
Block a user