-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
58 lines (45 loc) · 1.56 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import logging
import random
import numpy as np
from task.collection import Task2Class
from model.collection import Model2Class
from trainer.collection import Trainer2Class
def get_logger(args):
# Create a logger
logger = logging.getLogger("LABO")
logger.setLevel(logging.INFO)
# Create a file handler that writes to output.log
file_handler = logging.FileHandler(os.path.join(args.output_dir, "output.log"))
file_handler.setLevel(logging.INFO)
# Create a stream handler that prints to the screen
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
# Create a formatter for the log messages
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(message)s')
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
# Avoid interference with guidance logger
logger.propagate = False
return logger
def get_task(args, logger):
_cls = Task2Class(args.task)
task = _cls(args, logger, args.data_dir)
task.load_data()
return task
def get_model(args, logger):
_cls = Model2Class(args.model)
model = _cls(args, logger)
model.load_prompt()
return model
def get_trainer(args, logger):
_cls = Trainer2Class(args.trainer)
trainer = _cls(args, logger)
return trainer
def seed_everything(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)