


"""simple docstring"""

import torch

from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool

class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):
     lowercase						       =						"facebook/bart-large-mnli"
     lowercase						       =						(
         "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which "
         "should be the text to classify, and `labels`, which should be the list of labels to use for classification. "
         "It returns the most likely label in the list of provided `labels` for the input text."
     )
     lowercase						       =						"text_classifier"
     lowercase						       =						AutoTokenizer
     lowercase						       =						AutoModelForSequenceClassification

     lowercase						       =						["text", ["text"]]
     lowercase						       =						["text"]





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().setup()
      __UpperCamelCase      =	self.model.config
      __UpperCamelCase      =	-1
      for idx, label in config.idalabel.items():
       if label.lower().startswith('entail'				):
        __UpperCamelCase      =	int(__UpperCAmelCase				)
      if self.entailment_id == -1:
       raise ValueError('Could not determine the entailment ID from the model config, please pass it at init.'				)





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	labels
      return self.pre_processor(
          [text] * len(__UpperCAmelCase				)     ,					[F'This example is {label}' for label in labels]     ,					return_tensors='pt'     ,					padding='max_length'     ,					)







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	outputs.logits
      __UpperCamelCase      =	torch.argmax(logits[:, 2]				).item()
      return self._labels[label_id]





"""simple docstring"""

from typing import Union

import fire
import torch
from tqdm import tqdm






def 							A						(    snake_case				:str    , snake_case				:str = "cpu"    , snake_case				:Union[str, None] = None		)				->     None:
 __UpperCamelCase      =	torch.load(snake_case    , map_location=snake_case		)
 for k, v in tqdm(state_dict.items()		):
  if not isinstance(snake_case    , torch.Tensor		):
   raise TypeError('FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin'		)
  __UpperCamelCase      =	v.half()
 if save_path is None:  # overwrite src_path
  __UpperCamelCase      =	src_path
 torch.save(snake_case    , snake_case		)


if __name__ == "__main__":
      fire.Fire(convert)



