client.py 2.2 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 paddlex_hps_client import triton_request, utils
  18. from tritonclient import grpc as triton_grpc
  19. def main():
  20. parser = argparse.ArgumentParser()
  21. parser.add_argument("--image", type=str, required=True)
  22. parser.add_argument("--query", type=str, required=True)
  23. parser.add_argument("--max-image-tokens", type=int, default=None)
  24. parser.add_argument("--url", type=str, default="localhost:8001")
  25. args = parser.parse_args()
  26. client = triton_grpc.InferenceServerClient(
  27. args.url,
  28. # HACK
  29. keepalive_options=triton_grpc.KeepAliveOptions(keepalive_timeout_ms=1000000),
  30. )
  31. image = utils.prepare_input_file(args.image, include_header=True)
  32. input_ = {
  33. "model": "pp-docbee",
  34. "messages": [
  35. {"role": "system", "content": "You are a helpful assistant."},
  36. {
  37. "role": "user",
  38. "content": [
  39. {"type": "text", "text": args.query},
  40. {"type": "image_url", "image_url": {"url": image}},
  41. ],
  42. },
  43. ],
  44. "max_image_tokens": args.max_image_tokens,
  45. }
  46. output = triton_request(client, "document-understanding", input_)
  47. if output["errorCode"] != 0:
  48. print(f"Error code: {output['errorCode']}", file=sys.stderr)
  49. print(f"Error message: {output['errorMsg']}", file=sys.stderr)
  50. sys.exit(1)
  51. result = output["result"]
  52. print("Final result:")
  53. print(result["choices"][0]["message"]["content"])
  54. if __name__ == "__main__":
  55. main()