





"""simple docstring"""


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      __SCREAMING_SNAKE_CASE    (						_a   ):







  '''simple docstring'''


  def __init__(		self   :       int					,   __a   :       Optional[Any]					,   __a   :       Optional[Any]					,   __a   :       Dict = None					,   __a   :       Union[str, Any] = None					,   __a   :       Dict = False					,   **__a   :       Union[str, Any]					,   )		->				Dict:
        super().__init__(features=_A					,   cache_dir=_A					,   keep_in_memory=_A					,   **_A	)
        _UpperCamelCase      :	List[str]						=       Sql(
            cache_dir=_A					,   features=_A					,   sql=_A					,   con=_A					,   **_A					,   )


  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]	)		->				str:
        _UpperCamelCase      :	Any						=       None
        _UpperCamelCase      :	Optional[int]						=       None
        _UpperCamelCase      :	Optional[int]						=       None
        _UpperCamelCase      :	int						=       None

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

        # Build dataset for splits
        _UpperCamelCase      :	int						=       self.builder.as_dataset(
            split="train"					,   verification_mode=_A					,   in_memory=self.keep_in_memory	)
        return dataset





class      __SCREAMING_SNAKE_CASE    :







  '''simple docstring'''


  def __init__(		self   :       Union[str, Any]					,   __a   :       Dict					,   __a   :       Dict					,   __a   :       Any					,   __a   :       Union[str, Any] = None					,   __a   :       Optional[int] = None					,   **__a   :       int					,   )		->				Tuple:
        if num_proc is not None and num_proc <= 0:
              raise ValueError(F'''num_proc {num_proc} must be an integer > 0.'''	)

        _UpperCamelCase      :	List[Any]						=       dataset
        _UpperCamelCase      :	List[str]						=       name
        _UpperCamelCase      :	Any						=       con
        _UpperCamelCase      :	Tuple						=       batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
        _UpperCamelCase      :	List[str]						=       num_proc
        _UpperCamelCase      :	Optional[int]						=       to_sql_kwargs

  def    __SCREAMING_SNAKE_CASE				(		self   :       Any	)		->				Tuple:
        _UpperCamelCase      :	Optional[int]						=       self.to_sql_kwargs.pop("sql"					,   _A	)
        _UpperCamelCase      :	int						=       self.to_sql_kwargs.pop("con"					,   _A	)
        _UpperCamelCase      :	List[str]						=       self.to_sql_kwargs.pop("index"					,   _A	)

        _UpperCamelCase      :	Tuple						=       self._write(index=_A					,   **self.to_sql_kwargs	)
        return written

  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple					,   __a   :       Optional[Any]	)		->				List[Any]:
        _UpperCamelCase,  _UpperCamelCase,  _UpperCamelCase      :	str						=       args
        _UpperCamelCase      :	Optional[int]						=       {**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs
        _UpperCamelCase      :	List[Any]						=       query_table(
            table=self.dataset.data					,   key=slice(_A					,   offset + self.batch_size	)					,   indices=self.dataset._indices					,   )
        _UpperCamelCase      :	List[Any]						=       batch.to_pandas()
        _UpperCamelCase      :	Dict						=       df.to_sql(self.name					,   self.con					,   index=_A					,   **_A	)
        return num_rows or len(_A	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       str					,   __a   :       Optional[Any]					,   **__a   :       Any	)		->				Union[str, Any]:
        _UpperCamelCase      :	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 SQL from Arrow format"					,   ):
                    written += self._batch_sql((offset, index, to_sql_kwargs)	)
        else:
              _UpperCamelCase,  _UpperCamelCase      :	Optional[Any]						=       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					,   _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 SQL from Arrow format"					,   ):
                          written += num_rows

        return written












"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


lowerCamelCase__				  =   {
    "configuration_owlvit": [
        "OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "OwlViTConfig",
        "OwlViTOnnxConfig",
        "OwlViTTextConfig",
        "OwlViTVisionConfig",
    ],
    "processing_owlvit": ["OwlViTProcessor"],
}


try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowerCamelCase__				  =   ["OwlViTFeatureExtractor"]
  lowerCamelCase__				  =   ["OwlViTImageProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowerCamelCase__				  =   [
      "OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "OwlViTModel",
      "OwlViTPreTrainedModel",
      "OwlViTTextModel",
      "OwlViTVisionModel",
      "OwlViTForObjectDetection",
  ]

if TYPE_CHECKING:
  from .configuration_owlvit import (
      OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      OwlViTConfig,
      OwlViTOnnxConfig,
      OwlViTTextConfig,
      OwlViTVisionConfig,
  )
  from .processing_owlvit import OwlViTProcessor

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_owlvit import OwlViTFeatureExtractor
    from .image_processing_owlvit import OwlViTImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_owlvit import (
        OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OwlViTForObjectDetection,
        OwlViTModel,
        OwlViTPreTrainedModel,
        OwlViTTextModel,
        OwlViTVisionModel,
    )

else:
  import sys

  lowerCamelCase__				  =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
