





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.
UpperCAmelCase_      							=     {"""LayoutLMv2Config""", """LayoutLMv3Config"""}
@is_pipeline_test
class 			UpperCamelCase_		(  unittest.TestCase					):
					lowerCAmelCase_							=	MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
					lowerCAmelCase_							=	TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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




					@require_torch
					def 	lowerCAmelCase			(		self  )		->							Optional[int]:
										_snake_case									=				pipeline(
										    task='text-classification'				,						model='hf-internal-testing/tiny-random-distilbert'				,						framework='pt'  )

										_snake_case									=				text_classifier('This is great !'  )
										self.assertEqual(nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}]  )

										_snake_case									=				text_classifier('This is great !'				,						top_k=2  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}]  )

										_snake_case									=				text_classifier(['This is great !', 'This is bad']				,						top_k=2  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_  )				,						[
										        [{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}],
										        [{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}],
										    ]				,						)

										_snake_case									=				text_classifier('This is great !'				,						top_k=1  )
										self.assertEqual(nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}]  )

										# Legacy behavior
										_snake_case									=				text_classifier('This is great !'				,						return_all_scores=lowerCAmelCase_  )
										self.assertEqual(nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}]  )

										_snake_case									=				text_classifier('This is great !'				,						return_all_scores=lowerCAmelCase_  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_  )				,						[[{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}]]  )

										_snake_case									=				text_classifier(['This is great !', 'Something else']				,						return_all_scores=lowerCAmelCase_  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_  )				,						[
										        [{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}],
										        [{'label': 'LABEL_0', 'score': 0.5_04}, {'label': 'LABEL_1', 'score': 0.4_96}],
										    ]				,						)

										_snake_case									=				text_classifier(['This is great !', 'Something else']				,						return_all_scores=lowerCAmelCase_  )
										self.assertEqual(
										    nested_simplify(lowerCAmelCase_  )				,						[
										        {'label': 'LABEL_0', 'score': 0.5_04},
										        {'label': 'LABEL_0', 'score': 0.5_04},
										    ]				,						)




					@require_torch
					def 	lowerCAmelCase			(		self  )		->							List[str]:
										import torch

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

										_snake_case									=				text_classifier('This is great !'  )
										self.assertEqual(nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}]  )




					@require_tf
					def 	lowerCAmelCase			(		self  )		->							int:
										_snake_case									=				pipeline(
										    task='text-classification'				,						model='hf-internal-testing/tiny-random-distilbert'				,						framework='tf'  )

										_snake_case									=				text_classifier('This is great !'  )
										self.assertEqual(nested_simplify(lowerCAmelCase_  )				,						[{'label': 'LABEL_0', 'score': 0.5_04}]  )




					@slow
					@require_torch
					def 	lowerCAmelCase			(		self  )		->							Union[str, Any]:
										_snake_case									=				pipeline('text-classification'  )

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




					@slow
					@require_tf
					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										_snake_case									=				pipeline('text-classification'				,						framework='tf'  )

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




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							str:
										_snake_case									=				TextClassificationPipeline(model=lowerCAmelCase_				,						tokenizer=lowerCAmelCase_  )
										return text_classifier, ["HuggingFace is in", "This is another test"]






					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Any:
										_snake_case									=				text_classifier.model
										# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
										_snake_case									=				'HuggingFace is in'
										_snake_case									=				text_classifier(lowerCAmelCase_  )

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

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

										_snake_case									=				{'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'}
										_snake_case									=				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.
										_snake_case									=				[['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
										_snake_case									=				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()  )










import argparse
from collections import defaultdict

import yaml


UpperCAmelCase_      							=     """docs/source/en/_toctree.yml"""





def 						lowerCamelCase__     (     UpperCamelCase__			:       Optional[Any]						)			-> str:



					'''simple docstring'''




					_snake_case									=				defaultdict(UpperCamelCase__						)
					_snake_case									=				[]
					_snake_case									=				[]
					for doc in doc_list:
										if "local" in doc:
															counts[doc["local"]] += 1

										if doc["title"].lower() == "overview":
															overview_doc.append({'local': doc['local'], 'title': doc['title']}						)
										else:
															new_doc_list.append(UpperCamelCase__						)

					_snake_case									=				new_doc_list
					_snake_case									=				[key for key, value in counts.items() if value > 1]

					_snake_case									=				[]
					for duplicate_key in duplicates:
										_snake_case									=				list({doc['title'] for doc in doc_list if doc['local'] == duplicate_key}						)
										if len(UpperCamelCase__						) > 1:
															raise ValueError(
															    F'''{duplicate_key} is present several times in the documentation table of content at '''
															    '`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '
															    'others.'						)
										# Only add this once
										new_doc.append({'local': duplicate_key, 'title': titles[0]}						)

					# Add none duplicate-keys
					new_doc.extend([doc for doc in doc_list if 'local' not in counts or counts[doc['local']] == 1]						)
					_snake_case									=				sorted(UpperCamelCase__       ,    key=lambda UpperCamelCase__						: s["title"].lower()						)

					# "overview" gets special treatment and is always first
					if len(UpperCamelCase__						) > 1:
										raise ValueError('{doc_list} has two \'overview\' docs which is not allowed.'						)

					overview_doc.extend(UpperCamelCase__						)

					# Sort
					return overview_doc





def 						lowerCamelCase__     (     UpperCamelCase__			:       Dict=False						)			-> Optional[int]:



					'''simple docstring'''




					with open(UpperCamelCase__       ,    encoding='utf-8'						) as f:
										_snake_case									=				yaml.safe_load(f.read()						)

					# Get to the API doc
					_snake_case									=				0
					while content[api_idx]["title"] != "API":
										api_idx += 1
					_snake_case									=				content[api_idx]['sections']

					# Then to the model doc
					_snake_case									=				0
					while api_doc[scheduler_idx]["title"] != "Schedulers":
										scheduler_idx += 1

					_snake_case									=				api_doc[scheduler_idx]['sections']
					_snake_case									=				clean_doc_toc(UpperCamelCase__						)

					_snake_case									=				False
					if new_scheduler_doc != scheduler_doc:
										_snake_case									=				True
										if overwrite:
															_snake_case									=				new_scheduler_doc

					if diff:
										if overwrite:
															_snake_case									=				api_doc
															with open(UpperCamelCase__       ,    'w'       ,    encoding='utf-8'						) as f:
																				f.write(yaml.dump(UpperCamelCase__       ,    allow_unicode=UpperCamelCase__						)						)
										else:
															raise ValueError(
															    'The model doc part of the table of content is not properly sorted, run `make style` to fix this.'						)





def 						lowerCamelCase__     (     UpperCamelCase__			:       Tuple=False						)			-> List[Any]:



					'''simple docstring'''




					with open(UpperCamelCase__       ,    encoding='utf-8'						) as f:
										_snake_case									=				yaml.safe_load(f.read()						)

					# Get to the API doc
					_snake_case									=				0
					while content[api_idx]["title"] != "API":
										api_idx += 1
					_snake_case									=				content[api_idx]['sections']

					# Then to the model doc
					_snake_case									=				0
					while api_doc[pipeline_idx]["title"] != "Pipelines":
										pipeline_idx += 1

					_snake_case									=				False
					_snake_case									=				api_doc[pipeline_idx]['sections']
					_snake_case									=				[]

					# sort sub pipeline docs
					for pipeline_doc in pipeline_docs:
										if "section" in pipeline_doc:
															_snake_case									=				pipeline_doc['section']
															_snake_case									=				clean_doc_toc(UpperCamelCase__						)
															if overwrite:
																				_snake_case									=				new_sub_pipeline_doc
										new_pipeline_docs.append(UpperCamelCase__						)

					# sort overall pipeline doc
					_snake_case									=				clean_doc_toc(UpperCamelCase__						)

					if new_pipeline_docs != pipeline_docs:
										_snake_case									=				True
										if overwrite:
															_snake_case									=				new_pipeline_docs

					if diff:
										if overwrite:
															_snake_case									=				api_doc
															with open(UpperCamelCase__       ,    'w'       ,    encoding='utf-8'						) as f:
																				f.write(yaml.dump(UpperCamelCase__       ,    allow_unicode=UpperCamelCase__						)						)
										else:
															raise ValueError(
															    'The model doc part of the table of content is not properly sorted, run `make style` to fix this.'						)


if __name__ == "__main__":
					UpperCAmelCase_      							=     argparse.ArgumentParser()
					parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""")
					UpperCAmelCase_      							=     parser.parse_args()

					check_scheduler_doc(args.fix_and_overwrite)
					check_pipeline_doc(args.fix_and_overwrite)



