Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Finish ImageTask functionality, part of #69

davidmezzettib81116b00f4b
diff --git a/src/python/txtai/workflow/task/base.py b/src/python/txtai/workflow/task/base.py
index db186e83..b5daee59 100644
--- a/src/python/txtai/workflow/task/base.py
+++ b/src/python/txtai/workflow/task/base.py
@@ -10,7 +10,7 @@ class Task:
Base class for all workflow tasks.
"""
- def __init__(self, action, select=None, unpack=True):
+ def __init__(self, action=None, select=None, unpack=True):
"""
Creates a new task. A task defines two methods, type of data it accepts and the action to execute
for each data element. Actions are callable functions.
@@ -40,7 +40,7 @@ class Task:
elements = [self.prepare(element) for element in elements]
# Run data elements through workflow action
- return self.action(elements)
+ return self.action(elements) if self.action else elements
def accept(self, element):
"""
diff --git a/src/python/txtai/workflow/task/image.py b/src/python/txtai/workflow/task/image.py
index 3c908eed..6e8a63da 100644
--- a/src/python/txtai/workflow/task/image.py
+++ b/src/python/txtai/workflow/task/image.py
@@ -2,8 +2,13 @@
ImageTask module
"""
+import os
import re
+from urllib.parse import urlparse
+
+from PIL import Image
+
from .file import FileTask
@@ -15,3 +20,10 @@ class ImageTask(FileTask):
def accept(self, element):
# Only accept file URLs
return super().accept(element) and re.search(r"\.(gif|bmp|jpg|jpeg|png|webp)$", element.lower())
+
+ def prepare(self, element):
+ # Read image data
+ url = urlparse(element)
+ path = os.path.abspath(os.path.join(url.netloc, url.path))
+
+ return Image.open(path)