





import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
				from transformers.models.rag.configuration_rag import RagConfig
				from transformers.models.rag.tokenization_rag import RagTokenizer

@require_faiss
@require_torch
class 	A_       (a_			):
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				tempfile.mkdtemp()
				UpperCAmelCase   		=				8

				# DPR tok
				UpperCAmelCase   		=				[
				    '''[UNK]''',
				    '''[CLS]''',
				    '''[SEP]''',
				    '''[PAD]''',
				    '''[MASK]''',
				    '''want''',
				    '''##want''',
				    '''##ed''',
				    '''wa''',
				    '''un''',
				    '''runn''',
				    '''##ing''',
				    ''',''',
				    '''low''',
				    '''lowest''',
				]
				UpperCAmelCase   		=				os.path.join(self.tmpdirname							,					'''dpr_tokenizer'''		)
				os.makedirs(_A							,					exist_ok=_A		)
				UpperCAmelCase   		=				os.path.join(_A							,					DPR_VOCAB_FILES_NAMES['''vocab_file''']		)
				with open(self.vocab_file							,					'''w'''							,					encoding='''utf-8'''		) as vocab_writer:
							vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]		)		)

				# BART tok
				UpperCAmelCase   		=				[
				    '''l''',
				    '''o''',
				    '''w''',
				    '''e''',
				    '''r''',
				    '''s''',
				    '''t''',
				    '''i''',
				    '''d''',
				    '''n''',
				    '''\u0120''',
				    '''\u0120l''',
				    '''\u0120n''',
				    '''\u0120lo''',
				    '''\u0120low''',
				    '''er''',
				    '''\u0120lowest''',
				    '''\u0120newer''',
				    '''\u0120wider''',
				    '''<unk>''',
				]
				UpperCAmelCase   		=				dict(zip(_A							,					range(len(_A		)		)		)		)
				UpperCAmelCase   		=				['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
				UpperCAmelCase   		=				{'''unk_token''': '''<unk>'''}

				UpperCAmelCase   		=				os.path.join(self.tmpdirname							,					'''bart_tokenizer'''		)
				os.makedirs(_A							,					exist_ok=_A		)
				UpperCAmelCase   		=				os.path.join(_A							,					BART_VOCAB_FILES_NAMES['''vocab_file''']		)
				UpperCAmelCase   		=				os.path.join(_A							,					BART_VOCAB_FILES_NAMES['''merges_file''']		)
				with open(self.vocab_file							,					'''w'''							,					encoding='''utf-8'''		) as fp:
							fp.write(json.dumps(_A		) + '''\n'''		)
				with open(self.merges_file							,					'''w'''							,					encoding='''utf-8'''		) as fp:
							fp.write('''\n'''.join(_A		)		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname							,					'''dpr_tokenizer'''		)		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname							,					'''bart_tokenizer'''		)		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				shutil.rmtree(self.tmpdirname		)
	@require_tokenizers
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				os.path.join(self.tmpdirname							,					'''rag_tokenizer'''		)
				UpperCAmelCase   		=				RagConfig(question_encoder=DPRConfig().to_dict()							,					generator=BartConfig().to_dict()		)
				UpperCAmelCase   		=				RagTokenizer(question_encoder=self.get_dpr_tokenizer()							,					generator=self.get_bart_tokenizer()		)
				rag_config.save_pretrained(_A		)
				rag_tokenizer.save_pretrained(_A		)
				UpperCAmelCase   		=				RagTokenizer.from_pretrained(_A							,					config=_A		)
				self.assertIsInstance(new_rag_tokenizer.question_encoder							,					_A		)
				self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()							,					rag_tokenizer.question_encoder.get_vocab()		)
				self.assertIsInstance(new_rag_tokenizer.generator							,					_A		)
				self.assertEqual(new_rag_tokenizer.generator.get_vocab()							,					rag_tokenizer.generator.get_vocab()		)
	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				RagTokenizer.from_pretrained('''facebook/rag-token-nq'''		)
				UpperCAmelCase   		=				[
				    '''who got the first nobel prize in physics''',
				    '''when is the next deadpool movie being released''',
				    '''which mode is used for short wave broadcast service''',
				    '''who is the owner of reading football club''',
				    '''when is the next scandal episode coming out''',
				    '''when is the last time the philadelphia won the superbowl''',
				    '''what is the most current adobe flash player version''',
				    '''how many episodes are there in dragon ball z''',
				    '''what is the first step in the evolution of the eye''',
				    '''where is gall bladder situated in human body''',
				    '''what is the main mineral in lithium batteries''',
				    '''who is the president of usa right now''',
				    '''where do the greasers live in the outsiders''',
				    '''panda is a national animal of which country''',
				    '''what is the name of manchester united stadium''',
				]
				UpperCAmelCase   		=				tokenizer(_A		)
				self.assertIsNotNone(_A		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				RagTokenizer.from_pretrained('''facebook/rag-sequence-nq'''		)
				UpperCAmelCase   		=				[
				    '''who got the first nobel prize in physics''',
				    '''when is the next deadpool movie being released''',
				    '''which mode is used for short wave broadcast service''',
				    '''who is the owner of reading football club''',
				    '''when is the next scandal episode coming out''',
				    '''when is the last time the philadelphia won the superbowl''',
				    '''what is the most current adobe flash player version''',
				    '''how many episodes are there in dragon ball z''',
				    '''what is the first step in the evolution of the eye''',
				    '''where is gall bladder situated in human body''',
				    '''what is the main mineral in lithium batteries''',
				    '''who is the president of usa right now''',
				    '''where do the greasers live in the outsiders''',
				    '''panda is a national animal of which country''',
				    '''what is the name of manchester united stadium''',
				]
				UpperCAmelCase   		=				tokenizer(_A		)
				self.assertIsNotNone(_A		)










from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


__A				:    Union[str, Any]       =   {
    "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
    "processing_speech_to_text": ["Speech2TextProcessor"],
}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    List[Any]       =   ["Speech2TextTokenizer"]

try:
				if not is_speech_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    List[Any]       =   ["Speech2TextFeatureExtractor"]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    int       =   [
				    "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "TFSpeech2TextForConditionalGeneration",
				    "TFSpeech2TextModel",
				    "TFSpeech2TextPreTrainedModel",
				]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A				:    Tuple       =   [
				    "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "Speech2TextForConditionalGeneration",
				    "Speech2TextModel",
				    "Speech2TextPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
				from .processing_speech_to_text import SpeechaTextProcessor

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_speech_to_text import SpeechaTextTokenizer

				try:
								if not is_speech_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_speech_to_text import (
								    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFSpeechaTextForConditionalGeneration,
								    TFSpeechaTextModel,
								    TFSpeechaTextPreTrainedModel,
								)

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_speech_to_text import (
								    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SpeechaTextForConditionalGeneration,
								    SpeechaTextModel,
								    SpeechaTextPreTrainedModel,
								)

else:
				import sys

				__A				:    Dict       =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



