zodiac.streams.task_stream

  1#  # # <!-- // /*  SPDX-License-Identifier: MPL-2.0*/ -->
  2#  # # <!-- // /*  d a r k s h a p e s */ -->
  3
  4from typing import List, Any, Set
  5from toga.sources import Source
  6from zodiac.providers.registry_entry import RegistryEntry
  7
  8nfo = print
  9
 10flatten_map: List[Any] = lambda nested, unpack: [element for iterative in getattr(nested, unpack)() for element in iterative]
 11flatten_map.__annotations__ = {"nested": List[str], "unpack": str}
 12
 13
 14class TaskStream(Source):
 15    def __init__(self) -> None:
 16        self.basic_tasks = {
 17            "speech": ["Audio"],
 18            "image": ["ControlNet", "PAG"],
 19            "text": [
 20                "QuestionAnswering",
 21                "SequenceClassification",
 22            ],
 23        }
 24        self.exclusive_tasks = {
 25            ("image", "text"): ["Vision"],
 26            ("image", "image"): ["Img2Img", "Inpaint", "PAG", "Vision"],
 27            ("text", "text"): ["Text", "CasualLM", "SequenceClassification", "QuestionAnswering"],
 28        }
 29        self.all_tasks = set().union(*self.basic_tasks.values()).union(*self.exclusive_tasks.values())
 30
 31    async def set_filter_type(self, mode_in: str = "image", mode_out: str = "image") -> None:
 32        """Filter class items by modality
 33        :param mode_in: Input modality operation, defaults to "image"
 34        :param mode_out: Output modality operation, defaults to "image"""
 35
 36        self.tasks = None
 37        self.exclude = None
 38        self.tasks = set()
 39        self.exclude = set()
 40
 41        if (mode_in, mode_out) in self.exclusive_tasks:
 42            self.tasks = self.exclusive_tasks[(mode_in, mode_out)]
 43        else:
 44            if mode_in in self.basic_tasks:
 45                self.tasks.update(self.basic_tasks[mode_in])
 46            if mode_out in self.basic_tasks:
 47                self.tasks.update(self.basic_tasks[mode_out])
 48        self.exclude = self.all_tasks.difference(self.tasks)
 49
 50    async def filter_tasks(self, registry_entry: RegistryEntry) -> List[str]:
 51        """Processes preformatted task data by removing specified prefixes and keywords, then adds valid data to task_data.\n
 52        :param preformatted_task_data: A list of strings to be processed.
 53        :param snip_words: A list of prefixes or suffixes to be removed from each pipe.
 54        :return: A sorted list of unique task_data entries after processing."""
 55        import re
 56
 57        if registry_entry.package == "mflux":
 58            return registry_entry.tasks
 59        task_names = registry_entry.tasks
 60        snip_words: Set[str] = {"Model", "PreTrained", "ForConditionalGeneration", "Pipeline", "For"}
 61        class_snippets = snip_words | self.all_tasks
 62        if task_names:
 63            for task_class in task_names:
 64                for snip in class_snippets:
 65                    if task_class and isinstance(task_class, str):
 66                        snip_words.add(task_class.replace(snip, ""))
 67                    elif task_class[0]:
 68                        snip_words.add(task_class[0].replace(snip, ""))
 69                task_data = set()
 70                for pipe in task_names:
 71                    if isinstance(pipe, list):
 72                        for sub_pipe in pipe:
 73                            for word in snip_words:
 74                                sub_pipe = sub_pipe.replace(word, "")
 75                                if sub_pipe:
 76                                    for task in self.tasks:
 77                                        if task in sub_pipe:
 78                                            task_data.add(sub_pipe)
 79                    else:
 80                        for word in snip_words:
 81                            pipe = pipe.replace(word, "")
 82                            if pipe:
 83                                for task in self.tasks:
 84                                    if task in pipe:
 85                                        task_data.add(pipe)
 86                task_data = list(task_data)
 87                task_data.sort()
 88            return task_data
 89
 90    def __len__(self):
 91        return len(list(self._task_data()))
 92
 93    def __getitem__(self, index):
 94        return self._task_data()[index]
 95
 96    def index(self, entry):
 97        return self._task_data().index(entry)
 98
 99    async def clear(self):
