





from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class 			lowercase				(							a__     ):



						"""simple docstring"""

						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			Tuple  						=   SMALL_MODEL_IDENTIFIER
										_snake_case :			Optional[int]  						=   '''pt'''
										_snake_case :			Optional[Any]  						=   '''tf'''







						def 		__UpperCAmelCase       (    self			:		Optional[int] ,				lowerCamelCase_			:		int					):





										'''simple docstring'''







										_snake_case :			List[str]  						=   AutoModel.from_pretrained(self.test_model					)
										model_pt.save_pretrained(lowercase__					)







						def 		__UpperCAmelCase       (    self			:		Any ,				lowerCamelCase_			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			Dict  						=   TFAutoModel.from_pretrained(self.test_model ,				from_pt=lowercase__					)
										model_tf.save_pretrained(lowercase__					)







						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			List[str]  						=   '''mock_framework'''

										# Framework provided - return whatever the user provides
										_snake_case :			Tuple  						=   FeaturesManager.determine_framework(self.test_model ,				lowercase__					)
										self.assertEqual(lowercase__ ,				lowercase__					)

										# Local checkpoint and framework provided - return provided framework
										# PyTorch checkpoint
										with TemporaryDirectory() as local_pt_ckpt:
														self._setup_pt_ckpt(lowercase__					)
														_snake_case :			Optional[Any]  						=   FeaturesManager.determine_framework(lowercase__ ,				lowercase__					)
														self.assertEqual(lowercase__ ,				lowercase__					)

										# TensorFlow checkpoint
										with TemporaryDirectory() as local_tf_ckpt:
														self._setup_tf_ckpt(lowercase__					)
														_snake_case :			Tuple  						=   FeaturesManager.determine_framework(lowercase__ ,				lowercase__					)
														self.assertEqual(lowercase__ ,				lowercase__					)







						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										with TemporaryDirectory() as local_pt_ckpt:
														self._setup_pt_ckpt(lowercase__					)
														_snake_case :			Dict  						=   FeaturesManager.determine_framework(lowercase__					)
														self.assertEqual(lowercase__ ,				self.framework_pt					)

										# TensorFlow checkpoint
										with TemporaryDirectory() as local_tf_ckpt:
														self._setup_tf_ckpt(lowercase__					)
														_snake_case :			Optional[int]  						=   FeaturesManager.determine_framework(lowercase__					)
														self.assertEqual(lowercase__ ,				self.framework_tf					)

										# Invalid local checkpoint
										with TemporaryDirectory() as local_invalid_ckpt:
														with self.assertRaises(lowercase__					):
																		_snake_case :			Union[str, Any]  						=   FeaturesManager.determine_framework(lowercase__					)







						def 		__UpperCAmelCase       (    self			:		Any					):





										'''simple docstring'''







										_snake_case :			Optional[int]  						=   MagicMock(return_value=lowercase__					)
										with patch('transformers.onnx.features.is_tf_available' ,				lowercase__					):
														_snake_case :			Optional[Any]  						=   FeaturesManager.determine_framework(self.test_model					)
														self.assertEqual(lowercase__ ,				self.framework_pt					)

										# PyTorch not in environment -> use TensorFlow
										_snake_case :			Optional[Any]  						=   MagicMock(return_value=lowercase__					)
										with patch('transformers.onnx.features.is_torch_available' ,				lowercase__					):
														_snake_case :			Optional[Any]  						=   FeaturesManager.determine_framework(self.test_model					)
														self.assertEqual(lowercase__ ,				self.framework_tf					)

										# Both in environment -> use PyTorch
										_snake_case :			Optional[int]  						=   MagicMock(return_value=lowercase__					)
										_snake_case :			str  						=   MagicMock(return_value=lowercase__					)
										with patch('transformers.onnx.features.is_tf_available' ,				lowercase__					), patch(
										    'transformers.onnx.features.is_torch_available' ,				lowercase__					):
														_snake_case :			Tuple  						=   FeaturesManager.determine_framework(self.test_model					)
														self.assertEqual(lowercase__ ,				self.framework_pt					)

										# Both not in environment -> raise error
										_snake_case :			Dict  						=   MagicMock(return_value=lowercase__					)
										_snake_case :			Optional[int]  						=   MagicMock(return_value=lowercase__					)
										with patch('transformers.onnx.features.is_tf_available' ,				lowercase__					), patch(
										    'transformers.onnx.features.is_torch_available' ,				lowercase__					):
														with self.assertRaises(lowercase__					):
																		_snake_case :			Optional[Any]  						=   FeaturesManager.determine_framework(self.test_model					)








import warnings

from ...utils import logging
from .image_processing_segformer import SegformerImageProcessor


lowercase_		:				str				=						logging.get_logger(__name__)


class 			lowercase				(							a_     ):



						"""simple docstring"""

						def __init__(    self			:		int ,				*lowerCamelCase_			:		str ,				**lowerCamelCase_			:		Tuple					):





										'''simple docstring'''







										warnings.warn(
										    'The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'
										    ' Please use SegformerImageProcessor instead.' ,				lowerCamelCase_ ,				)
										super().__init__(*lowerCamelCase_ ,				**lowerCamelCase_					)






