Created
August 20, 2023 10:45
-
-
Save feiandxs/3665948c6b25c6094e8161a0240b0241 to your computer and use it in GitHub Desktop.
使用腾讯交互翻译API进行自动翻译的示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
该脚本演示了如何使用翻译API进行自动翻译。 | |
它生成翻译请求的JSON,发送请求到API端点,并打印翻译结果。 | |
脚本使用Pydantic库定义请求和响应模型,确保数据验证和类型安全。 | |
它还包括生成客户端密钥、生成翻译请求JSON和发送翻译请求的函数。 | |
翻译请求JSON包括源语言、目标语言和要翻译的文本列表。 | |
响应包含翻译后的文本。 | |
注意:在运行此脚本之前,请确保安装了所需的依赖项(pydantic,requests)。 | |
""" | |
import uuid | |
import time | |
import requests | |
from typing import List | |
from pydantic import BaseModel | |
def generate_client_key() -> str: | |
""" | |
生成客户端密钥字符串,格式为 "browser-chrome-116.0.0-Mac OS-{uuid}-{timestamp}"。 | |
Returns: | |
str: 生成的客户端密钥字符串 | |
""" | |
browser_info = "browser-chrome-116.0.0-Mac OS" | |
uuid_str = str(uuid.uuid4()) | |
timestamp = str(int(time.time() * 1000)) | |
client_key = f"{browser_info}-{uuid_str}-{timestamp}" | |
return client_key | |
class TranslationRequest(BaseModel): | |
""" | |
表示翻译请求的头部。 | |
""" | |
fn: str = "auto_translation" | |
session: str = "" | |
client_key: str | |
user: str = "" | |
class Source(BaseModel): | |
""" | |
表示源语言和要翻译的文本。 | |
""" | |
lang: str = "zh" | |
text_list: List[str] | |
class Target(BaseModel): | |
""" | |
表示翻译的目标语言。 | |
""" | |
lang: str = "en" | |
class RequestModel(BaseModel): | |
""" | |
表示完整的翻译请求模型。 | |
""" | |
header: TranslationRequest | |
type: str = "plain" | |
model_category: str = "normal" | |
text_domain: str = "" | |
source: Source | |
target: Target | |
def generate_translation_request( | |
text_list_param: List[str], source_lang_param: str = "zh", target_lang_param: str = "en" | |
) -> str: | |
""" | |
生成翻译请求的JSON字符串。 | |
Args: | |
text_list_param (List[str]): 要翻译的文本列表 | |
source_lang_param (str, optional): 源语言,默认为 "zh" | |
target_lang_param (str, optional): 目标语言,默认为 "en" | |
Returns: | |
str: 生成的请求JSON字符串 | |
""" | |
translation_request = RequestModel( | |
header=TranslationRequest(client_key=generate_client_key()), | |
source=Source(lang=source_lang_param, text_list=text_list_param), | |
target=Target(lang=target_lang_param), | |
) | |
return translation_request.json() | |
class Header(BaseModel): | |
""" | |
表示翻译响应中的头部字段。 | |
""" | |
type: str | |
ret_code: str | |
time_cost: float | |
request_id: str | |
class AutoTranslationResponse(BaseModel): | |
""" | |
表示完整的翻译响应模型。 | |
""" | |
header: Header | |
auto_translation: List[str] | |
def send_translation_request(request_json_param: str) -> AutoTranslationResponse: | |
""" | |
发送翻译请求并返回结果。 | |
Args: | |
request_json_param (str): 请求的JSON字符串 | |
Returns: | |
AutoTranslationResponse: 返回的翻译结果 | |
""" | |
url = "https://transmart.qq.com/api/imt" | |
headers = { | |
"Content-Type": "application/json", | |
} | |
response = requests.post(url, headers=headers, data=request_json_param) | |
# 将返回结果解析为AutoTranslationResponse对象 | |
response_data = AutoTranslationResponse.parse_raw(response.text) | |
return response_data | |
# 生成请求JSON | |
text_list_global = ["今天的天气真不错", "你好啊很高兴见到你我叫小明", "我的爸爸是一名老师"] | |
source_lang_global = "zh" | |
target_lang_global = "en" | |
request_json_global = generate_translation_request(text_list_global, source_lang_global, target_lang_global) | |
# 发送请求并获取返回结果 | |
translation_response_global = send_translation_request(request_json_global) | |
# 打印返回结果 | |
print("响应结果:") | |
print(translation_response_global) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment