



import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class    _A	(     _lowerCAmelCase				,_lowerCAmelCase				,_lowerCAmelCase				,unittest.TestCase   ):
       UpperCamelCase__  :   str						 =						StableDiffusionInstructPixaPixPipeline
       UpperCamelCase__  :   List[str]						 =						TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''cross_attention_kwargs'''}
       UpperCamelCase__  :   str						 =						TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
       UpperCamelCase__  :   Optional[Any]						 =						IMAGE_TO_IMAGE_IMAGE_PARAMS
       UpperCamelCase__  :   Union[str, Any]						 =						IMAGE_TO_IMAGE_IMAGE_PARAMS



       def 		_lowerCamelCase      (		self							:					str):


           '''simple docstring'''

           torch.manual_seed(0)
           __a							=    UNetaDConditionModel(
               block_out_channels=(32, 64)					,		layers_per_block=2					,		sample_size=32					,		in_channels=8					,		out_channels=4					,		down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')					,		up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')					,		cross_attention_dim=32					,		)
           __a							=    PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE_)
           torch.manual_seed(0)
           __a							=    AutoencoderKL(
               block_out_channels=[32, 64]					,		in_channels=3					,		out_channels=3					,		down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']					,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']					,		latent_channels=4					,		)
           torch.manual_seed(0)
           __a							=    CLIPTextConfig(
               bos_token_id=0					,		eos_token_id=2					,		hidden_size=32					,		intermediate_size=37					,		layer_norm_eps=1E-05					,		num_attention_heads=4					,		num_hidden_layers=5					,		pad_token_id=1					,		vocab_size=1_000					,		)
           __a							=    CLIPTextModel(SCREAMING_SNAKE_CASE_)
           __a							=    CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

           __a							=    {
               """unet""": unet,
               """scheduler""": scheduler,
               """vae""": vae,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
               """safety_checker""": None,
               """feature_extractor""": None,
           }
           return components



       def 		_lowerCamelCase      (		self							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=0):


           '''simple docstring'''

           __a							=    floats_tensor((1, 3, 32, 32)					,		rng=random.Random(SCREAMING_SNAKE_CASE_)).to(SCREAMING_SNAKE_CASE_)
           __a							=    image.cpu().permute(0					,		2					,		3					,		1)[0]
           __a							=    Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE_)).convert('''RGB''')
           if str(SCREAMING_SNAKE_CASE_).startswith('''mps'''):
               __a							=    torch.manual_seed(SCREAMING_SNAKE_CASE_)
           else:
               __a							=    torch.Generator(device=SCREAMING_SNAKE_CASE_).manual_seed(SCREAMING_SNAKE_CASE_)
           __a							=    {
               """prompt""": """A painting of a squirrel eating a burger""",
               """image""": image,
               """generator""": generator,
               """num_inference_steps""": 2,
               """guidance_scale""": 6.0,
               """image_guidance_scale""": 1,
               """output_type""": """numpy""",
           }
           return inputs



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    """cpu"""  # ensure determinism for the device-dependent torch.Generator
           __a							=    self.get_dummy_components()
           __a							=    StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe.to(SCREAMING_SNAKE_CASE_)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

           __a							=    self.get_dummy_inputs(SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[0, -3:, -3:, -1]
           assert image.shape == (1, 32, 32, 3)
           __a							=    np.array([0.75_26, 0.37_50, 0.45_47, 0.61_17, 0.58_66, 0.50_16, 0.43_27, 0.56_42, 0.48_15])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					Any):


           '''simple docstring'''

           __a							=    """cpu"""  # ensure determinism for the device-dependent torch.Generator
           __a							=    self.get_dummy_components()
           __a							=    StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe.to(SCREAMING_SNAKE_CASE_)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

           __a							=    self.get_dummy_inputs(SCREAMING_SNAKE_CASE_)
           __a							=    """french fries"""
           __a							=    sd_pipe(**SCREAMING_SNAKE_CASE_					,		negative_prompt=SCREAMING_SNAKE_CASE_)
           __a							=    output.images
           __a							=    image[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           __a							=    np.array([0.75_11, 0.36_42, 0.45_53, 0.62_36, 0.57_97, 0.50_13, 0.43_43, 0.56_11, 0.48_31])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           __a							=    """cpu"""  # ensure determinism for the device-dependent torch.Generator
           __a							=    self.get_dummy_components()
           __a							=    StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe.to(SCREAMING_SNAKE_CASE_)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

           __a							=    self.get_dummy_inputs(SCREAMING_SNAKE_CASE_)
           __a							=    [inputs["""prompt"""]] * 2

           __a							=    np.array(inputs['''image''']).astype(np.floataa) / 255.0
           __a							=    torch.from_numpy(SCREAMING_SNAKE_CASE_).unsqueeze(0).to(SCREAMING_SNAKE_CASE_)
           __a							=    image / 2 + 0.5
           __a							=    image.permute(0					,		3					,		1					,		2)
           __a							=    image.repeat(2					,		1					,		1					,		1)

           __a							=    sd_pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[-1, -3:, -3:, -1]

           assert image.shape == (2, 32, 32, 3)
           __a							=    np.array([0.58_12, 0.57_48, 0.52_22, 0.59_08, 0.56_95, 0.71_74, 0.68_04, 0.55_23, 0.55_79])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    """cpu"""  # ensure determinism for the device-dependent torch.Generator
           __a							=    self.get_dummy_components()
           __a							=    EulerAncestralDiscreteScheduler(
               beta_start=0.0_00_85					,		beta_end=0.0_12					,		beta_schedule='''scaled_linear''')
           __a							=    StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe.to(SCREAMING_SNAKE_CASE_)
           sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

           __a							=    self.get_dummy_inputs(SCREAMING_SNAKE_CASE_)
           __a							=    sd_pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[0, -3:, -3:, -1]

           __a							=    [round(SCREAMING_SNAKE_CASE_					,		4) for x in image_slice.flatten().tolist()]
           print(''','''.join([str(SCREAMING_SNAKE_CASE_) for x in slice]))

           assert image.shape == (1, 32, 32, 3)
           __a							=    np.array([0.74_17, 0.38_42, 0.47_32, 0.57_76, 0.58_91, 0.51_39, 0.40_52, 0.56_73, 0.49_86])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					Optional[Any]):


           '''simple docstring'''

           super().test_inference_batch_single_identical(expected_max_diff=3E-3)





       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    self.get_dummy_components()
           __a							=    StableDiffusionInstructPixaPixPipeline(**SCREAMING_SNAKE_CASE_)
           __a							=    VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE_					,		do_normalize=SCREAMING_SNAKE_CASE_)
           __a							=    pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)

           __a							=    pipe(**self.get_dummy_inputs_by_type(SCREAMING_SNAKE_CASE_					,		input_image_type='''pt'''))[0]

           __a							=    components["""vae"""]
           __a							=    self.get_dummy_inputs_by_type(SCREAMING_SNAKE_CASE_					,		input_image_type='''pt''')

           for image_param in self.image_latents_params:
               if image_param in inputs.keys():
                   __a							=    vae.encode(inputs[image_param]).latent_dist.mode()

           __a							=    pipe(**SCREAMING_SNAKE_CASE_)[0]

           __a							=    np.abs(out - out_latents_inputs).max()
           self.assertLess(SCREAMING_SNAKE_CASE_					,		1E-4					,		'''passing latents as image input generate different result from passing image''')


@slow
@require_torch_gpu
class    _A	(     unittest.TestCase   ):



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()



       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=0):


           '''simple docstring'''

           __a							=    torch.manual_seed(SCREAMING_SNAKE_CASE_)
           __a							=    load_image(
               '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg''')
           __a							=    {
               """prompt""": """turn him into a cyborg""",
               """image""": image,
               """generator""": generator,
               """num_inference_steps""": 3,
               """guidance_scale""": 7.5,
               """image_guidance_scale""": 1.0,
               """output_type""": """numpy""",
           }
           return inputs



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               '''timbrooks/instruct-pix2pix'''					,		safety_checker=SCREAMING_SNAKE_CASE_)
           pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing()

           __a							=    self.get_inputs()
           __a							=    pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           __a							=    np.array([0.59_02, 0.60_15, 0.60_27, 0.59_83, 0.60_92, 0.60_61, 0.57_65, 0.57_85, 0.55_55])

           assert np.abs(expected_slice - image_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               '''timbrooks/instruct-pix2pix'''					,		safety_checker=SCREAMING_SNAKE_CASE_)
           __a							=    LMSDiscreteScheduler.from_config(pipe.scheduler.config)
           pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing()

           __a							=    self.get_inputs()
           __a							=    pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           __a							=    np.array([0.65_78, 0.68_17, 0.69_72, 0.67_61, 0.68_56, 0.69_16, 0.64_28, 0.65_16, 0.63_01])

           assert np.abs(expected_slice - image_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					str):


           '''simple docstring'''

           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               '''timbrooks/instruct-pix2pix'''					,		safety_checker=SCREAMING_SNAKE_CASE_)
           __a							=    DDIMScheduler.from_config(pipe.scheduler.config)
           pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing()

           __a							=    self.get_inputs()
           __a							=    pipe(**SCREAMING_SNAKE_CASE_).images
           __a							=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           __a							=    np.array([0.38_28, 0.38_34, 0.38_18, 0.37_92, 0.38_65, 0.37_52, 0.37_92, 0.38_47, 0.37_53])

           assert np.abs(expected_slice - image_slice).max() < 1E-3



       def 		_lowerCamelCase      (		self							:					List[str]):


           '''simple docstring'''

           __a							=    0

           def callback_fn(__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					str) -> None:
               __a							=    True
               nonlocal number_of_steps
               number_of_steps += 1
               if step == 1:
                   __a							=    latents.detach().cpu().numpy()
                   assert latents.shape == (1, 4, 64, 64)
                   __a							=    latents[0, -3:, -3:, -1]
                   __a							=    np.array([-0.24_63, -0.46_44, -0.97_56, 1.51_76, 1.44_14, 0.78_66, 0.98_97, 0.85_21, 0.79_83])

                   assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2
               elif step == 2:
                   __a							=    latents.detach().cpu().numpy()
                   assert latents.shape == (1, 4, 64, 64)
                   __a							=    latents[0, -3:, -3:, -1]
                   __a							=    np.array([-0.26_44, -0.46_26, -0.96_53, 1.51_76, 1.45_51, 0.76_86, 0.98_05, 0.84_52, 0.81_15])

                   assert np.abs(latents_slice.flatten() - expected_slice).max() < 5E-2

           __a							=    False

           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               '''timbrooks/instruct-pix2pix'''					,		safety_checker=SCREAMING_SNAKE_CASE_					,		torch_dtype=torch.floataa)
           __a							=    pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing()

           __a							=    self.get_inputs()
           pipe(**SCREAMING_SNAKE_CASE_					,		callback=SCREAMING_SNAKE_CASE_					,		callback_steps=1)
           assert callback_fn.has_been_called
           assert number_of_steps == 3



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           torch.cuda.empty_cache()
           torch.cuda.reset_max_memory_allocated()
           torch.cuda.reset_peak_memory_stats()

           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               '''timbrooks/instruct-pix2pix'''					,		safety_checker=SCREAMING_SNAKE_CASE_					,		torch_dtype=torch.floataa)
           __a							=    pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing(1)
           pipe.enable_sequential_cpu_offload()

           __a							=    self.get_inputs()
           __a							=    pipe(**SCREAMING_SNAKE_CASE_)

           __a							=    torch.cuda.max_memory_allocated()
           # make sure that less than 2.2 GB is allocated
           assert mem_bytes < 2.2 * 10**9





       def 		_lowerCamelCase      (		self							:					Union[str, Any]):


           '''simple docstring'''

           __a							=    self.get_inputs()
           # resize to resolution that is divisible by 8 but not 16 or 32
           __a							=    inputs["""image"""].resize((504, 504))

           __a							=    """timbrooks/instruct-pix2pix"""
           __a							=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               SCREAMING_SNAKE_CASE_					,		safety_checker=SCREAMING_SNAKE_CASE_					,		)
           pipe.to(SCREAMING_SNAKE_CASE_)
           pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_)
           pipe.enable_attention_slicing()

           __a							=    pipe(**SCREAMING_SNAKE_CASE_)
           __a							=    output.images[0]

           __a							=    image[255:258, 383:386, -1]

           assert image.shape == (504, 504, 3)
           __a							=    np.array([0.27_26, 0.25_29, 0.26_64, 0.26_55, 0.26_41, 0.26_42, 0.25_91, 0.26_49, 0.25_90])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 5E-3










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    _A	(     __UpperCAmelCase   ):



       def __init__(		self							:					Dict					,		__SCREAMING_SNAKE_CASE							:					NestedDataStructureLike[PathLike]					,		__SCREAMING_SNAKE_CASE							:					Optional[NamedSplit] = None					,		__SCREAMING_SNAKE_CASE							:					Optional[Features] = None					,		__SCREAMING_SNAKE_CASE							:					str = None					,		__SCREAMING_SNAKE_CASE							:					bool = False					,		__SCREAMING_SNAKE_CASE							:					bool = False					,		__SCREAMING_SNAKE_CASE							:					Optional[str] = None					,		__SCREAMING_SNAKE_CASE							:					Optional[int] = None					,		**__SCREAMING_SNAKE_CASE							:					Any					,		):


           '''simple docstring'''

           super().__init__(
               __SCREAMING_SNAKE_CASE					,		split=__SCREAMING_SNAKE_CASE					,		features=__SCREAMING_SNAKE_CASE					,		cache_dir=__SCREAMING_SNAKE_CASE					,		keep_in_memory=__SCREAMING_SNAKE_CASE					,		streaming=__SCREAMING_SNAKE_CASE					,		num_proc=__SCREAMING_SNAKE_CASE					,		**__SCREAMING_SNAKE_CASE					,		)
           __a							=    field
           __a							=    path_or_paths if isinstance(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE) else {self.split: path_or_paths}
           __a							=    Json(
               cache_dir=__SCREAMING_SNAKE_CASE					,		data_files=__SCREAMING_SNAKE_CASE					,		features=__SCREAMING_SNAKE_CASE					,		field=__SCREAMING_SNAKE_CASE					,		**__SCREAMING_SNAKE_CASE					,		)





       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           if self.streaming:
               __a							=    self.builder.as_streaming_dataset(split=self.split)
           # Build regular (map-style) dataset
           else:
               __a							=    None
               __a							=    None
               __a							=    None
               __a							=    None

               self.builder.download_and_prepare(
                   download_config=__SCREAMING_SNAKE_CASE					,		download_mode=__SCREAMING_SNAKE_CASE					,		verification_mode=__SCREAMING_SNAKE_CASE					,		base_path=__SCREAMING_SNAKE_CASE					,		num_proc=self.num_proc					,		)
               __a							=    self.builder.as_dataset(
                   split=self.split					,		verification_mode=__SCREAMING_SNAKE_CASE					,		in_memory=self.keep_in_memory)
           return dataset


