PredictFile.py 1.9 KB

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