
# 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   UpperCAmelCase		(						A_ ):
   A__					:		List[Any]						     =     "dandelin/vilt-b32-finetuned-vqa"
   A__					:		List[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."
   )
   A__					:		str						     =     "image_qa"
   A__					:		List[str]						     =     AutoProcessor
   A__					:		Dict						     =     AutoModelForVisualQuestionAnswering

   A__					:		List[str]						     =     ["image", "text"]
   A__					:		Union[str, Any]						     =     ["text"]



   def __init__(self		:					int    ,						*snake_case__		:					int    ,						**snake_case__		:					str )		->	List[Any]:


          '''simple docstring'''


          requires_backends(self    ,						["vision"] )
          super().__init__(*snake_case__    ,						**snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple    ,						snake_case__		:					"Image"    ,						snake_case__		:					str )		->	Tuple:


          '''simple docstring'''


          return self.pre_processor(snake_case__    ,						snake_case__    ,						return_tensors="pt" )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple    ,						snake_case__		:					int )		->	Union[str, Any]:


          '''simple docstring'''


          with torch.no_grad():
                 return self.model(**snake_case__ ).logits



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str]    ,						snake_case__		:					List[Any] )		->	List[Any]:


          '''simple docstring'''


          snake_case       :   Optional[int]							=      outputs.argmax(-1 ).item()
          return self.model.config.idalabel[idx]







from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map


def 			UpperCamelCase					(    __lowerCamelCase     : Dict[str, torch.Tensor]    ):
       snake_case       :   List[str]							=      []
       snake_case       :   Optional[int]							=      []
       snake_case       :   Any							=      []

       for rt in rc.restypes:
              snake_case       :   List[Any]							=      rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
              restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]    )
              snake_case       :   str							=      {name: i for i, name in enumerate(__lowerCamelCase    )}
              restype_atomaa_to_atomaa_list.append(
                  [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]    )

              restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]    )

       # Add dummy mapping for restype 'UNK'
       restype_atomaa_to_atomaa_list.append([0] * 14    )
       restype_atomaa_to_atomaa_list.append([0] * 37    )
       restype_atomaa_mask_list.append([0.0] * 14    )

       snake_case       :   Optional[Any]							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.intaa				,					device=protein["aatype"].device				,					)
       snake_case       :   List[Any]							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.intaa				,					device=protein["aatype"].device				,					)
       snake_case       :   int							=      torch.tensor(
           __lowerCamelCase				,					dtype=torch.floataa				,					device=protein["aatype"].device				,					)
       snake_case       :   int							=      protein["aatype"].to(torch.long    )

       # create the mapping for (residx, atom14) --> atom37, i.e. an array
       # with shape (num_res, 14) containing the atom37 indices for this protein
       snake_case       :   List[Any]							=      restype_atomaa_to_atomaa[protein_aatype]
       snake_case       :   str							=      restype_atomaa_mask[protein_aatype]

       snake_case       :   str							=      residx_atomaa_mask
       snake_case       :   Any							=      residx_atomaa_to_atomaa.long()

       # create the gather indices for mapping back
       snake_case       :   List[str]							=      restype_atomaa_to_atomaa[protein_aatype]
       snake_case       :   List[Any]							=      residx_atomaa_to_atomaa.long()

       # create the corresponding mask
       snake_case       :   Union[str, Any]							=      torch.zeros([21, 37]				,					dtype=torch.floataa				,					device=protein["aatype"].device    )
       for restype, restype_letter in enumerate(rc.restypes    ):
              snake_case       :   Optional[int]							=      rc.restype_atoa[restype_letter]
              snake_case       :   Any							=      rc.residue_atoms[restype_name]
              for atom_name in atom_names:
                     snake_case       :   List[Any]							=      rc.atom_order[atom_name]
                     snake_case       :   Optional[Any]							=      1

       snake_case       :   List[Any]							=      restype_atomaa_mask[protein_aatype]
       snake_case       :   int							=      residx_atomaa_mask

       return protein




def 			UpperCamelCase					(    __lowerCamelCase     : Dict[str, torch.Tensor]    ):
       snake_case       :   Dict							=      tree_map(lambda __lowerCamelCase    : torch.tensor(__lowerCamelCase				,					device=batch["aatype"].device    )				,					__lowerCamelCase				,					np.ndarray    )
       snake_case       :   List[str]							=      tensor_tree_map(lambda __lowerCamelCase    : np.array(__lowerCamelCase    )				,					make_atomaa_masks(__lowerCamelCase    )    )
       return out





