

import os
import pickle
import unittest

from transformers import AutoTokenizer
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.models.bert_japanese.tokenization_bert_japanese import (
    VOCAB_FILES_NAMES,
    BertJapaneseTokenizer,
    CharacterTokenizer,
    JumanppTokenizer,
    MecabTokenizer,
    SudachiTokenizer,
    WordpieceTokenizer,
)
from transformers.testing_utils import custom_tokenizers, require_jumanpp, require_sudachi

from ...test_tokenization_common import TokenizerTesterMixin


@custom_tokenizers
class  A						(							_UpperCAmelCase    ,							unittest.TestCase ):







    """simple docstring"""





    lowerCamelCase			=							BertJapaneseTokenizer
    lowerCamelCase			=							False
    lowerCamelCase			=							True


    def 						snake_case__     (      self :       List[str]		)->    List[Any]:





        '''simple docstring'''







        super().setUp()

        A__          =   [
            '[UNK]',
            '[CLS]',
            '[SEP]',
            'こんにちは',
            'こん',
            'にちは',
            'ばんは',
            '##こん',
            '##にちは',
            '##ばんは',
            '世界',
            '##世界',
            '、',
            '##、',
            '。',
            '##。',
        ]

        A__          =   os.path.join(self.tmpdirname,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]		)		)


    def 						snake_case__     (      self :       str,lowercase_ :       Union[str, Any]		)->    str:





        '''simple docstring'''







        A__          =   'こんにちは、世界。 \nこんばんは、世界。'
        A__          =   'こんにちは 、 世界 。 こんばんは 、 世界 。'
        return input_text, output_text


    def 						snake_case__     (      self :       str,lowercase_ :       Dict		)->    Optional[int]:





        '''simple docstring'''







        A__      ,						A__          =   self.get_input_output_texts(lowercase_		)
        A__          =   tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
        A__          =   tokenizer.decode(lowercase_,clean_up_tokenization_spaces=lowercase_		)
        return text, ids


    def 						snake_case__     (      self :       int		)->    Optional[int]:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       Optional[Any]		)->    Dict:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       int		)->    Dict:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       Tuple		)->    Optional[Any]:





        '''simple docstring'''







        A__          =   self.tokenizer_class(self.vocab_file		)

        A__          =   tokenizer.tokenize('こんにちは、世界。\nこんばんは、世界。'		)
        self.assertListEqual(lowercase_,['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']		)
        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_		),[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]		)


    def 						snake_case__     (      self :       List[Any]		)->    int:





        '''simple docstring'''







        A__          =   self.tokenizer_class(self.vocab_file,word_tokenizer_type='mecab'		)
        self.assertIsNotNone(lowercase_		)

        A__          =   'こんにちは、世界。\nこんばんは、世界。'
        A__          =   tokenizer.tokenize(lowercase_		)
        self.assertListEqual(lowercase_,['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']		)
        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_		),[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]		)

        A__          =   os.path.join(self.tmpdirname,'tokenizer.bin'		)
        with open(lowercase_,'wb'		) as handle:
            pickle.dump(lowercase_,lowercase_		)

        with open(lowercase_,'rb'		) as handle:
            A__          =   pickle.load(lowercase_		)

        A__          =   tokenizer_new.tokenize(lowercase_		)

        self.assertListEqual(lowercase_,lowercase_		)


    def 						snake_case__     (      self :       int		)->    str:





        '''simple docstring'''







        A__          =   MecabTokenizer(mecab_dic='ipadic'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップルストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'],)


    def 						snake_case__     (      self :       Tuple		)->    List[str]:





        '''simple docstring'''







        try:
            A__          =   MecabTokenizer(mecab_dic='unidic_lite'		)
        except ModuleNotFoundError:
            return

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'],)


    def 						snake_case__     (      self :       Tuple		)->    int:





        '''simple docstring'''







        try:
            A__          =   MecabTokenizer(mecab_dic='unidic'		)
        except ModuleNotFoundError:
            return

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'],)


    def 						snake_case__     (      self :       Optional[Any]		)->    List[str]:





        '''simple docstring'''







        A__          =   MecabTokenizer(do_lower_case=lowercase_,mecab_dic='ipadic'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップルストア', 'で', 'iphone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'],)


    def 						snake_case__     (      self :       str		)->    int:





        '''simple docstring'''







        try:
            A__          =   MecabTokenizer(
                do_lower_case=lowercase_,normalize_text=lowercase_,mecab_option='-d /usr/local/lib/mecab/dic/jumandic'		)
        except RuntimeError:
            # if dict doesn't exist in the system, previous code raises this error.
            return

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['ｱｯﾌﾟﾙストア', 'で', 'iPhone', '８', 'が', '発売', 'さ', 'れた', '\u3000', '。'],)


    def 						snake_case__     (      self :       Dict		)->    int:





        '''simple docstring'''







        A__          =   MecabTokenizer(normalize_text=lowercase_,mecab_dic='ipadic'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['ｱｯﾌﾟﾙストア', 'で', 'iPhone', '８', 'が', '発売', 'さ', 'れ', 'た', '　', '。'],)


    @require_sudachi
    def 						snake_case__     (      self :       List[str]		)->    Tuple:





        '''simple docstring'''







        A__          =   self.tokenizer_class(self.vocab_file,word_tokenizer_type='sudachi'		)
        self.assertIsNotNone(lowercase_		)

        A__          =   'こんにちは、世界。\nこんばんは、世界。'
        A__          =   tokenizer.tokenize(lowercase_		)
        self.assertListEqual(lowercase_,['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']		)
        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_		),[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]		)

        A__          =   os.path.join(self.tmpdirname,'tokenizer.bin'		)
        with open(lowercase_,'wb'		) as handle:
            pickle.dump(lowercase_,lowercase_		)

        with open(lowercase_,'rb'		) as handle:
            A__          =   pickle.load(lowercase_		)

        A__          =   tokenizer_new.tokenize(lowercase_		)

        self.assertListEqual(lowercase_,lowercase_		)


    @require_sudachi
    def 						snake_case__     (      self :       List[str]		)->    str:





        '''simple docstring'''







        A__          =   SudachiTokenizer(sudachi_dict_type='core'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),[' ', '\t', 'アップル', 'ストア', 'で', 'iPhone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '],)


    @require_sudachi
    def 						snake_case__     (      self :       int		)->    Any:





        '''simple docstring'''







        A__          =   SudachiTokenizer(sudachi_dict_type='core',sudachi_split_mode='A'		)

        self.assertListEqual(tokenizer.tokenize('外国人参政権'		),['外国', '人', '参政', '権']		)


    @require_sudachi
    def 						snake_case__     (      self :       List[str]		)->    Union[str, Any]:





        '''simple docstring'''







        A__          =   SudachiTokenizer(sudachi_dict_type='core',sudachi_split_mode='B'		)

        self.assertListEqual(tokenizer.tokenize('外国人参政権'		),['外国人', '参政権']		)


    @require_sudachi
    def 						snake_case__     (      self :       str		)->    int:





        '''simple docstring'''







        A__          =   SudachiTokenizer(sudachi_dict_type='core',sudachi_split_mode='C'		)

        self.assertListEqual(tokenizer.tokenize('外国人参政権'		),['外国人参政権']		)


    @require_sudachi
    def 						snake_case__     (      self :       Optional[Any]		)->    str:





        '''simple docstring'''







        A__          =   SudachiTokenizer(do_lower_case=lowercase_,sudachi_dict_type='core'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),[' ', '\t', 'アップル', 'ストア', 'で', 'iphone', '8', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', ' ', '。', ' ', ' '],)


    @require_sudachi
    def 						snake_case__     (      self :       int		)->    int:





        '''simple docstring'''







        A__          =   SudachiTokenizer(normalize_text=lowercase_,sudachi_dict_type='core'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),[' ', '\t', 'ｱｯﾌﾟﾙ', 'ストア', 'で', 'iPhone', '８', ' ', 'が', ' ', ' ', '\n ', '発売', 'さ', 'れ', 'た', '\u3000', '。', ' ', ' '],)


    @require_sudachi
    def 						snake_case__     (      self :       Optional[Any]		)->    List[str]:





        '''simple docstring'''







        A__          =   SudachiTokenizer(trim_whitespace=lowercase_,sudachi_dict_type='core'		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れ', 'た', '。'],)


    @require_jumanpp
    def 						snake_case__     (      self :       int		)->    Tuple:





        '''simple docstring'''







        A__          =   self.tokenizer_class(self.vocab_file,word_tokenizer_type='jumanpp'		)
        self.assertIsNotNone(lowercase_		)

        A__          =   'こんにちは、世界。\nこんばんは、世界。'
        A__          =   tokenizer.tokenize(lowercase_		)
        self.assertListEqual(lowercase_,['こんにちは', '、', '世界', '。', 'こん', '##ばんは', '、', '世界', '。']		)
        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_		),[3, 1_2, 1_0, 1_4, 4, 9, 1_2, 1_0, 1_4]		)

        A__          =   os.path.join(self.tmpdirname,'tokenizer.bin'		)
        with open(lowercase_,'wb'		) as handle:
            pickle.dump(lowercase_,lowercase_		)

        with open(lowercase_,'rb'		) as handle:
            A__          =   pickle.load(lowercase_		)

        A__          =   tokenizer_new.tokenize(lowercase_		)

        self.assertListEqual(lowercase_,lowercase_		)


    @require_jumanpp
    def 						snake_case__     (      self :       Dict		)->    str:





        '''simple docstring'''







        A__          =   JumanppTokenizer()

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iPhone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'],)


    @require_jumanpp
    def 						snake_case__     (      self :       Any		)->    Dict:





        '''simple docstring'''







        A__          =   JumanppTokenizer(do_lower_case=lowercase_		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iphone', '8', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'],)


    @require_jumanpp
    def 						snake_case__     (      self :       Union[str, Any]		)->    Dict:





        '''simple docstring'''







        A__          =   JumanppTokenizer(normalize_text=lowercase_		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['ｱ', 'ｯ', 'ﾌ', 'ﾟ', 'ﾙ', 'ストア', 'で', 'iPhone', '８', '\u3000', 'が', '\u3000', '\u3000', '\u3000', '発売', 'さ', 'れた', '\u3000', '。'],)


    @require_jumanpp
    def 						snake_case__     (      self :       str		)->    Optional[Any]:





        '''simple docstring'''







        A__          =   JumanppTokenizer(trim_whitespace=lowercase_		)

        self.assertListEqual(
            tokenizer.tokenize(' \tｱｯﾌﾟﾙストアでiPhone８ が  \n 発売された　。  '		),['アップル', 'ストア', 'で', 'iPhone', '8', 'が', '発売', 'さ', 'れた', '。'],)


    @require_jumanpp
    def 						snake_case__     (      self :       Union[str, Any]		)->    str:





        '''simple docstring'''







        A__          =   JumanppTokenizer()

        self.assertListEqual(
            tokenizer.tokenize('ありがとうございますm(_ _)ｍ見つけるのが大変です。'		),['ありがとう', 'ございます', 'm(_ _)m', '見つける', 'の', 'が', '大変です', '。'],)


    def 						snake_case__     (      self :       Tuple		)->    List[str]:





        '''simple docstring'''







        A__          =   ['[UNK]', '[CLS]', '[SEP]', 'こんにちは', 'こん', 'にちは', 'ばんは', '##こん', '##にちは', '##ばんは']

        A__          =   {}
        for i, token in enumerate(lowercase_		):
            A__          =   i
        A__          =   WordpieceTokenizer(vocab=lowercase_,unk_token='[UNK]'		)

        self.assertListEqual(tokenizer.tokenize(''		),[]		)

        self.assertListEqual(tokenizer.tokenize('こんにちは'		),['こんにちは']		)

        self.assertListEqual(tokenizer.tokenize('こんばんは'		),['こん', '##ばんは']		)

        self.assertListEqual(tokenizer.tokenize('こんばんは こんばんにちは こんにちは'		),['こん', '##ばんは', '[UNK]', 'こんにちは']		)


    def 						snake_case__     (      self :       str		)->    int:





        '''simple docstring'''







        A__          =   BertJapaneseTokenizer.from_pretrained('nlp-waseda/roberta-base-japanese-with-auto-jumanpp'		)
        A__          =   tokenizer.subword_tokenizer

        A__          =   subword_tokenizer.tokenize('国境 の 長い トンネル を 抜ける と 雪国 であった 。'		)
        self.assertListEqual(lowercase_,['▁国境', '▁の', '▁長い', '▁トンネル', '▁を', '▁抜ける', '▁と', '▁雪', '国', '▁であった', '▁。']		)

        A__          =   subword_tokenizer.tokenize('こんばんは こんばん にち は こんにちは'		)
        self.assertListEqual(lowercase_,['▁こん', 'ばん', 'は', '▁こん', 'ばん', '▁に', 'ち', '▁は', '▁こんにちは']		)


    def 						snake_case__     (      self :       str		)->    Dict:





        '''simple docstring'''







        A__          =   self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese'		)

        A__          =   tokenizer.encode('ありがとう。',add_special_tokens=lowercase_		)
        A__          =   tokenizer.encode('どういたしまして。',add_special_tokens=lowercase_		)

        A__          =   tokenizer.build_inputs_with_special_tokens(lowercase_		)
        A__          =   tokenizer.build_inputs_with_special_tokens(lowercase_,lowercase_		)

        # 2 is for "[CLS]", 3 is for "[SEP]"
        assert encoded_sentence == [2] + text + [3]
        assert encoded_pair == [2] + text + [3] + text_a + [3]




@custom_tokenizers
class  A						(							_UpperCAmelCase    ,							unittest.TestCase ):







    """simple docstring"""





    lowerCamelCase			=							BertJapaneseTokenizer
    lowerCamelCase			=							False


    def 						snake_case__     (      self :       Any		)->    Tuple:





        '''simple docstring'''







        super().setUp()

        A__          =   ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。']

        A__          =   os.path.join(self.tmpdirname,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]		)		)


    def 						snake_case__     (      self :       Any,**lowercase_ :       Any		)->    Any:





        '''simple docstring'''







        return BertJapaneseTokenizer.from_pretrained(self.tmpdirname,subword_tokenizer_type='character',**lowercase_		)


    def 						snake_case__     (      self :       Any,lowercase_ :       int		)->    Union[str, Any]:





        '''simple docstring'''







        A__          =   'こんにちは、世界。 \nこんばんは、世界。'
        A__          =   'こ ん に ち は 、 世 界 。 こ ん ば ん は 、 世 界 。'
        return input_text, output_text


    def 						snake_case__     (      self :       Any		)->    Optional[int]:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       Any		)->    Optional[int]:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       Any		)->    Union[str, Any]:





        '''simple docstring'''







        pass  # TODO add if relevant


    def 						snake_case__     (      self :       str		)->    Tuple:





        '''simple docstring'''







        A__          =   self.tokenizer_class(self.vocab_file,subword_tokenizer_type='character'		)

        A__          =   tokenizer.tokenize('こんにちは、世界。 \nこんばんは、世界。'		)
        self.assertListEqual(
            lowercase_,['こ', 'ん', 'に', 'ち', 'は', '、', '世', '界', '。', 'こ', 'ん', 'ば', 'ん', 'は', '、', '世', '界', '。']		)
        self.assertListEqual(
            tokenizer.convert_tokens_to_ids(lowercase_		),[3, 4, 5, 6, 7, 1_1, 9, 1_0, 1_2, 3, 4, 8, 4, 7, 1_1, 9, 1_0, 1_2]		)


    def 						snake_case__     (      self :       Any		)->    Optional[Any]:





        '''simple docstring'''







        A__          =   ['[UNK]', '[CLS]', '[SEP]', 'こ', 'ん', 'に', 'ち', 'は', 'ば', '世', '界', '、', '。']

        A__          =   {}
        for i, token in enumerate(lowercase_		):
            A__          =   i
        A__          =   CharacterTokenizer(vocab=lowercase_,unk_token='[UNK]'		)

        self.assertListEqual(tokenizer.tokenize(''		),[]		)

        self.assertListEqual(tokenizer.tokenize('こんにちは'		),['こ', 'ん', 'に', 'ち', 'は']		)

        self.assertListEqual(tokenizer.tokenize('こんにちほ'		),['こ', 'ん', 'に', 'ち', '[UNK]']		)


    def 						snake_case__     (      self :       int		)->    Optional[Any]:





        '''simple docstring'''







        A__          =   self.tokenizer_class.from_pretrained('cl-tohoku/bert-base-japanese-char'		)

        A__          =   tokenizer.encode('ありがとう。',add_special_tokens=lowercase_		)
        A__          =   tokenizer.encode('どういたしまして。',add_special_tokens=lowercase_		)

        A__          =   tokenizer.build_inputs_with_special_tokens(lowercase_		)
        A__          =   tokenizer.build_inputs_with_special_tokens(lowercase_,lowercase_		)

        # 2 is for "[CLS]", 3 is for "[SEP]"
        assert encoded_sentence == [2] + text + [3]
        assert encoded_pair == [2] + text + [3] + text_a + [3]




@custom_tokenizers
class  A						(							unittest.TestCase ):







    """simple docstring"""





    def 						snake_case__     (      self :       Optional[Any]		)->    Optional[Any]:





        '''simple docstring'''







        A__          =   'cl-tohoku/bert-base-japanese'
        A__          =   AutoTokenizer.from_pretrained(lowercase_		)
        self.assertIsInstance(lowercase_,lowercase_		)




class  A						(							unittest.TestCase ):







    """simple docstring"""





    def 						snake_case__     (      self :       Union[str, Any]		)->    str:





        '''simple docstring'''







        A__          =   'cl-tohoku/bert-base-japanese'
        with self.assertLogs('transformers',level='WARNING'		) as cm:
            BertTokenizer.from_pretrained(lowercase_		)
            self.assertTrue(
                cm.records[0].message.startswith(
                    'The tokenizer class you load from this checkpoint is not the same type as the class this function'
                    ' is called from.'		)		)
        A__          =   'bert-base-cased'
        with self.assertLogs('transformers',level='WARNING'		) as cm:
            BertJapaneseTokenizer.from_pretrained(lowercase_		)
            self.assertTrue(
                cm.records[0].message.startswith(
                    'The tokenizer class you load from this checkpoint is not the same type as the class this function'
                    ' is called from.'		)		)






