
"""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.
_a														=		{"""LayoutLMv2Config""", """LayoutLMv3Config"""}






@is_pipeline_test
class     _UpperCAmelCase(    unittest.TestCase	):
		lowercase__						=      MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
		lowercase__						=      TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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



		@require_torch
		def 						UpperCAmelCase							(					self)       ->       List[str]:






							'''simple docstring'''
							_UpperCamelCase							 =  pipeline(
							    task='''text-classification'''				,		model='''hf-internal-testing/tiny-random-distilbert'''				,		framework='''pt''')

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

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

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

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

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

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

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

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



		@require_torch
		def 						UpperCAmelCase							(					self)       ->       Dict:






							'''simple docstring'''
							import torch

							_UpperCamelCase							 =  pipeline(
							    task='''text-classification'''				,		model='''hf-internal-testing/tiny-random-distilbert'''				,		framework='''pt'''				,		device=torch.device('''cpu''')				,		)

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



		@require_tf
		def 						UpperCAmelCase							(					self)       ->       int:






							'''simple docstring'''
							_UpperCamelCase							 =  pipeline(
							    task='''text-classification'''				,		model='''hf-internal-testing/tiny-random-distilbert'''				,		framework='''tf''')

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



		@slow
		@require_torch
		def 						UpperCAmelCase							(					self)       ->       Any:






							'''simple docstring'''
							_UpperCamelCase							 =  pipeline('''text-classification''')

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



		@slow
		@require_tf
		def 						UpperCAmelCase							(					self)       ->       int:






							'''simple docstring'''
							_UpperCamelCase							 =  pipeline('''text-classification'''				,		framework='''tf''')

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



		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a)       ->       Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase							 =  TextClassificationPipeline(model=__a				,		tokenizer=__a)
							return text_classifier, ["HuggingFace is in", "This is another test"]




		def 						UpperCAmelCase							(					self				,		__a				,		__a)       ->       Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase							 =  text_classifier.model
							# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
							_UpperCamelCase							 =  '''HuggingFace is in'''
							_UpperCamelCase							 =  text_classifier(__a)

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

							_UpperCamelCase							 =  ['''HuggingFace is in ''', '''Paris is in France''']
							_UpperCamelCase							 =  text_classifier(__a)
							self.assertEqual(
							    nested_simplify(__a)				,		[{'''label''': ANY(__a), '''score''': ANY(__a)}, {'''label''': ANY(__a), '''score''': ANY(__a)}]				,		)
							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
							_UpperCamelCase							 =  text_classifier(__a				,		top_k=__a)
							_UpperCamelCase							 =  len(model.config.idalabel.values())
							self.assertEqual(
							    nested_simplify(__a)				,		[[{'''label''': ANY(__a), '''score''': ANY(__a)}] * N, [{'''label''': ANY(__a), '''score''': ANY(__a)}] * N]				,		)

							_UpperCamelCase							 =  {'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
							_UpperCamelCase							 =  text_classifier(__a)
							self.assertEqual(
							    nested_simplify(__a)				,		{'''label''': ANY(__a), '''score''': ANY(__a)}				,		)
							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.
							_UpperCamelCase							 =  [['''HuggingFace is in ''', '''Paris is in France''']]
							with self.assertRaises(__a):
												text_classifier(__a)

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








"""simple docstring"""





import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


_a														=		"""http://www.mocksite.com/file1.txt"""
_a														=		"""\"text\": [\"foo\", \"foo\"]"""
_a														=		"""6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8"""






class     _UpperCAmelCase:
		lowercase__						=      2_00
		lowercase__						=      {'Content-Length': '100'}
		lowercase__						=      {}



		def 						UpperCAmelCase							(					self				,		**__a)       ->       Optional[int]:






							'''simple docstring'''
							return [bytes(__a				,		'''utf-8''')]





def       lowerCamelCase__		(			*__snake_case,     **__snake_case	)							->		int:


					"""simple docstring"""





					return MockResponse()



