





import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


_snake_case  :   Any						   =  logging.get_logger(__name__)

_snake_case  :   str						   =  {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}



class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			"""blip_2_vision_model"""






  def __init__(    self			:		List[Any] ,  lowerCAmelCase_			:		Tuple=1_4_0_8 ,  lowerCAmelCase_			:		List[str]=6_1_4_4 ,  lowerCAmelCase_			:		List[Any]=3_9 ,  lowerCAmelCase_			:		List[Any]=1_6 ,  lowerCAmelCase_			:		List[str]=2_2_4 ,  lowerCAmelCase_			:		Tuple=1_4 ,  lowerCAmelCase_			:		List[str]="gelu" ,  lowerCAmelCase_			:		List[str]=0.0_00_01 ,  lowerCAmelCase_			:		str=0.0 ,  lowerCAmelCase_			:		Optional[Any]=1e-10 ,  lowerCAmelCase_			:		int=True ,  **lowerCAmelCase_			:		Optional[Any] ,  )							->						str:
     super().__init__(**lowerCAmelCase_					)

     __lowerCAmelCase							     =     hidden_size
     __lowerCAmelCase							     =     intermediate_size
     __lowerCAmelCase							     =     num_hidden_layers
     __lowerCAmelCase							     =     num_attention_heads
     __lowerCAmelCase							     =     patch_size
     __lowerCAmelCase							     =     image_size
     __lowerCAmelCase							     =     initializer_range
     __lowerCAmelCase							     =     attention_dropout
     __lowerCAmelCase							     =     layer_norm_eps
     __lowerCAmelCase							     =     hidden_act
     __lowerCAmelCase							     =     qkv_bias






  @classmethod
  def     lowercase		(    cls			:		Union[str, Any] ,  lowerCAmelCase_			:		Union[str, os.PathLike] ,  **lowerCAmelCase_			:		List[str]					)							->						"PretrainedConfig":
     cls._set_token_in_kwargs(lowerCAmelCase_					)

     __lowerCAmelCase     ,     __lowerCAmelCase							     =     cls.get_config_dict(lowerCAmelCase_ ,  **lowerCAmelCase_					)

     # get the vision config dict if we are loading from Blip2Config
     if config_dict.get('model_type'					) == "blip-2":
        __lowerCAmelCase							     =     config_dict['vision_config']

     if "model_type" in config_dict and hasattr(cls ,  'model_type'					) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
            f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

     return cls.from_dict(lowerCAmelCase_ ,  **lowerCAmelCase_					)





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			"""blip_2_qformer"""






  def __init__(    self			:		Any ,  lowerCAmelCase_			:		Union[str, Any]=3_0_5_2_2 ,  lowerCAmelCase_			:		List[str]=7_6_8 ,  lowerCAmelCase_			:		List[Any]=1_2 ,  lowerCAmelCase_			:		List[Any]=1_2 ,  lowerCAmelCase_			:		str=3_0_7_2 ,  lowerCAmelCase_			:		Any="gelu" ,  lowerCAmelCase_			:		Any=0.1 ,  lowerCAmelCase_			:		Dict=0.1 ,  lowerCAmelCase_			:		Optional[int]=5_1_2 ,  lowerCAmelCase_			:		Optional[Any]=0.02 ,  lowerCAmelCase_			:		Dict=1e-12 ,  lowerCAmelCase_			:		Optional[Any]=0 ,  lowerCAmelCase_			:		List[str]="absolute" ,  lowerCAmelCase_			:		Optional[int]=2 ,  lowerCAmelCase_			:		Dict=1_4_0_8 ,  **lowerCAmelCase_			:		List[Any] ,  )							->						Any:
     super().__init__(pad_token_id=lowerCAmelCase_ ,  **lowerCAmelCase_					)

     __lowerCAmelCase							     =     vocab_size
     __lowerCAmelCase							     =     hidden_size
     __lowerCAmelCase							     =     num_hidden_layers
     __lowerCAmelCase							     =     num_attention_heads
     __lowerCAmelCase							     =     hidden_act
     __lowerCAmelCase							     =     intermediate_size
     __lowerCAmelCase							     =     hidden_dropout_prob
     __lowerCAmelCase							     =     attention_probs_dropout_prob
     __lowerCAmelCase							     =     max_position_embeddings
     __lowerCAmelCase							     =     initializer_range
     __lowerCAmelCase							     =     layer_norm_eps
     __lowerCAmelCase							     =     position_embedding_type
     __lowerCAmelCase							     =     cross_attention_frequency
     __lowerCAmelCase							     =     encoder_hidden_size






  @classmethod
  def     lowercase		(    cls			:		Optional[Any] ,  lowerCAmelCase_			:		Union[str, os.PathLike] ,  **lowerCAmelCase_			:		Any					)							->						"PretrainedConfig":
     cls._set_token_in_kwargs(lowerCAmelCase_					)

     __lowerCAmelCase     ,     __lowerCAmelCase							     =     cls.get_config_dict(lowerCAmelCase_ ,  **lowerCAmelCase_					)

     # get the qformer config dict if we are loading from Blip2Config
     if config_dict.get('model_type'					) == "blip-2":
        __lowerCAmelCase							     =     config_dict['qformer_config']

     if "model_type" in config_dict and hasattr(cls ,  'model_type'					) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
            f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

     return cls.from_dict(lowerCAmelCase_ ,  **lowerCAmelCase_					)





