6
0

WebServer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import eventlet
  2. import os
  3. import socketio
  4. from glob import glob
  5. from os import getcwd
  6. from os import path
  7. from os.path import exists
  8. from flask import Flask
  9. from flask import send_from_directory
  10. from pycs import app
  11. from pycs.database.Database import Database
  12. from pycs.frontend.endpoints.ListJobs import ListJobs
  13. from pycs.frontend.endpoints.ListLabelProviders import ListLabelProviders
  14. from pycs.frontend.endpoints.ListModels import ListModels
  15. from pycs.frontend.endpoints.ListProjects import ListProjects
  16. from pycs.frontend.endpoints.additional.FolderInformation import FolderInformation
  17. from pycs.frontend.endpoints.data.GetCroppedFile import GetCroppedFile
  18. from pycs.frontend.endpoints.data.GetFile import GetFile
  19. from pycs.frontend.endpoints.data.GetPreviousAndNextFile import GetPreviousAndNextFile
  20. from pycs.frontend.endpoints.data.GetResizedFile import GetResizedFile
  21. from pycs.frontend.endpoints.data.RemoveFile import RemoveFile
  22. from pycs.frontend.endpoints.data.UploadFile import UploadFile
  23. from pycs.frontend.endpoints.jobs.RemoveJob import RemoveJob
  24. from pycs.frontend.endpoints.labels.CreateLabel import CreateLabel
  25. from pycs.frontend.endpoints.labels.EditLabelName import EditLabelName
  26. from pycs.frontend.endpoints.labels.EditLabelParent import EditLabelParent
  27. from pycs.frontend.endpoints.labels.ListLabelTree import ListLabelTree
  28. from pycs.frontend.endpoints.labels.ListLabels import ListLabels
  29. from pycs.frontend.endpoints.labels.RemoveLabel import RemoveLabel
  30. from pycs.frontend.endpoints.pipelines.FitModel import FitModel
  31. from pycs.frontend.endpoints.pipelines.PredictFile import PredictFile
  32. from pycs.frontend.endpoints.pipelines.PredictModel import PredictModel
  33. from pycs.frontend.endpoints.projects.CreateProject import CreateProject
  34. from pycs.frontend.endpoints.projects.EditProjectDescription import EditProjectDescription
  35. from pycs.frontend.endpoints.projects.EditProjectName import EditProjectName
  36. from pycs.frontend.endpoints.projects.ExecuteExternalStorage import ExecuteExternalStorage
  37. from pycs.frontend.endpoints.projects.ExecuteLabelProvider import ExecuteLabelProvider
  38. from pycs.frontend.endpoints.projects.GetProjectModel import GetProjectModel
  39. from pycs.frontend.endpoints.projects.ListCollections import ListCollections
  40. from pycs.frontend.endpoints.projects.ListFiles import ListFiles
  41. from pycs.frontend.endpoints.projects.RemoveProject import RemoveProject
  42. from pycs.frontend.endpoints.results.ConfirmResult import ConfirmResult
  43. from pycs.frontend.endpoints.results.CreateResult import CreateResult
  44. from pycs.frontend.endpoints.results.EditResultData import EditResultData
  45. from pycs.frontend.endpoints.results.EditResultLabel import EditResultLabel
  46. from pycs.frontend.endpoints.results.GetProjectResults import GetProjectResults
  47. from pycs.frontend.endpoints.results.GetResults import GetResults
  48. from pycs.frontend.endpoints.results.RemoveResult import RemoveResult
  49. from pycs.frontend.endpoints.results.ResetResults import ResetResults
  50. from pycs.frontend.notifications.NotificationManager import NotificationManager
  51. from pycs.frontend.util.JSONEncoder import JSONEncoder
  52. from pycs.jobs.JobRunner import JobRunner
  53. from pycs.util.PipelineCache import PipelineCache
  54. class WebServer:
  55. """
  56. wrapper class for flask and socket.io which initializes most networking
  57. """
  58. # pylint: disable=line-too-long
  59. # pylint: disable=too-many-statements
  60. def __init__(self, app, settings: dict):
  61. self.database = Database()
  62. PRODUCTION = os.path.exists('webui/index.html')
  63. # initialize web server
  64. if PRODUCTION:
  65. print('production build')
  66. # find static files and folders
  67. static_files = {}
  68. for file_path in glob('webui/*'):
  69. file_path = file_path.replace('\\', '/')
  70. static_files[file_path[5:]] = file_path
  71. # separately add svg files and set their correct mime type
  72. for svg_path in glob('webui/img/*.svg'):
  73. svg_path = svg_path.replace('\\', '/')
  74. static_files[svg_path[5:]] = {'content_type': 'image/svg+xml', 'filename': svg_path}
  75. # create service objects
  76. if len(settings['allowedOrigins']) > 0:
  77. origins = settings['allowedOrigins']
  78. self.__sio = socketio.Server(cors_allowed_origins=origins, async_mode='eventlet')
  79. else:
  80. self.__sio = socketio.Server(async_mode='eventlet')
  81. self.__app = socketio.WSGIApp(self.__sio, app, static_files=static_files)
  82. # overwrite root path to serve index.html
  83. @app.route('/', methods=['GET'])
  84. def index():
  85. # pylint: disable=unused-variable
  86. return send_from_directory(path.join(getcwd(), 'webui'), 'index.html')
  87. else:
  88. print('development build')
  89. # create service objects
  90. self.__sio = socketio.Server(cors_allowed_origins='*', async_mode='eventlet')
  91. self.__app = socketio.WSGIApp(self.__sio, app)
  92. # set access control header to allow requests from Vue.js development server
  93. @app.after_request
  94. def after_request(response):
  95. # pylint: disable=unused-variable
  96. response.headers['Access-Control-Allow-Origin'] = '*'
  97. return response
  98. # set json encoder so database objects are serialized correctly
  99. app.json_encoder = JSONEncoder
  100. # create notification manager
  101. notifications = NotificationManager(self.__sio)
  102. jobs.on_create(notifications.create_job)
  103. jobs.on_start(notifications.edit_job)
  104. jobs.on_progress(notifications.edit_job)
  105. jobs.on_finish(notifications.edit_job)
  106. jobs.on_remove(notifications.remove_job)
  107. self.define_routes(jobs, notifications, pipelines)
  108. def define_routes(self, jobs, notifications, pipelines):
  109. # additional
  110. app.add_url_rule(
  111. '/folder',
  112. view_func=FolderInformation.as_view('folder_information')
  113. )
  114. # jobs
  115. app.add_url_rule(
  116. '/jobs',
  117. view_func=ListJobs.as_view('list_jobs', jobs)
  118. )
  119. app.add_url_rule(
  120. '/jobs/<identifier>/remove',
  121. view_func=RemoveJob.as_view('remove_job', jobs)
  122. )
  123. # models
  124. app.add_url_rule(
  125. '/models',
  126. view_func=ListModels.as_view('list_models', self.database)
  127. )
  128. app.add_url_rule(
  129. '/projects/<int:identifier>/model',
  130. view_func=GetProjectModel.as_view('get_project_model', self.database)
  131. )
  132. # labels
  133. app.add_url_rule(
  134. '/label_providers',
  135. view_func=ListLabelProviders.as_view('label_providers', self.database)
  136. )
  137. app.add_url_rule(
  138. '/projects/<int:identifier>/labels',
  139. view_func=ListLabels.as_view('list_labels', self.database)
  140. )
  141. app.add_url_rule(
  142. '/projects/<int:identifier>/labels/tree',
  143. view_func=ListLabelTree.as_view('list_label_tree', self.database)
  144. )
  145. app.add_url_rule(
  146. '/projects/<int:identifier>/labels',
  147. view_func=CreateLabel.as_view('create_label', self.database, notifications)
  148. )
  149. app.add_url_rule(
  150. '/projects/<int:project_id>/labels/<int:label_id>/remove',
  151. view_func=RemoveLabel.as_view('remove_label', self.database, notifications)
  152. )
  153. app.add_url_rule(
  154. '/projects/<int:project_id>/labels/<int:label_id>/name',
  155. view_func=EditLabelName.as_view('edit_label_name', self.database, notifications)
  156. )
  157. app.add_url_rule(
  158. '/projects/<int:project_id>/labels/<int:label_id>/parent',
  159. view_func=EditLabelParent.as_view('edit_label_parent', self.database, notifications)
  160. )
  161. # collections
  162. app.add_url_rule(
  163. '/projects/<int:project_id>/collections',
  164. view_func=ListCollections.as_view('list_collections', self.database)
  165. )
  166. app.add_url_rule(
  167. '/projects/<int:project_id>/data/<int:collection_id>/<int:start>/<int:length>',
  168. view_func=ListFiles.as_view('list_collection_files', self.database)
  169. )
  170. # data
  171. app.add_url_rule(
  172. '/projects/<int:identifier>/data',
  173. view_func=UploadFile.as_view('upload_file', self.database, notifications)
  174. )
  175. app.add_url_rule(
  176. '/projects/<int:project_id>/data/<int:start>/<int:length>',
  177. view_func=ListFiles.as_view('list_files', self.database)
  178. )
  179. app.add_url_rule(
  180. '/data/<int:identifier>/remove',
  181. view_func=RemoveFile.as_view('remove_file', self.database, notifications)
  182. )
  183. app.add_url_rule(
  184. '/data/<int:file_id>',
  185. view_func=GetFile.as_view('get_file', self.database)
  186. )
  187. app.add_url_rule(
  188. '/data/<int:file_id>/<resolution>',
  189. view_func=GetResizedFile.as_view('get_resized_file', self.database)
  190. )
  191. app.add_url_rule(
  192. '/data/<int:file_id>/<resolution>/<crop_box>',
  193. view_func=GetCroppedFile.as_view('crop_result', database)
  194. )
  195. app.add_url_rule(
  196. '/data/<int:file_id>/previous_next',
  197. view_func=GetPreviousAndNextFile.as_view('get_previous_and_next_file', self.database)
  198. )
  199. # results
  200. app.add_url_rule(
  201. '/projects/<int:project_id>/results',
  202. view_func=GetProjectResults.as_view('get_project_results', self.database)
  203. )
  204. app.add_url_rule(
  205. '/data/<int:file_id>/results',
  206. view_func=GetResults.as_view('get_results', self.database)
  207. )
  208. app.add_url_rule(
  209. '/data/<int:file_id>/results',
  210. view_func=CreateResult.as_view('create_result', self.database, notifications)
  211. )
  212. app.add_url_rule(
  213. '/data/<int:file_id>/reset',
  214. view_func=ResetResults.as_view('reset_results', self.database, notifications)
  215. )
  216. app.add_url_rule(
  217. '/results/<int:result_id>/remove',
  218. view_func=RemoveResult.as_view('remove_result', self.database, notifications)
  219. )
  220. app.add_url_rule(
  221. '/results/<int:result_id>/confirm',
  222. view_func=ConfirmResult.as_view('confirm_result', self.database, notifications)
  223. )
  224. app.add_url_rule(
  225. '/results/<int:result_id>/label',
  226. view_func=EditResultLabel.as_view('edit_result_label', self.database, notifications)
  227. )
  228. app.add_url_rule(
  229. '/results/<int:result_id>/data',
  230. view_func=EditResultData.as_view('edit_result_data', self.database, notifications)
  231. )
  232. # projects
  233. app.add_url_rule(
  234. '/projects',
  235. view_func=ListProjects.as_view('list_projects', self.database)
  236. )
  237. app.add_url_rule(
  238. '/projects',
  239. view_func=CreateProject.as_view('create_project', self.database, notifications, jobs)
  240. )
  241. app.add_url_rule(
  242. '/projects/<int:identifier>/label_provider',
  243. view_func=ExecuteLabelProvider.as_view('execute_label_provider', self.database,
  244. notifications, jobs)
  245. )
  246. app.add_url_rule(
  247. '/projects/<int:identifier>/external_storage',
  248. view_func=ExecuteExternalStorage.as_view('execute_external_storage', self.database,
  249. notifications, jobs)
  250. )
  251. app.add_url_rule(
  252. '/projects/<int:identifier>/remove',
  253. view_func=RemoveProject.as_view('remove_project', self.database, notifications)
  254. )
  255. app.add_url_rule(
  256. '/projects/<int:identifier>/name',
  257. view_func=EditProjectName.as_view('edit_project_name', self.database, notifications)
  258. )
  259. app.add_url_rule(
  260. '/projects/<int:identifier>/description',
  261. view_func=EditProjectDescription.as_view('edit_project_description', self.database,
  262. notifications)
  263. )
  264. # pipelines
  265. app.add_url_rule(
  266. '/projects/<int:project_id>/pipelines/fit',
  267. view_func=FitModel.as_view('fit_model', self.database, jobs, pipelines)
  268. )
  269. app.add_url_rule(
  270. '/projects/<int:project_id>/pipelines/predict',
  271. view_func=PredictModel.as_view('predict_model', self.database, notifications, jobs,
  272. pipelines)
  273. )
  274. app.add_url_rule(
  275. '/data/<int:file_id>/predict',
  276. view_func=PredictFile.as_view('predict_file', self.database, notifications, jobs, pipelines)
  277. )
  278. def run(self):
  279. # finally start web server
  280. eventlet.wsgi.server(eventlet.listen((self.host, self.port)), app)