


"""simple docstring"""







import itertools
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import pyarrow as pa

import datasets
from datasets.table import table_cast


@dataclass
class       SCREAMING_SNAKE_CASE__				(		datasets.BuilderConfig				):
       """simple docstring"""


       a  :				Optional[datasets.Features]   =None


class       SCREAMING_SNAKE_CASE__				(		datasets.ArrowBasedBuilder				):
       """simple docstring"""


       a  :				Tuple   =PandasConfig

       def        lowercase__							(						self						):






         """simple docstring"""





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

       def        lowercase__							(						self		,     snake_case__						):






         """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}"""						)
         lowerCAmelCase     :     Any								=							dl_manager.download_and_extract(self.config.data_files						)
         if isinstance(snake_case__		,     (str, list, tuple)						):
           lowerCAmelCase     :     List[Any]								=							data_files
           if isinstance(snake_case__		,     snake_case__						):
             lowerCAmelCase     :     Dict								=							[files]
           # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
           lowerCAmelCase     :     List[str]								=							[dl_manager.iter_files(snake_case__						) for file in files]
           return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,     gen_kwargs={"files": files}						)]
         lowerCAmelCase     :     Optional[Any]								=							[]
         for split_name, files in data_files.items():
           if isinstance(snake_case__		,     snake_case__						):
             lowerCAmelCase     :     Optional[int]								=							[files]
           # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
           lowerCAmelCase     :     List[Any]								=							[dl_manager.iter_files(snake_case__						) for file in files]
           splits.append(datasets.SplitGenerator(name=snake_case__		,     gen_kwargs={"files": files}						)						)
         return splits

       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         if self.config.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
           lowerCAmelCase     :     Union[str, Any]								=							table_cast(snake_case__		,     self.config.features.arrow_schema						)
         return pa_table







       def        lowercase__							(						self		,     snake_case__						):






         """simple docstring"""





         for i, file in enumerate(itertools.chain.from_iterable(snake_case__						)						):
           with open(snake_case__		,     "rb"						) as f:
             lowerCAmelCase     :     int								=							pa.Table.from_pandas(pd.read_pickle(snake_case__						)						)
             yield i, self._cast_table(snake_case__						)







"""simple docstring"""





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
							from PIL import Image
class 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  Tuple						   =		'''dandelin/vilt-b32-finetuned-vqa'''
		__lowercase							:  str						   =		(
		    '''This is a tool that answers a question about an image. It takes an input named `image` which should be the '''
		    '''image containing the information, as well as a `question` which should be the question in English. It '''
		    '''returns a text that is the answer to the question.'''
		)
		__lowercase							:  Optional[int]						   =		'''image_qa'''
		__lowercase							:  Union[str, Any]						   =		AutoProcessor
		__lowercase							:  Optional[Any]						   =		AutoModelForVisualQuestionAnswering

		__lowercase							:  List[str]						   =		['''image''', '''text''']
		__lowercase							:  Optional[Any]						   =		['''text''']




		def __init__(     self							,  *lowerCAmelCase__							,  **lowerCAmelCase__):
				requires_backends(self							,  ["""vision"""])
				super().__init__(*lowerCAmelCase__							,  **lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__):
				return self.pre_processor(lowerCAmelCase__							,  lowerCAmelCase__							,  return_tensors="""pt""")




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				with torch.no_grad():
						return self.model(**lowerCAmelCase__).logits




		def 	snake_case_     (     self							,  lowerCAmelCase__):
				__SCREAMING_SNAKE_CASE            = outputs.argmax(-1).item()
				return self.model.config.idalabel[idx]




