-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtasks.py
More file actions
30 lines (25 loc) · 886 Bytes
/
tasks.py
File metadata and controls
30 lines (25 loc) · 886 Bytes
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
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'}