




'''simple docstring'''


import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


_snake_case      :      Dict   							=		datasets.utils.logging.get_logger(__name__)

@dataclass
class 			A    (		datasets.BuilderConfig    ):
							lowercase_							=				None



class 			A    (		datasets.ArrowBasedBuilder    ):
							lowercase_							=				ArrowConfig







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Tuple:


														"""simple docstring"""


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







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Dict					)			->							Optional[int]:


														"""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}'					)
														_a	     =				dl_manager.download_and_extract(self.config.data_files					)
														if isinstance(lowerCAmelCase_							,			(str, list, tuple)					):
																					_a	     =				data_files
																					if isinstance(lowerCAmelCase_							,			lowerCAmelCase_					):
																												_a	     =				[files]
																					# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
																					_a	     =				[dl_manager.iter_files(lowerCAmelCase_					) for file in files]
																					return [datasets.SplitGenerator(name=datasets.Split.TRAIN							,			gen_kwargs={'''files''': files}					)]
														_a	     =				[]
														for split_name, files in data_files.items():
																					if isinstance(lowerCAmelCase_							,			lowerCAmelCase_					):
																												_a	     =				[files]
																					# Use `dl_manager.iter_files` to skip hidden files in an extracted archive
																					_a	     =				[dl_manager.iter_files(lowerCAmelCase_					) for file in files]
																					# Infer features is they are stoed in the arrow schema
																					if self.info.features is None:
																												for file in itertools.chain.from_iterable(lowerCAmelCase_					):
																																			with open(lowerCAmelCase_							,			'''rb'''					) as f:
																																										_a	     =				datasets.Features.from_arrow_schema(pa.ipc.open_stream(lowerCAmelCase_					).schema					)
																																			break
																					splits.append(datasets.SplitGenerator(name=lowerCAmelCase_							,			gen_kwargs={'''files''': files}					)					)
														return splits







							def    __lowerCAmelCase			(  self       :  Any							,			lowerCAmelCase_       :  pa.Table					)			->							pa.Table:


														"""simple docstring"""


														if self.info.features is not None:
																					# more expensive cast to support nested features with keys in a different order
																					# allows str <-> int/float or str to Audio for example
																					_a	     =				table_cast(lowerCAmelCase_							,			self.info.features.arrow_schema					)
														return pa_table







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Union[str, Any]					)			->							List[str]:


														"""simple docstring"""


														for file_idx, file in enumerate(itertools.chain.from_iterable(lowerCAmelCase_					)					):
																					with open(lowerCAmelCase_							,			'''rb'''					) as f:
																												try:
																																			for batch_idx, record_batch in enumerate(pa.ipc.open_stream(lowerCAmelCase_					)					):
																																										_a	     =				pa.Table.from_batches([record_batch]					)
																																										# 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 F'{file_idx}_{batch_idx}', self._cast_table(lowerCAmelCase_					)
																												except ValueError as e:
																																			logger.error(F'Failed to read file \'{file}\' with error {type(lowerCAmelCase_					)}: {e}'					)
																																			raise







'''simple docstring'''


import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
				from PIL import Image

class 			A    (		unittest.TestCase     ,_a    ):







							def    __lowerCAmelCase			(  self       :  Optional[int]					)			->							Optional[int]:


														"""simple docstring"""


														_a	     =				load_tool('''image-question-answering'''					)
														self.tool.setup()
														_a	     =				load_tool('''image-question-answering'''							,			remote=lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Optional[int]:


														"""simple docstring"""


														_a	     =				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''					)					) / '''000000039769.png'''					)
														_a	     =				self.tool(lowerCAmelCase_							,			'''How many cats are sleeping on the couch?'''					)
														self.assertEqual(lowerCAmelCase_							,			'''2'''					)







							def    __lowerCAmelCase			(  self       :  List[str]					)			->							Union[str, Any]:


														"""simple docstring"""


														_a	     =				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''					)					) / '''000000039769.png'''					)
														_a	     =				self.remote_tool(lowerCAmelCase_							,			'''How many cats are sleeping on the couch?'''					)
														self.assertEqual(lowerCAmelCase_							,			'''2'''					)







							def    __lowerCAmelCase			(  self       :  Dict					)			->							Tuple:


														"""simple docstring"""


														_a	     =				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''					)					) / '''000000039769.png'''					)
														_a	     =				self.tool(image=lowerCAmelCase_							,			question='''How many cats are sleeping on the couch?'''					)
														self.assertEqual(lowerCAmelCase_							,			'''2'''					)







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Dict:


														"""simple docstring"""


														_a	     =				Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''					)					) / '''000000039769.png'''					)
														_a	     =				self.remote_tool(image=lowerCAmelCase_							,			question='''How many cats are sleeping on the couch?'''					)
														self.assertEqual(lowerCAmelCase_							,			'''2'''					)

