Kaynağa Gözat

support to interactive get pipeline

gaotingquan 1 yıl önce
ebeveyn
işleme
0a9a37f4e6

+ 1 - 1
paddlex/configs/formula_recognition/LaTeX_OCR_rec.yaml

@@ -26,7 +26,7 @@ Train:
   save_interval: 1
 
 Evaluate:
-  weight_path: "output/bbest_accurac/best_accuracy.pdparams"
+  weight_path: output/best_accuracy/best_accuracy.pdparams
   log_interval: 1
 
 Export:

+ 7 - 8
paddlex/inference/pipelines/__init__.py

@@ -16,6 +16,7 @@ from pathlib import Path
 from typing import Any, Dict, Optional
 
 from ...utils.config import parse_config
+from ..utils.get_pipeline_path import get_pipeline_path
 from .base import BasePipeline
 from .single_model_pipeline import (
     _SingleModelPipeline,
@@ -53,14 +54,12 @@ def create_pipeline(
         BasePipeline: the pipeline, which is subclass of BasePipeline.
     """
     if not Path(pipeline).exists():
-        # XXX: using dict class to handle all pipeline configs
-        build_in_pipeline = (
-            Path(__file__).parent.parent.parent / "pipelines" / f"{pipeline}.yaml"
-        ).resolve()
-        if not Path(build_in_pipeline).exists():
-            raise Exception(f"The pipeline don't exist! ({pipeline})")
-        pipeline = build_in_pipeline
-    config = parse_config(pipeline)
+        pipeline_path = get_pipeline_path(pipeline)
+        if pipeline_path is None:
+            raise Exception(
+                f"The pipeline({pipeline}) don't exist! Please use the pipeline name or config yaml file!"
+            )
+    config = parse_config(pipeline_path)
     pipeline_name = config["Global"]["pipeline_name"]
     pipeline_setting = config["Pipeline"]
 

+ 1 - 1
paddlex/inference/pipelines/ppchatocrv3/ppchatocrv3.py

@@ -40,7 +40,7 @@ PROMPT_FILE = os.path.join(os.path.dirname(__file__), "ch_prompt.yaml")
 class PPChatOCRPipeline(TableRecPipeline):
     """PP-ChatOCRv3 Pileline"""
 
-    entities = "PP-ChatOCRv3"
+    entities = "PP-ChatOCRv3-doc"
 
     def __init__(
         self,

+ 26 - 0
paddlex/inference/utils/get_pipeline_path.py

@@ -0,0 +1,26 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+from pathlib import Path
+
+
+def get_pipeline_path(pipeline_name):
+    # XXX: using dict class to handle all pipeline configs
+    pipeline_path = (
+        Path(__file__).parent.parent.parent / "pipelines" / f"{pipeline_name}.yaml"
+    ).resolve()
+    if not Path(pipeline_path).exists():
+        return None
+    return pipeline_path

+ 13 - 8
paddlex/paddlex_cli.py

@@ -20,6 +20,7 @@ from types import SimpleNamespace
 from . import create_pipeline
 from .repo_manager import setup, get_all_supported_repo_names
 from .utils import logging
+from .utils.interactive_get_pipeline import interactive_get_pipeline
 
 
 def args_cfg():
@@ -61,7 +62,7 @@ def args_cfg():
 
     ################# pipeline predict #################
     parser.add_argument("--pipeline", type=str, help="")
-    parser.add_argument("--input", type=str, help="")
+    parser.add_argument("--input", type=str, default=None, help="")
     parser.add_argument("--save_path", type=str, default=None, help="")
     parser.add_argument("--device", type=str, default=None, help="")
 
@@ -90,7 +91,8 @@ def install(args):
 
 def pipeline_predict(pipeline, input, device=None, save_path=None):
     """pipeline predict"""
-    pipeline = create_pipeline(pipeline, device=device)
+    predictor_kwargs = {"device": device} if device else {}
+    pipeline = create_pipeline(pipeline)
     result = pipeline(input)
     for res in result:
         res.print(json_format=False)
@@ -105,9 +107,12 @@ def main():
     if args.install:
         install(args)
     else:
-        return pipeline_predict(
-            args.pipeline,
-            args.input,
-            args.device,
-            args.save_path,
-        )
+        if args.input is None:
+            interactive_get_pipeline(args.pipeline)
+        else:
+            return pipeline_predict(
+                args.pipeline,
+                args.input,
+                args.device,
+                args.save_path,
+            )

+ 1 - 1
paddlex/pipelines/PP-ChatOCRv3.yaml → paddlex/pipelines/PP-ChatOCRv3-doc.yaml

@@ -1,5 +1,5 @@
 Global:
-  pipeline_name: PP-ChatOCRv3
+  pipeline_name: PP-ChatOCRv3-doc
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/contract.pdf
   #input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/report.png
 Pipeline:

+ 54 - 0
paddlex/utils/interactive_get_pipeline.py

@@ -0,0 +1,54 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from pathlib import Path
+import shutil
+
+from ..utils import logging
+from ..inference.utils.get_pipeline_path import get_pipeline_path
+
+
+def interactive_get_pipeline(pipeline):
+    file_path = get_pipeline_path(pipeline)
+    file_name = Path(file_path).name
+
+    target_path = (
+        input(
+            "Please enter the path that you want to save the pipeline config file: (default `./`)\n"
+        )
+        or "."
+    )
+    target_path = Path(target_path)
+
+    if not target_path.suffix in (".yaml", ".yml"):
+        if not target_path.exists():
+            try:
+                target_path.mkdir(parents=True, exist_ok=True)
+            except Exception as e:
+                logging.error(f"Failed to create directory: {e}")
+                return
+        target_path = target_path / file_name
+
+    if target_path.exists():
+        overwrite = input(
+            f"The file({target_path}) already exists. Is it covered? (y/N): \n"
+        ).lower()
+        if overwrite != "y":
+            logging.warning("Exit!")
+            return
+    try:
+        shutil.copy2(file_path, target_path)
+        logging.info(f"The pipeline config has been saved to: {target_path}")
+    except Exception as e:
+        logging.error(f"File saving failed: {e}")