




'''simple docstring'''







import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


_lowercase       						=			'http://www.mocksite.com/file1.txt'
_lowercase       						=			'"text": ["foo", "foo"]'
_lowercase       						=			'6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'
class 			_lowercase						:
     _UpperCAmelCase										=					200
     _UpperCAmelCase										=					{'''Content-Length''': '''100'''}
     _UpperCAmelCase										=					{}







     def     UpperCamelCase			( self    ,   **A__    ) ->	str:
      return [bytes(A__    ,   '''utf-8'''    )]




def    __UpperCamelCase	(	*a    : List[str]					,   **a    : List[str]     ) ->List[Any]:
 return MockResponse()

@pytest.mark.parametrize('''urls_type'''					,   [str, list, dict]     )
def    __UpperCamelCase	(	a    : Tuple					,   a    : int					,   a    : List[str]     ) ->Optional[Any]:
 import requests

 monkeypatch.setattr(a					,   '''request'''					,   a     )

 snake_case				=			URL
 if issubclass(a					,   a     ):
  snake_case				=			url
 elif issubclass(a					,   a     ):
  snake_case				=			[url]
 elif issubclass(a					,   a     ):
  snake_case				=			{'''train''': url}
 snake_case				=			'''dummy'''
 snake_case				=			'''downloads'''
 snake_case				=			tmp_path
 snake_case				=			DownloadConfig(
     cache_dir=os.path.join(a					,   a     )					,   use_etag=a					,   )
 snake_case				=			DownloadManager(dataset_name=a					,   download_config=a     )
 snake_case				=			dl_manager.download(a     )
 snake_case				=			urls
 for downloaded_paths in [downloaded_paths]:
  if isinstance(a					,   a     ):
   snake_case				=			[downloaded_paths]
   snake_case				=			[urls]
  elif isinstance(a					,   a     ):
   assert "train" in downloaded_paths.keys()
   snake_case				=			downloaded_paths.values()
   snake_case				=			urls.values()
  assert downloaded_paths
  for downloaded_path, input_url in zip(a					,   a     ):
   assert downloaded_path == dl_manager.downloaded_paths[input_url]
   snake_case				=			Path(a     )
   snake_case				=			downloaded_path.parts
   assert parts[-1] == HASH
   assert parts[-2] == cache_subdir
   assert downloaded_path.exists()
   snake_case				=			downloaded_path.read_text()
   assert content == CONTENT
   snake_case				=			downloaded_path.with_suffix('''.json'''     )
   assert metadata_downloaded_path.exists()
   snake_case				=			json.loads(metadata_downloaded_path.read_text()     )
   assert metadata_content == {"url": URL, "etag": None}

@pytest.mark.parametrize('''paths_type'''					,   [str, list, dict]     )
def    __UpperCamelCase	(	a    : List[str]					,   a    : Optional[Any]					,   a    : Optional[int]     ) ->Tuple:
 snake_case				=			str(a     )
 if issubclass(a					,   a     ):
  snake_case				=			filename
 elif issubclass(a					,   a     ):
  snake_case				=			[filename]
 elif issubclass(a					,   a     ):
  snake_case				=			{'''train''': filename}
 snake_case				=			'''dummy'''
 snake_case				=			xz_file.parent
 snake_case				=			'''extracted'''
 snake_case				=			DownloadConfig(
     cache_dir=a					,   use_etag=a					,   )
 snake_case				=			DownloadManager(dataset_name=a					,   download_config=a     )
 snake_case				=			dl_manager.extract(a     )
 snake_case				=			paths
 for extracted_paths in [extracted_paths]:
  if isinstance(a					,   a     ):
   snake_case				=			[extracted_paths]
   snake_case				=			[paths]
  elif isinstance(a					,   a     ):
   assert "train" in extracted_paths.keys()
   snake_case				=			extracted_paths.values()
   snake_case				=			paths.values()
  assert extracted_paths
  for extracted_path, input_path in zip(a					,   a     ):
   assert extracted_path == dl_manager.extracted_paths[input_path]
   snake_case				=			Path(a     )
   snake_case				=			extracted_path.parts
   assert parts[-1] == hash_url_to_filename(a					,   etag=a     )
   assert parts[-2] == extracted_subdir
   assert extracted_path.exists()
   snake_case				=			extracted_path.read_text()
   snake_case				=			text_file.read_text()
   assert extracted_file_content == expected_file_content

