6
0

PredictFile.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from flask import make_response, request, abort
  2. from flask.views import View
  3. from pycs.database.File import File
  4. from pycs.frontend.endpoints.pipelines.PredictModel import PredictModel
  5. from pycs.frontend.notifications.NotificationList import NotificationList
  6. from pycs.jobs.JobGroupBusyException import JobGroupBusyException
  7. from pycs.jobs.JobRunner import JobRunner
  8. from pycs.util.PipelineCache import PipelineCache
  9. class PredictFile(View):
  10. """
  11. load a model and create predictions or a given file
  12. """
  13. # pylint: disable=arguments-differ
  14. methods = ['POST']
  15. def __init__(self, pipelines: PipelineCache):
  16. # pylint: disable=invalid-name
  17. self.pipelines = pipelines
  18. def dispatch_request(self, file_id):
  19. # extract request data
  20. data = request.get_json(force=True)
  21. if 'predict' not in data or data['predict'] is not True:
  22. return abort(400)
  23. # find file
  24. file = File.query.get(file_id)
  25. if file is None:
  26. return abort(404)
  27. # get project and model
  28. project = file.project
  29. # create job
  30. try:
  31. notifications = NotificationList()
  32. JobRunner.Run(project,
  33. 'Model Interaction',
  34. f'{project.name} (create predictions)',
  35. f'{project.name}/model-interaction',
  36. PredictModel.load_and_predict,
  37. self.pipelines, notifications, project.id, [file],
  38. progress=PredictModel.progress)
  39. except JobGroupBusyException:
  40. return abort(400)
  41. return make_response()