- **Add SPDX license headers to python source files** - **Check for SPDX headers using pre-commit** commit 9d7ef44c3cfb72ca4c32e1c677d99259d10d4745 Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:18:24 2025 -0500 Add SPDX license headers to python source files This commit adds SPDX license headers to python source files as recommended to the project by the Linux Foundation. These headers provide a concise way that is both human and machine readable for communicating license information for each source file. It helps avoid any ambiguity about the license of the code and can also be easily used by tools to help manage license compliance. The Linux Foundation runs license scans against the codebase to help ensure we are in compliance with the licenses of the code we use, including dependencies. Having these headers in place helps that tool do its job. More information can be found on the SPDX site: - https://spdx.dev/learn/handling-license-info/ Signed-off-by: Russell Bryant <rbryant@redhat.com> commit 5a1cf1cb3b80759131c73f6a9dddebccac039dea Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:36:32 2025 -0500 Check for SPDX headers using pre-commit Signed-off-by: Russell Bryant <rbryant@redhat.com> --------- Signed-off-by: Russell Bryant <rbryant@redhat.com>
107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
from functools import lru_cache
|
|
from typing import Any, cast
|
|
|
|
from transformers.processing_utils import ProcessorMixin
|
|
|
|
|
|
def get_processor(
|
|
processor_name: str,
|
|
*args: Any,
|
|
trust_remote_code: bool = False,
|
|
processor_cls: type[ProcessorMixin] = ProcessorMixin,
|
|
**kwargs: Any,
|
|
):
|
|
"""Load a 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 AutoProcessor
|
|
|
|
processor_factory = (AutoProcessor
|
|
if processor_cls == ProcessorMixin else processor_cls)
|
|
|
|
try:
|
|
processor = processor_factory.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, 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)
|
|
|
|
|
|
cached_get_processor = lru_cache(get_processor)
|
|
|
|
|
|
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)
|