AI不靠谱,有时候还是得靠人。
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import base64
import datetime
import hashlib
import hmac
import json
import random
import requests
import tkinter as tk
class LangboatOpenClient:
"""澜舟开放平台客户端"""
def __init__(self,
access_key: str,
access_secret: str,
url: str = "https://open.langboat.com"):
self.access_key = access_key
self.access_secret = access_secret
self.url = url
def _build_header(self, query: dict, data: dict) -> dict:
accept = "application/json"
# 1. body MD5 加密
content_md5 = base64.b64encode(
hashlib.md5(
json.dumps(data).encode("utf-8")
).digest()
).decode()
content_type = "application/json"
gmt_format = '%a, %d %b %Y %H:%M:%S GMT'
date = datetime.datetime.utcnow().strftime(gmt_format)
signature_method = "HMAC-SHA256"
signature_nonce = str(random.randint(0, 65535))
header_string = f"POST\n{accept}\n{content_md5}\n{content_type}\n" \
f"{date}\n{signature_method}\n{signature_nonce}\n"
# 2. 计算 queryToSign
queries_str = []
for k, v in sorted(query.items(), key=lambda item: item[0]):
if isinstance(v, list):
for i in v:
queries_str.append(f"{k}={i}")
else:
queries_str.append(f"{k}={v}")
queries_string = '&'.join(queries_str)
# 3.计算 stringToSign
sign_string = header_string + queries_string
# 4.计算 HMAC-SHA256 + Base64
secret_bytes = self.access_secret.encode("utf-8")
# 5.计算签名
signature = base64.b64encode(
hmac.new(secret_bytes, sign_string.encode("utf-8"), hashlib.sha256).digest()
).decode()
res = {
"Content-Type": content_type,
"Content-MD5": content_md5,
"Date": date,
"Accept": accept,
"X-Langboat-Signature-Method": signature_method,
"X-Langboat-Signature-Nonce": signature_nonce,
"Authorization": f"{self.access_key}:{signature}"
}
return res
def inference(self, queries: dict, data: dict) -> (int, dict):
"""
服务调用
:param queries: query 参数
:param data: request body 数据
:return: response status, response body to json
"""
headers = self._build_header(queries, data)
response = requests.post(url=self.url, headers=headers, params=queries, json=data)
return response.status_code, response.json()
class LangboatTranslatorUI:
"""澜舟翻译UI"""
def __init__(self, master):
self.master = master
master.title("Langboat Translator")
self.label = tk.Label(master, text="请输入要翻译的文本:")
self.label.pack()
self.entry = tk.Entry(master)
self.entry.pack()
self.button = tk.Button(master, text="翻译", command=self.translate)
self.button.pack()
self.result_label = tk.Label(master, text="")
self.result_label.pack()
self.client = LangboatOpenClient(
access_key=_access_key,
access_secret=_access_secret
)
def translate(self):
source_text = self.entry.get()
if not source_text:
self.result_label.config(text="请输入要翻译的文本")
return
_queries = {
"action": "translateText",
"domain": "general",
"sourceLanguage": "en",
"targetLanguage": "zh"
}
_data = {"sourceText": source_text}
status_code, result = self.client.inference(_queries, _data)
if "targetText" not in result:
self.result_label.config(text="翻译失败,请重试")
return
self.result_label.config(text=result["targetText"])
if __name__ == '__main__':
_access_key = '隐藏'
_access_secret = '隐藏'
root = tk.Tk()
my_gui = LangboatTranslatorUI(root)
root.mainloop()