


'''simple docstring'''




from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
       from ..tf_utils import stable_softmax

_a   :   List[Any]												=   logging.get_logger(__name__)




@add_end_docstrings(__lowercase							)
class 			_lowercase (	__lowercase							):
   def __init__(						self       :      Any       ,   **SCREAMING_SNAKE_CASE_       :      Union[str, Any]      ) ->							List[Any]:
        super().__init__(**SCREAMING_SNAKE_CASE_      )

        requires_backends(self       ,   'vision'      )
        self.check_model_type(
            TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
            if self.framework == 'tf'
            else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING      )
   def __call__(						self       :      int       ,   SCREAMING_SNAKE_CASE_       :      Union[str, List[str], "Image", List["Image"]]       ,   **SCREAMING_SNAKE_CASE_       :      List[str]      ) ->							int:
        return super().__call__(SCREAMING_SNAKE_CASE_       ,   **SCREAMING_SNAKE_CASE_      )
   def a  (						self       :      List[Any]       ,   **SCREAMING_SNAKE_CASE_       :      List[str]      ) ->							List[str]:
        __snake_case           =						{}
        if "candidate_labels" in kwargs:
             __snake_case           =						kwargs['candidate_labels']
        if "hypothesis_template" in kwargs:
             __snake_case           =						kwargs['hypothesis_template']

        return preprocess_params, {}, {}
   def a  (						self       :      str       ,   SCREAMING_SNAKE_CASE_       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      Tuple=None       ,   SCREAMING_SNAKE_CASE_       :      Optional[Any]="This is a photo of {}."      ) ->							Dict:
        __snake_case           =						load_image(SCREAMING_SNAKE_CASE_      )
        __snake_case           =						self.image_processor(images=[image]       ,   return_tensors=self.framework      )
        __snake_case           =						candidate_labels
        __snake_case           =						[hypothesis_template.format(SCREAMING_SNAKE_CASE_      ) for x in candidate_labels]
        __snake_case           =						self.tokenizer(SCREAMING_SNAKE_CASE_       ,   return_tensors=self.framework       ,   padding=SCREAMING_SNAKE_CASE_      )
        __snake_case           =						[text_inputs]
        return inputs
   def a  (						self       :      int       ,   SCREAMING_SNAKE_CASE_       :      List[Any]      ) ->							Union[str, Any]:
        __snake_case           =						model_inputs.pop('candidate_labels'      )
        __snake_case           =						model_inputs.pop('text_inputs'      )
        if isinstance(text_inputs[0]       ,   SCREAMING_SNAKE_CASE_      ):
             __snake_case           =						text_inputs[0]
        else:
             # Batching case.
             __snake_case           =						text_inputs[0][0]

        __snake_case           =						self.model(**SCREAMING_SNAKE_CASE_       ,   **SCREAMING_SNAKE_CASE_      )

        __snake_case           =						{
            'candidate_labels': candidate_labels,
            'logits': outputs.logits_per_image,
        }
        return model_outputs





   def a  (						self       :      Tuple       ,   SCREAMING_SNAKE_CASE_       :      Optional[int]      ) ->							List[str]:
        __snake_case           =						model_outputs.pop('candidate_labels'      )
        __snake_case           =						model_outputs['logits'][0]
        if self.framework == "pt":
             __snake_case           =						logits.softmax(dim=-1      ).squeeze(-1      )
             __snake_case           =						probs.tolist()
             if not isinstance(SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_      ):
                  __snake_case           =						[scores]
        elif self.framework == "tf":
             __snake_case           =						stable_softmax(SCREAMING_SNAKE_CASE_       ,   axis=-1      )
             __snake_case           =						probs.numpy().tolist()
        else:
             raise ValueError(f'Unsupported framework: {self.framework}'      )

        __snake_case           =						[
            {'score': score, 'label': candidate_label}
            for score, candidate_label in sorted(zip(SCREAMING_SNAKE_CASE_       ,   SCREAMING_SNAKE_CASE_      )       ,   key=lambda SCREAMING_SNAKE_CASE_      : -x[0]      )
        ]
        return result











import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
					import sqlitea

					import sqlalchemy


