Skip to content

Commit

Permalink
Merge pull request #320 from hellofinch/main
Browse files Browse the repository at this point in the history
feat (main) : add cli option and argostranslator
  • Loading branch information
Byaidu authored Dec 28, 2024
2 parents 246e3a1 + c9d9e67 commit 30075a8
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ In the following table, we list all advanced options for reference:
| `-f`, `-c` | [Exceptions](https://github.com/Byaidu/PDFMathTranslate/blob/main/docs/ADVANCED.md#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `-cp` | Compatibility Mode | `pdf2zh example.pdf --compatible` |
| `--share` | Public link | `pdf2zh -i --share` |
| `--authorized` | Authorization | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--authorized` | [Authorization](https://github.com/Byaidu/PDFMathTranslate/blob/main/docs/ADVANCED.md#auth) | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--prompt` | [Custom Prompt](https://github.com/Byaidu/PDFMathTranslate/blob/main/docs/ADVANCED.md#prompt) | `pdf2zh --prompt [prompt.txt]` |
| `--onnx` | [Use Custom DocLayout-YOLO ONNX model] | `pdf2zh --onnx [onnx/model/path]` |
| `--serverport` | [Use Custom WebUI port] | `pdf2zh --serverport 7860` |

For detailed explanations, please refer to our document about [Advanced Usage](./docs/ADVANCED.md) for a full list of each option.

Expand Down
43 changes: 42 additions & 1 deletion docs/ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ We've provided a detailed table on the required [environment variables](https://
| **Tencent** | `tencent` | `TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY` | `[Your ID]`, `[Your Key]` | See [Tencent](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104) |
| **Dify** | `dify` | `DIFY_API_URL`, `DIFY_API_KEY` | `[Your DIFY URL]`, `[Your Key]` | See [Dify](https://github.com/langgenius/dify),Three variables, lang_out, lang_in, and text, need to be defined in Dify's workflow input. |
| **AnythingLLM** | `anythingllm` | `AnythingLLM_URL`, `AnythingLLM_APIKEY` | `[Your AnythingLLM URL]`, `[Your Key]` | See [anything-llm](https://github.com/Mintplex-Labs/anything-llm) |
|**Argos Translate**|`argos`| | |See [argos-translate](https://github.com/argosopentech/argos-translate)|

For large language models that are compatible with the OpenAI API but not listed in the table above, you can set environment variables using the same method outlined for OpenAI in the table.

Use `-s service` or `-s service:model` to specify service:

Expand Down Expand Up @@ -117,7 +120,7 @@ pdf2zh example.pdf -t 1
Use `--prompt` to specify which prompt to use in llm:

```bash
pdf2zh example.pdf -pr prompt.txt
pdf2zh example.pdf --prompt prompt.txt
```

example prompt.txt
Expand Down Expand Up @@ -145,3 +148,41 @@ In custom prompt file, there are three variables can be used.
[⬆️ Back to top](#toc)

---

<h3 id="auth">Authorization</h3>

Use `--authorized` to specify which user to use Web UI and custom the login page:

```bash
pdf2zh example.pdf --authorized users.txt auth.html
```

example users.txt
Each line contains two elements, username, and password, separated by a comma.

```
admin,123456
user1,password1
user2,abc123
guest,guest123
test,test123
```

example auth.html

```html
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Welcome to my simple HTML page.</p>
</body>
</html>
```

[⬆️ Back to top](#toc)

---
2 changes: 1 addition & 1 deletion docs/APIS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[**Documentation**](https://github.com/Byaidu/PDFMathTranslate) > **API Details** _(current)_

<h2 id="toc">Table of Content</h2>
The present project supports two types of APIs;
The present project supports two types of APIs, All methods need the Redis;

- [Functional calls in Python](#api-python)
- [HTTP protocols](#api-http)
Expand Down
8 changes: 7 additions & 1 deletion docs/README_ja-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ Python環境を事前にインストールする必要はありません
| `-o` | 出力ディレクトリ | `pdf2zh example.pdf -o output` |
| `-f`, `-c` | [例外](#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `--share` | [gradio公開リンクを取得] | `pdf2zh -i --share` |
| `--authorized` | [ウェブ認証とカスタム認証ページの追加] | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--authorized` | [[ウェブ認証とカスタム認証ページの追加](https://github.com/Byaidu/PDFMathTranslate/blob/main/docs/ADVANCED.)] | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--prompt` | [カスタムビッグモデルのプロンプトを使用する] | `pdf2zh --prompt [prompt.txt]` |
| `--onnx` | [カスタムDocLayout-YOLO ONNXモデルの使用] | `pdf2zh --onnx [onnx/model/path]` |
| `--serverport` | [カスタムWebUIポートを使用する] | `pdf2zh --serverport 7860` |

<h3 id="partial">全文または部分的なドキュメント翻訳</h3>

Expand Down Expand Up @@ -221,6 +223,10 @@ pdf2zh example.pdf -li en -lo ja
|**Tencent**|`tencent`|`TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY`|`[Your ID]`, `[Your Key]`|See [Tencent](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104)|
|**Dify**|`dify`|`DIFY_API_URL`, `DIFY_API_KEY`|`[Your DIFY URL]`, `[Your Key]`|See [Dify](https://github.com/langgenius/dify),Three variables, lang_out, lang_in, and text, need to be defined in Dify's workflow input.|
|**AnythingLLM**|`anythingllm`|`AnythingLLM_URL`, `AnythingLLM_APIKEY`|`[Your AnythingLLM URL]`, `[Your Key]`|See [anything-llm](https://github.com/Mintplex-Labs/anything-llm)|
|**Argos Translate**|`argos`| | |See [argos-translate](https://github.com/argosopentech/argos-translate)|
(need Japenese translation)
For large language models that are compatible with the OpenAI API but not listed in the table above, you can set environment variables using the same method outlined for OpenAI in the table.
`-s service` または `-s service:model` を使用してサービスを指定します:
Expand Down
7 changes: 6 additions & 1 deletion docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ set HF_ENDPOINT=https://hf-mirror.com
| `-o` | 输出目录 | `pdf2zh example.pdf -o output` |
| `-f`, `-c` | [例外规则](#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `--share` | [获取 gradio 公开链接] | `pdf2zh -i --share` |
| `--authorized` | [添加网页认证和自定义认证页] | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--authorized` | [[添加网页认证和自定义认证页](https://github.com/Byaidu/PDFMathTranslate/blob/main/docs/ADVANCED.)] | `pdf2zh -i --authorized users.txt [auth.html]` |
| `--prompt` | [使用自定义的大模型prompt] | `pdf2zh --prompt [prompt.txt]` |
| `--onnx` | [使用自定义的 DocLayout-YOLO ONNX 模型] | `pdf2zh --onnx [onnx/model/path]` |
| `--serverport` | [使用自定义的 WebUI 端口] | `pdf2zh --serverport 7860` |

<h3 id="partial">全文或部分文档翻译</h3>

Expand Down Expand Up @@ -220,6 +222,9 @@ pdf2zh example.pdf -li en -lo ja
|**Tencent**|`tencent`|`TENCENTCLOUD_SECRET_ID`, `TENCENTCLOUD_SECRET_KEY`|`[Your ID]`, `[Your Key]`|See [Tencent](https://www.tencentcloud.com/products/tmt?from_qcintl=122110104)|
|**Dify**|`dify`|`DIFY_API_URL`, `DIFY_API_KEY`|`[Your DIFY URL]`, `[Your Key]`|See [Dify](https://github.com/langgenius/dify),Three variables, lang_out, lang_in, and text, need to be defined in Dify's workflow input.|
|**AnythingLLM**|`anythingllm`|`AnythingLLM_URL`, `AnythingLLM_APIKEY`|`[Your AnythingLLM URL]`, `[Your Key]`|See [anything-llm](https://github.com/Mintplex-Labs/anything-llm)|
|**Argos Translate**|`argos`| | |See [argos-translate](https://github.com/argosopentech/argos-translate)|
对于未在上述表格中的,并且兼容 OpenAI api 的大语言模型,可使用表格中的 OpenAI 的方式进行环境变量的设置。
使用 `-s service` 或 `-s service:model` 指定翻译服务:
Expand Down
2 changes: 2 additions & 0 deletions pdf2zh/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tqdm
import json
import io
from pdf2zh.pdf2zh import model

flask_app = Flask("pdf2zh")
flask_app.config.from_mapping(
Expand Down Expand Up @@ -47,6 +48,7 @@ def progress_bar(t: tqdm.tqdm):
doc_mono, doc_dual = translate_stream(
stream,
callback=progress_bar,
model=model,
**args,
)
return doc_mono, doc_dual
Expand Down
3 changes: 2 additions & 1 deletion pdf2zh/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TencentTranslator,
DifyTranslator,
AnythingLLMTranslator,
ArgosTranslator,
)
from pymupdf import Font

Expand Down Expand Up @@ -149,7 +150,7 @@ def __init__(
service_name = param[0]
service_model = param[1] if len(param) > 1 else None
for translator in [GoogleTranslator, BingTranslator, DeepLTranslator, DeepLXTranslator, OllamaTranslator, AzureOpenAITranslator,
OpenAITranslator, ZhipuTranslator, ModelScopeTranslator, SiliconTranslator, GeminiTranslator, AzureTranslator, TencentTranslator, DifyTranslator, AnythingLLMTranslator]:
OpenAITranslator, ZhipuTranslator, ModelScopeTranslator, SiliconTranslator, GeminiTranslator, AzureTranslator, TencentTranslator, DifyTranslator, AnythingLLMTranslator, ArgosTranslator]:
if service_name == translator.name:
self.translator = translator(lang_in, lang_out, service_model, envs=envs, prompt=prompt)
if not self.translator:
Expand Down
27 changes: 23 additions & 4 deletions pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from pdf2zh import __version__
from pdf2zh.high_level import translate
from pdf2zh.pdf2zh import model
from pdf2zh.translator import (
AnythingLLMTranslator,
AzureOpenAITranslator,
Expand All @@ -22,6 +23,7 @@
DeepLTranslator,
DeepLXTranslator,
DifyTranslator,
ArgosTranslator,
GeminiTranslator,
GoogleTranslator,
ModelScopeTranslator,
Expand Down Expand Up @@ -49,6 +51,7 @@
"Tencent": TencentTranslator,
"Dify": DifyTranslator,
"AnythingLLM": AnythingLLMTranslator,
"Argos Translate": ArgosTranslator,
}

# The following variables associate strings with specific languages
Expand Down Expand Up @@ -263,6 +266,7 @@ def progress_bar(t: tqdm.tqdm):
"cancellation_event": cancellation_event_map[session_id],
"envs": _envs,
"prompt": prompt,
"model": model,
}
try:
translate(**param)
Expand Down Expand Up @@ -585,7 +589,9 @@ def parse_user_passwd(file_path: str) -> tuple:
return tuple_list, content


def setup_gui(share: bool = False, auth_file: list = ["", ""]) -> None:
def setup_gui(
share: bool = False, auth_file: list = ["", ""], server_port=7860
) -> None:
"""
Setup the GUI with the given parameters.
Expand All @@ -603,21 +609,31 @@ def setup_gui(share: bool = False, auth_file: list = ["", ""]) -> None:
if len(user_list) == 0:
try:
demo.launch(
server_name="0.0.0.0", debug=True, inbrowser=True, share=share
server_name="0.0.0.0",
debug=True,
inbrowser=True,
share=share,
server_port=server_port,
)
except Exception:
print(
"Error launching GUI using 0.0.0.0.\nThis may be caused by global mode of proxy software."
)
try:
demo.launch(
server_name="127.0.0.1", debug=True, inbrowser=True, share=share
server_name="127.0.0.1",
debug=True,
inbrowser=True,
share=share,
server_port=server_port,
)
except Exception:
print(
"Error launching GUI using 127.0.0.1.\nThis may be caused by global mode of proxy software."
)
demo.launch(debug=True, inbrowser=True, share=True)
demo.launch(
debug=True, inbrowser=True, share=True, server_port=server_port
)
else:
try:
demo.launch(
Expand All @@ -627,6 +643,7 @@ def setup_gui(share: bool = False, auth_file: list = ["", ""]) -> None:
share=share,
auth=user_list,
auth_message=html,
server_port=server_port,
)
except Exception:
print(
Expand All @@ -640,6 +657,7 @@ def setup_gui(share: bool = False, auth_file: list = ["", ""]) -> None:
share=share,
auth=user_list,
auth_message=html,
server_port=server_port,
)
except Exception:
print(
Expand All @@ -651,6 +669,7 @@ def setup_gui(share: bool = False, auth_file: list = ["", ""]) -> None:
share=True,
auth=user_list,
auth_message=html,
server_port=server_port,
)


Expand Down
10 changes: 5 additions & 5 deletions pdf2zh/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
from pymupdf import Document, Font

from pdf2zh.converter import TranslateConverter
from pdf2zh.doclayout import DocLayoutModel
from pdf2zh.doclayout import OnnxModel
from pdf2zh.pdfinterp import PDFPageInterpreterEx

model = DocLayoutModel.load_available()

resfont_map = {
"zh-cn": "china-ss",
"zh-tw": "china-ts",
Expand Down Expand Up @@ -88,6 +86,7 @@ def translate_patch(
noto: Font = None,
callback: object = None,
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
**kwarg: Any,
) -> None:
rsrcmgr = PDFResourceManager()
Expand Down Expand Up @@ -179,6 +178,7 @@ def translate_stream(
vchar: str = "",
callback: object = None,
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
**kwarg: Any,
):
font_list = [("tiro", None)]
Expand Down Expand Up @@ -234,7 +234,7 @@ def translate_stream(

fp = io.BytesIO()
doc_zh.save(fp)
obj_patch: dict = translate_patch(fp, prompt=kwarg["prompt"], **locals())
obj_patch: dict = translate_patch(fp, **locals())

for obj_id, ops_new in obj_patch.items():
# ops_old=doc_en.xref_stream(obj_id)
Expand Down Expand Up @@ -312,6 +312,7 @@ def translate(
callback: object = None,
compatible: bool = False,
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
**kwarg: Any,
):
if not files:
Expand Down Expand Up @@ -364,7 +365,6 @@ def translate(

if file.startswith(tempfile.gettempdir()):
os.unlink(file)

s_mono, s_dual = translate_stream(
s_raw,
envs=kwarg.get("envs", {}),
Expand Down
Loading

0 comments on commit 30075a8

Please sign in to comment.