emanuelaboros
commited on
Commit
•
0e93feb
1
Parent(s):
fe05e2c
Initial commit including model and configuration
Browse files- models.py +22 -7
- push_to_hf.py +9 -6
models.py
CHANGED
@@ -1,28 +1,43 @@
|
|
1 |
from transformers.modeling_outputs import TokenClassifierOutput
|
2 |
import torch
|
3 |
import torch.nn as nn
|
4 |
-
from transformers import PreTrainedModel, AutoModel, AutoConfig
|
5 |
from torch.nn import CrossEntropyLoss
|
6 |
from typing import Optional, Tuple, Union
|
7 |
-
import logging
|
|
|
8 |
from .configuration_stacked import ImpressoConfig
|
9 |
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
class ExtendedMultitaskModelForTokenClassification(PreTrainedModel):
|
14 |
|
15 |
config_class = ImpressoConfig
|
16 |
_keys_to_ignore_on_load_missing = [r"position_ids"]
|
17 |
|
18 |
-
def __init__(self, config
|
19 |
super().__init__(config)
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
self.config = config
|
22 |
|
23 |
-
|
|
|
|
|
24 |
self.bert = AutoModel.from_pretrained(
|
25 |
-
config.
|
26 |
)
|
27 |
if "classifier_dropout" not in config.__dict__:
|
28 |
classifier_dropout = 0.1
|
@@ -46,7 +61,7 @@ class ExtendedMultitaskModelForTokenClassification(PreTrainedModel):
|
|
46 |
self.token_classifiers = nn.ModuleDict(
|
47 |
{
|
48 |
task: nn.Linear(config.hidden_size, num_labels)
|
49 |
-
for task, num_labels in num_token_labels_dict.items()
|
50 |
}
|
51 |
)
|
52 |
|
|
|
1 |
from transformers.modeling_outputs import TokenClassifierOutput
|
2 |
import torch
|
3 |
import torch.nn as nn
|
4 |
+
from transformers import PreTrainedModel, AutoModel, AutoConfig, BertConfig
|
5 |
from torch.nn import CrossEntropyLoss
|
6 |
from typing import Optional, Tuple, Union
|
7 |
+
import logging, json, os
|
8 |
+
|
9 |
from .configuration_stacked import ImpressoConfig
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
|
14 |
+
def get_info(label_map):
|
15 |
+
num_token_labels_dict = {task: len(labels) for task, labels in label_map.items()}
|
16 |
+
return num_token_labels_dict
|
17 |
+
|
18 |
+
|
19 |
class ExtendedMultitaskModelForTokenClassification(PreTrainedModel):
|
20 |
|
21 |
config_class = ImpressoConfig
|
22 |
_keys_to_ignore_on_load_missing = [r"position_ids"]
|
23 |
|
24 |
+
def __init__(self, config):
|
25 |
super().__init__(config)
|
26 |
+
print("Current folder path:", os.path.dirname(os.path.abspath(__file__)))
|
27 |
+
# Get the directory of the current script
|
28 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
29 |
+
# Construct the full path to label_map.json
|
30 |
+
label_map_path = os.path.join(current_dir, "label_map.json")
|
31 |
+
|
32 |
+
label_map = json.load(open(label_map_path, "r"))
|
33 |
+
self.num_token_labels_dict = get_info(label_map)
|
34 |
self.config = config
|
35 |
|
36 |
+
import pdb
|
37 |
+
|
38 |
+
pdb.set_trace()
|
39 |
self.bert = AutoModel.from_pretrained(
|
40 |
+
config.pretrained_config["_name_or_path"], config=config.pretrained_config
|
41 |
)
|
42 |
if "classifier_dropout" not in config.__dict__:
|
43 |
classifier_dropout = 0.1
|
|
|
61 |
self.token_classifiers = nn.ModuleDict(
|
62 |
{
|
63 |
task: nn.Linear(config.hidden_size, num_labels)
|
64 |
+
for task, num_labels in self.num_token_labels_dict.items()
|
65 |
}
|
66 |
)
|
67 |
|
push_to_hf.py
CHANGED
@@ -1,9 +1,15 @@
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import argparse
|
4 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
5 |
from huggingface_hub import HfApi, Repository
|
6 |
-
|
|
|
7 |
from .configuration_stacked import ImpressoConfig
|
8 |
from .models import ExtendedMultitaskModelForTokenClassification
|
9 |
import subprocess
|
@@ -27,16 +33,13 @@ def get_info(label_map):
|
|
27 |
|
28 |
def push_model_to_hub(checkpoint_dir, repo_name, script_path):
|
29 |
checkpoint_path = get_latest_checkpoint(checkpoint_dir)
|
30 |
-
label_map = json.load(open(os.path.join(checkpoint_dir, "label_map.json"), "r"))
|
31 |
-
num_token_labels_dict = get_info(label_map)
|
32 |
config = ImpressoConfig.from_pretrained(checkpoint_path)
|
33 |
config.pretrained_config = AutoConfig.from_pretrained(config.name_or_path)
|
34 |
config.save_pretrained("stacked_bert")
|
35 |
-
|
36 |
config = ImpressoConfig.from_pretrained("stacked_bert")
|
37 |
|
38 |
model = ExtendedMultitaskModelForTokenClassification.from_pretrained(
|
39 |
-
checkpoint_path, config=config
|
40 |
)
|
41 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
42 |
local_repo_path = "./repo"
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import argparse
|
4 |
+
from transformers import (
|
5 |
+
AutoTokenizer,
|
6 |
+
AutoConfig,
|
7 |
+
AutoModelForTokenClassification,
|
8 |
+
BertConfig,
|
9 |
+
)
|
10 |
from huggingface_hub import HfApi, Repository
|
11 |
+
|
12 |
+
# import json
|
13 |
from .configuration_stacked import ImpressoConfig
|
14 |
from .models import ExtendedMultitaskModelForTokenClassification
|
15 |
import subprocess
|
|
|
33 |
|
34 |
def push_model_to_hub(checkpoint_dir, repo_name, script_path):
|
35 |
checkpoint_path = get_latest_checkpoint(checkpoint_dir)
|
|
|
|
|
36 |
config = ImpressoConfig.from_pretrained(checkpoint_path)
|
37 |
config.pretrained_config = AutoConfig.from_pretrained(config.name_or_path)
|
38 |
config.save_pretrained("stacked_bert")
|
|
|
39 |
config = ImpressoConfig.from_pretrained("stacked_bert")
|
40 |
|
41 |
model = ExtendedMultitaskModelForTokenClassification.from_pretrained(
|
42 |
+
checkpoint_path, config=config
|
43 |
)
|
44 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
45 |
local_repo_path = "./repo"
|