class    _A	:



       def __init__(		self							:					Any					,		__SCREAMING_SNAKE_CASE							:					Dataset					,		__SCREAMING_SNAKE_CASE							:					Union[PathLike, BinaryIO]					,		__SCREAMING_SNAKE_CASE							:					Optional[int] = None					,		__SCREAMING_SNAKE_CASE							:					Optional[int] = None					,		**__SCREAMING_SNAKE_CASE							:					List[str]					,		):


           '''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							=    path_or_buf
           __a							=    batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
           __a							=    num_proc
           __a							=    '''utf-8'''
           __a							=    to_json_kwargs



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    self.to_json_kwargs.pop('''path_or_buf'''					,		__SCREAMING_SNAKE_CASE)
           __a							=    self.to_json_kwargs.pop('''orient'''					,		'''records''')
           __a							=    self.to_json_kwargs.pop('''lines'''					,		True if orient == '''records''' else False)
           __a							=    self.to_json_kwargs.pop('''index'''					,		False if orient in ['''split''', '''table'''] else True)
           __a							=    self.to_json_kwargs.pop('''compression'''					,		__SCREAMING_SNAKE_CASE)

           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=__SCREAMING_SNAKE_CASE) as buffer:
                   __a							=    self._write(file_obj=__SCREAMING_SNAKE_CASE					,		orient=__SCREAMING_SNAKE_CASE					,		lines=__SCREAMING_SNAKE_CASE					,		index=__SCREAMING_SNAKE_CASE					,		**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							=    self._write(
                   file_obj=self.path_or_buf					,		orient=__SCREAMING_SNAKE_CASE					,		lines=__SCREAMING_SNAKE_CASE					,		index=__SCREAMING_SNAKE_CASE					,		**self.to_json_kwargs)
           return written



       def 		_lowerCamelCase      (		self							:					Dict					,		__SCREAMING_SNAKE_CASE							:					str):


           '''simple docstring'''

           __a	,       __a	,       __a	,       __a	,       __a							=    args

           __a							=    query_table(
               table=self.dataset.data					,		key=slice(__SCREAMING_SNAKE_CASE					,		offset + self.batch_size)					,		indices=self.dataset._indices					,		)
           __a							=    batch.to_pandas().to_json(
               path_or_buf=__SCREAMING_SNAKE_CASE					,		orient=__SCREAMING_SNAKE_CASE					,		lines=__SCREAMING_SNAKE_CASE					,		index=__SCREAMING_SNAKE_CASE					,		**__SCREAMING_SNAKE_CASE)
           if not json_str.endswith('''\n'''):
               json_str += "\n"
           return json_str.encode(self.encoding)





       def 		_lowerCamelCase      (		self							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					BinaryIO					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					List[Any]					,		**__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		):


           '''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 json from Arrow format'''					,		):
                   __a							=    self._batch_json((offset, orient, lines, index, to_json_kwargs))
                   written += file_obj.write(__SCREAMING_SNAKE_CASE)
           else:
               __a	,       __a							=    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					,		__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE)]					,		)					,		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(__SCREAMING_SNAKE_CASE)

           return written





