client.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import base64
  2. import requests
  3. import numpy as np
  4. from loguru import logger
  5. from joblib import Parallel, delayed
  6. def to_b64(file_path):
  7. try:
  8. with open(file_path, 'rb') as f:
  9. return base64.b64encode(f.read()).decode('utf-8')
  10. except Exception as e:
  11. raise Exception(f'File: {file_path} - Info: {e}')
  12. def do_parse(file_path, url='http://127.0.0.1:8000/predict', **kwargs):
  13. try:
  14. response = requests.post(url, json={
  15. 'file': to_b64(file_path),
  16. 'kwargs': kwargs
  17. })
  18. if response.status_code == 200:
  19. output = response.json()
  20. output['file_path'] = file_path
  21. return output
  22. else:
  23. raise Exception(response.text)
  24. except Exception as e:
  25. logger.error(f'File: {file_path} - Info: {e}')
  26. if __name__ == '__main__':
  27. files = ['small_ocr.pdf']
  28. n_jobs = np.clip(len(files), 1, 8)
  29. results = Parallel(n_jobs, prefer='threads', verbose=10)(
  30. delayed(do_parse)(p) for p in files
  31. )
  32. print(results)