Upload operator.py with huggingface_hub
Browse files- operator.py +28 -6
operator.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import re
|
| 2 |
from abc import abstractmethod
|
| 3 |
from dataclasses import field
|
| 4 |
-
from typing import Any, Dict, Generator, List, Optional
|
| 5 |
|
| 6 |
from .artifact import Artifact
|
| 7 |
from .dataclass import InternalField, NonPositionalField
|
|
@@ -14,7 +14,18 @@ class Operator(Artifact):
|
|
| 14 |
|
| 15 |
|
| 16 |
class PackageRequirementsMixin(Artifact):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def verify(self):
|
| 20 |
super().verify()
|
|
@@ -23,19 +34,30 @@ class PackageRequirementsMixin(Artifact):
|
|
| 23 |
def check_missing_requirements(self, requirements=None):
|
| 24 |
if requirements is None:
|
| 25 |
requirements = self._requirements_list
|
|
|
|
|
|
|
|
|
|
| 26 |
missing_packages = []
|
| 27 |
-
|
|
|
|
| 28 |
if not is_module_available(package):
|
| 29 |
missing_packages.append(package)
|
|
|
|
| 30 |
if missing_packages:
|
| 31 |
-
raise MissingRequirementsError(
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
class MissingRequirementsError(Exception):
|
| 35 |
-
def __init__(self, class_name, missing_packages):
|
| 36 |
self.class_name = class_name
|
| 37 |
self.missing_packages = missing_packages
|
| 38 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
super().__init__(self.message)
|
| 40 |
|
| 41 |
|
|
|
|
| 1 |
import re
|
| 2 |
from abc import abstractmethod
|
| 3 |
from dataclasses import field
|
| 4 |
+
from typing import Any, Dict, Generator, List, Optional, Union
|
| 5 |
|
| 6 |
from .artifact import Artifact
|
| 7 |
from .dataclass import InternalField, NonPositionalField
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
class PackageRequirementsMixin(Artifact):
|
| 17 |
+
"""Base class used to automatically check for the existence of required python dependencies for an artifact (e.g. Operator or Metric).
|
| 18 |
+
|
| 19 |
+
The _requirement list is either a list of required packages
|
| 20 |
+
(e.g. ["torch","sentence_transformers"]) or a dictionary between required packages
|
| 21 |
+
and detailed installation instructions on how how to install each package.
|
| 22 |
+
(e.g. {"torch" : "Install Torch using `pip install torch`", "sentence_transformers" : Install Sentence Transformers using `pip install sentence-transformers`})
|
| 23 |
+
Note that the package names should be specified as they are used in the python import statement for the package.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
_requirements_list: Union[List[str], Dict[str, str]] = InternalField(
|
| 27 |
+
default_factory=list
|
| 28 |
+
)
|
| 29 |
|
| 30 |
def verify(self):
|
| 31 |
super().verify()
|
|
|
|
| 34 |
def check_missing_requirements(self, requirements=None):
|
| 35 |
if requirements is None:
|
| 36 |
requirements = self._requirements_list
|
| 37 |
+
if isinstance(requirements, List):
|
| 38 |
+
requirements = {package: "" for package in requirements}
|
| 39 |
+
|
| 40 |
missing_packages = []
|
| 41 |
+
installation_instructions = []
|
| 42 |
+
for package, installation_instruction in requirements.items():
|
| 43 |
if not is_module_available(package):
|
| 44 |
missing_packages.append(package)
|
| 45 |
+
installation_instructions.append(installation_instruction)
|
| 46 |
if missing_packages:
|
| 47 |
+
raise MissingRequirementsError(
|
| 48 |
+
self.__class__.__name__, missing_packages, installation_instructions
|
| 49 |
+
)
|
| 50 |
|
| 51 |
|
| 52 |
class MissingRequirementsError(Exception):
|
| 53 |
+
def __init__(self, class_name, missing_packages, installation_instructions):
|
| 54 |
self.class_name = class_name
|
| 55 |
self.missing_packages = missing_packages
|
| 56 |
+
self.installation_instruction = installation_instructions
|
| 57 |
+
self.message = (
|
| 58 |
+
f"{self.class_name} requires the following missing package(s): {', '.join(self.missing_packages)}. "
|
| 59 |
+
+ "\n".join(self.installation_instruction)
|
| 60 |
+
)
|
| 61 |
super().__init__(self.message)
|
| 62 |
|
| 63 |
|