1
1

PredictFile.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. 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, db: Database, nm: NotificationManager, jobs: JobRunner):
  16. # pylint: disable=invalid-name
  17. self.db = db
  18. self.nm = nm
  19. self.jobs = jobs
  20. def dispatch_request(self, file_id):
  21. # extract request data
  22. data = request.get_json(force=True)
  23. if 'predict' not in data or data['predict'] is not True:
  24. return abort(400)
  25. # find file
  26. file = self.db.file(file_id)
  27. if file is None:
  28. return abort(404)
  29. # get project and model
  30. project = file.project()
  31. # create job
  32. try:
  33. notifications = NotificationList(self.nm)
  34. self.jobs.run(project,
  35. 'Model Interaction',
  36. f'{project.name} (create predictions)',
  37. f'{project.name}/model-interaction',
  38. Predict.load_and_predict,
  39. self.db, project.identifier, [file], notifications,
  40. progress=Predict.progress)
  41. except JobGroupBusyException:
  42. return abort(400)
  43. return make_response()