100        self._task_data = []
101        self.notify("clear")
def nfo(*args, sep=' ', end='\n', file=None, flush=False):

Prints the values to a stream, or to sys.stdout by default.

sep string inserted between values, default a space. end string appended after the last value, default a newline. file a file-like object (stream); defaults to the current sys.stdout. flush whether to forcibly flush the stream.

def flatten_map(nested: List[str], unpack: str):
11flatten_map: List[Any] = lambda nested, unpack: [element for iterative in getattr(nested, unpack)() for element in iterative]
class TaskStream(toga.sources.base.Source):
 15class TaskStream(Source):
 16    def __init__(self) -> None:
 17        self.basic_tasks = {
 18            "speech": ["Audio"],
 19            "image": ["ControlNet", "PAG"],
 20            "text": [
 21                "QuestionAnswering",
 22                "SequenceClassification",
 23            ],
 24        }
 25        self.exclusive_tasks = {
 26            ("image", "text"): ["Vision"],
 27            ("image", "image"): ["Img2Img", "Inpaint", "PAG", "Vision"],
 28            ("text", "text"): ["Text", "CasualLM", "SequenceClassification", "QuestionAnswering"],
 29        }
 30        self.all_tasks = set().union(*self.basic_tasks.values()).union(*self.exclusive_tasks.values())
 31
 32    async def set_filter_type(self, mode_in: str = "image", mode_out: str = "image") -> None:
 33        """Filter class items by modality
 34        :param mode_in: Input modality operation, defaults to "image"
 35        :param mode_out: Output modality operation, defaults to "image"""
 36
 37        self.tasks = None
 38        self.exclude = None
 39        self.tasks = set()
 40        self.exclude = set()
 41
 42        if (mode_in, mode_out) in self.exclusive_tasks:
 43            self.tasks = self.exclusive_tasks[(mode_in, mode_out)]
 44        else:
 45            if mode_in in self.basic_tasks:
 46                self.tasks.update(self.basic_tasks[mode_in])
 47            if mode_out in self.basic_tasks:
 48                self.tasks.update(self.basic_tasks[mode_out])
 49        self.exclude = self.all_tasks.difference(self.tasks)
 50
 51    async def filter_tasks(self, registry_entry: RegistryEntry) -> List[str]:
 52        """Processes preformatted task data by removing specified prefixes and keywords, then adds valid data to task_data.\n
 53        :param preformatted_task_data: A list of strings to be processed.
 54        :param snip_words: A list of prefixes or suffixes to be removed from each pipe.
 55        :return: A sorted list of unique task_data entries after processing."""
 56        import re
 57
 58        if registry_entry.package == "mflux":
 59            return registry_entry.tasks
 60        task_names = registry_entry.tasks
 61        snip_words: Set[str] = {"Model", "PreTrained", "ForConditionalGeneration", "Pipeline", "For"}
 62        class_snippets = snip_words | self.all_tasks
 63        if task_names:
 64            for task_class in task_names:
 65                for snip in class_snippets:
 66                    if task_class and isinstance(task_class, str):
 67                        snip_words.add(task_class.replace(snip, ""))
 68                    elif task_class[0]:
 69                        snip_words.add(task_class[0].replace(snip, ""))
 70                task_data = set()
 71                for pipe in task_names:
 72                    if isinstance(pipe, list):
 73                        for sub_pipe in pipe:
 74                            for word in snip_words:
 75                                sub_pipe = sub_pipe.replace(word, "")
 76                                if sub_pipe:
 77                                    for task in self.tasks:
 78                                        if task in sub_pipe:
 79                                            task_data.add(sub_pipe)
 80                    else:
 81                        for word in snip_words:
 82                            pipe = pipe.replace(word, "")
 83                            if pipe:
 84                                for task in self.tasks:
 85                                    if task in pipe:
 86                                        task_data.add(pipe)
 87                task_data = list(task_data)
 88                task_data.sort()
 89            return task_data
 90
 91    def __len__(self):
 92        return len(list(self._task_data()))
 93
 94    def __getitem__(self, index):
 95        return self._task_data()[index]
 96
 97    def index(self, entry):
 98        return self._task_data().index(entry)
 99
