Upload jobs/BaseJob.py with huggingface_hub
Browse files- jobs/BaseJob.py +71 -0
jobs/BaseJob.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
from jobs.process import BaseProcess
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BaseJob:
|
| 9 |
+
|
| 10 |
+
def __init__(self, config: OrderedDict):
|
| 11 |
+
if not config:
|
| 12 |
+
raise ValueError('config is required')
|
| 13 |
+
self.process: List[BaseProcess]
|
| 14 |
+
|
| 15 |
+
self.config = config['config']
|
| 16 |
+
self.raw_config = config
|
| 17 |
+
self.job = config['job']
|
| 18 |
+
self.name = self.get_conf('name', required=True)
|
| 19 |
+
if 'meta' in config:
|
| 20 |
+
self.meta = config['meta']
|
| 21 |
+
else:
|
| 22 |
+
self.meta = OrderedDict()
|
| 23 |
+
|
| 24 |
+
def get_conf(self, key, default=None, required=False):
|
| 25 |
+
if key in self.config:
|
| 26 |
+
return self.config[key]
|
| 27 |
+
elif required:
|
| 28 |
+
raise ValueError(f'config file error. Missing "config.{key}" key')
|
| 29 |
+
else:
|
| 30 |
+
return default
|
| 31 |
+
|
| 32 |
+
def run(self):
|
| 33 |
+
print("")
|
| 34 |
+
print(f"#############################################")
|
| 35 |
+
print(f"# Running job: {self.name}")
|
| 36 |
+
print(f"#############################################")
|
| 37 |
+
print("")
|
| 38 |
+
# implement in child class
|
| 39 |
+
# be sure to call super().run() first
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
def load_processes(self, process_dict: dict):
|
| 43 |
+
# only call if you have processes in this job type
|
| 44 |
+
if 'process' not in self.config:
|
| 45 |
+
raise ValueError('config file is invalid. Missing "config.process" key')
|
| 46 |
+
if len(self.config['process']) == 0:
|
| 47 |
+
raise ValueError('config file is invalid. "config.process" must be a list of processes')
|
| 48 |
+
|
| 49 |
+
module = importlib.import_module('jobs.process')
|
| 50 |
+
|
| 51 |
+
# add the processes
|
| 52 |
+
self.process = []
|
| 53 |
+
for i, process in enumerate(self.config['process']):
|
| 54 |
+
if 'type' not in process:
|
| 55 |
+
raise ValueError(f'config file is invalid. Missing "config.process[{i}].type" key')
|
| 56 |
+
|
| 57 |
+
# check if dict key is process type
|
| 58 |
+
if process['type'] in process_dict:
|
| 59 |
+
if isinstance(process_dict[process['type']], str):
|
| 60 |
+
ProcessClass = getattr(module, process_dict[process['type']])
|
| 61 |
+
else:
|
| 62 |
+
# it is the class
|
| 63 |
+
ProcessClass = process_dict[process['type']]
|
| 64 |
+
self.process.append(ProcessClass(i, self, process))
|
| 65 |
+
else:
|
| 66 |
+
raise ValueError(f'config file is invalid. Unknown process type: {process["type"]}')
|
| 67 |
+
|
| 68 |
+
def cleanup(self):
|
| 69 |
+
# if you implement this in child clas,
|
| 70 |
+
# be sure to call super().cleanup() LAST
|
| 71 |
+
del self
|