

'''simple docstring'''

import sys
from typing import Tuple

import numpy as np
import torch
from PIL import Image
from torch import nn

from transformers.image_utils import PILImageResampling
from utils import img_tensorize





class 							_snake_case	:
   def __init__(	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Optional[Any]       ,SCREAMING_SNAKE_CASE__     : Dict=sys.maxsize							):
     SCREAMING_SNAKE_CASE:int   =   "bilinear"
     SCREAMING_SNAKE_CASE:Tuple   =   max_size
     SCREAMING_SNAKE_CASE:str   =   short_edge_length







   def __call__(	self     : List[Any]       ,SCREAMING_SNAKE_CASE__     : Optional[int]							):
     SCREAMING_SNAKE_CASE:Tuple   =   []
     for img in imgs:
       SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:List[str]   =   img.shape[:2]
       # later: provide list and randomly choose index for resize
       SCREAMING_SNAKE_CASE:List[str]   =   np.random.randint(self.short_edge_length[0]       ,self.short_edge_length[1] + 1							)
       if size == 0:
         return img
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   size * 1.0 / min(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
       if h < w:
         SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:int   =   size, scale * w
       else:
         SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Dict   =   scale * h, size
       if max(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							) > self.max_size:
         SCREAMING_SNAKE_CASE:List[str]   =   self.max_size * 1.0 / max(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
         SCREAMING_SNAKE_CASE:Any   =   newh * scale
         SCREAMING_SNAKE_CASE:Optional[int]   =   neww * scale
       SCREAMING_SNAKE_CASE:List[str]   =   int(neww + 0.5							)
       SCREAMING_SNAKE_CASE:Tuple   =   int(newh + 0.5							)

       if img.dtype == np.uinta:
         SCREAMING_SNAKE_CASE:Union[str, Any]   =   Image.fromarray(SCREAMING_SNAKE_CASE__							)
         SCREAMING_SNAKE_CASE:Tuple   =   pil_image.resize((neww, newh)       ,PILImageResampling.BILINEAR							)
         SCREAMING_SNAKE_CASE:List[str]   =   np.asarray(SCREAMING_SNAKE_CASE__							)
       else:
         SCREAMING_SNAKE_CASE:Optional[int]   =   img.permute(2       ,0       ,1							).unsqueeze(0							)  # 3, 0, 1)  # hw(c) -> nchw
         SCREAMING_SNAKE_CASE:Union[str, Any]   =   nn.functional.interpolate(
             SCREAMING_SNAKE_CASE__       ,(newh, neww)       ,mode=self.interp_method       ,align_corners=SCREAMING_SNAKE_CASE__							).squeeze(0							)
       img_augs.append(SCREAMING_SNAKE_CASE__							)

     return img_augs





class 							_snake_case	:
   def __init__(	self     : str       ,SCREAMING_SNAKE_CASE__     : List[str]							):
     SCREAMING_SNAKE_CASE:List[Any]   =   ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST]       ,cfg.INPUT.MAX_SIZE_TEST							)
     SCREAMING_SNAKE_CASE:int   =   cfg.INPUT.FORMAT
     SCREAMING_SNAKE_CASE:List[str]   =   cfg.SIZE_DIVISIBILITY
     SCREAMING_SNAKE_CASE:Dict   =   cfg.PAD_VALUE
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   cfg.INPUT.MAX_SIZE_TEST
     SCREAMING_SNAKE_CASE:Optional[Any]   =   cfg.MODEL.DEVICE
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   torch.tensor(cfg.MODEL.PIXEL_STD							).to(self.device							).view(len(cfg.MODEL.PIXEL_STD							)       ,1       ,1							)
     SCREAMING_SNAKE_CASE:List[str]   =   torch.tensor(cfg.MODEL.PIXEL_MEAN							).to(self.device							).view(len(cfg.MODEL.PIXEL_STD							)       ,1       ,1							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   lambda SCREAMING_SNAKE_CASE__							: (x - self.pixel_mean) / self.pixel_std
   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Optional[int]							):
     SCREAMING_SNAKE_CASE:Tuple   =   tuple(max(SCREAMING_SNAKE_CASE__							) for s in zip(*[img.shape for img in images]							)							)
     SCREAMING_SNAKE_CASE:Any   =   [im.shape[-2:] for im in images]
     SCREAMING_SNAKE_CASE:Any   =   [
         nn.functional.pad(
             SCREAMING_SNAKE_CASE__       ,[0, max_size[-1] - size[1], 0, max_size[-2] - size[0]]       ,value=self.pad_value       ,)
         for size, im in zip(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
     ]

     return torch.stack(SCREAMING_SNAKE_CASE__							), torch.tensor(SCREAMING_SNAKE_CASE__							)







   def __call__(	self     : Optional[Any]       ,SCREAMING_SNAKE_CASE__     : List[Any]       ,SCREAMING_SNAKE_CASE__     : Tuple=False							):
     with torch.no_grad():
       if not isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							):
         SCREAMING_SNAKE_CASE:List[str]   =   [images]
       if single_image:
         assert len(SCREAMING_SNAKE_CASE__							) == 1
       for i in range(len(SCREAMING_SNAKE_CASE__							)							):
         if isinstance(images[i]       ,torch.Tensor							):
           images.insert(SCREAMING_SNAKE_CASE__       ,images.pop(SCREAMING_SNAKE_CASE__							).to(self.device							).float()							)
         elif not isinstance(images[i]       ,torch.Tensor							):
           images.insert(
               SCREAMING_SNAKE_CASE__       ,torch.as_tensor(img_tensorize(images.pop(SCREAMING_SNAKE_CASE__							)       ,input_format=self.input_format							)							)
               .to(self.device							)
               .float()       ,)
            # resize smallest edge
       SCREAMING_SNAKE_CASE:Tuple   =   torch.tensor([im.shape[:2] for im in images]							)
       SCREAMING_SNAKE_CASE:Any   =   self.aug(SCREAMING_SNAKE_CASE__							)
       # transpose images and convert to torch tensors
       # images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images]
       # now normalize before pad to avoid useless arithmetic
       SCREAMING_SNAKE_CASE:Optional[int]   =   [self.normalizer(SCREAMING_SNAKE_CASE__							) for x in images]
       # now pad them to do the following operations
       SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:List[str]   =   self.pad(SCREAMING_SNAKE_CASE__							)
       # Normalize

       if self.size_divisibility > 0:
         raise NotImplementedError()
       # pad
       SCREAMING_SNAKE_CASE:Optional[int]   =   torch.true_divide(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
       if single_image:
         return images[0], sizes[0], scales_yx[0]
       else:
         return images, sizes, scales_yx





def 				A_					(	snake_case ,    snake_case	):
  boxes[:, 0::2] *= scale_yx[:, 1]
  boxes[:, 1::2] *= scale_yx[:, 0]
  return boxes





def 				A_					(	snake_case ,    snake_case	):
  assert torch.isfinite(snake_case	).all(), "Box tensor contains infinite or NaN!"
  SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:List[Any]   =   box_size
  tensor[:, 0].clamp_(min=0 ,    max=snake_case	)
  tensor[:, 1].clamp_(min=0 ,    max=snake_case	)
  tensor[:, 2].clamp_(min=0 ,    max=snake_case	)
  tensor[:, 3].clamp_(min=0 ,    max=snake_case	)









'''simple docstring'''

import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
    from transformers import WhisperFeatureExtractor, WhisperProcessor


A_      =							5_03_58
A_      =							5_03_62





@require_torch
@require_torchaudio
@require_sentencepiece
class 							_snake_case	(    unittest.TestCase  ):
   def       __UpperCamelCase  (	self     : Any							):
     SCREAMING_SNAKE_CASE:int   =   "openai/whisper-small.en"
     SCREAMING_SNAKE_CASE:Optional[Any]   =   tempfile.mkdtemp()
   def       __UpperCamelCase  (	self     : Any       ,**SCREAMING_SNAKE_CASE__     : List[str]							):
     return WhisperTokenizer.from_pretrained(self.checkpoint       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : List[str]       ,**SCREAMING_SNAKE_CASE__     : Dict							):
     return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,**SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : str							):
     shutil.rmtree(self.tmpdirname							)
   def       __UpperCamelCase  (	self     : Any							):
     SCREAMING_SNAKE_CASE:Dict   =   self.get_tokenizer()
     SCREAMING_SNAKE_CASE:Optional[int]   =   self.get_feature_extractor()

     SCREAMING_SNAKE_CASE:Dict   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     processor.save_pretrained(self.tmpdirname							)
     SCREAMING_SNAKE_CASE:List[Any]   =   WhisperProcessor.from_pretrained(self.tmpdirname							)

     self.assertEqual(processor.tokenizer.get_vocab()       ,tokenizer.get_vocab()							)
     self.assertIsInstance(processor.tokenizer       ,SCREAMING_SNAKE_CASE__							)

     self.assertEqual(processor.feature_extractor.to_json_string()       ,feature_extractor.to_json_string()							)
     self.assertIsInstance(processor.feature_extractor       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:int   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     processor.save_pretrained(self.tmpdirname							)

     SCREAMING_SNAKE_CASE:List[str]   =   self.get_tokenizer(bos_token="(BOS)"       ,eos_token="(EOS)"							)
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE__       ,padding_value=1.0							)

     SCREAMING_SNAKE_CASE:Any   =   WhisperProcessor.from_pretrained(
         self.tmpdirname       ,bos_token="(BOS)"       ,eos_token="(EOS)"       ,do_normalize=SCREAMING_SNAKE_CASE__       ,padding_value=1.0							)

     self.assertEqual(processor.tokenizer.get_vocab()       ,tokenizer_add_kwargs.get_vocab()							)
     self.assertIsInstance(processor.tokenizer       ,SCREAMING_SNAKE_CASE__							)

     self.assertEqual(processor.feature_extractor.to_json_string()       ,feature_extractor_add_kwargs.to_json_string()							)
     self.assertIsInstance(processor.feature_extractor       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : List[Any]							):
     SCREAMING_SNAKE_CASE:Any   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:List[Any]   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Optional[Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Any   =   floats_list((3, 1_000)							)

     SCREAMING_SNAKE_CASE:Any   =   feature_extractor(SCREAMING_SNAKE_CASE__       ,return_tensors="np"							)
     SCREAMING_SNAKE_CASE:List[str]   =   processor(SCREAMING_SNAKE_CASE__       ,return_tensors="np"							)

     for key in input_feat_extract.keys():
       self.assertAlmostEqual(input_feat_extract[key].sum()       ,input_processor[key].sum()       ,delta=1e-2							)
   def       __UpperCamelCase  (	self     : List[Any]							):
     SCREAMING_SNAKE_CASE:int   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:str   =   "This is a test string"

     SCREAMING_SNAKE_CASE:int   =   processor(text=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:str   =   tokenizer(SCREAMING_SNAKE_CASE__							)

     for key in encoded_tok.keys():
       self.assertListEqual(encoded_tok[key]       ,encoded_processor[key]							)
   def       __UpperCamelCase  (	self     : str							):
     SCREAMING_SNAKE_CASE:List[Any]   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:List[str]   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Optional[Any]   =   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     SCREAMING_SNAKE_CASE:Any   =   processor.batch_decode(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   tokenizer.batch_decode(SCREAMING_SNAKE_CASE__							)

     self.assertListEqual(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : int							):
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Optional[int]   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Union[str, Any]   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)

     self.assertListEqual(
         processor.model_input_names       ,feature_extractor.model_input_names       ,msg="`processor` and `feature_extractor` model input names do not match"       ,)
   def       __UpperCamelCase  (	self     : int							):
     SCREAMING_SNAKE_CASE:Tuple   =   self.get_feature_extractor()
     SCREAMING_SNAKE_CASE:Any   =   self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Any   =   WhisperProcessor(tokenizer=SCREAMING_SNAKE_CASE__       ,feature_extractor=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   processor.get_decoder_prompt_ids(task="transcribe"       ,no_timestamps=SCREAMING_SNAKE_CASE__							)

     self.assertIsInstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
     for ids in forced_decoder_ids:
       self.assertIsInstance(SCREAMING_SNAKE_CASE__       ,(list, tuple)							)

     SCREAMING_SNAKE_CASE:Any   =   [TRANSCRIBE, NOTIMESTAMPS]
     self.assertListEqual([ids[-1] for ids in forced_decoder_ids]       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Dict							):
     SCREAMING_SNAKE_CASE:Tuple   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     SCREAMING_SNAKE_CASE:Dict   =   processor.get_prompt_ids("Mr. Quilter"							)
     SCREAMING_SNAKE_CASE:int   =   processor.tokenizer.decode(SCREAMING_SNAKE_CASE__							)

     self.assertListEqual(prompt_ids.tolist()       ,[50_360, 1_770, 13, 2_264, 346, 353]							)
     self.assertEqual(SCREAMING_SNAKE_CASE__       ,"<|startofprev|> Mr. Quilter"							)
   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:Dict   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)
     SCREAMING_SNAKE_CASE:List[Any]   =   processor.get_prompt_ids(""							)
     SCREAMING_SNAKE_CASE:Tuple   =   processor.tokenizer.decode(SCREAMING_SNAKE_CASE__							)

     self.assertListEqual(prompt_ids.tolist()       ,[50_360, 220]							)
     self.assertEqual(SCREAMING_SNAKE_CASE__       ,"<|startofprev|> "							)







   def       __UpperCamelCase  (	self     : Union[str, Any]							):
     SCREAMING_SNAKE_CASE:Tuple   =   WhisperProcessor(tokenizer=self.get_tokenizer()       ,feature_extractor=self.get_feature_extractor()							)

     def _test_prompt_error_raised_helper(SCREAMING_SNAKE_CASE__     : int       ,SCREAMING_SNAKE_CASE__     : List[str]							):
       with pytest.raises(SCREAMING_SNAKE_CASE__							) as excinfo:
         processor.get_prompt_ids(SCREAMING_SNAKE_CASE__							)
       SCREAMING_SNAKE_CASE:List[Any]   =   F'''Encountered text in the prompt corresponding to disallowed special token: {special_token}.'''
       self.assertEqual(SCREAMING_SNAKE_CASE__       ,str(excinfo.value							)							)

     _test_prompt_error_raised_helper("<|startofprev|> test"       ,"<|startofprev|>"							)
     _test_prompt_error_raised_helper("test <|notimestamps|>"       ,"<|notimestamps|>"							)
     _test_prompt_error_raised_helper("test <|zh|> test <|transcribe|>"       ,"<|zh|>"							)






