|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +import sys |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from paddlex_hps_client import triton_request, utils |
| 22 | +from tritonclient import grpc as triton_grpc |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + parser = argparse.ArgumentParser() |
| 27 | + parser.add_argument("--file", type=str, required=True) |
| 28 | + parser.add_argument("--file-type", type=int, choices=[0, 1]) |
| 29 | + parser.add_argument("--no-visualization", action="store_true") |
| 30 | + parser.add_argument("--url", type=str, default="localhost:8001") |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + client = triton_grpc.InferenceServerClient(args.url) |
| 35 | + input_ = {"file": utils.prepare_input_file(args.file)} |
| 36 | + if args.file_type is not None: |
| 37 | + input_["fileType"] = args.file_type |
| 38 | + if args.no_visualization: |
| 39 | + input_["visualize"] = False |
| 40 | + output = triton_request(client, "layout-parsing", input_) |
| 41 | + if output["errorCode"] != 0: |
| 42 | + print(f"Error code: {output['errorCode']}", file=sys.stderr) |
| 43 | + print(f"Error message: {output['errorMsg']}", file=sys.stderr) |
| 44 | + sys.exit(1) |
| 45 | + result = output["result"] |
| 46 | + for i, res in enumerate(result["layoutParsingResults"]): |
| 47 | + print(res["prunedResult"]) |
| 48 | + md_dir = Path(f"markdown_{i}") |
| 49 | + md_dir.mkdir(exist_ok=True) |
| 50 | + (md_dir / "doc.md").write_text(res["markdown"]["text"]) |
| 51 | + for img_path, img in res["markdown"]["images"].items(): |
| 52 | + img_path = md_dir / img_path |
| 53 | + img_path.parent.mkdir(parents=True, exist_ok=True) |
| 54 | + utils.save_output_file(img, img_path) |
| 55 | + print(f"Markdown document saved at {md_dir / 'doc.md'}") |
| 56 | + for img_name, img in res["outputImages"].items(): |
| 57 | + img_path = f"{img_name}_{i}.jpg" |
| 58 | + Path(img_path).parent.mkdir(exist_ok=True) |
| 59 | + utils.save_output_file(img, img_path) |
| 60 | + print(f"Output image saved at {img_path}") |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments