-
Notifications
You must be signed in to change notification settings - Fork 183
[#149] Pipeline - task template #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0da52c6
[#149] feat: Implement Config and TaskConfig
lovit d8b067e
[#149] feat: Implement Abstract Task class
lovit 2c53c4f
[#149] feat: Implement DummyTask
lovit b266d86
[#149] feat: Implement Pipeline and cli
lovit e824a53
[#149] feat: Dummy pipeline example
lovit a027da7
[#149] fix: Remove out-dated cli function; dummy
lovit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/lovit/soynlp/pull/150/files#r1941706190 와 동일한 오류 발생. 임시로 무시함 |
||
| 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 hidden or 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 hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# type: ignore제거 시 pyright failedyaml.full_load(file) 가 반환하는 값의 타입이 dict[Unknown, Unknown] 으로 추론되기에 data_class 와 타입 불일치가 일어남. 이후에 해결하기 위해 임시로 pyright 체크 과정을 건너 띔
이로 인해 from_yaml 함수의 line 21 에서도 동일한 오류 발생