image_processor.py 2.21 KB
Newer Older
1
from typing import cast
2
3


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def get_video_processor(
    processor_name: str,
    trust_remote_code: bool = False,
):
    """
    Gets a processor for the given model name via HuggingFace.
    """
    from transformers import AutoProcessor

    try:
        processor = AutoProcessor.from_pretrained(processor_name)
        video_processor = processor.video_processor

    except ValueError as e:
        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 video_processor


31
32
33
34
35
def get_image_processor(
    processor_name: str,
    *args,
    trust_remote_code: bool = False,
    **kwargs,
36
):
37
    """Gets an image processor for the given model name via HuggingFace."""
38
39
40
41
42
    # 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

43
    try:
44
        processor = AutoImageProcessor.from_pretrained(
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            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

64
    return cast(BaseImageProcessor, processor)