llm-annotator 0.3.2__tar.gz → 0.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/PKG-INFO +1 -1
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/src/llm_annotator/annotator.py +39 -11
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/.gitignore +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/.pre-commit-config.yaml +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/LICENSE +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/Makefile +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/README.md +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/examples/sentiment.py +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/pyproject.toml +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/src/llm_annotator/__init__.py +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/src/llm_annotator/utils.py +0 -0
- {llm_annotator-0.3.2 → llm_annotator-0.3.3}/tests/conftest.py +0 -0
|
@@ -385,15 +385,30 @@ class Annotator:
|
|
|
385
385
|
"""Load and configure the tokenizer for the model.
|
|
386
386
|
|
|
387
387
|
Sets up the tokenizer with appropriate padding settings and ensures
|
|
388
|
-
a pad token is available.
|
|
388
|
+
a pad token is available. Some models (like recent Mistral) do not have
|
|
389
|
+
a chat template defined in their tokenizer, so we attempt to load
|
|
390
|
+
a processor instead in that case.
|
|
389
391
|
"""
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
392
|
+
try:
|
|
393
|
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model)
|
|
394
|
+
if self.tokenizer.chat_template is None:
|
|
395
|
+
raise ValueError("No chat template defined in the tokenizer.")
|
|
396
|
+
except ValueError as tokenizer_error:
|
|
397
|
+
try:
|
|
398
|
+
self.tokenizer = self.AutoProcessor.from_pretrained(self.model)
|
|
399
|
+
if self.tokenizer.chat_template is None:
|
|
400
|
+
raise ValueError("No chat template defined in the processor either.") from tokenizer_error
|
|
401
|
+
except Exception as exc:
|
|
402
|
+
raise ValueError(
|
|
403
|
+
"Failed to load tokenizer or processor with chat template."
|
|
404
|
+
" Are you correctly using an instruct model?"
|
|
405
|
+
) from exc
|
|
406
|
+
else:
|
|
407
|
+
self.tokenizer.padding_side = "left"
|
|
393
408
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
409
|
+
if not self.tokenizer.pad_token_id:
|
|
410
|
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
|
411
|
+
self.tokenizer.pad_token_id = self.tokenizer.convert_tokens_to_ids(self.tokenizer.pad_token)
|
|
397
412
|
|
|
398
413
|
def _load_pipeline(self) -> None:
|
|
399
414
|
"""Load and initialize the vLLM pipeline for inference.
|
|
@@ -576,7 +591,8 @@ class Annotator:
|
|
|
576
591
|
task_prefix: str = "",
|
|
577
592
|
sort_by_length: bool = False,
|
|
578
593
|
validate_fn: Callable | None = None,
|
|
579
|
-
num_retries_invalid: int =
|
|
594
|
+
num_retries_invalid: int = 5,
|
|
595
|
+
keep_idx_column: bool = False,
|
|
580
596
|
) -> Dataset:
|
|
581
597
|
"""Annotate an entire dataset using the configured model and prompt.
|
|
582
598
|
|
|
@@ -634,6 +650,10 @@ class Annotator:
|
|
|
634
650
|
num_retries_invalid: Number of retries for samples that produce invalid outputs (when
|
|
635
651
|
a JSON schema is given and {prefix}valid_fields is False, or when a validate_fn is given
|
|
636
652
|
and it {prefix}valid is False).
|
|
653
|
+
keep_idx_column: Whether to keep the idx_column in the final dataset before uploading and returning.
|
|
654
|
+
|
|
655
|
+
Returns:
|
|
656
|
+
The concatenated dataset of all annotation results (JSON-invalid samples are NOT removed)
|
|
637
657
|
"""
|
|
638
658
|
# Verify shared_prompt_template
|
|
639
659
|
if prompt_template_prefix:
|
|
@@ -846,9 +866,13 @@ class Annotator:
|
|
|
846
866
|
if new_hub_id and upload_every_n_samples > 0:
|
|
847
867
|
self.push_dir_to_hub(pdout, new_hub_id=new_hub_id)
|
|
848
868
|
|
|
849
|
-
return self._post_annotate(
|
|
869
|
+
return self._post_annotate(
|
|
870
|
+
pdout=pdout, idx_column=idx_column, new_hub_id=new_hub_id, keep_idx_column=keep_idx_column
|
|
871
|
+
)
|
|
850
872
|
|
|
851
|
-
def _post_annotate(
|
|
873
|
+
def _post_annotate(
|
|
874
|
+
self, *, pdout: Path, idx_column: str, new_hub_id: str | None = None, keep_idx_column: bool = False
|
|
875
|
+
) -> Dataset:
|
|
852
876
|
"""Clean up after annotation is complete.
|
|
853
877
|
|
|
854
878
|
Removes empty output files and performs any final cleanup operations.
|
|
@@ -856,6 +880,8 @@ class Annotator:
|
|
|
856
880
|
Args:
|
|
857
881
|
pdout: Output directory path to clean up.
|
|
858
882
|
new_hub_id: Optional Hugging Face dataset ID for uploads.
|
|
883
|
+
idx_column: Column name used as unique identifier.
|
|
884
|
+
keep_idx_column: Whether to keep the idx_column in the final dataset before uploading and returning
|
|
859
885
|
|
|
860
886
|
Returns:
|
|
861
887
|
The concatenated dataset of all annotation results (JSON-invalid samples are NOT removed)
|
|
@@ -865,7 +891,9 @@ class Annotator:
|
|
|
865
891
|
if pfin.stat().st_size > 0:
|
|
866
892
|
ds_parts.append(Dataset.from_json(str(pfin)))
|
|
867
893
|
|
|
868
|
-
ds: Dataset = concatenate_datasets(ds_parts).sort(idx_column)
|
|
894
|
+
ds: Dataset = concatenate_datasets(ds_parts).sort(idx_column)
|
|
895
|
+
if not keep_idx_column:
|
|
896
|
+
ds = ds.remove_columns([idx_column])
|
|
869
897
|
|
|
870
898
|
if new_hub_id:
|
|
871
899
|
ds.push_to_hub(new_hub_id, private=True)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|