processor.py 3.5 KB
Newer Older
1
from functools import lru_cache
2
from typing import Any, cast
3

4
5
from transformers.processing_utils import ProcessorMixin

6
7
8

def get_processor(
    processor_name: str,
9
    *args: Any,
10
    trust_remote_code: bool = False,
11
    processor_cls: type[ProcessorMixin] = ProcessorMixin,
12
    **kwargs: Any,
13
):
14
    """Load a processor for the given model name via HuggingFace."""
15
16
17
    # don't put this import at the top level
    # it will call torch.cuda.device_count()
    from transformers import AutoProcessor
18
19
20

    processor_factory = (AutoProcessor
                         if processor_cls == ProcessorMixin else processor_cls)
21
22

    try:
23
        processor = processor_factory.from_pretrained(
24
25
26
            processor_name,
            *args,
            trust_remote_code=trust_remote_code,
27
28
            **kwargs,
        )
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    except ValueError as e:
        # If the error pertains to the processor class not existing or not
        # currently being imported, suggest using the --trust-remote-code flag.
        # Unlike AutoTokenizer, AutoProcessor does not separate such errors
        if not trust_remote_code:
            err_msg = (
                "Failed to load the processor. If the processor is "
                "a custom processor not yet available in the HuggingFace "
                "transformers library, consider setting "
                "`trust_remote_code=True` in LLM or using the "
                "`--trust-remote-code` flag in the CLI.")
            raise RuntimeError(err_msg) from e
        else:
            raise e

    return cast(ProcessorMixin, processor)
45
46


47
48
49
cached_get_processor = lru_cache(get_processor)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
def get_image_processor(
    processor_name: str,
    *args: Any,
    trust_remote_code: bool = False,
    **kwargs: Any,
):
    """Load an image processor for the given model name via HuggingFace."""
    # don't put this import at the top level
    # it will call torch.cuda.device_count()
    from transformers import AutoImageProcessor
    from transformers.image_processing_utils import BaseImageProcessor

    try:
        processor = AutoImageProcessor.from_pretrained(
            processor_name,
            *args,
            trust_remote_code=trust_remote_code,
            **kwargs)
    except ValueError as e:
        # If the error pertains to the processor class not existing or not
        # currently being imported, suggest using the --trust-remote-code flag.
        # Unlike AutoTokenizer, AutoImageProcessor does not separate such errors
        if not trust_remote_code:
            err_msg = (
                "Failed to load the image processor. If the image processor is "
                "a custom processor not yet available in the HuggingFace "
                "transformers library, consider setting "
                "`trust_remote_code=True` in LLM or using the "
                "`--trust-remote-code` flag in the CLI.")
            raise RuntimeError(err_msg) from e
        else:
            raise e

    return cast(BaseImageProcessor, processor)


def get_video_processor(
    processor_name: str,
    *args: Any,
    trust_remote_code: bool = False,
    **kwargs: Any,
):
    """Load a video processor for the given model name via HuggingFace."""
    # don't put this import at the top level
    # it will call torch.cuda.device_count()
    from transformers.image_processing_utils import BaseImageProcessor

    processor = get_processor(
        processor_name,
        *args,
        trust_remote_code=trust_remote_code,
        **kwargs,
    )

    return cast(BaseImageProcessor, processor.video_processor)