"""simple docstring"""







import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
SCREAMING_SNAKE_CASE									=						{'LayoutLMv2Config', 'LayoutLMv3Config'}



@is_pipeline_test
class  _lowerCamelCase			(unittest.TestCase     ):
				_snake_case     =  MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
				_snake_case     =  TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

				if model_mapping is not None:
								_snake_case     =  {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
				if tf_model_mapping is not None:
								_snake_case     =  {
								    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
								}







				@require_torch
				def 							__UpperCAmelCase					( self    :       Any						):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							pipeline(
					    task='text-classification'			,					model='hf-internal-testing/tiny-random-distilbert'			,					framework='pt'						)

					_lowercase :    Dict							=							text_classifier('This is great !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}]						)

					_lowercase :    Tuple							=							text_classifier('This is great !'			,					top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}]						)

					_lowercase :    str							=							text_classifier(['This is great !', 'This is bad']			,					top_k=2						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[
					        [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}],
					        [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}],
					    ]			,					)

					_lowercase :    str							=							text_classifier('This is great !'			,					top_k=1						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}]						)

					# Legacy behavior
					_lowercase :    List[Any]							=							text_classifier('This is great !'			,					return_all_scores=lowerCamelCase_						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}]						)

					_lowercase :    Optional[Any]							=							text_classifier('This is great !'			,					return_all_scores=lowerCamelCase_						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[[{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}]]						)

					_lowercase :    Any							=							text_classifier(['This is great !', 'Something else']			,					return_all_scores=lowerCamelCase_						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[
					        [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}],
					        [{'label': 'LABEL_0', 'score': 0.504}, {'label': 'LABEL_1', 'score': 0.496}],
					    ]			,					)

					_lowercase :    int							=							text_classifier(['This is great !', 'Something else']			,					return_all_scores=lowerCamelCase_						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[
					        {'label': 'LABEL_0', 'score': 0.504},
					        {'label': 'LABEL_0', 'score': 0.504},
					    ]			,					)







				@require_torch
				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					import torch

					_lowercase :    Optional[int]							=							pipeline(
					    task='text-classification'			,					model='hf-internal-testing/tiny-random-distilbert'			,					framework='pt'			,					device=torch.device('cpu'						)			,					)

					_lowercase :    Optional[Any]							=							text_classifier('This is great !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}]						)







				@require_tf
				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					_lowercase :    str							=							pipeline(
					    task='text-classification'			,					model='hf-internal-testing/tiny-random-distilbert'			,					framework='tf'						)

					_lowercase :    List[str]							=							text_classifier('This is great !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'LABEL_0', 'score': 0.504}]						)







				@slow
				@require_torch
				def 							__UpperCAmelCase					( self    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    List[str]							=							pipeline('text-classification'						)

					_lowercase :    str							=							text_classifier('This is great !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'POSITIVE', 'score': 1.0}]						)
					_lowercase :    Optional[int]							=							text_classifier('This is bad !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'NEGATIVE', 'score': 1.0}]						)
					_lowercase :    Optional[Any]							=							text_classifier('Birds are a type of animal'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'POSITIVE', 'score': 0.988}]						)







				@slow
				@require_tf
				def 							__UpperCAmelCase					( self    :       int						):

					"""simple docstring"""






					_lowercase :    Tuple							=							pipeline('text-classification'			,					framework='tf'						)

					_lowercase :    Tuple							=							text_classifier('This is great !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'POSITIVE', 'score': 1.0}]						)
					_lowercase :    str							=							text_classifier('This is bad !'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'NEGATIVE', 'score': 1.0}]						)
					_lowercase :    List[Any]							=							text_classifier('Birds are a type of animal'						)
					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': 'POSITIVE', 'score': 0.988}]						)







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

					"""simple docstring"""






					_lowercase :    str							=							TextClassificationPipeline(model=lowerCamelCase_			,					tokenizer=lowerCamelCase_						)
					return text_classifier, ["HuggingFace is in", "This is another test"]







				def 							__UpperCAmelCase					( self    :       str			,					lowerCamelCase_    :       Any			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					_lowercase :    Any							=							text_classifier.model
					# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
					_lowercase :    Dict							=							'HuggingFace is in'
					_lowercase :    Optional[int]							=							text_classifier(lowerCamelCase_						)

					self.assertEqual(nested_simplify(lowerCamelCase_						)			,					[{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}]						)
					self.assertTrue(outputs[0]['label'] in model.config.idalabel.values()						)

					_lowercase :    Dict							=							['HuggingFace is in ', 'Paris is in France']
					_lowercase :    Optional[int]							=							text_classifier(lowerCamelCase_						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}, {'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}]			,					)
					self.assertTrue(outputs[0]['label'] in model.config.idalabel.values()						)
					self.assertTrue(outputs[1]['label'] in model.config.idalabel.values()						)

					# Forcing to get all results with `top_k=None`
					# This is NOT the legacy format
					_lowercase :    List[str]							=							text_classifier(lowerCamelCase_			,					top_k=lowerCamelCase_						)
					_lowercase :    str							=							len(model.config.idalabel.values()						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[[{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}] * N, [{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}] * N]			,					)

					_lowercase :    str							=							{'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'}
					_lowercase :    int							=							text_classifier(lowerCamelCase_						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}			,					)
					self.assertTrue(outputs['label'] in model.config.idalabel.values()						)

					# This might be used a text pair, but tokenizer + pipe interaction
					# makes it hard to understand that it's not using the pair properly
					# https://github.com/huggingface/transformers/issues/17305
					# We disabled this usage instead as it was outputting wrong outputs.
					_lowercase :    Dict							=							[['HuggingFace is in ', 'Paris is in France']]
					with self.assertRaises(lowerCamelCase_						):
						text_classifier(lowerCamelCase_						)

					# This used to be valid for doing text pairs
					# We're keeping it working because of backward compatibility
					_lowercase :    Any							=							text_classifier([[['HuggingFace is in ', 'Paris is in France']]]						)
					self.assertEqual(
					    nested_simplify(lowerCamelCase_						)			,					[{'label': ANY(lowerCamelCase_						), 'score': ANY(lowerCamelCase_						)}]			,					)
					self.assertTrue(outputs[0]['label'] in model.config.idalabel.values()						)
"""simple docstring"""







import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  ""
				_snake_case     =  (
				    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
				)
				_snake_case     =  None  # compression type in fsspec. ex: "gzip"
				_snake_case     =  None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz







				def __init__( self    :       str			,					lowerCamelCase_    :       str = ""			,					lowerCamelCase_    :       Optional[str] = None			,					lowerCamelCase_    :       Optional[dict] = None			,					**lowerCamelCase_    :       List[str]						):

					"""simple docstring"""






					super().__init__(self			,					**lowerCamelCase_						)
					# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
					_lowercase :    Union[str, Any]							=							fsspec.open(
					    lowerCamelCase_			,					mode='rb'			,					protocol=lowerCamelCase_			,					compression=self.compression			,					client_kwargs={
					        'requote_redirect_url': False,  # see https://github.com/huggingface/datasets/pull/5459
					        'trust_env': True,  # Enable reading proxy env variables.
					        **(target_options or {}).pop('client_kwargs'			,					{}						),  # To avoid issues if it was already passed.
					    }			,					**(target_options or {})			,					)
					_lowercase :    str							=							os.path.basename(self.file.path.split('::'						)[0]						)
					_lowercase :    Optional[int]							=							(
					    self.compressed_name[: self.compressed_name.rindex('.'						)]
					    if '.' in self.compressed_name
					    else self.compressed_name
					)
					_lowercase :    str							=							None







				@classmethod
				def 							__UpperCAmelCase					( cls    :       int			,					lowerCamelCase_    :       List[str]						):

					"""simple docstring"""






					return super()._strip_protocol(lowerCamelCase_						).lstrip('/'						)







				def 							__UpperCAmelCase					( self    :       Optional[Any]						):

					"""simple docstring"""






					if self.dir_cache is None:
						_lowercase :    Tuple							=							{**self.file.fs.info(self.file.path						), 'name': self.uncompressed_name}
						_lowercase :    int							=							{f['name']: f}







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

					"""simple docstring"""






					return self.file.open().read()







				def 							__UpperCAmelCase					( self    :       Dict			,					lowerCamelCase_    :       str			,					lowerCamelCase_    :       str = "rb"			,					lowerCamelCase_    :       str=None			,					lowerCamelCase_    :       str=True			,					lowerCamelCase_    :       Union[str, Any]=None			,					**lowerCamelCase_    :       Optional[int]			,					):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							self._strip_protocol(lowerCamelCase_						)
					if mode != "rb":
						raise ValueError(F'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\''''						)
					return self.file.open()



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "bz2"
				_snake_case     =  "bz2"
				_snake_case     =  ".bz2"



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "gzip"
				_snake_case     =  "gzip"
				_snake_case     =  ".gz"



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "lz4"
				_snake_case     =  "lz4"
				_snake_case     =  ".lz4"



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "xz"
				_snake_case     =  "xz"
				_snake_case     =  ".xz"



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "zstd"
				_snake_case     =  "zstd"
				_snake_case     =  ".zst"







				def __init__( self    :       Union[str, Any]			,					lowerCamelCase_    :       str			,					lowerCamelCase_    :       str = "rb"			,					lowerCamelCase_    :       Optional[str] = None			,					lowerCamelCase_    :       Optional[dict] = None			,					lowerCamelCase_    :       int = DEFAULT_BLOCK_SIZE			,					**lowerCamelCase_    :       int			,					):

					"""simple docstring"""






					super().__init__(
					    fo=lowerCamelCase_			,					mode=lowerCamelCase_			,					target_protocol=lowerCamelCase_			,					target_options=lowerCamelCase_			,					block_size=lowerCamelCase_			,					**lowerCamelCase_			,					)
					# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
					#
					# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
					#     out.close = close
					# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
					#
					# see https://github.com/intake/filesystem_spec/issues/725
					_lowercase :    Any							=							self.file.__enter__



					class  _lowerCamelCase			:







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

										"""simple docstring"""






										_lowercase :    Tuple							=							file_







									def __enter__( self    :       str						):

										"""simple docstring"""






										self._file.__enter__()
										return self







									def __exit__( self    :       Union[str, Any]			,					*lowerCamelCase_    :       Optional[Any]			,					**lowerCamelCase_    :       Union[str, Any]						):

										"""simple docstring"""






										self._file.__exit__(*lowerCamelCase_			,					**lowerCamelCase_						)







									def __iter__( self    :       Optional[int]						):

										"""simple docstring"""






										return iter(self._file						)







									def 							__UpperCAmelCase					( self    :       int						):

										"""simple docstring"""






										return next(self._file						)







									def __getattr__( self    :       List[str]			,					lowerCamelCase_    :       str						):

										"""simple docstring"""






										return getattr(self._file			,					lowerCamelCase_						)

					def fixed_enter(*lowerCamelCase_    :       List[Any]			,					**lowerCamelCase_    :       int						):
						return WrappedFile(_enter(*lowerCamelCase_			,					**lowerCamelCase_						)						)

					_lowercase :    Optional[int]							=							fixed_enter



