client.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ensure_no_error(output, additional_msg):
  21. if output["errorCode"] != 0:
  22. print(additional_msg, file=sys.stderr)
  23. print(f"Error code: {output['errorCode']}", file=sys.stderr)
  24. print(f"Error message: {output['errorMsg']}", file=sys.stderr)
  25. sys.exit(1)
  26. def main():
  27. parser = argparse.ArgumentParser()
  28. parser.add_argument("--file", type=str, required=True)
  29. parser.add_argument("--target-language", type=str, default="zh")
  30. parser.add_argument("--file-type", type=int, choices=[0, 1])
  31. parser.add_argument("--no-visualization", action="store_true")
  32. parser.add_argument("--url", type=str, default="localhost:8001")
  33. args = parser.parse_args()
  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, "doctrans-visual", input_)
  41. ensure_no_error(output, "Failed to analyze the images")
  42. result_visual = output["result"]
  43. markdown_list = []
  44. for i, res in enumerate(result_visual["layoutParsingResults"]):
  45. print(res["prunedResult"])
  46. md_dir = Path(f"markdown_{i}")
  47. md_dir.mkdir(exist_ok=True)
  48. (md_dir / "doc.md").write_text(res["markdown"]["text"])
  49. for img_path, img in res["markdown"]["images"].items():
  50. img_path = md_dir / img_path
  51. img_path.parent.mkdir(parents=True, exist_ok=True)
  52. utils.save_output_file(img, img_path)
  53. print(f"Markdown document to be translated is saved at {md_dir / 'doc.md'}")
  54. del res["markdown"]["images"]
  55. markdown_list.append(res["markdown"])
  56. for img_name, img in res["outputImages"].items():
  57. img_path = f"{img_name}_{i}.jpg"
  58. utils.save_output_file(img, img_path)
  59. print(f"Output image saved at {img_path}")
  60. input_ = {
  61. "markdownList": markdown_list,
  62. "targetLanguage": args.target_language,
  63. }
  64. output = triton_request(client, "doctrans-translate", input_)
  65. ensure_no_error(output, "Failed to translate the markdown")
  66. result_translate = output["result"]
  67. for i, res in enumerate(result_translate["translationResults"]):
  68. md_dir = Path(f"markdown_{i}")
  69. (md_dir / "doc_translated.md").write_text(res["markdown"]["text"])
  70. print(f"Translated markdown document saved at {md_dir / 'doc_translated.md'}")
  71. if __name__ == "__main__":
  72. main()