-
Notifications
You must be signed in to change notification settings - Fork 3k
Improve task api code quality #2376
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
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
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 |
|---|---|---|
| @@ -1,30 +1,40 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Dict, List | ||
| from typing import ClassVar, Dict, List | ||
|
|
||
| from ..features import ClassLabel, Features, Value | ||
| from .base import TaskTemplate | ||
|
|
||
|
|
||
| class FeaturesWithLazyClassLabel: | ||
| def __init__(self, features, label_column="labels"): | ||
| assert label_column in features, f"Key '{label_column}' missing in features {features}" | ||
| self._features = features | ||
| self._label_column = label_column | ||
|
|
||
| def __get__(self, obj, objtype=None): | ||
| if obj is None: | ||
| return self._features | ||
|
|
||
| assert hasattr(obj, self._label_column), f"Object has no attribute '{self._label_column}'" | ||
| features = self._features.copy() | ||
| features["labels"] = ClassLabel(names=getattr(obj, self._label_column)) | ||
| return features | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class TextClassification(TaskTemplate): | ||
| task = "text-classification" | ||
| input_schema = Features({"text": Value("string")}) | ||
| # TODO(lewtun): Since we update this in __post_init__ do we need to set a default? We'll need it for __init__ so | ||
| # investigate if there's a more elegant approach. | ||
| label_schema = Features({"labels": ClassLabel}) | ||
| task: ClassVar[str] = "text-classification" | ||
|
Member
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. thanks for spotting this! |
||
| input_schema: ClassVar[Features] = Features({"text": Value("string")}) | ||
| # TODO(lewtun): Find a more elegant approach without descriptors. | ||
| label_schema: ClassVar[Features] = FeaturesWithLazyClassLabel(Features({"labels": ClassLabel})) | ||
| labels: List[str] | ||
| text_column: str = "text" | ||
| label_column: str = "labels" | ||
|
|
||
| def __post_init__(self): | ||
| assert sorted(set(self.labels)) == sorted(self.labels), "Labels must be unique" | ||
| assert len(self.labels) == len(set(self.labels)), "Labels must be unique" | ||
| # Cast labels to tuple to allow hashing | ||
| object.__setattr__(self, "labels", tuple(sorted(self.labels))) | ||
| object.__setattr__(self, "text_column", self.text_column) | ||
| object.__setattr__(self, "label_column", self.label_column) | ||
| self.label_schema["labels"] = ClassLabel(names=self.labels) | ||
| object.__setattr__(self, "label2id", {label: idx for idx, label in enumerate(self.labels)}) | ||
| object.__setattr__(self, "id2label", {idx: label for label, idx in self.label2id.items()}) | ||
| self.__dict__["labels"] = tuple(sorted(self.labels)) | ||
|
|
||
| @property | ||
| def column_mapping(self) -> Dict[str, str]: | ||
|
|
@@ -33,10 +43,10 @@ def column_mapping(self) -> Dict[str, str]: | |
| self.label_column: "labels", | ||
| } | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, template_dict: dict) -> "TextClassification": | ||
| return cls( | ||
| text_column=template_dict["text_column"], | ||
| label_column=template_dict["label_column"], | ||
| labels=template_dict["labels"], | ||
| ) | ||
| @property | ||
| def label2id(self): | ||
| return {label: idx for idx, label in enumerate(self.labels)} | ||
|
|
||
| @property | ||
| def id2label(self): | ||
| return {idx: label for idx, label in enumerate(self.labels)} | ||
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.
very elegant approach - thanks!