
# 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       _snake_case   (	UpperCAmelCase_							):
 __lowerCAmelCase  :     Any        =			'dandelin/vilt-b32-finetuned-vqa'
 __lowerCAmelCase  :     Union[str, Any]        =			(
     '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.'
 )
 __lowerCAmelCase  :     str        =			'image_qa'
 __lowerCAmelCase  :     int        =			AutoProcessor
 __lowerCAmelCase  :     Optional[Any]        =			AutoModelForVisualQuestionAnswering

 __lowerCAmelCase  :     str        =			['image', 'text']
 __lowerCAmelCase  :     Optional[int]        =			['text']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




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



 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Dict    		=      outputs.argmax(-1).item()
   return self.model.config.idalabel[idx]








from __future__ import annotations

from collections.abc import Iterator
from typing import Any



class       _snake_case   :


 def __init__(					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Any    		=      data
   lowercase__  :   Node | None    		=      None



class       _snake_case   :


 def __init__(					self):


   '''simple docstring'''




   lowercase__  :   List[Any]    		=      None
   lowercase__  :   int    		=      None


 def __iter__(					self):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      self.head
   while self.head:
     yield node.data
     lowercase__  :   str    		=      node.next
     if node == self.head:
       break


 def __len__(					self):


   '''simple docstring'''




   return sum(1 for _ in self)


 def __repr__(					self):


   '''simple docstring'''




   return "->".join(str(SCREAMING_SNAKE_CASE_) for item in iter(self))


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.insert_nth(len(self)					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.insert_nth(0					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   if index < 0 or index > len(self):
     raise IndexError("""list index out of range.""")
   lowercase__  :   Optional[int]    		=      Node(SCREAMING_SNAKE_CASE_)
   if self.head is None:
     lowercase__  :   Union[str, Any]    		=      new_node  # first node points itself
     lowercase__  :   Dict    		=      new_node
   elif index == 0:  # insert at head
     lowercase__  :   int    		=      self.head
     lowercase__  :   int    		=      new_node
   else:
     lowercase__  :   List[str]    		=      self.head
     for _ in range(index - 1):
       lowercase__  :   Union[str, Any]    		=      temp.next
     lowercase__  :   int    		=      temp.next
     lowercase__  :   Optional[Any]    		=      new_node
     if index == len(self) - 1:  # insert at tail
       lowercase__  :   Union[str, Any]    		=      new_node


 def      lowercase__     (					self):


   '''simple docstring'''




   return self.delete_nth(0)


 def      lowercase__     (					self):


   '''simple docstring'''




   return self.delete_nth(len(self) - 1)


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_ = 0):


   '''simple docstring'''




   if not 0 <= index < len(self):
     raise IndexError("""list index out of range.""")
   lowercase__  :   str    		=      self.head
   if self.head == self.tail:  # just one node
     lowercase__  :   List[Any]    		=      None
   elif index == 0:  # delete head node
     lowercase__  :   str    		=      self.tail.next.next
     lowercase__  :   List[Any]    		=      self.head.next
   else:
     lowercase__  :   Optional[int]    		=      self.head
     for _ in range(index - 1):
       lowercase__  :   List[Any]    		=      temp.next
     lowercase__  :   Optional[Any]    		=      temp.next
     lowercase__  :   Dict    		=      temp.next.next
     if index == len(self) - 1:  # delete at tail
       lowercase__  :   Tuple    		=      temp
   return delete_node.data



 def      lowercase__     (					self):


   '''simple docstring'''




   return len(self) == 0

def 		UpperCamelCase	(      )		->	None:


  '''simple docstring'''






  lowercase__  :   int    		=      CircularLinkedList()
  assert len(lowercase_   ) == 0
  assert circular_linked_list.is_empty() is True
  assert str(lowercase_   ) == ""

  try:
    circular_linked_list.delete_front()
    raise AssertionError  # This should not happen
  except IndexError:
    assert True  # This should happen

  try:
    circular_linked_list.delete_tail()
    raise AssertionError  # This should not happen
  except IndexError:
    assert True  # This should happen

  try:
    circular_linked_list.delete_nth(-1   )
    raise AssertionError
  except IndexError:
    assert True

  try:
    circular_linked_list.delete_nth(0   )
    raise AssertionError
  except IndexError:
    assert True

  assert circular_linked_list.is_empty() is True
  for i in range(5   ):
    assert len(lowercase_   ) == i
    circular_linked_list.insert_nth(lowercase_ , i + 1   )
  assert str(lowercase_   ) == "->".join(str(lowercase_   ) for i in range(1 , 6   )   )

  circular_linked_list.insert_tail(6   )
  assert str(lowercase_   ) == "->".join(str(lowercase_   ) for i in range(1 , 7   )   )
  circular_linked_list.insert_head(0   )
  assert str(lowercase_   ) == "->".join(str(lowercase_   ) for i in range(0 , 7   )   )

  assert circular_linked_list.delete_front() == 0
  assert circular_linked_list.delete_tail() == 6
  assert str(lowercase_   ) == "->".join(str(lowercase_   ) for i in range(1 , 6   )   )
  assert circular_linked_list.delete_nth(2   ) == 3

  circular_linked_list.insert_nth(2 , 3   )
  assert str(lowercase_   ) == "->".join(str(lowercase_   ) for i in range(1 , 6   )   )

  assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
     import doctest

     doctest.testmod()