class     A     (SCREAMING_SNAKE_CASE       ):
			'''simple docstring'''




			def __init__(     self  :       Any				,      __lowerCAmelCase  :       Union[str, "sqlalchemy.sql.Selectable"]				,      __lowerCAmelCase  :       Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]				,      __lowerCAmelCase  :       Optional[Features] = None				,      __lowerCAmelCase  :       str = None				,      __lowerCAmelCase  :       bool = False				,      **__lowerCAmelCase  :       Dict				,      )      ->      Any:







					"""simple docstring"""






					super().__init__(features=__lowerCAmelCase				,      cache_dir=__lowerCAmelCase				,      keep_in_memory=__lowerCAmelCase				,      **__lowerCAmelCase  )
					A__     =  Sql(
					    cache_dir=__lowerCAmelCase				,      features=__lowerCAmelCase				,      sql=__lowerCAmelCase				,      con=__lowerCAmelCase				,      **__lowerCAmelCase				,      )







			def   a_			(     self  :       Optional[int]  )      ->      Optional[int]:







					"""simple docstring"""






					A__     =  None
					A__     =  None
					A__     =  None
					A__     =  None

					self.builder.download_and_prepare(
					    download_config=__lowerCAmelCase				,      download_mode=__lowerCAmelCase				,      verification_mode=__lowerCAmelCase				,      base_path=__lowerCAmelCase				,      )

					# Build dataset for splits
					A__     =  self.builder.as_dataset(
					    split="""train"""				,      verification_mode=__lowerCAmelCase				,      in_memory=self.keep_in_memory  )
					return dataset


class     A     :
			'''simple docstring'''




			def __init__(     self  :       Optional[int]				,      __lowerCAmelCase  :       Dataset				,      __lowerCAmelCase  :       str				,      __lowerCAmelCase  :       Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]				,      __lowerCAmelCase  :       Optional[int] = None				,      __lowerCAmelCase  :       Optional[int] = None				,      **__lowerCAmelCase  :       List[Any]				,      )      ->      Any:







					"""simple docstring"""






					if num_proc is not None and num_proc <= 0:
							raise ValueError(f'num_proc {num_proc} must be an integer > 0.'  )

					A__     =  dataset
					A__     =  name
					A__     =  con
					A__     =  batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
					A__     =  num_proc
					A__     =  to_sql_kwargs






			def   a_			(     self  :       Any  )      ->      int:







					"""simple docstring"""






					A__     =  self.to_sql_kwargs.pop("""sql"""				,      __lowerCAmelCase  )
					A__     =  self.to_sql_kwargs.pop("""con"""				,      __lowerCAmelCase  )
					A__     =  self.to_sql_kwargs.pop("""index"""				,      __lowerCAmelCase  )

					A__     =  self._write(index=__lowerCAmelCase				,      **self.to_sql_kwargs  )
					return written






			def   a_			(     self  :       Union[str, Any]				,      __lowerCAmelCase  :       Optional[Any]  )      ->      Optional[Any]:







					"""simple docstring"""






					A__ ,     A__ ,     A__     =  args
					A__     =  {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
					A__     =  query_table(
					    table=self.dataset.data				,      key=slice(__lowerCAmelCase				,      offset + self.batch_size  )				,      indices=self.dataset._indices				,      )
					A__     =  batch.to_pandas()
					A__     =  df.to_sql(self.name				,      self.con				,      index=__lowerCAmelCase				,      **__lowerCAmelCase  )
					return num_rows or len(__lowerCAmelCase  )







			def   a_			(     self  :       Optional[int]				,      __lowerCAmelCase  :       Tuple				,      **__lowerCAmelCase  :       Dict  )      ->      int:







					"""simple docstring"""






					A__     =  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 SQL from Arrow format"""				,      ):
									written += self._batch_sql((offset, index, to_sql_kwargs)  )
					else:
							A__ ,     A__     =  len(self.dataset  ), self.batch_size
							with multiprocessing.Pool(self.num_proc  ) as pool:
									for num_rows in logging.tqdm(
									    pool.imap(
									        self._batch_sql				,      [(offset, index, to_sql_kwargs) for offset in range(0				,      __lowerCAmelCase				,      __lowerCAmelCase  )]				,      )				,      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 SQL from Arrow format"""				,      ):
											written += num_rows

					return written