class     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			"""blip-2"""
  a_      =			True






  def __init__(    self			:		int ,  lowerCAmelCase_			:		List[Any]=None ,  lowerCAmelCase_			:		int=None ,  lowerCAmelCase_			:		Optional[int]=None ,  lowerCAmelCase_			:		List[Any]=3_2 ,  **lowerCAmelCase_			:		Union[str, Any]					)							->						Union[str, Any]:
     super().__init__(**lowerCAmelCase_					)

     if vision_config is None:
        __lowerCAmelCase							     =     {}
        logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'					)

     if qformer_config is None:
        __lowerCAmelCase							     =     {}
        logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.'					)

     if text_config is None:
        __lowerCAmelCase							     =     {}
        logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'					)

     __lowerCAmelCase							     =     BlipaVisionConfig(**lowerCAmelCase_					)
     __lowerCAmelCase							     =     BlipaQFormerConfig(**lowerCAmelCase_					)
     __lowerCAmelCase							     =     text_config['model_type'] if 'model_type' in text_config else 'opt'
     __lowerCAmelCase							     =     CONFIG_MAPPING[text_model_type](**lowerCAmelCase_					)

     __lowerCAmelCase							     =     self.text_config.tie_word_embeddings
     __lowerCAmelCase							     =     self.text_config.is_encoder_decoder

     __lowerCAmelCase							     =     num_query_tokens
     __lowerCAmelCase							     =     self.vision_config.hidden_size
     __lowerCAmelCase							     =     self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
     __lowerCAmelCase							     =     1.0
     __lowerCAmelCase							     =     0.02






  @classmethod
  def     lowercase		(    cls			:		Optional[Any] ,  lowerCAmelCase_			:		BlipaVisionConfig ,  lowerCAmelCase_			:		BlipaQFormerConfig ,  lowerCAmelCase_			:		PretrainedConfig ,  **lowerCAmelCase_			:		Tuple ,  )							->						Optional[Any]:
     return cls(
         vision_config=vision_config.to_dict() ,  qformer_config=qformer_config.to_dict() ,  text_config=text_config.to_dict() ,  **lowerCAmelCase_ ,  )






  def     lowercase		(    self			:		Dict					)							->						Optional[Any]:
     __lowerCAmelCase							     =     copy.deepcopy(self.__dict__					)
     __lowerCAmelCase							     =     self.vision_config.to_dict()
     __lowerCAmelCase							     =     self.qformer_config.to_dict()
     __lowerCAmelCase							     =     self.text_config.to_dict()
     __lowerCAmelCase							     =     self.__class__.model_type
     return output








import argparse
import json
from typing import List

from ltp import LTP

from transformers.models.bert.tokenization_bert import BertTokenizer

def   a_   (			lowerCAmelCase_				:      Optional[Any]     ):
   # This defines a "chinese character" as anything in the CJK Unicode block:
   #   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
   #
   # Note that the CJK Unicode block is NOT all Japanese and Korean characters,
   # despite its name. The modern Korean Hangul alphabet is a different block,
   # as is Japanese Hiragana and Katakana. Those alphabets are used to write
   # space-separated words, so they are not treated specially and handled
   # like the all of the other languages.
   if (
       (cp >= 0X4e00 and cp <= 0X9fff)
       or (cp >= 0X3400 and cp <= 0X4dbf)  #
       or (cp >= 0X20000 and cp <= 0X2a6df)  #
       or (cp >= 0X2a700 and cp <= 0X2b73f)  #
       or (cp >= 0X2b740 and cp <= 0X2b81f)  #
       or (cp >= 0X2b820 and cp <= 0X2ceaf)  #
       or (cp >= 0Xf900 and cp <= 0Xfaff)
       or (cp >= 0X2f800 and cp <= 0X2fa1f)  #
   ):  #
      return True

   return False

def   a_   (			lowerCAmelCase_				:      str     ):
   # word like '180' or '身高' or '神'
   for char in word:
      __lowerCAmelCase							     =     ord(lowerCAmelCase_     )
      if not _is_chinese_char(lowerCAmelCase_     ):
         return 0
   return 1

def   a_   (			lowerCAmelCase_				:      List[str]     ):
   __lowerCAmelCase							     =     set()

   for token in tokens:
      __lowerCAmelCase							     =     len(lowerCAmelCase_     ) > 1 and is_chinese(lowerCAmelCase_     )
      if chinese_word:
         word_set.add(lowerCAmelCase_     )
   __lowerCAmelCase							     =     list(lowerCAmelCase_     )
   return word_list

