-
Notifications
You must be signed in to change notification settings - Fork 5
/
tasks.py
42 lines (35 loc) · 1.26 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import logging
from celery import Task
from celery.exceptions import MaxRetriesExceededError
from .app_worker import app
from .yolo import YoloModel
class PredictTask(Task):
def __init__(self):
super().__init__()
self.model = None
def __call__(self, *args, **kwargs):
if not self.model:
logging.info('Loading Model...')
self.model = YoloModel()
logging.info('Model loaded')
return self.run(*args, **kwargs)
@app.task(ignore_result=False, bind=True, base=PredictTask)
def predict_image(self, data):
try:
data_pred = self.model.predict(data)
return {'status': 'SUCCESS', 'result': data_pred}
except Exception as ex:
try:
self.retry(countdown=2)
except MaxRetriesExceededError as ex:
return {'status': 'FAIL', 'result': 'max retried achieved'}
@app.task(ignore_result=False, bind=True, base=PredictTask)
def predict_video(self, data):
try:
data_pred = self.model.predict_video(data)
return {'status': 'SUCCESS', 'result': data_pred}
except Exception as ex:
try:
self.retry(countdown=2)
except MaxRetriesExceededError as ex:
return {'status': 'FAIL', 'result': 'max retried achieved'}