From 6222e254b3dc7c6cae0869a057788a84e40dbe00 Mon Sep 17 00:00:00 2001
From: Bastien Abadie <bastien@nextcairn.com>
Date: Mon, 30 Jul 2018 12:57:25 +0200
Subject: [PATCH] Connect to ML worker for classification.

---
 arkindex/documents/models.py | 10 ++++++++++
 arkindex/project/settings.py |  6 ++++++
 2 files changed, 16 insertions(+)

diff --git a/arkindex/documents/models.py b/arkindex/documents/models.py
index 1fb40615c7..957dd36f43 100644
--- a/arkindex/documents/models.py
+++ b/arkindex/documents/models.py
@@ -3,6 +3,7 @@ from django.contrib.postgres.indexes import GinIndex
 from django.urls import reverse
 from enumfields import EnumField, Enum
 from arkindex.project.models import IndexableModel
+from arkindex.project.celery import app as celery_app
 from arkindex.documents.managers import ElementManager
 from arkindex.project.fields import ArrayField
 import uuid
@@ -329,6 +330,15 @@ class Page(Element):
             out.append(self.direction.value)
         return ' '.join(out)
 
+    def ml_classify(self):
+        '''
+        Use a machine learning worker to classify the page
+        using its image
+        Celery is triggered through an external signature
+        '''
+        signature = celery_app.signature('arkindex_ml.tasks.classify')
+        return signature.delay(self.zone.image.get_thumbnail_url(max_width=None))
+
 
 class Act(Element):
     """
diff --git a/arkindex/project/settings.py b/arkindex/project/settings.py
index be963cbad7..1c3c670343 100644
--- a/arkindex/project/settings.py
+++ b/arkindex/project/settings.py
@@ -312,6 +312,12 @@ CELERY_ONCE = {
         'default_timeout': 3600,
     }
 }
+CELERY_TASK_ROUTES = {
+    # Route all machine learning work to specific workers
+    'arkindex_ml.*': {
+        'queue': 'ml',
+    }
+}
 CELERY_WORKING_DIR = os.environ.get('CELERY_WORKING_DIR', os.path.join(BASE_DIR, 'workers'))
 
 # Email
-- 
GitLab