Upload jobs/process/BaseProcess.py with huggingface_hub
Browse files- jobs/process/BaseProcess.py +64 -0
jobs/process/BaseProcess.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import copy
|
| 2 |
+
import json
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
|
| 5 |
+
from toolkit.timer import Timer
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BaseProcess(object):
|
| 9 |
+
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
process_id: int,
|
| 13 |
+
job: 'BaseJob',
|
| 14 |
+
config: OrderedDict
|
| 15 |
+
):
|
| 16 |
+
self.process_id = process_id
|
| 17 |
+
self.meta: OrderedDict
|
| 18 |
+
self.job = job
|
| 19 |
+
self.config = config
|
| 20 |
+
self.raw_process_config = config
|
| 21 |
+
self.name = self.get_conf('name', self.job.name)
|
| 22 |
+
self.meta = copy.deepcopy(self.job.meta)
|
| 23 |
+
self.timer: Timer = Timer(f'{self.name} Timer')
|
| 24 |
+
self.performance_log_every = self.get_conf('performance_log_every', 0)
|
| 25 |
+
|
| 26 |
+
print(json.dumps(self.config, indent=4))
|
| 27 |
+
|
| 28 |
+
def on_error(self, e: Exception):
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def get_conf(self, key, default=None, required=False, as_type=None):
|
| 32 |
+
# split key by '.' and recursively get the value
|
| 33 |
+
keys = key.split('.')
|
| 34 |
+
|
| 35 |
+
# see if it exists in the config
|
| 36 |
+
value = self.config
|
| 37 |
+
for subkey in keys:
|
| 38 |
+
if subkey in value:
|
| 39 |
+
value = value[subkey]
|
| 40 |
+
else:
|
| 41 |
+
value = None
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
if value is not None:
|
| 45 |
+
if as_type is not None:
|
| 46 |
+
value = as_type(value)
|
| 47 |
+
return value
|
| 48 |
+
elif required:
|
| 49 |
+
raise ValueError(f'config file error. Missing "config.process[{self.process_id}].{key}" key')
|
| 50 |
+
else:
|
| 51 |
+
if as_type is not None and default is not None:
|
| 52 |
+
return as_type(default)
|
| 53 |
+
return default
|
| 54 |
+
|
| 55 |
+
def run(self):
|
| 56 |
+
# implement in child class
|
| 57 |
+
# be sure to call super().run() first incase something is added here
|
| 58 |
+
pass
|
| 59 |
+
|
| 60 |
+
def add_meta(self, additional_meta: OrderedDict):
|
| 61 |
+
self.meta.update(additional_meta)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
from jobs import BaseJob
|