100    async def clear(self):
101        self._task_data = []
102        self.notify("clear")

A base class for data sources, providing an implementation of data notifications.

basic_tasks
exclusive_tasks
all_tasks
async def set_filter_type(self, mode_in: str = 'image', mode_out: str = 'image') -> None:
32    async def set_filter_type(self, mode_in: str = "image", mode_out: str = "image") -> None:
33        """Filter class items by modality
34        :param mode_in: Input modality operation, defaults to "image"
35        :param mode_out: Output modality operation, defaults to "image"""
36
37        self.tasks = None
38        self.exclude = None
39        self.tasks = set()
40        self.exclude = set()
41
42        if (mode_in, mode_out) in self.exclusive_tasks:
43            self.tasks = self.exclusive_tasks[(mode_in, mode_out)]
44        else:
45            if mode_in in self.basic_tasks:
46                self.tasks.update(self.basic_tasks[mode_in])
47            if mode_out in self.basic_tasks:
48                self.tasks.update(self.basic_tasks[mode_out])
49        self.exclude = self.all_tasks.difference(self.tasks)

Filter class items by modality

Parameters
  • mode_in: Input modality operation, defaults to "image"
  • mode_out: Output modality operation, defaults to "image
async def filter_tasks( self, registry_entry: zodiac.providers.registry_entry.RegistryEntry) -> List[str]:
51    async def filter_tasks(self, registry_entry: RegistryEntry) -> List[str]:
52        """Processes preformatted task data by removing specified prefixes and keywords, then adds valid data to task_data.\n
53        :param preformatted_task_data: A list of strings to be processed.
54        :param snip_words: A list of prefixes or suffixes to be removed from each pipe.
55        :return: A sorted list of unique task_data entries after processing."""
56        import re
57
58        if registry_entry.package == "mflux":
59            return registry_entry.tasks
60        task_names = registry_entry.tasks
61        snip_words: Set[str] = {"Model", "PreTrained", "ForConditionalGeneration", "Pipeline", "For"}
62        class_snippets = snip_words | self.all_tasks
63        if task_names:
64            for task_class in task_names:
65                for snip in class_snippets:
66                    if task_class and isinstance(task_class, str):
67                        snip_words.add(task_class.replace(snip, ""))
68                    elif task_class[0]:
69                        snip_words.add(task_class[0].replace(snip, ""))
70                task_data = set()
71                for pipe in task_names:
72                    if isinstance(pipe, list):
73                        for sub_pipe in pipe:
74                            for word in snip_words:
75                                sub_pipe = sub_pipe.replace(word, "")
76                                if sub_pipe:
77                                    for task in self.tasks:
78                                        if task in sub_pipe:
79                                            task_data.add(sub_pipe)
80                    else:
81                        for word in snip_words:
82                            pipe = pipe.replace(word, "")
83                            if pipe:
84                                for task in self.tasks:
85                                    if task in pipe:
86                                        task_data.add(pipe)
87                task_data = list(task_data)
88                task_data.sort()
89            return task_data

Processes preformatted task data by removing specified prefixes and keywords, then adds valid data to task_data.

Parameters
  • preformatted_task_data: A list of strings to be processed.
  • snip_words: A list of prefixes or suffixes to be removed from each pipe.
Returns

A sorted list of unique task_data entries after processing.

def index(self, entry):
97    def index(self, entry):
98        return self._task_data().index(entry)
async def clear(self):
100    async def clear(self):
101        self._task_data = []
102        self.notify("clear")