PredictModel.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from typing import List
  2. from typing import Union
  3. from flask import abort
  4. from flask import make_response
  5. from flask import request
  6. from flask.views import View
  7. from pycs import db
  8. from pycs.database.Project import Project
  9. from pycs.frontend.notifications.NotificationList import NotificationList
  10. from pycs.frontend.notifications.NotificationManager import NotificationManager
  11. from pycs.interfaces.MediaFile import MediaFile
  12. from pycs.interfaces.MediaStorage import MediaStorage
  13. from pycs.jobs.JobGroupBusyException import JobGroupBusyException
  14. from pycs.jobs.JobRunner import JobRunner
  15. from pycs.util.PipelineCache import PipelineCache
  16. class PredictModel(View):
  17. """
  18. load a model and create predictions
  19. """
  20. # pylint: disable=arguments-differ
  21. methods = ['POST']
  22. def __init__(self, nm: NotificationManager, jobs: JobRunner, pipelines: PipelineCache):
  23. # pylint: disable=invalid-name
  24. self.nm = nm
  25. self.jobs = jobs
  26. self.pipelines = pipelines
  27. def dispatch_request(self, project_id):
  28. project = Project.get_or_404(project_id)
  29. # extract request data
  30. data = request.get_json(force=True)
  31. predict = data.get('predict')
  32. if predict is None:
  33. abort(400, "predict argument is missing")
  34. if predict not in ['all', 'new']:
  35. abort(400, "predict must be either 'all' or 'new'")
  36. # create job
  37. try:
  38. notifications = NotificationList(self.nm)
  39. self.jobs.run(project,
  40. 'Model Interaction',
  41. f'{project.name} (create predictions)',
  42. f'{project.id}/model-interaction',
  43. PredictModel.load_and_predict,
  44. self.pipelines, notifications,
  45. project.id, predict,
  46. progress=self.progress)
  47. except JobGroupBusyException:
  48. abort(400, "Model prediction is already running")
  49. return make_response()
  50. @staticmethod
  51. def load_and_predict(pipelines: PipelineCache,
  52. notifications: NotificationList,
  53. project_id: int, file_filter: Union[str, List[int]]):
  54. """
  55. load the pipeline and call the execute function
  56. :param database: database object
  57. :param pipelines: pipeline cache
  58. :param notifications: notification object
  59. :param project_id: project id
  60. :param file_filter: list of file ids or 'new' / 'all'
  61. :return:
  62. """
  63. pipeline = None
  64. # create new database instance
  65. project = Project.query.get(project_id)
  66. model_root = project.model.root_folder
  67. storage = MediaStorage(project_id, notifications)
  68. # create a list of MediaFile
  69. if isinstance(file_filter, str):
  70. if file_filter == 'new':
  71. files = project.files_without_results()
  72. length = project.count_files_without_results()
  73. else:
  74. files = project.files.all()
  75. length = project.files.count()
  76. else:
  77. files = [project.file(identifier) for identifier in file_filter]
  78. length = len(files)
  79. media_files = map(lambda f: MediaFile(f, notifications), files)
  80. # load pipeline
  81. try:
  82. pipeline = pipelines.load_from_root_folder(project_id, model_root)
  83. # iterate over media files
  84. index = 0
  85. for file in media_files:
  86. # remove old predictions
  87. file.remove_predictions()
  88. # create new predictions
  89. pipeline.execute(storage, file)
  90. # commit changes and yield progress
  91. db.session.commit()
  92. yield index / length, notifications
  93. index += 1
  94. finally:
  95. if pipeline is not None:
  96. pipelines.free_instance(model_root)
  97. @staticmethod
  98. def progress(progress: float, notifications: NotificationList):
  99. """
  100. fire notifications from the correct thread
  101. :param progress: [0, 1]
  102. :param notifications: Notificationlist
  103. :return: progress
  104. """
  105. notifications.fire()
  106. return progress