
"""simple docstring"""



import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
    from PIL import Image

    from transformers.image_utils import load_image
else:





    class 				a			:



          @staticmethod
          def  __snake_case							(   *UpperCamelCase_     ,	**UpperCamelCase_      ):
              pass




    def 					lowerCamelCase   (     _snake_case ):
        return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
UpperCamelCase__          =	(
    'https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png'
)





@is_pipeline_test
@require_torch
@require_vision
class 				a			(					unittest.TestCase      ):
      UpperCamelCase							:   Dict           =     MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING



      @require_pytesseract
      @require_vision
      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
          UpperCAmelCase__	:      Union[str, Any]     							=					pipeline(
              'document-question-answering'     ,	model=__lowerCAmelCase     ,	tokenizer=__lowerCAmelCase     ,	image_processor=__lowerCAmelCase      )

          UpperCAmelCase__	:      Any     							=					INVOICE_URL
          UpperCAmelCase__	:      List[Any]     							=					list(zip(*apply_tesseract(load_image(__lowerCAmelCase      )     ,	__lowerCAmelCase     ,	''      )      )      )
          UpperCAmelCase__	:      Optional[int]     							=					'What is the placebo?'
          UpperCAmelCase__	:      Optional[Any]     							=					[
              {
                  'image': load_image(__lowerCAmelCase      ),
                  'question': question,
              },
              {
                  'image': image,
                  'question': question,
              },
              {
                  'image': image,
                  'question': question,
                  'word_boxes': word_boxes,
              },
          ]
          return dqa_pipeline, examples



      def  __snake_case							(   self     ,	UpperCamelCase_     ,	UpperCamelCase_      ):
          UpperCAmelCase__	:      List[Any]     							=					dqa_pipeline(__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(
              __lowerCAmelCase     ,	[
                  [
                      {'score': ANY(__lowerCAmelCase      ), 'answer': ANY(__lowerCAmelCase      ), 'start': ANY(__lowerCAmelCase      ), 'end': ANY(__lowerCAmelCase      )},
                      {'score': ANY(__lowerCAmelCase      ), 'answer': ANY(__lowerCAmelCase      ), 'start': ANY(__lowerCAmelCase      ), 'end': ANY(__lowerCAmelCase      )},
                  ]
              ]
              * 3     ,	)



      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      str     							=					pipeline('document-question-answering'     ,	model='hf-internal-testing/tiny-random-layoutlmv2'      )
          UpperCAmelCase__	:      Optional[int]     							=					INVOICE_URL
          UpperCAmelCase__	:      str     							=					'How many cats are there?'

          UpperCAmelCase__	:      List[str]     							=					[
              {'score': 0.0001, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
              {'score': 0.0001, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
          ]
          UpperCAmelCase__	:      int     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	__lowerCAmelCase      )

          UpperCAmelCase__	:      Any     							=					dqa_pipeline({'image': image, 'question': question}     ,	top_k=2      )
          self.assertEqual(nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	__lowerCAmelCase      )

          # This image does not detect ANY text in it, meaning layoutlmv2 should fail.
          # Empty answer probably
          UpperCAmelCase__	:      List[Any]     							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'
          UpperCAmelCase__	:      Any     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(__lowerCAmelCase     ,	[]      )

          # We can optionnally pass directly the words and bounding boxes
          UpperCAmelCase__	:      List[Any]     							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'
          UpperCAmelCase__	:      Dict     							=					[]
          UpperCAmelCase__	:      Optional[int]     							=					[]
          UpperCAmelCase__	:      Optional[Any]     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	words=__lowerCAmelCase     ,	boxes=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(__lowerCAmelCase     ,	[]      )



      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Any     							=					pipeline(
              'document-question-answering'     ,	model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,	revision='9977165'     ,	)
          UpperCAmelCase__	:      Any     							=					INVOICE_URL
          UpperCAmelCase__	:      int     							=					'What is the invoice number?'

          UpperCAmelCase__	:      Optional[int]     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9944, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0009, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)

          UpperCAmelCase__	:      int     							=					dqa_pipeline({'image': image, 'question': question}     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9944, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0009, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)

          UpperCAmelCase__	:      Any     							=					dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  [
                      {'score': 0.9944, 'answer': 'us-001', 'start': 16, 'end': 16},
                      {'score': 0.0009, 'answer': 'us-001', 'start': 16, 'end': 16},
                  ],
              ]
              * 2     ,	)



      @slow
      @require_torch
      @require_detectrona
      @require_pytesseract
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Tuple     							=					pipeline(
              'document-question-answering'     ,	model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,	revision='9977165'     ,	max_seq_len=50     ,	)
          UpperCAmelCase__	:      Optional[int]     							=					INVOICE_URL
          UpperCAmelCase__	:      Optional[int]     							=					'What is the invoice number?'

          UpperCAmelCase__	:      Dict     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9974, 'answer': '1110212019', 'start': 23, 'end': 23},
                  {'score': 0.9948, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)

          UpperCAmelCase__	:      List[Any]     							=					dqa_pipeline({'image': image, 'question': question}     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9974, 'answer': '1110212019', 'start': 23, 'end': 23},
                  {'score': 0.9948, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)

          UpperCAmelCase__	:      Any     							=					dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  [
                      {'score': 0.9974, 'answer': '1110212019', 'start': 23, 'end': 23},
                      {'score': 0.9948, 'answer': 'us-001', 'start': 16, 'end': 16},
                  ]
              ]
              * 2     ,	)



      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Tuple     							=					AutoTokenizer.from_pretrained(
              'impira/layoutlm-document-qa'     ,	revision='3dc6de3'     ,	add_prefix_space=__lowerCAmelCase      )
          UpperCAmelCase__	:      Union[str, Any]     							=					pipeline(
              'document-question-answering'     ,	model='impira/layoutlm-document-qa'     ,	tokenizer=__lowerCAmelCase     ,	revision='3dc6de3'     ,	)
          UpperCAmelCase__	:      int     							=					INVOICE_URL
          UpperCAmelCase__	:      Optional[int]     							=					'What is the invoice number?'

          UpperCAmelCase__	:      int     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
              ]     ,	)

          UpperCAmelCase__	:      int     							=					dqa_pipeline({'image': image, 'question': question}     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
              ]     ,	)

          UpperCAmelCase__	:      List[str]     							=					dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  [
                      {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
                      {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
                  ]
              ]
              * 2     ,	)

          UpperCAmelCase__	:      List[str]     							=					list(zip(*apply_tesseract(load_image(__lowerCAmelCase      )     ,	__lowerCAmelCase     ,	''      )      )      )

          # This model should also work if `image` is set to None
          UpperCAmelCase__	:      Optional[Any]     							=					dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.4251, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.0819, 'answer': '1110212019', 'start': 23, 'end': 23},
              ]     ,	)



      @slow
      @require_torch
      @require_pytesseract
      @require_vision
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Any     							=					AutoTokenizer.from_pretrained(
              'impira/layoutlm-document-qa'     ,	revision='3dc6de3'     ,	add_prefix_space=__lowerCAmelCase      )
          UpperCAmelCase__	:      Dict     							=					pipeline(
              'document-question-answering'     ,	model='impira/layoutlm-document-qa'     ,	tokenizer=__lowerCAmelCase     ,	revision='3dc6de3'     ,	max_seq_len=50     ,	)
          UpperCAmelCase__	:      str     							=					INVOICE_URL
          UpperCAmelCase__	:      Optional[Any]     							=					'What is the invoice number?'

          UpperCAmelCase__	:      Optional[int]     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)

          UpperCAmelCase__	:      Dict     							=					dqa_pipeline(
              [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  [
                      {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                      {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
                  ]
              ]
              * 2     ,	)

          UpperCAmelCase__	:      Tuple     							=					list(zip(*apply_tesseract(load_image(__lowerCAmelCase      )     ,	__lowerCAmelCase     ,	''      )      )      )

          # This model should also work if `image` is set to None
          UpperCAmelCase__	:      List[Any]     							=					dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,	top_k=2      )
          self.assertEqual(
              nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[
                  {'score': 0.9999, 'answer': 'us-001', 'start': 16, 'end': 16},
                  {'score': 0.9998, 'answer': 'us-001', 'start': 16, 'end': 16},
              ]     ,	)



      @slow
      @require_torch
      def  __snake_case							(   self      ):
          UpperCAmelCase__	:      Tuple     							=					pipeline(
              'document-question-answering'     ,	model='naver-clova-ix/donut-base-finetuned-docvqa'     ,	tokenizer=AutoTokenizer.from_pretrained('naver-clova-ix/donut-base-finetuned-docvqa'      )     ,	feature_extractor='naver-clova-ix/donut-base-finetuned-docvqa'     ,	)

          UpperCAmelCase__	:      Optional[int]     							=					INVOICE_URL
          UpperCAmelCase__	:      str     							=					'What is the invoice number?'
          UpperCAmelCase__	:      Tuple     							=					dqa_pipeline(image=__lowerCAmelCase     ,	question=__lowerCAmelCase     ,	top_k=2      )
          self.assertEqual(nested_simplify(__lowerCAmelCase     ,	decimals=4      )     ,	[{'answer': 'us-001'}]      )



      @require_tf
      @unittest.skip('Document question answering not implemented in TF'      )
      def  __snake_case							(   self      ):
          pass


"""simple docstring"""


from __future__ import annotations

import pandas as pd






def      __UpperCAmelCase       (      lowercase ,lowercase ,lowercase		):







  """simple docstring"""
  _UpperCAmelCase    			=   [0] * no_of_processes
  _UpperCAmelCase    			=   [0] * no_of_processes
  # Copy the burst time into remaining_time[]
  for i in range(lowercase		):
    _UpperCAmelCase    			=   burst_time[i]

  _UpperCAmelCase    			=   0
  _UpperCAmelCase    			=   0
  _UpperCAmelCase    			=   9_99_99_99_99
  _UpperCAmelCase    			=   0
  _UpperCAmelCase    			=   False

  # Process until all processes are completed
  while complete != no_of_processes:
    for j in range(lowercase		):
      if arrival_time[j] <= increment_time and remaining_time[j] > 0:
        if remaining_time[j] < minm:
          _UpperCAmelCase    			=   remaining_time[j]
          _UpperCAmelCase    			=   j
          _UpperCAmelCase    			=   True

    if not check:
      increment_time += 1
      continue
    remaining_time[short] -= 1

    _UpperCAmelCase    			=   remaining_time[short]
    if minm == 0:
      _UpperCAmelCase    			=   9_99_99_99_99

    if remaining_time[short] == 0:
      complete += 1
      _UpperCAmelCase    			=   False

      # Find finish time of current process
      _UpperCAmelCase    			=   increment_time + 1

      # Calculate waiting time
      _UpperCAmelCase    			=   finish_time - arrival_time[short]
      _UpperCAmelCase    			=   finar - burst_time[short]

      if waiting_time[short] < 0:
        _UpperCAmelCase    			=   0

        # Increment time
    increment_time += 1
  return waiting_time






def      __UpperCAmelCase       (      lowercase ,lowercase ,lowercase		):







  """simple docstring"""
  _UpperCAmelCase    			=   [0] * no_of_processes
  for i in range(lowercase		):
    _UpperCAmelCase    			=   burst_time[i] + waiting_time[i]
  return turn_around_time






def      __UpperCAmelCase       (      lowercase ,lowercase ,lowercase		):







  """simple docstring"""
  _UpperCAmelCase    			=   0
  _UpperCAmelCase    			=   0
  for i in range(lowercase		):
    _UpperCAmelCase    			=   total_waiting_time + waiting_time[i]
    _UpperCAmelCase    			=   total_turn_around_time + turn_around_time[i]
  print(f'''Average waiting time = {total_waiting_time / no_of_processes:.5f}'''		)
  print("""Average turn around time =""" ,total_turn_around_time / no_of_processes		)


if __name__ == "__main__":
    print("""Enter how many process you want to analyze""")
    UpperCAmelCase__			=    int(input())
    UpperCAmelCase__			=    [0] * no_of_processes
    UpperCAmelCase__			=    [0] * no_of_processes
    UpperCAmelCase__			=    list(range(1, no_of_processes + 1))

    for i in range(no_of_processes):
        print("""Enter the arrival time and burst time for process:--""" + str(i + 1))
        UpperCAmelCase__		, UpperCAmelCase__			=    map(int, input().split())

    UpperCAmelCase__			=    calculate_waitingtime(arrival_time, burst_time, no_of_processes)

    UpperCAmelCase__			=    burst_time
    UpperCAmelCase__			=    no_of_processes
    UpperCAmelCase__			=    waiting_time
    UpperCAmelCase__			=    calculate_turnaroundtime(bt, n, wt)

    calculate_average_times(waiting_time, turn_around_time, no_of_processes)

    UpperCAmelCase__			=    pd.DataFrame(
        list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)),
        columns=[
            """Process""",
            """BurstTime""",
            """ArrivalTime""",
            """WaitingTime""",
            """TurnAroundTime""",
        ],
    )

    # Printing the dataFrame
    pd.set_option("""display.max_rows""", fcfs.shape[0] + 1)
    print(fcfs)