def   a_   (			lowerCAmelCase_				:      List[str],     lowerCAmelCase_				:      set()     ):
   if not chinese_word_set:
      return bert_tokens
   __lowerCAmelCase							     =     max([len(lowerCAmelCase_     ) for w in chinese_word_set]     )

   __lowerCAmelCase							     =     bert_tokens
   __lowerCAmelCase     ,     __lowerCAmelCase							     =     0, len(lowerCAmelCase_     )
   while start < end:
      __lowerCAmelCase							     =     True
      if is_chinese(bert_word[start]     ):
         __lowerCAmelCase							     =     min(end - start,     lowerCAmelCase_     )
         for i in range(lowerCAmelCase_,     1,     -1     ):
            __lowerCAmelCase							     =     ''.join(bert_word[start : start + i]     )
            if whole_word in chinese_word_set:
               for j in range(start + 1,     start + i     ):
                  __lowerCAmelCase							     =     '##' + bert_word[j]
               __lowerCAmelCase							     =     start + i
               __lowerCAmelCase							     =     False
               break
      if single_word:
         start += 1
   return bert_word

def   a_   (			lowerCAmelCase_				:      List[str],     lowerCAmelCase_				:      LTP,     lowerCAmelCase_				:      BertTokenizer     ):
   __lowerCAmelCase							     =     []

   for i in range(0,     len(lowerCAmelCase_     ),     100     ):
      __lowerCAmelCase							     =     ltp_tokenizer.pipeline(lines[i : i + 100],     tasks=['cws']     ).cws
      __lowerCAmelCase							     =     [get_chinese_word(lowerCAmelCase_     ) for r in res]
      ltp_res.extend(lowerCAmelCase_     )
   assert len(lowerCAmelCase_     ) == len(lowerCAmelCase_     )

   __lowerCAmelCase							     =     []
   for i in range(0,     len(lowerCAmelCase_     ),     100     ):
      __lowerCAmelCase							     =     bert_tokenizer(lines[i : i + 100],     add_special_tokens=lowerCAmelCase_,     truncation=lowerCAmelCase_,     max_length=512     )
      bert_res.extend(res['input_ids']     )
   assert len(lowerCAmelCase_     ) == len(lowerCAmelCase_     )

   __lowerCAmelCase							     =     []
   for input_ids, chinese_word in zip(lowerCAmelCase_,     lowerCAmelCase_     ):
      __lowerCAmelCase							     =     []
      for id in input_ids:
         __lowerCAmelCase							     =     bert_tokenizer._convert_id_to_token(lowerCAmelCase_     )
         input_tokens.append(lowerCAmelCase_     )
      __lowerCAmelCase							     =     add_sub_symbol(lowerCAmelCase_,     lowerCAmelCase_     )
      __lowerCAmelCase							     =     []
      # We only save pos of chinese subwords start with ##, which mean is part of a whole word.
      for i, token in enumerate(lowerCAmelCase_     ):
         if token[:2] == "##":
            __lowerCAmelCase							     =     token[2:]
            # save chinese tokens' pos
            if len(lowerCAmelCase_     ) == 1 and _is_chinese_char(ord(lowerCAmelCase_     )     ):
               ref_id.append(lowerCAmelCase_     )
      ref_ids.append(lowerCAmelCase_     )

   assert len(lowerCAmelCase_     ) == len(lowerCAmelCase_     )

   return ref_ids

def   a_   (			lowerCAmelCase_				:      int     ):
   # For Chinese (Ro)Bert, the best result is from : RoBERTa-wwm-ext (https://github.com/ymcui/Chinese-BERT-wwm)
   # If we want to fine-tune these model, we have to use same tokenizer : LTP (https://github.com/HIT-SCIR/ltp)
   with open(args.file_name,     'r',     encoding='utf-8'     ) as f:
      __lowerCAmelCase							     =     f.readlines()
   __lowerCAmelCase							     =     [line.strip() for line in data if len(lowerCAmelCase_     ) > 0 and not line.isspace()]  # avoid delimiter like '\u2029'
   __lowerCAmelCase							     =     LTP(args.ltp     )  # faster in GPU device
   __lowerCAmelCase							     =     BertTokenizer.from_pretrained(args.bert     )

   __lowerCAmelCase							     =     prepare_ref(lowerCAmelCase_,     lowerCAmelCase_,     lowerCAmelCase_     )

   with open(args.save_path,     'w',     encoding='utf-8'     ) as f:
      __lowerCAmelCase							     =     [json.dumps(lowerCAmelCase_     ) + '\n' for ref in ref_ids]
      f.writelines(lowerCAmelCase_     )


if __name__ == "__main__":
     _snake_case  :   int						   =  argparse.ArgumentParser(description='prepare_chinese_ref')
     parser.add_argument(
         '--file_name',
         required=False,
         type=str,
         default='./resources/chinese-demo.txt',
         help='file need process, same as training data in lm',
     )
     parser.add_argument(
         '--ltp',
         required=False,
         type=str,
         default='./resources/ltp',
         help='resources for LTP tokenizer, usually a path',
     )
     parser.add_argument(
         '--bert',
         required=False,
         type=str,
         default='./resources/robert',
         help='resources for Bert tokenizer',
     )
     parser.add_argument(
         '--save_path',
         required=False,
         type=str,
         default='./resources/ref.txt',
         help='path to save res',
     )

     _snake_case  :   List[str]						   =  parser.parse_args()
     main(args)

