




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


lowerCamelCase__				:     Any			       =							50_358
lowerCamelCase__				:     List[Any]			       =							50_362





@require_torch
@require_torchaudio
@require_sentencepiece
class  lowerCamelCase_							(   unittest.TestCase      ):


 '''simple docstring'''







 def    lowerCAmelCase_						(							self						:	List[Any]		):
       SCREAMING_SNAKE_CASE_					=    'openai/whisper-small.en'
       SCREAMING_SNAKE_CASE_					=    tempfile.mkdtemp()



 def    lowerCAmelCase_						(							self						:	Tuple       ,       **_lowerCAmelCase						:	int		):
       return WhisperTokenizer.from_pretrained(self.checkpoint       ,       **_lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	int       ,       **_lowerCAmelCase						:	Optional[int]		):
       return WhisperFeatureExtractor.from_pretrained(self.checkpoint       ,       **_lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Tuple		):
       shutil.rmtree(self.tmpdirname		)



 def    lowerCAmelCase_						(							self						:	str		):
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)

       processor.save_pretrained(self.tmpdirname		)
       SCREAMING_SNAKE_CASE_					=    WhisperProcessor.from_pretrained(self.tmpdirname		)

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

       self.assertEqual(processor.feature_extractor.to_json_string()       ,       feature_extractor.to_json_string()		)
       self.assertIsInstance(processor.feature_extractor       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=self.get_tokenizer()       ,       feature_extractor=self.get_feature_extractor()		)
       processor.save_pretrained(self.tmpdirname		)

       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer(bos_token='(BOS)'       ,       eos_token='(EOS)'		)
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor(do_normalize=_lowerCAmelCase       ,       padding_value=1.0		)

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor.from_pretrained(
           self.tmpdirname       ,       bos_token='(BOS)'       ,       eos_token='(EOS)'       ,       do_normalize=_lowerCAmelCase       ,       padding_value=1.0		)

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

       self.assertEqual(processor.feature_extractor.to_json_string()       ,       feature_extractor_add_kwargs.to_json_string()		)
       self.assertIsInstance(processor.feature_extractor       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Tuple		):
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    floats_list((3, 1_000)		)

       SCREAMING_SNAKE_CASE_					=    feature_extractor(_lowerCAmelCase       ,       return_tensors='np'		)
       SCREAMING_SNAKE_CASE_					=    processor(_lowerCAmelCase       ,       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    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    'This is a test string'

       SCREAMING_SNAKE_CASE_					=    processor(text=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    tokenizer(_lowerCAmelCase		)

       for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]       ,       encoded_processor[key]		)



 def    lowerCAmelCase_						(							self						:	int		):
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       SCREAMING_SNAKE_CASE_					=    processor.batch_decode(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    tokenizer.batch_decode(_lowerCAmelCase		)

       self.assertListEqual(_lowerCAmelCase       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Dict		):
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)

       self.assertListEqual(
           processor.model_input_names       ,       feature_extractor.model_input_names       ,       msg='`processor` and `feature_extractor` model input names do not match'       ,       )



 def    lowerCAmelCase_						(							self						:	Tuple		):
       SCREAMING_SNAKE_CASE_					=    self.get_feature_extractor()
       SCREAMING_SNAKE_CASE_					=    self.get_tokenizer()

       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=_lowerCAmelCase       ,       feature_extractor=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    processor.get_decoder_prompt_ids(task='transcribe'       ,       no_timestamps=_lowerCAmelCase		)

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

       SCREAMING_SNAKE_CASE_					=    [TRANSCRIBE, NOTIMESTAMPS]
       self.assertListEqual([ids[-1] for ids in forced_decoder_ids]       ,       _lowerCAmelCase		)



 def    lowerCAmelCase_						(							self						:	Dict		):
       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=self.get_tokenizer()       ,       feature_extractor=self.get_feature_extractor()		)
       SCREAMING_SNAKE_CASE_					=    processor.get_prompt_ids('Mr. Quilter'		)
       SCREAMING_SNAKE_CASE_					=    processor.tokenizer.decode(_lowerCAmelCase		)

       self.assertListEqual(prompt_ids.tolist()       ,       [50_360, 1_770, 13, 2_264, 346, 353]		)
       self.assertEqual(_lowerCAmelCase       ,       '<|startofprev|> Mr. Quilter'		)



 def    lowerCAmelCase_						(							self						:	List[str]		):
       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=self.get_tokenizer()       ,       feature_extractor=self.get_feature_extractor()		)
       SCREAMING_SNAKE_CASE_					=    processor.get_prompt_ids(''		)
       SCREAMING_SNAKE_CASE_					=    processor.tokenizer.decode(_lowerCAmelCase		)

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




 def    lowerCAmelCase_						(							self						:	int		):
       SCREAMING_SNAKE_CASE_					=    WhisperProcessor(tokenizer=self.get_tokenizer()       ,       feature_extractor=self.get_feature_extractor()		)

       def _test_prompt_error_raised_helper(_lowerCAmelCase						:	Optional[int]       ,       _lowerCAmelCase						:	List[Any]		):
             with pytest.raises(_lowerCAmelCase		) as excinfo:
                   processor.get_prompt_ids(_lowerCAmelCase		)
             SCREAMING_SNAKE_CASE_					=    F"Encountered text in the prompt corresponding to disallowed special token: {special_token}."
             self.assertEqual(_lowerCAmelCase       ,       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|>'		)





import bsa
import requests

def       UpperCAmelCase_							(   __UpperCAmelCase		:			bsa.element.ResultSet							)      ->				dict[str, str]:
      return {
          "name": soup.ha.a.text,
          "genre": soup.find('span'    ,    class_='genre'							).text.strip(),
          "rating": soup.strong.text,
          "page_link": f"https://www.imdb.com{soup.a.get('href'							)}",
      }

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int = 5							)      ->				tuple:
      SCREAMING_SNAKE_CASE_					=    int(float(__UpperCAmelCase							)							)
      if num_movies < 1:
            return ()
      SCREAMING_SNAKE_CASE_					=    (
          'https://www.imdb.com/search/title?title_type='
          f"feature&sort=num_votes,desc&count={num_movies}"
      )
      SCREAMING_SNAKE_CASE_					=    bsa.BeautifulSoup(requests.get(__UpperCAmelCase							).content    ,    'html.parser'							)
      return tuple(
          get_movie_data_from_soup(__UpperCAmelCase							)
          for movie in source.find_all('div'    ,    class_='lister-item mode-advanced'							)							)


if __name__ == "__main__":
   import json

   lowerCamelCase__				:     int			       =							int(input('How many movies would you like to see? '))
   print(
       ', '.join(
           json.dumps(movie, indent=4) for movie in get_imdb_top_movies(num_movies)
       )
   )