client.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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("--url", type=str, default="localhost:8001")
  32. args = parser.parse_args()
  33. client = triton_grpc.InferenceServerClient(args.url)
  34. input_ = {"file": utils.prepare_input_file(args.file)}
  35. if args.file_type is not None:
  36. input_["fileType"] = args.file_type
  37. if args.no_visualization:
  38. input_["visualize"] = False
  39. output = triton_request(client, "chatocr-visual", input_)
  40. ensure_no_error(output, "Failed to analyze the images")
  41. result_visual = output["result"]
  42. for i, res in enumerate(result_visual["layoutParsingResults"]):
  43. print(res["prunedResult"])
  44. for img_name, img in res["outputImages"].items():
  45. img_path = f"{img_name}_{i}.jpg"
  46. utils.save_output_file(img, img_path)
  47. print(f"Output image saved at {img_path}")
  48. input_ = {
  49. "visualInfo": result_visual["visualInfo"],
  50. }
  51. output = triton_request(client, "chatocr-vector", input_)
  52. ensure_no_error(output, "Failed to build a vector store")
  53. result_vector = output["result"]
  54. input_ = {
  55. "keyList": args.key_list,
  56. "visualInfo": result_visual["visualInfo"],
  57. "useVectorRetrieval": True,
  58. "vectorInfo": result_vector["vectorInfo"],
  59. }
  60. output = triton_request(client, "chatocr-chat", input_)
  61. ensure_no_error(output, "Failed to chat with the LLM")
  62. result_chat = output["result"]
  63. print("Final result:")
  64. print(result_chat["chatResult"])
  65. if __name__ == "__main__":
  66. main()