


# 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      lowerCAmelCase__       (				a):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE  			= "dandelin/vilt-b32-finetuned-vqa"
     __SCREAMING_SNAKE_CASE  			= (
         "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."
     )
     __SCREAMING_SNAKE_CASE  			= "image_qa"
     __SCREAMING_SNAKE_CASE  			= AutoProcessor
     __SCREAMING_SNAKE_CASE  			= AutoModelForVisualQuestionAnswering

     __SCREAMING_SNAKE_CASE  			= ["image", "text"]
     __SCREAMING_SNAKE_CASE  			= ["text"]


     def __init__(						self ,						*__lowerCamelCase ,						**__lowerCamelCase)			->				int:
            requires_backends(self ,						["vision"])
            super().__init__(*__lowerCamelCase ,						**__lowerCamelCase)


     def  _lowerCamelCase (						self ,						__lowerCamelCase ,						__lowerCamelCase)			->				Tuple:
            return self.pre_processor(__lowerCamelCase ,						__lowerCamelCase ,						return_tensors="pt")


     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				List[Any]:
            with torch.no_grad():
                   return self.model(**__lowerCamelCase).logits


     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				List[Any]:
            _A      :       str				    = outputs.argmax(-1).item()
            return self.model.config.idalabel[idx]







"""simple docstring"""
import pytest

from datasets.splits import SplitDict, SplitInfo
from datasets.utils.py_utils import asdict



@pytest.mark.parametrize(
    '''split_dict''' ,   [
        SplitDict(),
        SplitDict({'''train''': SplitInfo(name='''train''' ,   num_bytes=13_37 ,   num_examples=42 ,   dataset_name='''my_dataset'''							)}							),
        SplitDict({'''train''': SplitInfo(name='''train''' ,   num_bytes=13_37 ,   num_examples=42							)}							),
        SplitDict({'''train''': SplitInfo()}							),
    ] ,   )
def      __lowerCamelCase   (							a_     :		SplitDict							)			->      Optional[int]:
      __SCREAMING_SNAKE_CASE   :Tuple						=  split_dict._to_yaml_list()
      assert len(a_							) == len(a_							)
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  SplitDict._from_yaml_list(a_							)
      for split_name, split_info in split_dict.items():
            # dataset_name field is deprecated, and is therefore not part of the YAML dump
            __SCREAMING_SNAKE_CASE   :int						=  None
            # the split name of split_dict takes over the name of the split info object
            __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  split_name
      assert split_dict == reloaded







@pytest.mark.parametrize(
    '''split_info''' ,   [SplitInfo(), SplitInfo(dataset_name=a_							), SplitInfo(dataset_name='''my_dataset'''							)]							)
def      __lowerCamelCase   (							a_     :		Dict							)			->      Any:
      # For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name"
      # field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  asdict(SplitDict({'''train''': split_info}							)							)
      assert "dataset_name" in split_dict_asdict["train"]
      assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name