@pytest.mark.parametrize('''urls_type''',     [str, list, dict]	)
def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case	)							->		Dict:


					"""simple docstring"""





					import requests

					monkeypatch.setattr(__snake_case,     '''request''',     __snake_case	)

					_UpperCamelCase							 =  URL
					if issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  url
					elif issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  [url]
					elif issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  {'''train''': url}
					_UpperCamelCase							 =  '''dummy'''
					_UpperCamelCase							 =  '''downloads'''
					_UpperCamelCase							 =  tmp_path
					_UpperCamelCase							 =  DownloadConfig(
					    cache_dir=os.path.join(__snake_case,     __snake_case	),     use_etag=__snake_case,     )
					_UpperCamelCase							 =  DownloadManager(dataset_name=__snake_case,     download_config=__snake_case	)
					_UpperCamelCase							 =  dl_manager.download(__snake_case	)
					_UpperCamelCase							 =  urls
					for downloaded_paths in [downloaded_paths]:
										if isinstance(__snake_case,     __snake_case	):
															_UpperCamelCase							 =  [downloaded_paths]
															_UpperCamelCase							 =  [urls]
										elif isinstance(__snake_case,     __snake_case	):
															assert "train" in downloaded_paths.keys()
															_UpperCamelCase							 =  downloaded_paths.values()
															_UpperCamelCase							 =  urls.values()
										assert downloaded_paths
										for downloaded_path, input_url in zip(__snake_case,     __snake_case	):
															assert downloaded_path == dl_manager.downloaded_paths[input_url]
															_UpperCamelCase							 =  Path(__snake_case	)
															_UpperCamelCase							 =  downloaded_path.parts
															assert parts[-1] == HASH
															assert parts[-2] == cache_subdir
															assert downloaded_path.exists()
															_UpperCamelCase							 =  downloaded_path.read_text()
															assert content == CONTENT
															_UpperCamelCase							 =  downloaded_path.with_suffix('''.json'''	)
															assert metadata_downloaded_path.exists()
															_UpperCamelCase							 =  json.loads(metadata_downloaded_path.read_text()	)
															assert metadata_content == {"url": URL, "etag": None}



@pytest.mark.parametrize('''paths_type''',     [str, list, dict]	)
def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case	)							->		List[str]:


					"""simple docstring"""





					_UpperCamelCase							 =  str(__snake_case	)
					if issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  filename
					elif issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  [filename]
					elif issubclass(__snake_case,     __snake_case	):
										_UpperCamelCase							 =  {'''train''': filename}
					_UpperCamelCase							 =  '''dummy'''
					_UpperCamelCase							 =  xz_file.parent
					_UpperCamelCase							 =  '''extracted'''
					_UpperCamelCase							 =  DownloadConfig(
					    cache_dir=__snake_case,     use_etag=__snake_case,     )
					_UpperCamelCase							 =  DownloadManager(dataset_name=__snake_case,     download_config=__snake_case	)
					_UpperCamelCase							 =  dl_manager.extract(__snake_case	)
					_UpperCamelCase							 =  paths
					for extracted_paths in [extracted_paths]:
										if isinstance(__snake_case,     __snake_case	):
															_UpperCamelCase							 =  [extracted_paths]
															_UpperCamelCase							 =  [paths]
										elif isinstance(__snake_case,     __snake_case	):
															assert "train" in extracted_paths.keys()
															_UpperCamelCase							 =  extracted_paths.values()
															_UpperCamelCase							 =  paths.values()
										assert extracted_paths
										for extracted_path, input_path in zip(__snake_case,     __snake_case	):
															assert extracted_path == dl_manager.extracted_paths[input_path]
															_UpperCamelCase							 =  Path(__snake_case	)
															_UpperCamelCase							 =  extracted_path.parts
															assert parts[-1] == hash_url_to_filename(__snake_case,     etag=__snake_case	)
															assert parts[-2] == extracted_subdir
															assert extracted_path.exists()
															_UpperCamelCase							 =  extracted_path.read_text()
															_UpperCamelCase							 =  text_file.read_text()
															assert extracted_file_content == expected_file_content



def       lowerCamelCase__		(			__snake_case,     __snake_case	)							->		Optional[Any]:


					"""simple docstring"""





					assert path.endswith('''.jsonl'''	)
					for num_items, line in enumerate(__snake_case,     start=1	):
										_UpperCamelCase							 =  json.loads(line.decode('''utf-8'''	)	)
										assert item.keys() == {"col_1", "col_2", "col_3"}
					assert num_items == 4



@pytest.mark.parametrize('''archive_jsonl''',     ['''tar_jsonl_path''', '''zip_jsonl_path''']	)
def       lowerCamelCase__		(			__snake_case,     __snake_case	)							->		int:


					"""simple docstring"""





					_UpperCamelCase							 =  request.getfixturevalue(__snake_case	)
					_UpperCamelCase							 =  DownloadManager()
					for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(__snake_case	),     start=1	):
										_test_jsonl(__snake_case,     __snake_case	)
					assert num_jsonl == 2



@pytest.mark.parametrize('''archive_nested_jsonl''',     ['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path''']	)
def       lowerCamelCase__		(			__snake_case,     __snake_case	)							->		int:


					"""simple docstring"""





					_UpperCamelCase							 =  request.getfixturevalue(__snake_case	)
					_UpperCamelCase							 =  DownloadManager()
					for num_tar, (path, file) in enumerate(dl_manager.iter_archive(__snake_case	),     start=1	):
										for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(__snake_case	),     start=1	):
															_test_jsonl(__snake_case,     __snake_case	)
					assert num_tar == 1
					assert num_jsonl == 2



def       lowerCamelCase__		(			__snake_case	)							->		Dict:


					"""simple docstring"""





					_UpperCamelCase							 =  DownloadManager()
					for num_file, file in enumerate(dl_manager.iter_files(__snake_case	),     start=1	):
										assert os.path.basename(__snake_case	) == ("test.txt" if num_file == 1 else "train.txt")
					assert num_file == 2






