소스 검색

feat: 添加分享功能

Esing 1 년 전
부모
커밋
bb6a820810
2개의 변경된 파일40개의 추가작업 그리고 14개의 파일을 삭제
  1. 28 12
      pdf2zh/gui.py
  2. 12 2
      pdf2zh/pdf2zh.py

+ 28 - 12
pdf2zh/gui.py

@@ -694,23 +694,39 @@ with gr.Blocks(
     # gr.Markdown("Technical details", elem_classes=["info-text"])
 
 
-def setup_gui():
-    try:
-        demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=False)
-    except Exception:
-        print(
-            "Error launching GUI using 0.0.0.0.\nThis may be caused by global mode of proxy software."
-        )
+def setup_gui(enable_gradio_share=False):
+    if(enable_gradio_share):
         try:
-            demo.launch(
-                server_name="127.0.0.1", debug=True, inbrowser=True, share=False
+            demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=True)
+        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=True
+                )
+            except Exception:
+                print(
+                    "Error launching GUI using 127.0.0.1.\nThis may be caused by global mode of proxy software."
+                )
+                demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=True)
+    else:
+        try:
+            demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=False)
         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(server_name="0.0.0.0", debug=True, inbrowser=True, share=True)
-
+            try:
+                demo.launch(
+                    server_name="127.0.0.1", debug=True, inbrowser=True, share=False
+                )
+            except Exception:
+                print(
+                    "Error launching GUI using 127.0.0.1.\nThis may be caused by global mode of proxy software.Force enable Gradio sharing feature."
+                )
+                demo.launch(server_name="0.0.0.0", debug=True, inbrowser=True, share=True)
 
 # For auto-reloading while developing
 if __name__ == "__main__":

+ 12 - 2
pdf2zh/pdf2zh.py

@@ -235,6 +235,12 @@ def create_parser() -> argparse.ArgumentParser:
         action="store_true",
         help="Interact with GUI.",
     )
+    parse_params.add_argument(
+        "--share",
+        type=str,
+        action="store_true",
+        help="Enable Gradio Share",
+    )
 
     return parser
 
@@ -267,8 +273,12 @@ def main(args: Optional[List[str]] = None) -> int:
     if parsed_args.interactive:
         from pdf2zh.gui import setup_gui
 
-        setup_gui()
-        return 0
+        if parse_args.share:
+            setup_gui(True)
+            return 0
+        else:
+            setup_gui()
+            return 0
 
     setup_log()
     extract_text(**vars(parsed_args))