






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     :   Dict 							=						{'LayoutLMv2Config', 'LayoutLMv3Config'}
@is_pipeline_test
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 _SCREAMING_SNAKE_CASE							:			Any =					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  _SCREAMING_SNAKE_CASE							:			Optional[Any] =					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  _SCREAMING_SNAKE_CASE							:			str =					{
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }


 @require_torch
 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				pipeline(
         task='''text-classification'''							,  model='''hf-internal-testing/tiny-random-distilbert'''							,  framework='''pt''')

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

     _UpperCAmelCase				:      List[str]			  =				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				:      str			  =				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				:      str			  =				text_classifier('''This is great !'''							,  top_k=1)
     self.assertEqual(nested_simplify(_A)							,  [{'''label''': '''LABEL_0''', '''score''': 0.504}])

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

     _UpperCAmelCase				:      Optional[Any]			  =				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				:      Optional[int]			  =				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				:      Optional[int]			  =				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  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     import torch

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

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


 @require_tf
 def  snake_case__				(	self) ->		Union[str, Any]:

     """simple docstring"""



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

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


 @slow
 @require_torch
 def  snake_case__				(	self) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				pipeline('''text-classification''')

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


 @slow
 @require_tf
 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				pipeline('''text-classification'''							,  framework='''tf''')

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


 def  snake_case__				(	self							,  _A							,  _A							,  _A) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				TextClassificationPipeline(model=_A							,  tokenizer=_A)
     return text_classifier, ["HuggingFace is in", "This is another test"]


 def  snake_case__				(	self							,  _A							,  _A) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				text_classifier.model
     # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
     _UpperCAmelCase				:      Optional[Any]			  =				'''HuggingFace is in'''
     _UpperCAmelCase				:      int			  =				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				:      List[str]			  =				['''HuggingFace is in ''', '''Paris is in France''']
     _UpperCAmelCase				:      List[str]			  =				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				:      Optional[int]			  =				text_classifier(_A							,  top_k=_A)
     _UpperCAmelCase				:      Any			  =				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				:      Dict			  =				{'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
     _UpperCAmelCase				:      str			  =				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				:      Tuple			  =				[['''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				:      Union[str, Any]			  =				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())












import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel
class 							A_    (						pl.LightningModule			):






 '''simple docstring'''




 def __init__(	self							,  _A) ->		List[str]:

     """simple docstring"""



     super().__init__()
     _UpperCAmelCase				:      Dict			  =				model
     _UpperCAmelCase				:      Union[str, Any]			  =				2
     _UpperCAmelCase				:      List[Any]			  =				nn.Linear(self.model.config.hidden_size							,  self.num_labels)


 def  snake_case__				(	self) ->		Optional[Any]:

     """simple docstring"""



     pass



def      _lowerCamelCase	(						__A    :							str		,		__A    :							str		,		__A    :							str )      ->					List[str]:
    # load longformer model from model identifier
    _UpperCAmelCase				:      Optional[Any]			  =				LongformerModel.from_pretrained(__A )
    _UpperCAmelCase				:      Optional[Any]			  =				LightningModel(__A )

    _UpperCAmelCase				:      Optional[int]			  =				torch.load(__A		,		map_location=torch.device('''cpu''' ) )
    lightning_model.load_state_dict(ckpt['''state_dict'''] )

    # init longformer question answering model
    _UpperCAmelCase				:      int			  =				LongformerForQuestionAnswering.from_pretrained(__A )

    # transfer weights
    longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict() )
    longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict() )
    longformer_for_qa.eval()

    # save model
    longformer_for_qa.save_pretrained(__A )

    print(f'''Conversion successful. Model saved under {pytorch_dump_folder_path}''' )


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE 							=						argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--longformer_model',
      default=None,
      type=str,
      required=True,
      help='model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.',
  )
  parser.add_argument(
      '--longformer_question_answering_ckpt_path',
      default=None,
      type=str,
      required=True,
      help='Path the official PyTorch Lightning Checkpoint.',
  )
  parser.add_argument(
      '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
  )
  SCREAMING_SNAKE_CASE 							=						parser.parse_args()
  convert_longformer_qa_checkpoint_to_pytorch(
      args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
  )