'''simple docstring'''

import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch

class 	__magic_name__						(    _UpperCamelCase   ):







							@require_torch
							def 							__lowercase			(							self					:       Tuple		):
									# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
									# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
									# while running an external program

									# python one-liner segments

									# this must be loaded before socket.socket is monkey-patched
									_a     :      Optional[int]        =							'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

									_a     :      List[str]        =							'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

									_a     :      Tuple        =							'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

									# Force fetching the files so that we can use the cache
									_a     :      List[Any]        =							'hf-internal-testing/tiny-random-bert'
									BertConfig.from_pretrained(_UpperCAmelCase		)
									BertModel.from_pretrained(_UpperCAmelCase		)
									BertTokenizer.from_pretrained(_UpperCAmelCase		)
									pipeline(task='fill-mask'	,model=_UpperCAmelCase		)

									# baseline - just load from_pretrained with normal network
									_a     :      Optional[int]        =							[sys.executable, '-c', '\n'.join([load, run, mock]		)]

									# should succeed
									_a     :      Tuple        =							self.get_env()
									# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
									_a     :      int        =							'1'
									_a     :      List[Any]        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)







							@require_torch
							def 							__lowercase			(							self					:       Any		):
									# python one-liner segments
									# this must be loaded before socket.socket is monkey-patched
									_a     :      Dict        =							'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

									_a     :      Optional[int]        =							'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

									_a     :      Optional[Any]        =							'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

									# Force fetching the files so that we can use the cache
									_a     :      int        =							'hf-internal-testing/tiny-random-bert'
									BertConfig.from_pretrained(_UpperCAmelCase		)
									BertModel.from_pretrained(_UpperCAmelCase		)
									BertTokenizer.from_pretrained(_UpperCAmelCase		)
									pipeline(task='fill-mask'	,model=_UpperCAmelCase		)

									# baseline - just load from_pretrained with normal network
									_a     :      Optional[int]        =							[sys.executable, '-c', '\n'.join([load, run, mock]		)]

									# should succeed
									_a     :      str        =							self.get_env()
									_a     :      Optional[Any]        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)







							@require_torch
							def 							__lowercase			(							self					:       List[str]		):
									# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
									# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
									# while running an external program

									# python one-liner segments

									# this must be loaded before socket.socket is monkey-patched
									_a     :      Union[str, Any]        =							'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

									_a     :      Optional[Any]        =							'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

									_a     :      str        =							'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

									# baseline - just load from_pretrained with normal network
									_a     :      Optional[Any]        =							[sys.executable, '-c', '\n'.join([load, run]		)]

									# should succeed
									_a     :      Dict        =							self.get_env()
									_a     :      int        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)

									# next emulate no network
									_a     :      List[Any]        =							[sys.executable, '-c', '\n'.join([load, mock, run]		)]

									# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
									# env["TRANSFORMERS_OFFLINE"] = "0"
									# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
									# self.assertEqual(result.returncode, 1, result.stderr)

									# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
									_a     :      int        =							'1'
									_a     :      Any        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)







							@require_torch
							def 							__lowercase			(							self					:       int		):
									_a     :      Optional[Any]        =							'\nfrom transformers import pipeline\n        '
									_a     :      str        =							'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

									_a     :      List[str]        =							'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
									_a     :      List[Any]        =							self.get_env()
									_a     :      Dict        =							'1'
									_a     :      Dict        =							[sys.executable, '-c', '\n'.join([load, mock, run]		)]
									_a     :      str        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,1	,result.stderr		)
									self.assertIn(
									    'You cannot infer task automatically within `pipeline` when using offline mode'	,result.stderr.decode().replace('\n'	,''		)	,)







							@require_torch
							def 							__lowercase			(							self					:       int		):
									_a     :      Optional[int]        =							'\nfrom transformers import AutoModel\n        '
									_a     :      List[Any]        =							'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

									# baseline - just load from_pretrained with normal network
									_a     :      Union[str, Any]        =							[sys.executable, '-c', '\n'.join([load, run]		)]

									# should succeed
									_a     :      Tuple        =							self.get_env()
									_a     :      List[str]        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)

									# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
									_a     :      Optional[Any]        =							'1'
									_a     :      Any        =							subprocess.run(_UpperCAmelCase	,env=_UpperCAmelCase	,check=_UpperCAmelCase	,capture_output=_UpperCAmelCase		)
									self.assertEqual(result.returncode	,0	,result.stderr		)
									self.assertIn('success'	,result.stdout.decode()		)




