





import itertools
from dataclasses import dataclass
from io import StringIO
from typing import Optional

import pyarrow as pa

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


UpperCamelCase__ :			Optional[Any]       	=			datasets.utils.logging.get_logger(__name__)







@dataclass
class 	_UpperCamelCase	(							datasets.BuilderConfig	):







					'''simple docstring'''

					lowerCamelCase  :  Optional[int]           =							None
					lowerCamelCase  :  List[Any]           =							'utf-8'
					lowerCamelCase  :  str           =							None
					lowerCamelCase  :  Any           =							10 << 20  # 10MB
					lowerCamelCase  :  Any           =							False
					lowerCamelCase  :  Optional[int]           =							'line'







class 	_UpperCamelCase	(							datasets.ArrowBasedBuilder	):







					'''simple docstring'''

					lowerCamelCase  :  Optional[Any]           =							TextConfig





					def 							SCREAMING_SNAKE_CASE				(    self     :	Optional[Any]		):






						'''simple docstring'''







						return datasets.DatasetInfo(features=self.config.features		)





					def 							SCREAMING_SNAKE_CASE				(    self     :	str			,       __lowercase     :	List[Any]		):






						'''simple docstring'''







						if not self.config.data_files:
							raise ValueError(F"""At least one data file must be specified, but got data_files={self.config.data_files}"""		)
						UpperCAmelCase_		    =  dl_manager.download_and_extract(self.config.data_files		)
						if isinstance(__lowercase			,       (str, list, tuple)		):
							UpperCAmelCase_		    =  data_files
							if isinstance(__lowercase			,       __lowercase		):
								UpperCAmelCase_		    =  [files]
							UpperCAmelCase_		    =  [dl_manager.iter_files(__lowercase		) for file in files]
							return [datasets.SplitGenerator(name=datasets.Split.TRAIN			,       gen_kwargs={"""files""": files}		)]
						UpperCAmelCase_		    =  []
						for split_name, files in data_files.items():
							if isinstance(__lowercase			,       __lowercase		):
								UpperCAmelCase_		    =  [files]
							UpperCAmelCase_		    =  [dl_manager.iter_files(__lowercase		) for file in files]
							splits.append(datasets.SplitGenerator(name=__lowercase			,       gen_kwargs={"""files""": files}		)		)
						return splits





					def 							SCREAMING_SNAKE_CASE				(    self     :	List[Any]			,       __lowercase     :	Union[str, Any]		):






						'''simple docstring'''







						if self.config.features is not None:
							UpperCAmelCase_		    =  self.config.features.arrow_schema
							if all(not require_storage_cast(__lowercase		) for feature in self.config.features.values()		):
								# cheaper cast
								UpperCAmelCase_		    =  pa_table.cast(__lowercase		)
							else:
								# more expensive cast; allows str <-> int/float or str to Audio for example
								UpperCAmelCase_		    =  table_cast(__lowercase			,       __lowercase		)
							return pa_table
						else:
							return pa_table.cast(pa.schema({"""text""": pa.string()}		)		)





					def 							SCREAMING_SNAKE_CASE				(    self     :	Union[str, Any]			,       __lowercase     :	Optional[Any]		):






						'''simple docstring'''







						UpperCAmelCase_		    =  list(self.config.features		) if self.config.features is not None else ["""text"""]
						for file_idx, file in enumerate(itertools.chain.from_iterable(__lowercase		)		):
							# open in text mode, by default translates universal newlines ("\n", "\r\n" and "\r") into "\n"
							with open(__lowercase			,       encoding=self.config.encoding			,       errors=self.config.errors		) as f:
								if self.config.sample_by == "line":
									UpperCAmelCase_		    =  0
									while True:
										UpperCAmelCase_		    =  f.read(self.config.chunksize		)
										if not batch:
											break
										batch += f.readline()  # finish current line
										# StringIO.readlines, by default splits only on "\n" (and keeps line breaks)
										UpperCAmelCase_		    =  StringIO(__lowercase		).readlines()
										if not self.config.keep_linebreaks:
											UpperCAmelCase_		    =  [line.rstrip("""\n"""		) for line in batch]
										UpperCAmelCase_		    =  pa.Table.from_arrays([pa.array(__lowercase		)]			,       names=__lowercase		)
										# Uncomment for debugging (will print the Arrow table size and elements)
										# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
										# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
										yield (file_idx, batch_idx), self._cast_table(__lowercase		)
										batch_idx += 1
								elif self.config.sample_by == "paragraph":
									UpperCAmelCase_		    =  0
									UpperCAmelCase_		    =  """"""
									while True:
										UpperCAmelCase_		    =  f.read(self.config.chunksize		)
										if not new_batch:
											break
										batch += new_batch
										batch += f.readline()  # finish current line
										UpperCAmelCase_		    =  batch.split("""\n\n"""		)
										UpperCAmelCase_		    =  pa.Table.from_arrays(
										    [pa.array([example for example in batch[:-1] if example]		)]			,       names=__lowercase		)
										# Uncomment for debugging (will print the Arrow table size and elements)
										# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
										# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
										yield (file_idx, batch_idx), self._cast_table(__lowercase		)
										batch_idx += 1
										UpperCAmelCase_		    =  batch[-1]
									if batch:
										UpperCAmelCase_		    =  pa.Table.from_arrays([pa.array([batch]		)]			,       names=__lowercase		)
										yield (file_idx, batch_idx), self._cast_table(__lowercase		)
								elif self.config.sample_by == "document":
									UpperCAmelCase_		    =  f.read()
									UpperCAmelCase_		    =  pa.Table.from_arrays([pa.array([text]		)]			,       names=__lowercase		)
									yield file_idx, self._cast_table(__lowercase		)






'''simple docstring'''






from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available




@dataclass
class 			a  (  _SCREAMING_SNAKE_CASE							):
 _lowerCAmelCase			       =						{
     """sld_warmup_steps""": 1_5,
     """sld_guidance_scale""": 2_0,
     """sld_threshold""": 0.0,
     """sld_momentum_scale""": 0.0,
     """sld_mom_beta""": 0.0,
 }
 _lowerCAmelCase			       =						{
     """sld_warmup_steps""": 1_0,
     """sld_guidance_scale""": 1_0_0_0,
     """sld_threshold""": 0.01,
     """sld_momentum_scale""": 0.3,
     """sld_mom_beta""": 0.4,
 }
 _lowerCAmelCase			       =						{
     """sld_warmup_steps""": 7,
     """sld_guidance_scale""": 2_0_0_0,
     """sld_threshold""": 0.025,
     """sld_momentum_scale""": 0.5,
     """sld_mom_beta""": 0.7,
 }
 _lowerCAmelCase			       =						{
     """sld_warmup_steps""": 0,
     """sld_guidance_scale""": 5_0_0_0,
     """sld_threshold""": 1.0,
     """sld_momentum_scale""": 0.5,
     """sld_mom_beta""": 0.7,
 }





@dataclass
class 			a  (  _SCREAMING_SNAKE_CASE							):
 _lowerCAmelCase			       =						42
 _lowerCAmelCase			       =						42
 _lowerCAmelCase			       =						42
 _lowerCAmelCase			       =						42


if is_transformers_available() and is_torch_available():
 from .pipeline_stable_diffusion_safe import StableDiffusionPipelineSafe
 from .safety_checker import SafeStableDiffusionSafetyChecker

