Upload train.py with huggingface_hub
Browse files
train.py
CHANGED
@@ -8,105 +8,12 @@ from span_marker.model_card import SpanMarkerModelCardData
|
|
8 |
from huggingface_hub import upload_folder, upload_file
|
9 |
|
10 |
|
11 |
-
"""
|
12 |
-
FEATURES = Features({"tokens": Sequence(feature=Value(dtype='string')), "ner_tags": Sequence(feature=ClassLabel(names=['O', 'B-ORG', 'I-ORG']))})
|
13 |
-
|
14 |
-
|
15 |
-
def load_fewnerd():
|
16 |
-
def mapper(sample):
|
17 |
-
sample["ner_tags"] = [int(tag == 5) for tag in sample["ner_tags"]]
|
18 |
-
sample["ner_tags"] = [2 if tag == 1 and idx > 0 and sample["ner_tags"][idx - 1] == 1 else tag for idx, tag in enumerate(sample["ner_tags"])]
|
19 |
-
return sample
|
20 |
-
|
21 |
-
dataset = load_dataset("DFKI-SLT/few-nerd", "supervised")
|
22 |
-
dataset = dataset.map(mapper, remove_columns=["id", "fine_ner_tags"])
|
23 |
-
dataset = dataset.cast(FEATURES)
|
24 |
-
return dataset
|
25 |
-
|
26 |
-
|
27 |
-
def load_conll():
|
28 |
-
label_mapping = {3: 1, 4: 2}
|
29 |
-
def mapper(sample):
|
30 |
-
sample["ner_tags"] = [label_mapping.get(tag, 0) for tag in sample["ner_tags"]]
|
31 |
-
return sample
|
32 |
-
|
33 |
-
dataset = load_dataset("conll2003")
|
34 |
-
dataset = dataset.map(mapper, remove_columns=["id", "pos_tags", "chunk_tags"])
|
35 |
-
dataset = dataset.cast(FEATURES)
|
36 |
-
return dataset
|
37 |
-
|
38 |
-
|
39 |
-
def load_ontonotes():
|
40 |
-
label_mapping = {11: 1, 12: 2}
|
41 |
-
def mapper(sample):
|
42 |
-
sample["ner_tags"] = [label_mapping.get(tag, 0) for tag in sample["ner_tags"]]
|
43 |
-
return sample
|
44 |
-
|
45 |
-
dataset = load_dataset("tner/ontonotes5")
|
46 |
-
dataset = dataset.rename_column("tags", "ner_tags")
|
47 |
-
dataset = dataset.map(mapper)
|
48 |
-
dataset = dataset.cast(FEATURES)
|
49 |
-
return dataset
|
50 |
-
|
51 |
-
|
52 |
-
def load_multinerd():
|
53 |
-
label_mapping = {5: 1, 6: 2}
|
54 |
-
def mapper(sample):
|
55 |
-
sample["ner_tags"] = [label_mapping.get(tag, 0) for tag in sample["ner_tags"]]
|
56 |
-
return sample
|
57 |
-
|
58 |
-
def lang_filter(sample):
|
59 |
-
return sample["lang"] == "en"
|
60 |
-
|
61 |
-
dataset = load_dataset("Babelscape/multinerd")
|
62 |
-
dataset = dataset.filter(lang_filter)
|
63 |
-
dataset = dataset.map(mapper, remove_columns="lang")
|
64 |
-
dataset = dataset.cast(FEATURES)
|
65 |
-
return dataset
|
66 |
-
|
67 |
-
|
68 |
-
def preprocess_raw_dataset(raw_dataset):
|
69 |
-
# Set the number of sentences without an org equal to the number of sentences with an org
|
70 |
-
def has_org(sample):
|
71 |
-
return bool(sum(sample["ner_tags"]))
|
72 |
-
|
73 |
-
def has_no_org(sample):
|
74 |
-
return not has_org(sample)
|
75 |
-
|
76 |
-
dataset_org = raw_dataset.filter(has_org)
|
77 |
-
dataset_no_org = raw_dataset.filter(has_no_org)
|
78 |
-
dataset_no_org = dataset_no_org.select(random.sample(range(len(dataset_no_org)), k=len(dataset_org)))
|
79 |
-
dataset = concatenate_datasets([dataset_org, dataset_no_org])
|
80 |
-
return dataset
|
81 |
-
"""
|
82 |
-
|
83 |
|
84 |
def main() -> None:
|
85 |
# Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
|
86 |
labels = ["O", "B-ORG", "I-ORG"]
|
87 |
-
""
|
88 |
-
|
89 |
-
conll_dataset = load_conll()
|
90 |
-
ontonotes_dataset = load_ontonotes()
|
91 |
-
multinerd_dataset = load_multinerd()
|
92 |
-
|
93 |
-
raw_train_dataset = concatenate_datasets([fewnerd_dataset["train"], conll_dataset["train"], ontonotes_dataset["train"], multinerd_dataset["train"]])
|
94 |
-
raw_eval_dataset = concatenate_datasets([fewnerd_dataset["validation"], conll_dataset["validation"], ontonotes_dataset["validation"], multinerd_dataset["validation"]])
|
95 |
-
raw_test_dataset = concatenate_datasets([fewnerd_dataset["test"], conll_dataset["test"], ontonotes_dataset["test"], multinerd_dataset["test"]])
|
96 |
-
|
97 |
-
train_dataset = preprocess_raw_dataset(raw_train_dataset)
|
98 |
-
eval_dataset = preprocess_raw_dataset(raw_eval_dataset)
|
99 |
-
test_dataset = preprocess_raw_dataset(raw_test_dataset)
|
100 |
-
|
101 |
-
dataset_dict = DatasetDict({
|
102 |
-
"train": train_dataset,
|
103 |
-
"validation": eval_dataset,
|
104 |
-
"test": test_dataset,
|
105 |
-
})
|
106 |
-
dataset_dict.push_to_hub("ner-orgs", private=True)
|
107 |
-
"""
|
108 |
-
# breakpoint()
|
109 |
-
dataset = load_dataset("tomaarsen/ner-orgs")
|
110 |
|
111 |
train_dataset = dataset["train"]
|
112 |
eval_dataset = dataset["validation"]
|
@@ -126,8 +33,10 @@ def main() -> None:
|
|
126 |
# Model card variables
|
127 |
model_card_data=SpanMarkerModelCardData(
|
128 |
model_id=model_id,
|
|
|
129 |
encoder_id=encoder_id,
|
130 |
-
dataset_name="FewNERD, CoNLL2003, OntoNotes v5
|
|
|
131 |
language=["en"],
|
132 |
),
|
133 |
)
|
|
|
8 |
from huggingface_hub import upload_folder, upload_file
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def main() -> None:
|
13 |
# Load the dataset, ensure "tokens" and "ner_tags" columns, and get a list of labels
|
14 |
labels = ["O", "B-ORG", "I-ORG"]
|
15 |
+
dataset_id = "tomaarsen/ner-orgs"
|
16 |
+
dataset = load_dataset(dataset_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
train_dataset = dataset["train"]
|
19 |
eval_dataset = dataset["validation"]
|
|
|
33 |
# Model card variables
|
34 |
model_card_data=SpanMarkerModelCardData(
|
35 |
model_id=model_id,
|
36 |
+
dataset_id=dataset_id,
|
37 |
encoder_id=encoder_id,
|
38 |
+
dataset_name="FewNERD, CoNLL2003, and OntoNotes v5",
|
39 |
+
license="cc-by-sa-4.0",
|
40 |
language=["en"],
|
41 |
),
|
42 |
)
|