Run this model with HF Jobs
Run vyach-g/test-ner-tuned on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/vyach-g/test-ner-tuned. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/vyach-g/test-ner-tuned on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use vyach-g/test-ner-tuned with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers.js
How to use vyach-g/test-ner-tuned with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('token-classification', 'vyach-g/test-ner-tuned');
Download training/src/gbert_ner/data.py from vyach-g/test-ner-tuned: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/vyach-g/test-ner-tuned/resolve/main/training/src/gbert_ner/data.py
- Command line
-
hf download hf://vyach-g/test-ner-tuned/training/src/gbert_ner/data.py
-
curl -L -o data.py https://huggingface.co/vyach-g/test-ner-tuned/resolve/main/training/src/gbert_ner/data.py
5.91 kB
| from __future__ import annotations | |
| import re | |
| from typing import Any | |
| from datasets import Dataset, DatasetDict, concatenate_datasets, load_dataset | |
| from gbert_ner.labels import ( | |
| GERMEVAL_DATASET_FALLBACK_ID, | |
| GERMEVAL_DATASET_ID, | |
| GPTNERMED_CLASS_TO_TYPE, | |
| GPTNERMED_JSONL_URL, | |
| remap_germeval_tag, | |
| ) | |
| WORD_PATTERN = re.compile(r"\S+") | |
| def _as_tag_list(tags: Any, class_names: list[str] | None) -> list[str]: | |
| if not tags: | |
| return [] | |
| if isinstance(tags[0], str): | |
| return list(tags) | |
| if class_names is None: | |
| raise TypeError("Integer NER tags require ClassLabel names.") | |
| return [class_names[int(tag)] for tag in tags] | |
| def load_germeval() -> DatasetDict: | |
| try: | |
| raw = load_dataset(GERMEVAL_DATASET_ID) | |
| tag_key = "ner_tag" | |
| class_names = None | |
| except Exception: | |
| raw = load_dataset(GERMEVAL_DATASET_FALLBACK_ID) | |
| tag_key = "ner_tags" | |
| class_names = raw["train"].features[tag_key].feature.names | |
| def map_split(split: Dataset) -> Dataset: | |
| tokens_column: list[list[str]] = [] | |
| tags_column: list[list[str]] = [] | |
| for row in split: | |
| tokens = list(row["tokens"]) | |
| tags = [ | |
| remap_germeval_tag(tag) | |
| for tag in _as_tag_list(row[tag_key], class_names) | |
| ] | |
| if len(tokens) != len(tags): | |
| continue | |
| tokens_column.append(tokens) | |
| tags_column.append(tags) | |
| return Dataset.from_dict({"tokens": tokens_column, "ner_tags": tags_column}) | |
| return DatasetDict({name: map_split(split) for name, split in raw.items()}) | |
| def _span_end(entity: dict[str, Any]) -> int: | |
| for key in ("stop", "end", "end_char"): | |
| if key in entity and entity[key] is not None: | |
| return int(entity[key]) | |
| raise KeyError("GPTNERMED entity is missing a stop/end offset.") | |
| def _span_start(entity: dict[str, Any]) -> int: | |
| for key in ("start", "start_char"): | |
| if key in entity and entity[key] is not None: | |
| return int(entity[key]) | |
| raise KeyError("GPTNERMED entity is missing a start offset.") | |
| def _entity_class(entity: dict[str, Any]) -> str: | |
| for key in ("ner_class", "label", "entity"): | |
| value = entity.get(key) | |
| if isinstance(value, str) and value: | |
| return value | |
| raise KeyError("GPTNERMED entity is missing a class name.") | |
| def _gptnermed_entities(row: dict[str, Any]) -> list[dict[str, Any]]: | |
| if row.get("ner_labels"): | |
| return list(row["ner_labels"]) | |
| entities: list[dict[str, Any]] = [] | |
| for item in row.get("label") or []: | |
| if isinstance(item, dict): | |
| entities.append(item) | |
| continue | |
| start, stop, label_class = item | |
| entities.append( | |
| {"start": int(start), "stop": int(stop), "ner_class": str(label_class)} | |
| ) | |
| return entities | |
| def sentence_to_bio( | |
| text: str, entities: list[dict[str, Any]] | |
| ) -> tuple[list[str], list[str]]: | |
| words = [ | |
| (match.group(), match.start(), match.end()) | |
| for match in WORD_PATTERN.finditer(text) | |
| ] | |
| tags = ["O"] * len(words) | |
| for entity in sorted(entities, key=_span_start): | |
| type_name = GPTNERMED_CLASS_TO_TYPE.get(_entity_class(entity)) | |
| if type_name is None: | |
| continue | |
| start = _span_start(entity) | |
| end = _span_end(entity) | |
| first = True | |
| for index, (_, word_start, word_end) in enumerate(words): | |
| if word_end <= start or word_start >= end: | |
| continue | |
| tags[index] = f"{'B' if first else 'I'}-{type_name}" | |
| first = False | |
| return [word for word, _, _ in words], tags | |
| def load_gptnermed() -> DatasetDict: | |
| # Hub repo still ships a loading script; datasets>=4 refuses to run it. | |
| raw = load_dataset("json", data_files=GPTNERMED_JSONL_URL, split="train") | |
| tokens_column: list[list[str]] = [] | |
| tags_column: list[list[str]] = [] | |
| for row in raw: | |
| text = row.get("text") or row.get("sentence") | |
| if not isinstance(text, str) or not text.strip(): | |
| continue | |
| tokens, tags = sentence_to_bio(text, _gptnermed_entities(row)) | |
| if not tokens: | |
| continue | |
| tokens_column.append(tokens) | |
| tags_column.append(tags) | |
| mapped = Dataset.from_dict({"tokens": tokens_column, "ner_tags": tags_column}) | |
| holdout = mapped.train_test_split(test_size=0.2, seed=0) | |
| tails = holdout["test"].train_test_split(test_size=0.5, seed=0) | |
| return DatasetDict( | |
| { | |
| "train": holdout["train"], | |
| "validation": tails["train"], | |
| "test": tails["test"], | |
| } | |
| ) | |
| def mix_datasets( | |
| germeval: DatasetDict, | |
| gptnermed: DatasetDict, | |
| *, | |
| dis_repeats: int, | |
| max_samples: int | None, | |
| seed: int, | |
| ) -> DatasetDict: | |
| def take(split_name: str, source: DatasetDict) -> Dataset: | |
| if split_name in source: | |
| return source[split_name] | |
| if split_name == "validation" and "test" in source: | |
| return source["test"] | |
| return source["train"] | |
| mixed: dict[str, Dataset] = {} | |
| for split_name in ("train", "validation", "test"): | |
| news = take(split_name, germeval) | |
| clinical = take(split_name, gptnermed) | |
| if split_name == "train" and dis_repeats > 1: | |
| clinical = concatenate_datasets([clinical] * dis_repeats) | |
| split = concatenate_datasets([news, clinical]).shuffle(seed=seed) | |
| if max_samples is not None: | |
| split = split.select(range(min(max_samples, len(split)))) | |
| mixed[split_name] = split | |
| return DatasetDict(mixed) | |
| def count_labels(dataset: Dataset) -> dict[str, int]: | |
| counts: dict[str, int] = {} | |
| for tags in dataset["ner_tags"]: | |
| for tag in tags: | |
| counts[tag] = counts.get(tag, 0) + 1 | |
| return dict(sorted(counts.items())) | |