def    __UpperCamelCase	(	a    : Any					,   a    : Dict     ) ->Tuple:
 assert path.endswith('''.jsonl'''     )
 for num_items, line in enumerate(a					,   start=1     ):
  snake_case				=			json.loads(line.decode('''utf-8'''     )     )
  assert item.keys() == {"col_1", "col_2", "col_3"}
 assert num_items == 4

@pytest.mark.parametrize('''archive_jsonl'''					,   ['''tar_jsonl_path''', '''zip_jsonl_path''']     )
def    __UpperCamelCase	(	a    : Dict					,   a    : Tuple     ) ->List[Any]:
 snake_case				=			request.getfixturevalue(a     )
 snake_case				=			DownloadManager()
 for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(a     )					,   start=1     ):
  _test_jsonl(a					,   a     )
 assert num_jsonl == 2

@pytest.mark.parametrize('''archive_nested_jsonl'''					,   ['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path''']     )
def    __UpperCamelCase	(	a    : Tuple					,   a    : Union[str, Any]     ) ->Any:
 snake_case				=			request.getfixturevalue(a     )
 snake_case				=			DownloadManager()
 for num_tar, (path, file) in enumerate(dl_manager.iter_archive(a     )					,   start=1     ):
  for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(a     )					,   start=1     ):
   _test_jsonl(a					,   a     )
 assert num_tar == 1
 assert num_jsonl == 2






def    __UpperCamelCase	(	a    : List[str]     ) ->Optional[Any]:
 snake_case				=			DownloadManager()
 for num_file, file in enumerate(dl_manager.iter_files(a     )					,   start=1     ):
  assert os.path.basename(a     ) == ("test.txt" if num_file == 1 else "train.txt")
 assert num_file == 2








'''simple docstring'''







import random
import unittest

import torch

from diffusers import IFInpaintingSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_lowercase						(       __a				,		__a				,		unittest.TestCase					):
     _UpperCAmelCase										=					IFInpaintingSuperResolutionPipeline
     _UpperCAmelCase										=					TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {'''width''', '''height'''}
     _UpperCAmelCase										=					TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS.union({'''original_image'''}					)
     _UpperCAmelCase										=					PipelineTesterMixin.required_optional_params - {'''latents'''}







     def     UpperCamelCase			( self    ) ->	int:
      return self._get_superresolution_dummy_components()







     def     UpperCamelCase			( self    ,   A__    ,   A__=0    ) ->	Union[str, Any]:
      if str(A__    ).startswith('''mps'''    ):
       snake_case				=			torch.manual_seed(A__    )
      else:
       snake_case				=			torch.Generator(device=A__    ).manual_seed(A__    )

      snake_case				=			floats_tensor((1, 3, 16, 16)    ,   rng=random.Random(A__    )    ).to(A__    )
      snake_case				=			floats_tensor((1, 3, 32, 32)    ,   rng=random.Random(A__    )    ).to(A__    )
      snake_case				=			floats_tensor((1, 3, 32, 32)    ,   rng=random.Random(A__    )    ).to(A__    )

      snake_case				=			{
          '''prompt''': '''A painting of a squirrel eating a burger''',
          '''image''': image,
          '''original_image''': original_image,
          '''mask_image''': mask_image,
          '''generator''': generator,
          '''num_inference_steps''': 2,
          '''output_type''': '''numpy''',
      }

      return inputs







     @unittest.skipIf(
         torch_device != '''cuda''' or not is_xformers_available()    ,   reason='''XFormers attention is only available with CUDA and `xformers` installed'''    ,   )
     def     UpperCamelCase			( self    ) ->	List[Any]:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      self._test_save_load_optional_components()







     @unittest.skipIf(torch_device != '''cuda'''    ,   reason='''float16 requires CUDA'''    )
     def     UpperCamelCase			( self    ) ->	List[str]:
      # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
      super().test_save_load_floataa(expected_max_diff=1e-1    )







     def     UpperCamelCase			( self    ) ->	int:
      self._test_attention_slicing_forward_pass(expected_max_diff=1e-2    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      self._test_save_load_local()







     def     UpperCamelCase			( self    ) ->	Dict:
      self._test_inference_batch_single_identical(
          expected_max_diff=1e-2    ,   )

