From 95e03554bac1aaaf8c3e60630089c6bf3cfa8d36 Mon Sep 17 00:00:00 2001 From: mnjnhuang <1090031856@qq.com> Date: Fri, 16 Jan 2026 17:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=81=AB=E5=B1=B1=E7=BF=BB?= =?UTF-8?q?=E8=AF=91,=20=E7=BF=BB=E8=AF=91=E8=AF=8D=E6=9D=A1=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E16=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/model/VolcEngine.py | 52 ++++++++++++++++++++++++++--- python/routers/translate_with_ai.py | 8 +++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/python/model/VolcEngine.py b/python/model/VolcEngine.py index a417214..942776d 100644 --- a/python/model/VolcEngine.py +++ b/python/model/VolcEngine.py @@ -6,14 +6,58 @@ from model.TranslateBaseModel import TranslateBaseModel from concurrent.futures import ThreadPoolExecutor, as_completed -class VolcEngine(TranslateParent): - def translate_text_with_ai(self, tag_list: list, target_language: str = "zh") -> dict | None: - pass - +class VolcEngine(TranslateBaseModel): def __init__(self, ): from main import server self.access_id, self.access_key = server.tell_volc_api() + def translate_text_with_ai(self, goto_translate_map: dict, source_language: str = "en", + target_language: str = "zh") -> dict: + pass + + def _start_translate_with_machine_thread(self, goto_translate_map: dict, api_instance, rebuild_list, + source_language: str = "en", target_language: str = "zh"): + thread_status_code = {"code": 10500, "status": False} + translate_text_request = volcenginesdktranslate20250301.TranslateTextRequest( + source_language=source_language, + target_language=target_language, + text_list=rebuild_list, + ) + try: + response = api_instance.translate_text(translate_text_request) + except ApiException as e: + print(e) + thread_status_code.update({"code": 10501}) # API接口错误 + return thread_status_code + else: + translate_list = response.translation_list + if not self.check_translate_match_source(translate_list, goto_translate_map): + thread_status_code.update({"code": 10502}) # 翻译结果不匹配 + return thread_status_code + for item in range(len(rebuild_list)): + goto_translate_map[rebuild_list[item]].update({"translate_model": "VolcEngine机器翻译", + "translate": translate_list[item].translation}) + thread_status_code.update({"status": True}) + return thread_status_code + + def translate_text_with_machine(self, goto_translate_map: dict, source_language: str = "en", + target_language: str = "zh") -> dict: + if len(goto_translate_map) == 0: + return {"status": True, "translate_map": {}} + list_limit = 15 + _, rebuild_list, return_false_code = self.rebuild_text_and_list_to_translate(goto_translate_map) + self._set_configure() + api_instance = volcenginesdktranslate20250301.TRANSLATE20250301Api() + if len(rebuild_list) <= list_limit: + err_code, thread_status = self._start_translate_with_machine_thread(goto_translate_map, api_instance, + rebuild_list, source_language, + target_language).values() + if not thread_status: + return_false_code.update({"code": err_code}) + return return_false_code + return {"status": True, "translate_map": goto_translate_map} + return return_false_code + def _set_configure(self): configuration = volcenginesdkcore.Configuration() configuration.ak = self.access_id diff --git a/python/routers/translate_with_ai.py b/python/routers/translate_with_ai.py index 9654b8d..53da641 100644 --- a/python/routers/translate_with_ai.py +++ b/python/routers/translate_with_ai.py @@ -9,6 +9,7 @@ from dataclasses import dataclass from model.TencentCloud import TencentCloud from model.BaiduCloud import BaiduCloud from model.NiuTrans import NiuTrans +from model.VolcEngine import VolcEngine @dataclass @@ -120,9 +121,12 @@ class OverwriteServerHandler(ServerHandler): niu_trans: NiuTrans = NiuTrans() response_dict: dict = niu_trans.translate_text_with_machine(goto_ai_translate_map) translate_network_map: dict = response_dict.get("translate_map") + case "VolcEngineModelMachine": # 火山引擎机器翻译 + volc_engine: VolcEngine = VolcEngine() + response_dict: dict = volc_engine.translate_text_with_machine(goto_ai_translate_map) + translate_network_map: dict = response_dict.get("translate_map") case _: - self.set_response_code(ResponseCode.RESPONSE_103_MODEL_ERROR) - return + return self.set_response_code(ResponseCode.RESPONSE_103_MODEL_ERROR) if not response_dict["status"]: match response_dict["code"]: case 10500: