

"""simple docstring"""





import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
				import sqlitea

				import sqlalchemy


_SCREAMING_SNAKE_CASE				:     str       				=       datasets.utils.logging.get_logger(__name__)

@dataclass
class 			__a      (					datasets.BuilderConfig			):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  True
			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  None
			SCREAMING_SNAKE_CASE_     =  10_000
			SCREAMING_SNAKE_CASE_     =  None






			def       _lowerCAmelCase	(							self   :    Dict  ):
								if self.sql is None:
													raise ValueError('''sql must be specified'''  )
								if self.con is None:
													raise ValueError('''con must be specified'''  )






			def       _lowerCAmelCase	(							self   :    Optional[int]  ,     lowercase_   :    dict  ,     lowercase_   :    Optional[datasets.Features] = None  ,     ):
								UpperCamelCase__						:			Tuple				     =config_kwargs.copy()
								# We need to stringify the Selectable object to make its hash deterministic

								# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
								UpperCamelCase__						:			Dict				     =config_kwargs['''sql''']
								if not isinstance(SCREAMING_SNAKE_CASE__  ,     SCREAMING_SNAKE_CASE__  ):
													if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																		import sqlalchemy

																		if isinstance(SCREAMING_SNAKE_CASE__  ,     sqlalchemy.sql.Selectable  ):
																							UpperCamelCase__						:			Dict				     =sqlalchemy.create_engine(config_kwargs['''con'''].split('''://'''  )[0] + '''://'''  )
																							UpperCamelCase__						:			Optional[int]				     =str(sql.compile(dialect=engine.dialect  )  )
																							UpperCamelCase__						:			Tuple				     =sql_str
																		else:
																							raise TypeError(
																							    f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(SCREAMING_SNAKE_CASE__  )}: {sql}'''  )
													else:
																		raise TypeError(
																		    f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(SCREAMING_SNAKE_CASE__  )}: {sql}'''  )
								UpperCamelCase__						:			int				     =config_kwargs['''con''']
								if not isinstance(SCREAMING_SNAKE_CASE__  ,     SCREAMING_SNAKE_CASE__  ):
													UpperCamelCase__						:			str				     =id(SCREAMING_SNAKE_CASE__  )
													logger.info(
													    f'''SQL connection \'con\' of type {type(SCREAMING_SNAKE_CASE__  )} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''  )

								return super().create_config_id(SCREAMING_SNAKE_CASE__  ,     custom_features=SCREAMING_SNAKE_CASE__  )






			@property
			def       _lowerCAmelCase	(							self   :    List[str]  ):
								UpperCamelCase__						:			int				     ={
								    '''index_col''': self.index_col,
								    '''columns''': self.columns,
								    '''params''': self.params,
								    '''coerce_float''': self.coerce_float,
								    '''parse_dates''': self.parse_dates,
								}
								return pd_read_sql_kwargs



class 			__a      (					datasets.ArrowBasedBuilder			):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_     =  SqlConfig






			def       _lowerCAmelCase	(							self   :    Optional[Any]  ):
								return datasets.DatasetInfo(features=self.config.features  )






			def       _lowerCAmelCase	(							self   :    str  ,     lowercase_   :    Tuple  ):
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,     gen_kwargs={}  )]






			def       _lowerCAmelCase	(							self   :    Dict  ,     lowercase_   :    pa.Table  ):
								if self.config.features is not None:
													UpperCamelCase__						:			Dict				     =self.config.features.arrow_schema
													if all(not require_storage_cast(SCREAMING_SNAKE_CASE__  ) for feature in self.config.features.values()  ):
																		# cheaper cast
																		UpperCamelCase__						:			List[Any]				     =pa.Table.from_arrays([pa_table[field.name] for field in schema]  ,     schema=SCREAMING_SNAKE_CASE__  )
													else:
																		# more expensive cast; allows str <-> int/float or str to Audio for example
																		UpperCamelCase__						:			List[str]				     =table_cast(SCREAMING_SNAKE_CASE__  ,     SCREAMING_SNAKE_CASE__  )
								return pa_table






			def       _lowerCAmelCase	(							self   :    List[str]  ):
								UpperCamelCase__						:			List[str]				     =self.config.chunksize
								UpperCamelCase__						:			Dict				     =pd.read_sql(
								    self.config.sql  ,     self.config.con  ,     chunksize=SCREAMING_SNAKE_CASE__  ,     **self.config.pd_read_sql_kwargs  )
								UpperCamelCase__						:			Optional[Any]				     =[sql_reader] if chunksize is None else sql_reader
								for chunk_idx, df in enumerate(SCREAMING_SNAKE_CASE__  ):
													UpperCamelCase__						:			Optional[Any]				     =pa.Table.from_pandas(SCREAMING_SNAKE_CASE__  )
													yield chunk_idx, self._cast_table(SCREAMING_SNAKE_CASE__  )








import os
import pathlib
import tempfile
import uuid

import numpy as np

from ..utils import is_soundfile_availble, is_torch_available, is_vision_available, logging


UpperCamelCase								=     logging.get_logger(__name__)

if is_vision_available():
     import PIL.Image
     from PIL import Image
     from PIL.Image import Image as ImageType
else:
     UpperCamelCase								=     object

if is_torch_available():
     import torch

if is_soundfile_availble():
     import soundfile as sf





class  __lowerCamelCase       :




    """simple docstring"""

    def __init__(       self     :     Tuple   ,	SCREAMING_SNAKE_CASE__     :     List[str]   )      ->       Union[str, Any]:
         lowerCAmelCase__		= value


    def __str__(       self     :     int   )      ->       List[Any]:
         return self.to_string()


    def    a      (       self     :     str   )      ->       Union[str, Any]:
         logger.error(
             "This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable"   )
         return self._value


    def    a      (       self     :     str   )      ->       str:
         logger.error(
             "This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable"   )
         return str(self._value   )






class  __lowerCamelCase       (    UpperCamelCase__	,				UpperCamelCase__    ):




    """simple docstring"""

    def    a      (       self     :     Optional[Any]   )      ->       int:
         return self._value


    def    a      (       self     :     Optional[Any]   )      ->       Any:
         return self._value






class  __lowerCamelCase       (    UpperCamelCase__	,				UpperCamelCase__    ):




    """simple docstring"""

    def __init__(       self     :     Union[str, Any]   ,	SCREAMING_SNAKE_CASE__     :     Optional[Any]   )      ->       Optional[int]:
         super().__init__(SCREAMING_SNAKE_CASE__   )

         if not is_vision_available():
              raise ImportError("PIL must be installed in order to handle images."   )

         lowerCAmelCase__		= None
         lowerCAmelCase__		= None
         lowerCAmelCase__		= None

         if isinstance(SCREAMING_SNAKE_CASE__   ,	SCREAMING_SNAKE_CASE__   ):
              lowerCAmelCase__		= value
         elif isinstance(SCREAMING_SNAKE_CASE__   ,	(str, pathlib.Path)   ):
              lowerCAmelCase__		= value
         elif isinstance(SCREAMING_SNAKE_CASE__   ,	torch.Tensor   ):
              lowerCAmelCase__		= value
         else:
              raise ValueError(f'Unsupported type for {self.__class__.__name__}: {type(SCREAMING_SNAKE_CASE__   )}'   )


    def    a      (       self     :     Optional[int]   ,	SCREAMING_SNAKE_CASE__     :     Any=None   ,	SCREAMING_SNAKE_CASE__     :     str=None   )      ->       Union[str, Any]:
         from IPython.display import Image, display

         display(Image(self.to_string()   )   )


    def    a      (       self     :     List[Any]   )      ->       int:
         if self._raw is not None:
              return self._raw

         if self._path is not None:
              lowerCAmelCase__		= Image.open(self._path   )
              return self._raw


    def    a      (       self     :     Optional[int]   )      ->       Union[str, Any]:
         if self._path is not None:
              return self._path

         if self._raw is not None:
              lowerCAmelCase__		= tempfile.mkdtemp()
              lowerCAmelCase__		= os.path.join(SCREAMING_SNAKE_CASE__   ,	str(uuid.uuida()   ) + ".png"   )
              self._raw.save(self._path   )

              return self._path

         if self._tensor is not None:
              lowerCAmelCase__		= self._tensor.cpu().detach().numpy()

              # There is likely simpler than load into image into save
              lowerCAmelCase__		= Image.fromarray((array * 255).astype(np.uinta   )   )

              lowerCAmelCase__		= tempfile.mkdtemp()
              lowerCAmelCase__		= os.path.join(SCREAMING_SNAKE_CASE__   ,	str(uuid.uuida()   ) + ".png"   )

              img.save(self._path   )

              return self._path






class  __lowerCamelCase       (    UpperCamelCase__    ):




    """simple docstring"""

    def __init__(       self     :     List[str]   ,	SCREAMING_SNAKE_CASE__     :     Union[str, Any]   ,	SCREAMING_SNAKE_CASE__     :     int=16_000   )      ->       Union[str, Any]:
         super().__init__(SCREAMING_SNAKE_CASE__   )

         if not is_soundfile_availble():
              raise ImportError("soundfile must be installed in order to handle audio."   )

         lowerCAmelCase__		= None
         lowerCAmelCase__		= None

         lowerCAmelCase__		= samplerate

         if isinstance(SCREAMING_SNAKE_CASE__   ,	(str, pathlib.Path)   ):
              lowerCAmelCase__		= value
         elif isinstance(SCREAMING_SNAKE_CASE__   ,	torch.Tensor   ):
              lowerCAmelCase__		= value
         else:
              raise ValueError(f'Unsupported audio type: {type(SCREAMING_SNAKE_CASE__   )}'   )


    def    a      (       self     :     str   ,	SCREAMING_SNAKE_CASE__     :     List[str]=None   ,	SCREAMING_SNAKE_CASE__     :     List[str]=None   )      ->       Any:
         from IPython.display import Audio, display

         display(Audio(self.to_string()   ,	rate=self.samplerate   )   )


    def    a      (       self     :     Optional[Any]   )      ->       List[Any]:
         if self._tensor is not None:
              return self._tensor

         if self._path is not None:
              lowerCAmelCase__	,		lowerCAmelCase__		= sf.read(self._path   )
              lowerCAmelCase__		= torch.tensor(SCREAMING_SNAKE_CASE__   )
              return self._tensor


    def    a      (       self     :     Dict   )      ->       List[str]:
         if self._path is not None:
              return self._path

         if self._tensor is not None:
              lowerCAmelCase__		= tempfile.mkdtemp()
              lowerCAmelCase__		= os.path.join(SCREAMING_SNAKE_CASE__   ,	str(uuid.uuida()   ) + ".wav"   )
              sf.write(self._path   ,	self._tensor   ,	samplerate=self.samplerate   )
              return self._path






UpperCamelCase								=     {'text': AgentText, 'image': AgentImage, 'audio': AgentAudio}
UpperCamelCase								=     {str: AgentText}

if is_vision_available():
     UpperCamelCase								=     AgentImage



def 							_A   (						*lowerCAmelCase_     :					List[str]   ,				**lowerCAmelCase_     :					Optional[int]			):
     """simple docstring"""

     lowerCAmelCase__		= [(arg.to_raw() if isinstance(lowerCAmelCase_   ,				lowerCAmelCase_			) else arg) for arg in args]
     lowerCAmelCase__		= {k: (v.to_raw() if isinstance(lowerCAmelCase_   ,				lowerCAmelCase_			) else v) for k, v in kwargs.items()}
     return args, kwargs






def 							_A   (						lowerCAmelCase_     :					Dict   ,				lowerCAmelCase_     :					Optional[int]=None			):
     """simple docstring"""

     if isinstance(lowerCAmelCase_   ,				lowerCAmelCase_			):
          lowerCAmelCase__		= {}
          for i, (k, v) in enumerate(outputs.items()			):
               if output_types is not None:
                    # If the class has defined outputs, we can map directly according to the class definition
                    if output_types[i] in AGENT_TYPE_MAPPING:
                         lowerCAmelCase__		= AGENT_TYPE_MAPPING[output_types[i]](lowerCAmelCase_			)
                    else:
                         lowerCAmelCase__		= AgentType(lowerCAmelCase_			)

               else:
                    # If the class does not have defined output, then we map according to the type
                    for _k, _v in INSTANCE_TYPE_MAPPING.items():
                         if isinstance(lowerCAmelCase_   ,				_k			):
                              lowerCAmelCase__		= _v(lowerCAmelCase_			)
                    if k not in decoded_outputs:
                         lowerCAmelCase__		= AgentType[v]

     elif isinstance(lowerCAmelCase_   ,				(list, tuple)			):
          lowerCAmelCase__		= type(lowerCAmelCase_			)()
          for i, v in enumerate(lowerCAmelCase_			):
               if output_types is not None:
                    # If the class has defined outputs, we can map directly according to the class definition
                    if output_types[i] in AGENT_TYPE_MAPPING:
                         decoded_outputs.append(AGENT_TYPE_MAPPING[output_types[i]](lowerCAmelCase_			)			)
                    else:
                         decoded_outputs.append(AgentType(lowerCAmelCase_			)			)
               else:
                    # If the class does not have defined output, then we map according to the type
                    lowerCAmelCase__		= False
                    for _k, _v in INSTANCE_TYPE_MAPPING.items():
                         if isinstance(lowerCAmelCase_   ,				_k			):
                              decoded_outputs.append(_v(lowerCAmelCase_			)			)
                              lowerCAmelCase__		= True

                    if not found:
                         decoded_outputs.append(AgentType(lowerCAmelCase_			)			)

     else:
          if output_types[0] in AGENT_TYPE_MAPPING:
               # If the class has defined outputs, we can map directly according to the class definition
               lowerCAmelCase__		= AGENT_TYPE_MAPPING[output_types[0]](lowerCAmelCase_			)

          else:
               # If the class does not have defined output, then we map according to the type
               for _k, _v in INSTANCE_TYPE_MAPPING.items():
                    if isinstance(lowerCAmelCase_   ,				_k			):
                         return _v(lowerCAmelCase_			)
               return AgentType(lowerCAmelCase_			)

     return decoded_outputs
