checkpoint_app.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #
  2. # Copyright 2021 The FATE 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. #
  16. from flask import request, abort
  17. from fate_flow.model.checkpoint import CheckpointManager
  18. from fate_flow.utils.api_utils import error_response, get_json_result
  19. from fate_flow.utils.detect_utils import check_config
  20. def load_checkpoints():
  21. required_args = ['role', 'party_id', 'model_id', 'model_version', 'component_name']
  22. try:
  23. check_config(request.json, required_args)
  24. except Exception as e:
  25. abort(error_response(400, str(e)))
  26. checkpoint_manager = CheckpointManager(**{i: request.json[i] for i in required_args})
  27. checkpoint_manager.load_checkpoints_from_disk()
  28. return checkpoint_manager
  29. @manager.route('/list', methods=['POST'])
  30. def list_checkpoints():
  31. checkpoint_manager = load_checkpoints()
  32. return get_json_result(data=checkpoint_manager.to_dict())
  33. @manager.route('/get', methods=['POST'])
  34. def get_checkpoint():
  35. checkpoint_manager = load_checkpoints()
  36. if 'step_index' in request.json:
  37. try:
  38. request.json['step_index'] = int(request.json['step_index'])
  39. except Exception:
  40. return error_response(400, "Invalid 'step_index'")
  41. checkpoint = checkpoint_manager.get_checkpoint_by_index(request.json['step_index'])
  42. elif 'step_name' in request.json:
  43. checkpoint = checkpoint_manager.get_checkpoint_by_name(request.json['step_name'])
  44. else:
  45. return error_response(400, "'step_index' or 'step_name' is required")
  46. if checkpoint is None:
  47. return error_response(404, "The checkpoint was not found.")
  48. return get_json_result(data=checkpoint.to_dict(True))