client.py 2.4 KB

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