From c2b07b947b2ab860a1597421b6bd8b1e10e60a9a Mon Sep 17 00:00:00 2001 From: Byaidu <909756245@qq.com> Date: Sat, 7 Dec 2024 01:48:28 +0800 Subject: [PATCH] doc: envs table --- README.md | 102 ++++++++++++------------------------------- README_zh-CN.md | 97 ++++++++++------------------------------ pdf2zh/translator.py | 46 +++++++++---------- 3 files changed, 75 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index 5988bbc2..ff69f661 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,20 @@ English | [简体中文](README_zh-CN.md)

- + - + - + - + - +

@@ -131,17 +131,16 @@ See [documentation for GUI](./docs/README_GUI.md) for more details. For docker deployment on cloud service: +
Deploy - Deploy to Koyeb - Deploy on Zeabur - Deploy to Koyeb +

Advanced Options

@@ -190,76 +189,33 @@ pdf2zh example.pdf -li en -lo ja

Translate with Different Services

-- **DeepL** - - See [DeepL](https://support.deepl.com/hc/en-us/articles/360020695820-API-Key-for-DeepL-s-API) - - Set ENVs to construct an endpoint like: `{DEEPL_SERVER_URL}/translate` - - `DEEPL_SERVER_URL` (Optional), e.g., `export DEEPL_SERVER_URL=https://api.deepl.com` - - `DEEPL_AUTH_KEY`, e.g., `export DEEPL_AUTH_KEY=xxx` - - ```bash - pdf2zh example.pdf -s deepl - ``` - -- **DeepLX** +The table below outlines the required environment variables for each translation service. Make sure to set them before using the respective service. - See [DeepLX](https://github.com/OwO-Network/DeepLX) +|**Translator**|**Service**|**Environment Variables**|**Default Values**|**Notes**| +|-|-|-|-|-| +|**Google (Default)**|`google`|None|N/A|None| +|**Bing**|`bing`|None|N/A|None| +|**DeepL**|`deepl`|`DEEPL_SERVER_URL`,`DEEPL_AUTH_KEY`|`https://api.deepl.com`, `[Your Key]`|See [DeepL](https://support.deepl.com/hc/en-us/articles/360020695820-API-Key-for-DeepL-s-API)| +|**DeepLX**|`deeplx`|`DEEPLX_ENDPOINT`|`https://api.deepl.com/translate`|See [DeepLX](https://github.com/OwO-Network/DeepLX)| +|**Ollama**|`ollama`|`OLLAMA_HOST`, `OLLAMA_MODEL`|`http://127.0.0.1:11434`, `gemma2`|See [Ollama](https://github.com/ollama/ollama)| +|**OpenAI**|`openai`|`OPENAI_BASE_URL`, `OPENAI_API_KEY`, `OPENAI_MODEL`|`https://api.openai.com/v1`, `[Your Key]`, `gpt-4o-mini`|See [OpenAI](https://platform.openai.com/docs/overview)| +|**Zhipu**|`zhipu`|`ZHIPU_API_KEY`, `ZHIPU_MODEL`|`[Your Key]`, `glm-4-flash`|See [Zhipu](https://open.bigmodel.cn/dev/api/thirdparty-frame/openai-sdk)| +|**Silicon**|`silicon`|`SILICON_API_KEY`, `SILICON_MODEL`|`[Your Key]`, `Qwen/Qwen2.5-7B-Instruct`|See [SiliconCloud](https://docs.siliconflow.cn/quickstart)| +|**Azure**|`azure`|`AZURE_ENDPOINT`, `AZURE_API_KEY`|`https://api.translator.azure.cn`, `[Your Key]`|See [Azure](https://docs.azure.cn/en-us/ai-services/translator/text-translation-overview)| +|**Tencent**|`tencent`|`TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY`|`[Your ID]`, `[Your Key]`|See [Tencent](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104)| - Set ENVs to construct an endpoint like: `{DEEPLX_SERVER_URL}/{DEEPLX_AUTH_KEY}/translate` - - `DEEPLX_SERVER_URL` (Optional), e.g., `export DEEPLX_SERVER_URL=https://api.deeplx.org` - - `DEEPLX_AUTH_KEY`, e.g., `export DEEPLX_AUTH_KEY=xxx` - - ```bash - pdf2zh example.pdf -s deeplx - ``` - -- **Ollama** - - See [Ollama](https://github.com/ollama/ollama) - - Set ENVs to construct an endpoint like: `{OLLAMA_HOST}/api/chat` - - `OLLAMA_HOST` (Optional), e.g., `export OLLAMA_HOST=https://localhost:11434` - - ```bash - pdf2zh example.pdf -s ollama:gemma2 - ``` +Use `-s service` or `-s service:model` to specify service: -- **LLM with OpenAI compatible schemas (OpenAI / SiliconCloud / Zhipu)** - - See [SiliconCloud](https://docs.siliconflow.cn/quickstart), [Zhipu](https://open.bigmodel.cn/dev/api/thirdparty-frame/openai-sdk) - - Set ENVs to construct an endpoint like: `{OPENAI_BASE_URL}/chat/completions` - - `OPENAI_BASE_URL` (Optional), e.g., `export OPENAI_BASE_URL=https://api.openai.com/v1` - - `OPENAI_API_KEY`, e.g., `export OPENAI_API_KEY=xxx` - - ```bash - pdf2zh example.pdf -s openai:gpt-4o-mini - ``` - -- **Azure** - - See [Azure Text Translation](https://docs.azure.cn/en-us/ai-services/translator/text-translation-overview) - - Following ENVs are required: - - `AZURE_APIKEY`, e.g., `export AZURE_APIKEY=xxx` - - `AZURE_ENDPOINT`, e.g, `export AZURE_ENDPOINT=https://api.translator.azure.cn/` - - `AZURE_REGION`, e.g., `export AZURE_REGION=chinaeast2` - - ```bash - pdf2zh example.pdf -s azure - ``` -- **Tencent Machine Translation** - - See [Tencent Machine Translation](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104) +```bash +pdf2zh example.pdf -s openai:gpt-4o-mini +``` - Following ENVs are required: - - `TENCENT_SECRET_ID`, e.g., `export TENCENT_SECRET_ID=AKIDxxx` - - `TENCENT_SECRET_KEY`, e.g, `export TENCENT_SECRET_KEY=xxx` +Or specify model with environment variables: - ```bash - pdf2zh example.pdf -s tencent - ``` +```bash +set OPENAI_MODEL=gpt-4o-mini +pdf2zh example.pdf -s openai +```

Translate wih exceptions

diff --git a/README_zh-CN.md b/README_zh-CN.md index 0229e7eb..d3314ddb 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -33,7 +33,7 @@ - 🌐 支持 [多种语言](#language) 和 [诸多翻译服务](#services) - 🤖 提供 [命令行工具](#usage),[图形交互界面](#gui),以及 [容器化部署](#docker) -欢迎在 [GitHub Issues](https://github.com/Byaidu/PDFMathTranslate/issues)、[Telegram 用户群](https://t.me/+Z9_SgnxmsmA5NzBl) 或 [QQ 用户群](https://qm.qq.com/q/DixZCxQej0) 中提供反馈。 +欢迎在 [GitHub Issues](https://github.com/Byaidu/PDFMathTranslate/issues)、[Telegram 用户群](https://t.me/+Z9_SgnxmsmA5NzBl) 或 [QQ 用户群](https://qm.qq.com/q/DixZCxQej0) 中提供反馈

近期更新

@@ -57,12 +57,12 @@ ### 免费服务 () -你可以立即尝试 [免费公共服务](https://pdf2zh.com/) 而无需安装。 +你可以立即尝试 [免费公共服务](https://pdf2zh.com/) 而无需安装 ### Hugging Face 在线演示 -你可以立即尝试 [在 HuggingFace 上的在线演示](https://huggingface.co/spaces/reycn/PDFMathTranslate-Docker) 而无需安装。 -请注意,演示的计算资源有限,因此请避免滥用。 +你可以立即尝试 [在 HuggingFace 上的在线演示](https://huggingface.co/spaces/reycn/PDFMathTranslate-Docker) 而无需安装 +请注意,演示的计算资源有限,因此请避免滥用

安装和使用

@@ -131,17 +131,16 @@ 用于在云服务上部署容器镜像: +
Deploy - Deploy to Koyeb - Deploy on Zeabur - Deploy to Koyeb +

高级选项

@@ -190,82 +189,32 @@ pdf2zh example.pdf -li en -lo ja

使用不同的翻译服务

-- **DeepL** - -参考 [DeepL](https://support.deepl.com/hc/en-us/articles/360020695820-API-Key-for-DeepL-s-API) - -设置环境变量构建接入点:`{DEEPL_SERVER_URL}/translate` - -- `DEEPL_SERVER_URL`(可选), e.g., `export DEEPL_SERVER_URL=https://api.deepl.com` -- `DEEPL_AUTH_KEY`, e.g., `export DEEPL_AUTH_KEY=xxx` - -```bash -pdf2zh example.pdf -s deepl -``` - -- **DeepLX** - -参考 [DeepLX](https://github.com/OwO-Network/DeepLX) - -设置环境变量构建接入点:`{DEEPLX_SERVER_URL}/{DEEPLX_AUTH_KEY}/translate` - -- `DEEPLX_SERVER_URL`(可选), e.g., `export DEEPLX_SERVER_URL=https://api.deeplx.org` -- `DEEPLX_AUTH_KEY`, e.g., `export DEEPLX_AUTH_KEY=xxx` - -```bash -pdf2zh example.pdf -s deeplx -``` - -- **Ollama** +下表列出了每个翻译服务所需的环境变量,在使用相应服务之前,请确保已设置这些变量 -参考 [Ollama](https://github.com/ollama/ollama) +|**Translator**|**Service**|**Environment Variables**|**Default Values**|**Notes**| +|-|-|-|-|-| +|**Google (Default)**|`google`|None|N/A|None| +|**Bing**|`bing`|None|N/A|None| +|**DeepL**|`deepl`|`DEEPL_SERVER_URL`,`DEEPL_AUTH_KEY`|`https://api.deepl.com`, `[Your Key]`|See [DeepL](https://support.deepl.com/hc/en-us/articles/360020695820-API-Key-for-DeepL-s-API)| +|**DeepLX**|`deeplx`|`DEEPLX_ENDPOINT`|`https://api.deepl.com/translate`|See [DeepLX](https://github.com/OwO-Network/DeepLX)| +|**Ollama**|`ollama`|`OLLAMA_HOST`, `OLLAMA_MODEL`|`http://127.0.0.1:11434`, `gemma2`|See [Ollama](https://github.com/ollama/ollama)| +|**OpenAI**|`openai`|`OPENAI_BASE_URL`, `OPENAI_API_KEY`, `OPENAI_MODEL`|`https://api.openai.com/v1`, `[Your Key]`, `gpt-4o-mini`|See [OpenAI](https://platform.openai.com/docs/overview)| +|**Zhipu**|`zhipu`|`ZHIPU_API_KEY`, `ZHIPU_MODEL`|`[Your Key]`, `glm-4-flash`|See [Zhipu](https://open.bigmodel.cn/dev/api/thirdparty-frame/openai-sdk)| +|**Silicon**|`silicon`|`SILICON_API_KEY`, `SILICON_MODEL`|`[Your Key]`, `Qwen/Qwen2.5-7B-Instruct`|See [SiliconCloud](https://docs.siliconflow.cn/quickstart)| +|**Azure**|`azure`|`AZURE_ENDPOINT`, `AZURE_API_KEY`|`https://api.translator.azure.cn`, `[Your Key]`|See [Azure](https://docs.azure.cn/en-us/ai-services/translator/text-translation-overview)| +|**Tencent**|`tencent`|`TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY`|`[Your ID]`, `[Your Key]`|See [Tencent](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104)| -设置环境变量构建接入点:`{OLLAMA_HOST}/api/chat` - -- `OLLAMA_HOST`(可选), e.g., `export OLLAMA_HOST=https://localhost:11434` - -```bash -pdf2zh example.pdf -s ollama:gemma2 -``` - -- **支持 OpenAI 协议的 LLM(如 OpenAI、SiliconCloud、Zhipu)** - -参考 [SiliconCloud](https://docs.siliconflow.cn/quickstart), [Zhipu](https://open.bigmodel.cn/dev/api/thirdparty-frame/openai-sdk) - -设置环境变量构建接入点:`{OPENAI_BASE_URL}/chat/completions` - -- `OPENAI_BASE_URL`(可选), e.g., `export OPENAI_BASE_URL=https://api.openai.com/v1` -- `OPENAI_API_KEY`, e.g., `export OPENAI_API_KEY=xxx` +使用 `-s service` 或 `-s service:model` 指定翻译服务: ```bash pdf2zh example.pdf -s openai:gpt-4o-mini ``` -- **Azure** - -参考 [Azure Text Translation](https://docs.azure.cn/en-us/ai-services/translator/text-translation-overview) - -需设置以下环境变量: - -- `AZURE_APIKEY`, e.g., `export AZURE_APIKEY=xxx` -- `AZURE_ENDPOINT`, e.g., `export AZURE_ENDPOINT=https://api.translator.azure.cn/` -- `AZURE_REGION`, e.g., `export AZURE_REGION=chinaeast2` - -```bash -pdf2zh example.pdf -s azure -``` - -- **腾讯机器翻译** - -参考 [腾讯机器翻译](https://cloud.tencent.com/product/tmt) - -需设置以下环境变量: - -- `TENCENT_SECRET_ID`, e.g., `export TENCENT_SECRET_ID=AKIDxxx` -- `TENCENT_SECRET_KEY`, e.g., `export TENCENT_SECRET_KEY=xxx` +或者使用环境变量指定模型: ```bash -pdf2zh example.pdf -s tencent +set OPENAI_MODEL=gpt-4o-mini +pdf2zh example.pdf -s openai ```

指定例外规则

diff --git a/pdf2zh/translator.py b/pdf2zh/translator.py index c510d33f..ca885c25 100644 --- a/pdf2zh/translator.py +++ b/pdf2zh/translator.py @@ -121,29 +121,6 @@ def translate(self, text): return resp.json()[0]["translations"][0]["text"] -class TencentTranslator(BaseTranslator): - # https://github.com/TencentCloud/tencentcloud-sdk-python - name = "tencent" - envs = { - "TENCENTCLOUD_SECRET_ID": None, - "TENCENTCLOUD_SECRET_KEY": None, - } - - def __init__(self, service, lang_out, lang_in, model): - super().__init__(service, lang_out, lang_in, model) - cred = credential.DefaultCredentialProvider().get_credential() - self.client = TmtClient(cred, "ap-beijing") - self.req = TextTranslateRequest() - self.req.Source = self.lang_in - self.req.Target = self.lang_out - self.req.ProjectId = 0 - - def translate(self, text): - self.req.SourceText = text - resp: TextTranslateResponse = self.client.TextTranslate(self.req) - return resp.TargetText - - class DeepLTranslator(BaseTranslator): # https://github.com/DeepLcom/deepl-python name = "deepl" @@ -306,3 +283,26 @@ def translate(self, text) -> str: ) translated_text = response[0].translations[0].text return translated_text + + +class TencentTranslator(BaseTranslator): + # https://github.com/TencentCloud/tencentcloud-sdk-python + name = "tencent" + envs = { + "TENCENTCLOUD_SECRET_ID": None, + "TENCENTCLOUD_SECRET_KEY": None, + } + + def __init__(self, service, lang_out, lang_in, model): + super().__init__(service, lang_out, lang_in, model) + cred = credential.DefaultCredentialProvider().get_credential() + self.client = TmtClient(cred, "ap-beijing") + self.req = TextTranslateRequest() + self.req.Source = self.lang_in + self.req.Target = self.lang_out + self.req.ProjectId = 0 + + def translate(self, text): + self.req.SourceText = text + resp: TextTranslateResponse = self.client.TextTranslate(self.req) + return resp.TargetText