1
0

processor.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from typing import Any, cast
  2. def get_processor(
  3. processor_name: str,
  4. *args: Any,
  5. trust_remote_code: bool = False,
  6. **kwargs: Any,
  7. ):
  8. """Load a processor for the given model name via HuggingFace."""
  9. # don't put this import at the top level
  10. # it will call torch.cuda.device_count()
  11. from transformers import AutoProcessor
  12. from transformers.processing_utils import ProcessorMixin
  13. try:
  14. processor = AutoProcessor.from_pretrained(
  15. processor_name, *args, trust_remote_code=trust_remote_code, **kwargs
  16. )
  17. except ValueError as e:
  18. # If the error pertains to the processor class not existing or not
  19. # currently being imported, suggest using the --trust-remote-code flag.
  20. # Unlike AutoTokenizer, AutoProcessor does not separate such errors
  21. if not trust_remote_code:
  22. err_msg = (
  23. "Failed to load the processor. If the processor is "
  24. "a custom processor not yet available in the HuggingFace "
  25. "transformers library, consider setting "
  26. "`trust_remote_code=True` in LLM or using the "
  27. "`--trust-remote-code` flag in the CLI."
  28. )
  29. raise RuntimeError(err_msg) from e
  30. else:
  31. raise e
  32. return cast(ProcessorMixin, processor)
  33. def get_image_processor(
  34. processor_name: str,
  35. *args: Any,
  36. trust_remote_code: bool = False,
  37. **kwargs: Any,
  38. ):
  39. """Load an image processor for the given model name via HuggingFace."""
  40. # don't put this import at the top level
  41. # it will call torch.cuda.device_count()
  42. from transformers import AutoImageProcessor
  43. from transformers.image_processing_utils import BaseImageProcessor
  44. try:
  45. processor = AutoImageProcessor.from_pretrained(
  46. processor_name,
  47. *args,
  48. trust_remote_code=trust_remote_code,
  49. **kwargs)
  50. except ValueError as e:
  51. # If the error pertains to the processor class not existing or not
  52. # currently being imported, suggest using the --trust-remote-code flag.
  53. # Unlike AutoTokenizer, AutoImageProcessor does not separate such errors
  54. if not trust_remote_code:
  55. err_msg = (
  56. "Failed to load the image processor. If the image processor is "
  57. "a custom processor not yet available in the HuggingFace "
  58. "transformers library, consider setting "
  59. "`trust_remote_code=True` in LLM or using the "
  60. "`--trust-remote-code` flag in the CLI.")
  61. raise RuntimeError(err_msg) from e
  62. else:
  63. raise e
  64. return cast(BaseImageProcessor, processor)
  65. def get_video_processor(
  66. processor_name: str,
  67. *args: Any,
  68. trust_remote_code: bool = False,
  69. **kwargs: Any,
  70. ):
  71. """Load a video processor for the given model name via HuggingFace."""
  72. # don't put this import at the top level
  73. # it will call torch.cuda.device_count()
  74. from transformers.image_processing_utils import BaseImageProcessor
  75. processor = get_processor(
  76. processor_name,
  77. *args,
  78. trust_remote_code=trust_remote_code,
  79. **kwargs,
  80. )
  81. return cast(BaseImageProcessor, processor.video_processor)