


'''simple docstring'''






import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 		a__    :





									'''simple docstring'''






									@staticmethod
									def   __SCREAMING_SNAKE_CASE							(		*lowerCamelCase_ ,   **lowerCamelCase_       )	->							int:
																pass




@is_pipeline_test
@require_torch
@require_vision
class 		a__    (							unittest.TestCase  ):





							'''simple docstring'''






							lowercase__      :       List[str]     						=				MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_       )	->							str:
														lowerCAmelCase__	       =     pipeline('''visual-question-answering''' ,   model='''hf-internal-testing/tiny-vilt-random-vqa'''       )
														lowerCAmelCase__	       =     [
														    {
														        '''image''': Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       ),
														        '''question''': '''How many cats are there?''',
														    },
														    {
														        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
														        '''question''': '''How many cats are there?''',
														    },
														]
														return vqa_pipeline, examples







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_       )	->							int:
														lowerCAmelCase__	       =     vqa_pipeline(lowerCamelCase_ ,   top_k=1       )
														self.assertEqual(
														    lowerCamelCase_ ,   [
														        [{'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}],
														        [{'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}],
														    ] ,   )







							@require_torch
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Dict:
														lowerCAmelCase__	       =     pipeline('''visual-question-answering''' ,   model='''hf-internal-testing/tiny-vilt-random-vqa'''       )
														lowerCAmelCase__	       =     '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
														lowerCAmelCase__	       =     '''How many cats are there?'''

														lowerCAmelCase__	       =     vqa_pipeline(image=lowerCamelCase_ ,   question='''How many cats are there?''' ,   top_k=2       )
														self.assertEqual(
														    lowerCamelCase_ ,   [{'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}, {'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}]       )

														lowerCAmelCase__	       =     vqa_pipeline({'''image''': image, '''question''': question} ,   top_k=2       )
														self.assertEqual(
														    lowerCamelCase_ ,   [{'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}, {'''score''': ANY(lowerCamelCase_       ), '''answer''': ANY(lowerCamelCase_       )}]       )







							@slow
							@require_torch
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Tuple:
														lowerCAmelCase__	       =     pipeline('''visual-question-answering''' ,   model='''dandelin/vilt-b32-finetuned-vqa'''       )
														lowerCAmelCase__	       =     '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
														lowerCAmelCase__	       =     '''How many cats are there?'''

														lowerCAmelCase__	       =     vqa_pipeline(image=lowerCamelCase_ ,   question=lowerCamelCase_ ,   top_k=2       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_ ,   decimals=4       ) ,   [{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]       )

														lowerCAmelCase__	       =     vqa_pipeline({'''image''': image, '''question''': question} ,   top_k=2       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_ ,   decimals=4       ) ,   [{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]       )

														lowerCAmelCase__	       =     vqa_pipeline(
														    [{'''image''': image, '''question''': question}, {'''image''': image, '''question''': question}] ,   top_k=2       )
														self.assertEqual(
														    nested_simplify(lowerCamelCase_ ,   decimals=4       ) ,   [[{'''score''': 0.8_799, '''answer''': '''2'''}, {'''score''': 0.296, '''answer''': '''1'''}]] * 2 ,   )







							@require_tf
							@unittest.skip('''Visual question answering not implemented in TF'''       )
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														pass



'''simple docstring'''






import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Dict     						=				""
							lowercase__      :       str     						=				(
							    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
							)
							lowercase__      :       str     						=				None  # compression type in fsspec. ex: "gzip"
							lowercase__      :       str     						=				None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz







							def __init__(		self ,   lowerCamelCase_ = "" ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   **lowerCamelCase_       )	->							Any:
														super().__init__(self ,   **lowerCamelCase_       )
														# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
														lowerCAmelCase__	       =     fsspec.open(
														    lowerCamelCase_ ,   mode='''rb''' ,   protocol=lowerCamelCase_ ,   compression=self.compression ,   client_kwargs={
														        '''requote_redirect_url''': False,  # see https://github.com/huggingface/datasets/pull/5459
														        '''trust_env''': True,  # Enable reading proxy env variables.
														        **(target_options or {}).pop('''client_kwargs''' ,   {}       ),  # To avoid issues if it was already passed.
														    } ,   **(target_options or {}) ,   )
														lowerCAmelCase__	       =     os.path.basename(self.file.path.split('''::'''       )[0]       )
														lowerCAmelCase__	       =     (
														    self.compressed_name[: self.compressed_name.rindex('''.'''       )]
														    if '''.''' in self.compressed_name
														    else self.compressed_name
														)
														lowerCAmelCase__	       =     None







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls ,   lowerCamelCase_       )	->							Any:
														# compressed file paths are always relative to the archive root
														return super()._strip_protocol(lowerCamelCase_       ).lstrip('''/'''       )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														if self.dir_cache is None:
																					lowerCAmelCase__	       =     {**self.file.fs.info(self.file.path       ), '''name''': self.uncompressed_name}
																					lowerCAmelCase__	       =     {f['''name''']: f}







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_       )	->							List[Any]:
														return self.file.open().read()







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = "rb" ,   lowerCamelCase_=None ,   lowerCamelCase_=True ,   lowerCamelCase_=None ,   **lowerCamelCase_ ,   )	->							List[str]:
														lowerCAmelCase__	       =     self._strip_protocol(lowerCamelCase_       )
														if mode != "rb":
																					raise ValueError(F"""Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'"""       )
														return self.file.open()




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[Any]     						=				"bz2"
							lowercase__      :       str     						=				"bz2"
							lowercase__      :       Optional[int]     						=				".bz2"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Any     						=				"gzip"
							lowercase__      :       int     						=				"gzip"
							lowercase__      :       int     						=				".gz"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Tuple     						=				"lz4"
							lowercase__      :       Optional[Any]     						=				"lz4"
							lowercase__      :       int     						=				".lz4"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[int]     						=				"xz"
							lowercase__      :       str     						=				"xz"
							lowercase__      :       List[Any]     						=				".xz"




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Union[str, Any]     						=				"zstd"
							lowercase__      :       Union[str, Any]     						=				"zstd"
							lowercase__      :       Dict     						=				".zst"







							def __init__(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = "rb" ,   lowerCamelCase_ = None ,   lowerCamelCase_ = None ,   lowerCamelCase_ = DEFAULT_BLOCK_SIZE ,   **lowerCamelCase_ ,   )	->							List[Any]:
														super().__init__(
														    fo=lowerCamelCase_ ,   mode=lowerCamelCase_ ,   target_protocol=lowerCamelCase_ ,   target_options=lowerCamelCase_ ,   block_size=lowerCamelCase_ ,   **lowerCamelCase_ ,   )
														# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
														#
														# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
														#     out.close = close
														# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
														#
														# see https://github.com/intake/filesystem_spec/issues/725
														lowerCAmelCase__	       =     self.file.__enter__




														class 		a__    :





																					'''simple docstring'''






																					def __init__(		self ,   lowerCamelCase_       )	->							List[str]:
																												lowerCAmelCase__	       =     file_







																					def __enter__(		self       )	->							Tuple:
																												self._file.__enter__()
																												return self







																					def __exit__(		self ,   *lowerCamelCase_ ,   **lowerCamelCase_       )	->							str:
																												self._file.__exit__(*lowerCamelCase_ ,   **lowerCamelCase_       )







																					def __iter__(		self       )	->							Any:
																												return iter(self._file       )







																					def   __SCREAMING_SNAKE_CASE							(		self       )	->							Union[str, Any]:
																												return next(self._file       )







																					def __getattr__(		self ,   lowerCamelCase_       )	->							str:
																												return getattr(self._file ,   lowerCamelCase_       )

														def fixed_enter(*lowerCamelCase_ ,   **lowerCamelCase_       ):
																					return WrappedFile(_enter(*lowerCamelCase_ ,   **lowerCamelCase_       )       )

														lowerCAmelCase__	       =     fixed_enter