Created
October 16, 2025 08:07
-
-
Save IUHHUI/0a5f5dd23a05b04f893c169dac3f04c5 to your computer and use it in GitHub Desktop.
使用腾讯混元 hunyuan-translation 或hunyuan-translation-lite翻译
This file contains hidden or 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
| import json | |
| from tencentcloud.common import credential | |
| from tencentcloud.common.profile.client_profile import ClientProfile | |
| from tencentcloud.common.profile.http_profile import HttpProfile | |
| from tencentcloud.hunyuan.v20230901 import hunyuan_client, models | |
| def main( | |
| TENCENTCLOUD_SECRET_ID: str, | |
| TENCENTCLOUD_SECRET_KEY: str, | |
| quick: str, | |
| text: str, | |
| source_lang: str, | |
| ): | |
| try: | |
| cred = credential.Credential(TENCENTCLOUD_SECRET_ID, TENCENTCLOUD_SECRET_KEY) | |
| httpProfile = HttpProfile() | |
| httpProfile.endpoint = "hunyuan.tencentcloudapi.com" | |
| # 实例化一个client选项,可选的,没有特殊需求可以跳过 | |
| clientProfile = ClientProfile() | |
| clientProfile.httpProfile = httpProfile | |
| # 实例化要请求产品的client对象,clientProfile是可选的 | |
| client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", clientProfile) | |
| # 实例化一个请求对象,每个接口都会对应一个request对象 | |
| req = models.ChatTranslationsRequest() | |
| model_name = "hunyuan-translation-lite" | |
| if quick and quick == "0": | |
| model_name = "hunyuan-translation" | |
| lang_map = { | |
| "Chinese": "zh", | |
| "English": "en", | |
| "zh": "zh", | |
| "en": "en", | |
| "中文": "zh", | |
| "英文": "en", | |
| } | |
| source_code = lang_map.get(source_lang, source_lang) | |
| target_code = "zh" if source_code == "en" else "en" | |
| params = { | |
| "Model": model_name, | |
| "Stream": False, | |
| "Text": text, | |
| "Source": source_code, | |
| "Target": target_code, | |
| "Field": "金融领域", | |
| } | |
| req.from_json_string(json.dumps(params)) | |
| # 返回的resp是一个ChatTranslationsResponse的实例,与请求对象对应 | |
| resp = client.ChatTranslations(req) | |
| result = resp.Choices[0].Message.Content | |
| return {"result": result} | |
| except Exception as err: | |
| return {"result": ""} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment