



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

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

class       snake_case__(UpperCAmelCase_			):

					"""simple docstring"""






					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  snake_case     (							self					:	Union[str, Any]     ):
									super().setup()
									lowercase__     :    int													=     self.model.config
									lowercase__     :    Optional[Any]													=     -1
									for idx, label in config.idalabel.items():
													if label.lower().startswith("entail"     ):
																	lowercase__     :    List[Any]													=     int(__lowercase     )
									if self.entailment_id == -1:
													raise ValueError("Could not determine the entailment ID from the model config, please pass it at init."     )







					def  snake_case     (							self					:	Dict      ,   SCREAMING_SNAKE_CASE					:	List[Any]      ,   SCREAMING_SNAKE_CASE					:	Union[str, Any]     ):
									lowercase__     :    Any													=     labels
									return self.pre_processor(
									    [text] * len(__lowercase     )      ,   [f"""This example is {label}""" for label in labels]      ,   return_tensors="pt"      ,   padding="max_length"      ,   )







					def  snake_case     (							self					:	List[Any]      ,   SCREAMING_SNAKE_CASE					:	Tuple     ):
									lowercase__     :    List[Any]													=     outputs.logits
									lowercase__     :    Any													=     torch.argmax(logits[:, 2]     ).item()
									return self._labels[label_id]





import argparse
import json

from tqdm import tqdm



def lowerCamelCase  (   ):
    '''simple docstring'''







    __UpperCamelCase     :Tuple			  =		argparse.ArgumentParser()

    # Required parameters
    parser.add_argument(
        '''--src_path'''  ,			type=SCREAMING_SNAKE_CASE  ,			default='''biencoder-nq-dev.json'''  ,			help='''Path to raw DPR training data'''  ,			)
    parser.add_argument(
        '''--evaluation_set'''  ,			type=SCREAMING_SNAKE_CASE  ,			help='''where to store parsed evaluation_set file'''  ,			)
    parser.add_argument(
        '''--gold_data_path'''  ,			type=SCREAMING_SNAKE_CASE  ,			help='''where to store parsed gold_data_path file'''  ,			)
    __UpperCamelCase     :str			  =		parser.parse_args()

    with open(args.src_path  ,			'''r'''	) as src_file, open(args.evaluation_set  ,			'''w'''	) as eval_file, open(
        args.gold_data_path  ,			'''w'''	) as gold_file:
        __UpperCamelCase     :List[str]			  =		json.load(SCREAMING_SNAKE_CASE	)
        for dpr_record in tqdm(SCREAMING_SNAKE_CASE	):
            __UpperCamelCase     :List[str]			  =		dpr_record['''question''']
            __UpperCamelCase     :Tuple			  =		[context['''title'''] for context in dpr_record['''positive_ctxs''']]
            eval_file.write(question + '''\n'''	)
            gold_file.write('''\t'''.join(SCREAMING_SNAKE_CASE	) + '''\n'''	)


if __name__ == "__main__":
    main()





