






"""simple docstring"""
import multiprocessing
import os
from typing import BinaryIO, Optional, Union

import fsspec

from .. import Dataset, Features, NamedSplit, config
from ..formatting import query_table
from ..packaged_modules.json.json import Json
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader



class      SCREAMING_SNAKE_CASE  (				_lowerCamelCase						):





	"""simple docstring"""




	def __init__(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_ = None     ,     lowerCAmelCase_ = None     ,     lowerCAmelCase_ = None     ,     lowerCAmelCase_ = False     ,     lowerCAmelCase_ = False     ,     lowerCAmelCase_ = None     ,     lowerCAmelCase_ = None     ,     **lowerCAmelCase_     ,     ):
		super().__init__(
		    A__     ,     split=A__     ,     features=A__     ,     cache_dir=A__     ,     keep_in_memory=A__     ,     streaming=A__     ,     num_proc=A__     ,     **A__     ,     )
		A_		:						Any	    =   field
		A_		:						Tuple	    =   path_or_paths if isinstance(A__     ,     A__							) else {self.split: path_or_paths}
		A_		:						List[str]	    =   Json(
		    cache_dir=A__     ,     data_files=A__     ,     features=A__     ,     field=A__     ,     **A__     ,     )





	def    lowerCamelCase(self							):
		# Build iterable dataset
		if self.streaming:
			A_		:						Optional[Any]	    =   self.builder.as_streaming_dataset(split=self.split							)
		# Build regular (map-style) dataset
		else:
			A_		:						List[str]	    =   None
			A_		:						Optional[int]	    =   None
			A_		:						List[str]	    =   None
			A_		:						str	    =   None

			self.builder.download_and_prepare(
			    download_config=A__     ,     download_mode=A__     ,     verification_mode=A__     ,     base_path=A__     ,     num_proc=self.num_proc     ,     )
			A_		:						List[str]	    =   self.builder.as_dataset(
			    split=self.split     ,     verification_mode=A__     ,     in_memory=self.keep_in_memory							)
		return dataset






class      SCREAMING_SNAKE_CASE  :





	"""simple docstring"""




	def __init__(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_ = None     ,     lowerCAmelCase_ = None     ,     **lowerCAmelCase_     ,     ):
		if num_proc is not None and num_proc <= 0:
			raise ValueError(f"""num_proc {num_proc} must be an integer > 0."""							)

		A_		:						Optional[Any]	    =   dataset
		A_		:						List[Any]	    =   path_or_buf
		A_		:						int	    =   batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
		A_		:						List[Any]	    =   num_proc
		A_		:						Dict	    =   """utf-8"""
		A_		:						Optional[Any]	    =   to_json_kwargs





	def    lowerCamelCase(self							):
		A_		:						Any	    =   self.to_json_kwargs.pop("""path_or_buf"""     ,     A__							)
		A_		:						Union[str, Any]	    =   self.to_json_kwargs.pop("""orient"""     ,     """records"""							)
		A_		:						Optional[Any]	    =   self.to_json_kwargs.pop("""lines"""     ,     True if orient == """records""" else False							)
		A_		:						Dict	    =   self.to_json_kwargs.pop("""index"""     ,     False if orient in ["""split""", """table"""] else True							)
		A_		:						Optional[int]	    =   self.to_json_kwargs.pop("""compression"""     ,     A__							)

		if compression not in [None, "infer", "gzip", "bz2", "xz"]:
			raise NotImplementedError(f"""`datasets` currently does not support {compression} compression"""							)

		if isinstance(self.path_or_buf     ,     (str, bytes, os.PathLike)							):
			with fsspec.open(self.path_or_buf     ,     """wb"""     ,     compression=A__							) as buffer:
				A_		:						Tuple	    =   self._write(file_obj=A__     ,     orient=A__     ,     lines=A__     ,     index=A__     ,     **self.to_json_kwargs							)
		else:
			if compression:
				raise NotImplementedError(
				    f"""The compression parameter is not supported when writing to a buffer, but compression={compression}"""
				    """ was passed. Please provide a local path instead."""							)
			A_		:						Union[str, Any]	    =   self._write(
			    file_obj=self.path_or_buf     ,     orient=A__     ,     lines=A__     ,     index=A__     ,     **self.to_json_kwargs							)
		return written





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_    ,     A_    ,     A_    ,     A_    ,     A_		:						List[str]	    =   args

		A_		:						List[str]	    =   query_table(
		    table=self.dataset.data     ,     key=slice(A__     ,     offset + self.batch_size							)     ,     indices=self.dataset._indices     ,     )
		A_		:						List[Any]	    =   batch.to_pandas().to_json(
		    path_or_buf=A__     ,     orient=A__     ,     lines=A__     ,     index=A__     ,     **A__							)
		if not json_str.endswith("""\n"""							):
			json_str += "\n"
		return json_str.encode(self.encoding							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     **lowerCAmelCase_     ,     ):
		A_		:						Optional[Any]	    =   0

		if self.num_proc is None or self.num_proc == 1:
			for offset in logging.tqdm(
			    range(0     ,     len(self.dataset							)     ,     self.batch_size							)     ,     unit="""ba"""     ,     disable=not logging.is_progress_bar_enabled()     ,     desc="""Creating json from Arrow format"""     ,     ):
				A_		:						Optional[Any]	    =   self._batch_json((offset, orient, lines, index, to_json_kwargs)							)
				written += file_obj.write(A__							)
		else:
			A_    ,     A_		:						Any	    =   len(self.dataset							), self.batch_size
			with multiprocessing.Pool(self.num_proc							) as pool:
				for json_str in logging.tqdm(
				    pool.imap(
				        self._batch_json     ,     [(offset, orient, lines, index, to_json_kwargs) for offset in range(0     ,     A__     ,     A__							)]     ,     )     ,     total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size     ,     unit="""ba"""     ,     disable=not logging.is_progress_bar_enabled()     ,     desc="""Creating json from Arrow format"""     ,     ):
					written += file_obj.write(A__							)

		return written









"""simple docstring"""
# 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      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				Optional[int]    =				"""facebook/bart-large-mnli"""
	_A       :				str    =				(
	    """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."""
	)
	_A       :				List[str]    =				"""text_classifier"""
	_A       :				Optional[int]    =				AutoTokenizer
	_A       :				Optional[Any]    =				AutoModelForSequenceClassification

	_A       :				List[str]    =				["""text""", ["""text"""]]
	_A       :				Dict    =				["""text"""]





	def    lowerCamelCase(self							):
		super().setup()
		A_		:						int	    =   self.model.config
		A_		:						List[str]	    =   -1
		for idx, label in config.idalabel.items():
			if label.lower().startswith("""entail"""							):
				A_		:						List[Any]	    =   int(lowerCAmelCase_							)
		if self.entailment_id == -1:
			raise ValueError("""Could not determine the entailment ID from the model config, please pass it at init."""							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						List[Any]	    =   labels
		return self.pre_processor(
		    [text] * len(lowerCAmelCase_							)     ,     [f"""This example is {label}""" for label in labels]     ,     return_tensors="""pt"""     ,     padding="""max_length"""     ,     )





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_		:						str	    =   outputs.logits
		A_		:						Optional[int]	    =   torch.argmax(logits[:, 2]							).item()
		return self._labels[label_id]

