-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from lovit/feature/149
[#149] Pipeline - task template
- Loading branch information
Showing
12 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pipeline: | ||
- name: Dummy | ||
args: | ||
name: soynlp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
soynlp pipeline -c pipeline.yaml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from dataclasses import dataclass | ||
|
||
import dacite | ||
import yaml | ||
|
||
|
||
@dataclass | ||
class TaskConfig: | ||
name: str | ||
args: dict | ||
|
||
|
||
@dataclass | ||
class Config: | ||
pipeline: list[TaskConfig] | ||
|
||
|
||
def from_yaml(path: str) -> Config: | ||
with open(path) as file: | ||
data = yaml.full_load(file) | ||
return from_dict(data) # type: ignore | ||
|
||
|
||
def from_dict(data: dict) -> Config: | ||
return dacite.from_dict(data_class=Config, data=data) # type: ignore |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import importlib | ||
|
||
import dacite | ||
|
||
from soynlp.configs.config import Config, from_yaml | ||
from soynlp.pipeline.tasks import Task | ||
|
||
|
||
class Pipeline: | ||
def __call__(self, config: Config): | ||
tasks: list[Task] = self._load_tasks(config) | ||
parameters: dict = {} | ||
|
||
for task in tasks: | ||
parameters |= task(parameters) | ||
return parameters | ||
|
||
def _load_tasks(self, config: Config) -> list[Task]: | ||
tasks = [] | ||
for task_config in config.pipeline: | ||
task_module = importlib.import_module("soynlp.pipeline.tasks") | ||
task_class: type[Task] = getattr(task_module, f"{task_config.name}Task") | ||
task_args = dacite.from_dict(data_class=task_class.args(), data=task_config.args) # type: ignore | ||
task = task_class(task_args) | ||
tasks.append(task) | ||
return tasks | ||
|
||
@classmethod | ||
def run(cls, config_file: str): | ||
config = from_yaml(config_file) | ||
pipeline = Pipeline() | ||
print(pipeline(config)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from soynlp.pipeline.tasks.dummy import DummyTask # noqa F401 | ||
from soynlp.pipeline.tasks.task import Task # noqa F401 | ||
|
||
|
||
__all__ = ("Task", "DummyTask") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from dataclasses import dataclass | ||
|
||
from soynlp.pipeline.tasks.task import Task, TaskArgs | ||
|
||
|
||
@dataclass | ||
class DummyTaskArgs(TaskArgs): | ||
name: str = "Dummy Task" | ||
|
||
|
||
class DummyTask(Task[DummyTaskArgs]): | ||
def __call__(self, parameters: dict) -> dict: | ||
print(f"Called in DummyTask({self._args.name})") | ||
return parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from typing import Generic, TypeVar, get_args | ||
|
||
|
||
@dataclass | ||
class TaskArgs: | ||
pass | ||
|
||
|
||
TaskArgsType = TypeVar("TaskArgsType", bound=TaskArgs) | ||
|
||
|
||
class Task(ABC, Generic[TaskArgsType]): | ||
def __init__(self, args: TaskArgsType): | ||
self._args = args | ||
|
||
@classmethod | ||
def args(cls) -> type[TaskArgs]: | ||
generic_type = cls.__orig_bases__[0] # type: ignore[attr-defined] | ||
return get_args(generic_type)[0] | ||
|
||
@abstractmethod | ||
def __call__(self, parameters: dict) -> dict: | ||
raise NotImplementedError |