Merge branch 'server_test' of https://github.com/MaxReimann/WISE-Editing into main
Browse files- worker/serve.py +34 -30
worker/serve.py
CHANGED
@@ -28,31 +28,7 @@ from parameter_optimization.strotss_org import strotss, pil_resize_long_edge_to
|
|
28 |
from helpers import torch_to_np, np_to_torch
|
29 |
from effects import get_default_settings, MinimalPipelineEffect
|
30 |
|
31 |
-
class JSONExceptionHandler(object):
|
32 |
-
|
33 |
-
def __init__(self, app=None):
|
34 |
-
if app:
|
35 |
-
self.init_app(app)
|
36 |
-
|
37 |
-
def std_handler(self, error):
|
38 |
-
response = jsonify(message=error.message)
|
39 |
-
response.status_code = error.code if isinstance(error, HTTPException) else 500
|
40 |
-
return response
|
41 |
-
|
42 |
-
|
43 |
-
def init_app(self, app):
|
44 |
-
self.app = app
|
45 |
-
self.register(HTTPException)
|
46 |
-
for code, v in default_exceptions.items():
|
47 |
-
self.register(code)
|
48 |
-
|
49 |
-
def register(self, exception_or_code, handler=None):
|
50 |
-
self.app.errorhandler(exception_or_code)(handler or self.std_handler)
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
app = Flask(__name__)
|
55 |
-
handler = JSONExceptionHandler(app)
|
56 |
|
57 |
image_folder = 'img_received'
|
58 |
photos = UploadSet('photos', IMAGES)
|
@@ -160,6 +136,8 @@ class StyleTask:
|
|
160 |
return
|
161 |
|
162 |
self.status = "finished"
|
|
|
|
|
163 |
print("finished styling task: " + str(self.task_id))
|
164 |
|
165 |
class StylerQueue:
|
@@ -208,6 +186,26 @@ class StylerQueue:
|
|
208 |
|
209 |
styler_queue = StylerQueue()
|
210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
@app.route('/upload', methods=['POST'])
|
213 |
def upload():
|
@@ -221,15 +219,17 @@ def upload():
|
|
221 |
print('added new stylization task', style_filename, content_filename)
|
222 |
|
223 |
return jsonify({"task_id": job_id})
|
224 |
-
abort(
|
225 |
|
226 |
@app.route('/get_status')
|
227 |
def get_status():
|
|
|
|
|
228 |
task_id = int(request.args.get("task_id"))
|
229 |
task = styler_queue.get_task(task_id)
|
230 |
|
231 |
if task is None:
|
232 |
-
abort(
|
233 |
|
234 |
status = {
|
235 |
"status": task.status,
|
@@ -257,27 +257,31 @@ def get_queue_length():
|
|
257 |
|
258 |
@app.route('/get_image')
|
259 |
def get_image():
|
|
|
|
|
260 |
task_id = int(request.args.get("task_id"))
|
261 |
task = styler_queue.get_task(task_id)
|
262 |
|
263 |
if task is None:
|
264 |
-
abort(
|
265 |
|
266 |
if task.status != "finished":
|
267 |
-
abort(
|
268 |
|
269 |
return send_file(os.path.join(image_folder, task.output_filename), mimetype='image/jpg')
|
270 |
|
271 |
@app.route('/get_vp')
|
272 |
def get_vp():
|
|
|
|
|
273 |
task_id = int(request.args.get("task_id"))
|
274 |
task = styler_queue.get_task(task_id)
|
275 |
|
276 |
if task is None:
|
277 |
-
abort(
|
278 |
|
279 |
if task.status != "finished":
|
280 |
-
abort(
|
281 |
|
282 |
return send_file(os.path.join(image_folder, task.vp_output_filename), mimetype='application/zip')
|
283 |
|
|
|
28 |
from helpers import torch_to_np, np_to_torch
|
29 |
from effects import get_default_settings, MinimalPipelineEffect
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
app = Flask(__name__)
|
|
|
32 |
|
33 |
image_folder = 'img_received'
|
34 |
photos = UploadSet('photos', IMAGES)
|
|
|
136 |
return
|
137 |
|
138 |
self.status = "finished"
|
139 |
+
del self.neural_optimizer
|
140 |
+
torch.cuda.empty_cache()
|
141 |
print("finished styling task: " + str(self.task_id))
|
142 |
|
143 |
class StylerQueue:
|
|
|
186 |
|
187 |
styler_queue = StylerQueue()
|
188 |
|
189 |
+
@app.errorhandler(404)
|
190 |
+
def resource_not_found(e):
|
191 |
+
return jsonify(message=str(e)), 404
|
192 |
+
|
193 |
+
@app.errorhandler(500)
|
194 |
+
def internal_server_error(e):
|
195 |
+
return jsonify(message=str(e)), 500
|
196 |
+
|
197 |
+
@app.errorhandler(400)
|
198 |
+
def caught_error(e, *args):
|
199 |
+
print(args)
|
200 |
+
print(e)
|
201 |
+
return jsonify(message=str(e.description)), 400
|
202 |
+
|
203 |
+
@app.route('/', defaults={'path': ''})
|
204 |
+
@app.route('/<path:path>')
|
205 |
+
def catch_all(path):
|
206 |
+
abort(404, "route not found")
|
207 |
+
|
208 |
+
|
209 |
|
210 |
@app.route('/upload', methods=['POST'])
|
211 |
def upload():
|
|
|
219 |
print('added new stylization task', style_filename, content_filename)
|
220 |
|
221 |
return jsonify({"task_id": job_id})
|
222 |
+
abort(400, description="request needs style, content image")
|
223 |
|
224 |
@app.route('/get_status')
|
225 |
def get_status():
|
226 |
+
if request.args.get("task_id") is None:
|
227 |
+
abort(400, description="task_id needs to be supplied as parameter")
|
228 |
task_id = int(request.args.get("task_id"))
|
229 |
task = styler_queue.get_task(task_id)
|
230 |
|
231 |
if task is None:
|
232 |
+
abort(400, description="task with id %d not found"%task_id)
|
233 |
|
234 |
status = {
|
235 |
"status": task.status,
|
|
|
257 |
|
258 |
@app.route('/get_image')
|
259 |
def get_image():
|
260 |
+
if request.args.get("task_id") is None:
|
261 |
+
abort(400, description="task_id needs to be supplied as parameter")
|
262 |
task_id = int(request.args.get("task_id"))
|
263 |
task = styler_queue.get_task(task_id)
|
264 |
|
265 |
if task is None:
|
266 |
+
abort(400, description="task with id %d not found"%task_id)
|
267 |
|
268 |
if task.status != "finished":
|
269 |
+
abort(400, description="task with id %d not in finished state"%task_id)
|
270 |
|
271 |
return send_file(os.path.join(image_folder, task.output_filename), mimetype='image/jpg')
|
272 |
|
273 |
@app.route('/get_vp')
|
274 |
def get_vp():
|
275 |
+
if request.args.get("task_id") is None:
|
276 |
+
abort(400, description="task_id needs to be supplied as parameter")
|
277 |
task_id = int(request.args.get("task_id"))
|
278 |
task = styler_queue.get_task(task_id)
|
279 |
|
280 |
if task is None:
|
281 |
+
abort(400, description="task with id %d not found"%task_id)
|
282 |
|
283 |
if task.status != "finished":
|
284 |
+
abort(400, description="task with id %d not in finished state"%task_id)
|
285 |
|
286 |
return send_file(os.path.join(image_folder, task.vp_output_filename), mimetype='application/zip')
|
287 |
|