client.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 paddlex_hps_client import triton_request, utils
  18. from tritonclient import grpc as triton_grpc
  19. def ensure_no_error(output, additional_msg):
  20. if output["errorCode"] != 0:
  21. print(additional_msg, file=sys.stderr)
  22. print(f"Error code: {output['errorCode']}", file=sys.stderr)
  23. print(f"Error message: {output['errorMsg']}", file=sys.stderr)
  24. sys.exit(1)
  25. def main():
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument("--file", type=str, required=True)
  28. parser.add_argument("--key-list", type=str, nargs="+", required=True)
  29. parser.add_argument("--file-type", type=int, choices=[0, 1])
  30. parser.add_argument("--no-visualization", action="store_true")
  31. parser.add_argument("--invoke-mllm", 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, "chatocr-visual", input_)
  41. ensure_no_error(output, "Failed to analyze the images")
  42. result_visual = output["result"]
  43. for i, res in enumerate(result_visual["layoutParsingResults"]):
  44. print(res["prunedResult"])
  45. for img_name, img in res["outputImages"].items():
  46. img_path = f"{img_name}_{i}.jpg"
  47. utils.save_output_file(img, img_path)
  48. print(f"Output image saved at {img_path}")
  49. input_ = {
  50. "visualInfo": result_visual["visualInfo"],
  51. }
  52. output = triton_request(client, "chatocr-vector", input_)
  53. ensure_no_error(output, "Failed to build a vector store")
  54. result_vector = output["result"]
  55. if args.invoke_mllm:
  56. input_ = {
  57. "image": utils.prepare_input_file(args.file),
  58. "keyList": args.key_list,
  59. }
  60. output = triton_request(client, "chatocr-mllm", input_)
  61. ensure_no_error(output, "Failed to invoke the MLLM")
  62. result_mllm = output["result"]
  63. input_ = {
  64. "keyList": args.key_list,
  65. "visualInfo": result_visual["visualInfo"],
  66. "useVectorRetrieval": True,
  67. "vectorInfo": result_vector["vectorInfo"],
  68. }
  69. if args.invoke_mllm:
  70. input_["mllmPredictInfo"] = result_mllm["mllmPredictInfo"]
  71. output = triton_request(client, "chatocr-chat", input_)
  72. ensure_no_error(output, "Failed to chat with the LLM")
  73. result_chat = output["result"]
  74. print("Final result:")
  75. print(result_chat["chatResult"])
  76. if __name__ == "__main__":
  77. main()