Skip to content

Commit

Permalink
feat (gui): option for gui authentication #177
Browse files Browse the repository at this point in the history
  • Loading branch information
reycn authored Dec 17, 2024
2 parents d2d36bd + 2dc8699 commit 457a0ce
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ In the following table, we list all advanced options for reference:
| `-t` | [Multi-threads](#threads) | `pdf2zh example.pdf -t 1` |
| `-o` | Output dir | `pdf2zh example.pdf -o output` |
| `-f`, `-c` | [Exceptions](#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `--share` | Get gradio public link | `pdf2zh -i --share` |
| `--share` | [Get gradio public link] | `pdf2zh -i --share` |
| `-a` | [add authorization and custom login page] | `pdf2zh -i -a users.txt [auth.html]` |

<h3 id="partial">Full / partial document translation</h3>

Expand Down
3 changes: 2 additions & 1 deletion README_ja-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ Python環境を事前にインストールする必要はありません
| `-t` | [マルチスレッド](#threads) | `pdf2zh example.pdf -t 1` |
| `-o` | 出力ディレクトリ | `pdf2zh example.pdf -o output` |
| `-f`, `-c` | [例外](#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `--share` | gradio公開リンクを取得 | `pdf2zh -i --share` |
| `--share` | [gradio公開リンクを取得] | `pdf2zh -i --share` |
| `-a` | [ウェブ認証とカスタム認証ページの追加] | `pdf2zh -i -a users.txt [auth.html]` |

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

Expand Down
3 changes: 2 additions & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
| `-t` | [多线程](#threads) | `pdf2zh example.pdf -t 1` |
| `-o` | 输出目录 | `pdf2zh example.pdf -o output` |
| `-f`, `-c` | [例外规则](#exceptions) | `pdf2zh example.pdf -f "(MS.*)"` |
| `--share` | 获取 gradio 公开链接 | `pdf2zh -i --share` |
| `--share` | [获取 gradio 公开链接] | `pdf2zh -i --share` |
| `-a` | [添加网页认证和自定义认证页] | `pdf2zh -i -a users.txt [auth.html]` |

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

Expand Down
77 changes: 67 additions & 10 deletions pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,25 +438,82 @@ def on_select_filetype(file_type):
)


def setup_gui(share=False):
def readuserandpasswd(file_path):
tuple_list = []
content = ""
if len(file_path) == 2:
try:
with open(file_path[1], "r", encoding="utf-8") as file:
content = file.read()
except FileNotFoundError:
print(f"Error: File '{file_path[1]}' not found.")
try:
with open(file_path[0], "r", encoding="utf-8") as file:
tuple_list = [
tuple(line.strip().split(",")) for line in file if line.strip()
]
except FileNotFoundError:
print(f"Error: File '{file_path[0]}' not found.")
return tuple_list, content


def setup_gui(share=False, authfile=["", ""]):
userlist, html = readuserandpasswd(authfile)
if flag_demo:
demo.launch(server_name="0.0.0.0", max_file_size="5mb", inbrowser=True)
else:
try:
demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=share)
except Exception:
print(
"Error launching GUI using 0.0.0.0.\nThis may be caused by global mode of proxy software."
)
if len(userlist) == 0:
try:
demo.launch(
server_name="127.0.0.1", debug=True, inbrowser=True, share=share
server_name="0.0.0.0", debug=True, inbrowser=True, share=share
)
except Exception:
print(
"Error launching GUI using 127.0.0.1.\nThis may be caused by global mode of proxy software."
"Error launching GUI using 0.0.0.0.\nThis may be caused by global mode of proxy software."
)
demo.launch(debug=True, inbrowser=True, share=True)
try:
demo.launch(
server_name="127.0.0.1", debug=True, inbrowser=True, share=share
)
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)
else:
try:
demo.launch(
server_name="0.0.0.0",
debug=True,
inbrowser=True,
share=share,
auth=userlist,
auth_message=html,
)
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,
auth=userlist,
auth_message=html,
)
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,
auth=userlist,
auth_message=html,
)


# For auto-reloading while developing
Expand Down
10 changes: 9 additions & 1 deletion pdf2zh/pdf2zh.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def create_parser() -> argparse.ArgumentParser:
action="store_true",
help="celery",
)
parse_params.add_argument(
"--authorized",
"-a",
type=str,
nargs="+",
default=["./users.txt", "./auth.html"],
help="user name and password.",
)

return parser

Expand Down Expand Up @@ -146,7 +154,7 @@ def main(args: Optional[List[str]] = None) -> int:
if parsed_args.interactive:
from pdf2zh.gui import setup_gui

setup_gui(parsed_args.share)
setup_gui(parsed_args.share, parsed_args.authorized)
return 0

if parsed_args.flask:
Expand Down

0 comments on commit 457a0ce

Please sign in to comment.