Browse Source

添加网页认证选项。

hellofinch 11 months ago
parent
commit
bf252754a2
4 changed files with 80 additions and 13 deletions
  1. 2 1
      README.md
  2. 2 1
      README_zh-CN.md
  3. 67 10
      pdf2zh/gui.py
  4. 9 1
      pdf2zh/pdf2zh.py

+ 2 - 1
README.md

@@ -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>
 

+ 2 - 1
README_zh-CN.md

@@ -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>
 

+ 67 - 10
pdf2zh/gui.py

@@ -438,25 +438,82 @@ with gr.Blocks(
     )
 
 
-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

+ 9 - 1
pdf2zh/pdf2zh.py

@@ -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
 
@@ -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: