mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-06 11:35:49 +00:00
feat: support Tencent TranSmart (#382)
This commit is contained in:
parent
79866d9548
commit
95bdd1bf6b
@ -24,8 +24,9 @@ bilingual_book_maker 是一个 AI 翻译工具,使用 ChatGPT 帮助用户制
|
||||
- 可以使用 DeepL free `--model deeplfree`
|
||||
- 可以使用 [Claude](https://console.anthropic.com/docs) 模型进行翻译 `--model claude --claude_key ${claude_key}`
|
||||
- 可以使用 google 来翻译 `--model google`
|
||||
- 可用使用彩云进行翻译 `--model caiyun --caiyun_key ${caiyun_key}`
|
||||
- 可用使用 Gemini 进行翻译 `--model gemini --gemini_key ${gemini_key}`
|
||||
- 可以使用彩云进行翻译 `--model caiyun --caiyun_key ${caiyun_key}`
|
||||
- 可以使用 Gemini 进行翻译 `--model gemini --gemini_key ${gemini_key}`
|
||||
- 可以使用腾讯交互翻译(免费)进行翻译`--model tencentransmart`
|
||||
- 使用 `--test` 命令如果大家没付费可以加上这个先看看效果(有 limit 稍微有些慢)
|
||||
- 使用 `--language` 指定目标语言,例如: `--language "Simplified Chinese"`,预设值为 `"Simplified Chinese"`.
|
||||
请阅读 helper message 来查找可用的目标语言: `python make_book.py --help`
|
||||
|
@ -30,6 +30,7 @@ Find more info here for using liteLLM: https://github.com/BerriAI/litellm/blob/m
|
||||
- support DeepL free model `--model deeplfree`
|
||||
- support Google [Gemini](https://makersuite.google.com/app/apikey) model `--model gemini --gemini_key ${gemini_key}`
|
||||
- Support [Claude](https://console.anthropic.com/docs) model, use `--model claude --claude_key ${claude_key}`
|
||||
- Support [Tencent TranSmart](https://transmart.qq.com) model (Free), use `--model tencentransmart`
|
||||
- Use `--test` option to preview the result if you haven't paid for the service. Note that there is a limit and it may take some time.
|
||||
- Set the target language like `--language "Simplified Chinese"`. Default target language is `"Simplified Chinese"`.
|
||||
Read available languages by helper message: `python make_book.py --help`
|
||||
|
@ -5,6 +5,7 @@ from book_maker.translator.deepl_free_translator import DeepLFree
|
||||
from book_maker.translator.google_translator import Google
|
||||
from book_maker.translator.claude_translator import Claude
|
||||
from book_maker.translator.gemini_translator import Gemini
|
||||
from book_maker.translator.tencent_transmart_translator import TencentTranSmart
|
||||
from book_maker.translator.custom_api_translator import CustomAPI
|
||||
|
||||
MODEL_DICT = {
|
||||
@ -16,6 +17,7 @@ MODEL_DICT = {
|
||||
"gpt4": ChatGPTAPI,
|
||||
"claude": Claude,
|
||||
"gemini": Gemini,
|
||||
"tencentransmart": TencentTranSmart,
|
||||
"customapi": CustomAPI,
|
||||
# add more here
|
||||
}
|
||||
|
86
book_maker/translator/tencent_transmart_translator.py
Normal file
86
book_maker/translator/tencent_transmart_translator.py
Normal file
@ -0,0 +1,86 @@
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
from rich import print
|
||||
from .base_translator import Base
|
||||
|
||||
|
||||
class TencentTranSmart(Base):
|
||||
"""
|
||||
Tencent TranSmart translator
|
||||
"""
|
||||
|
||||
def __init__(self, key, language, **kwargs) -> None:
|
||||
super().__init__(key, language)
|
||||
self.api_url = "https://transmart.qq.com/api/imt"
|
||||
self.header = {
|
||||
"authority": "transmart.qq.com",
|
||||
"content-type": "application/json",
|
||||
"origin": "https://transmart.qq.com",
|
||||
"referer": "https://transmart.qq.com/zh-CN/index",
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
||||
}
|
||||
self.uuid = str(uuid.uuid4())
|
||||
self.session = requests.Session()
|
||||
self.translate_type = "zh"
|
||||
if self.language == "english":
|
||||
self.translate_type = "en"
|
||||
|
||||
def rotate_key(self):
|
||||
pass
|
||||
|
||||
def translate(self, text):
|
||||
print(text)
|
||||
source_language, text_list = self.text_analysis(text)
|
||||
client_key = self.get_client_key()
|
||||
api_form_data = {
|
||||
"header": {
|
||||
"fn": "auto_translation",
|
||||
"client_key": client_key,
|
||||
},
|
||||
"type": "plain",
|
||||
"model_category": "normal",
|
||||
"source": {
|
||||
"lang": source_language,
|
||||
"text_list": [""] + text_list + [""],
|
||||
},
|
||||
"target": {"lang": self.translate_type},
|
||||
}
|
||||
|
||||
response = self.session.post(
|
||||
self.api_url, json=api_form_data, headers=self.header, timeout=3
|
||||
)
|
||||
t_text = "".join(response.json()["auto_translation"])
|
||||
print("[bold green]" + re.sub("\n{3,}", "\n\n", t_text) + "[/bold green]")
|
||||
return t_text
|
||||
|
||||
def text_analysis(self, text):
|
||||
client_key = self.get_client_key()
|
||||
self.header.update({"Cookie": "TSMT_CLIENT_KEY={}".format(client_key)})
|
||||
analysis_request_data = {
|
||||
"header": {
|
||||
"fn": "text_analysis",
|
||||
"session": "",
|
||||
"client_key": client_key,
|
||||
"user": "",
|
||||
},
|
||||
"text": text,
|
||||
"type": "plain",
|
||||
"normalize": {"merge_broken_line": "false"},
|
||||
}
|
||||
r = self.session.post(
|
||||
self.api_url, json=analysis_request_data, headers=self.header
|
||||
)
|
||||
if not r.ok:
|
||||
return text
|
||||
response_json_data = r.json()
|
||||
text_list = [item["tgt_str"] for item in response_json_data["sentence_list"]]
|
||||
language = response_json_data["language"]
|
||||
return language, text_list
|
||||
|
||||
def get_client_key(self):
|
||||
return "browser-chrome-121.0.0-Windows_10-{}-{}".format(
|
||||
self.uuid, int(time.time() * 1e3)
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user