






'''simple docstring'''







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 	SCREAMING_SNAKE_CASE					(					__snake_case       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			str:
							a_		:  Union[str, Any]											=      SMALL_MODEL_IDENTIFIER
							a_		:  List[Any]											=      """pt"""
							a_		:  Union[str, Any]											=      """tf"""






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict					)			->			str:
							a_		:  Tuple											=      AutoModel.from_pretrained(self.test_model					)
							model_pt.save_pretrained(_lowercase					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]					)			->			Tuple:
							a_		:  List[str]											=      TFAutoModel.from_pretrained(self.test_model  ,						from_pt=_lowercase					)
							model_tf.save_pretrained(_lowercase					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Tuple:
							a_		:  Any											=      """mock_framework"""

							# Framework provided - return whatever the user provides
							a_		:  str											=      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					)
											a_		:  int											=      FeaturesManager.determine_framework(_lowercase  ,						_lowercase					)
											self.assertEqual(_lowercase  ,						_lowercase					)

							# TensorFlow checkpoint
							with TemporaryDirectory() as local_tf_ckpt:
											self._setup_tf_ckpt(_lowercase					)
											a_		:  List[str]											=      FeaturesManager.determine_framework(_lowercase  ,						_lowercase					)
											self.assertEqual(_lowercase  ,						_lowercase					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[int]:

							with TemporaryDirectory() as local_pt_ckpt:
											self._setup_pt_ckpt(_lowercase					)
											a_		:  List[Any]											=      FeaturesManager.determine_framework(_lowercase					)
											self.assertEqual(_lowercase  ,						self.framework_pt					)

							# TensorFlow checkpoint
							with TemporaryDirectory() as local_tf_ckpt:
											self._setup_tf_ckpt(_lowercase					)
											a_		:  str											=      FeaturesManager.determine_framework(_lowercase					)
											self.assertEqual(_lowercase  ,						self.framework_tf					)

							# Invalid local checkpoint
							with TemporaryDirectory() as local_invalid_ckpt:
											with self.assertRaises(_lowercase					):
															a_		:  str											=      FeaturesManager.determine_framework(_lowercase					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			str:
							a_		:  List[str]											=      MagicMock(return_value=_lowercase					)
							with patch('''transformers.onnx.features.is_tf_available'''  ,						_lowercase					):
											a_		:  Dict											=      FeaturesManager.determine_framework(self.test_model					)
											self.assertEqual(_lowercase  ,						self.framework_pt					)

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

							# Both in environment -> use PyTorch
							a_		:  List[Any]											=      MagicMock(return_value=_lowercase					)
							a_		:  List[Any]											=      MagicMock(return_value=_lowercase					)
							with patch('''transformers.onnx.features.is_tf_available'''  ,						_lowercase					), patch(
							    '''transformers.onnx.features.is_torch_available'''  ,						_lowercase					):
											a_		:  int											=      FeaturesManager.determine_framework(self.test_model					)
											self.assertEqual(_lowercase  ,						self.framework_pt					)

							# Both not in environment -> raise error
							a_		:  int											=      MagicMock(return_value=_lowercase					)
							a_		:  List[str]											=      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					):
															a_		:  Optional[Any]											=      FeaturesManager.determine_framework(self.test_model					)












'''simple docstring'''







from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import pyarrow as pa


if TYPE_CHECKING:
		from .features import FeatureType





@dataclass
class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				42
			snake_case__  =				None
			# Automatically constructed
			snake_case__  =				"dict"
			snake_case__  =				None
			snake_case__  =				field(default="Translation"	,   init=SCREAMING_SNAKE_CASE_	,   repr=SCREAMING_SNAKE_CASE_       )






			def __call__( self      :			Dict					)			->			Tuple:
							return pa.struct({lang: pa.string() for lang in sorted(self.languages					)}					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union["FeatureType", Dict[str, "FeatureType"]]:
							from .features import Value

							return {k: Value('''string'''					) for k in sorted(self.languages					)}





@dataclass
class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				None
			snake_case__  =				None
			snake_case__  =				None
			# Automatically constructed
			snake_case__  =				"dict"
			snake_case__  =				None
			snake_case__  =				field(default="TranslationVariableLanguages"	,   init=SCREAMING_SNAKE_CASE_	,   repr=SCREAMING_SNAKE_CASE_       )






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Dict:
							a_		:  List[str]											=      sorted(set(self.languages					)					) if self.languages else None
							a_		:  Optional[Any]											=      len(self.languages					) if self.languages else None






			def __call__( self      :			Any					)			->			Optional[Any]:
							return pa.struct({'''language''': pa.list_(pa.string()					), '''translation''': pa.list_(pa.string()					)}					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]					)			->			Optional[Any]:
							a_		:  str											=      set(self.languages					)
							if self.languages and set(__SCREAMING_SNAKE_CASE					) - lang_set:
											raise ValueError(
											    f'Some languages in example ({", ".join(sorted(set(__SCREAMING_SNAKE_CASE					) - lang_set					)					)}) are not in valid set ({", ".join(__SCREAMING_SNAKE_CASE					)}).'					)

							# Convert dictionary into tuples, splitting out cases where there are
							# multiple translations for a single language.
							a_		:  int											=      []
							for lang, text in translation_dict.items():
											if isinstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					):
															translation_tuples.append((lang, text)					)
											else:
															translation_tuples.extend([(lang, el) for el in text]					)

        # Ensure translations are in ascending order by language code.
							a_				,			a_		:  List[Any]											=      zip(*sorted(__SCREAMING_SNAKE_CASE					)					)

							return {"language": languages, "translation": translations}







			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Union["FeatureType", Dict[str, "FeatureType"]]:
							from .features import Sequence, Value

							return {
							    "language": Sequence(Value('''string'''					)					),
							    "translation": Sequence(Value('''string'''					)					),
							}




