"""simple docstring"""
import collections
import json
import math
import os
import re
import time
from fnmatch import fnmatch
from typing import Dict

import requests
from slack_sdk import WebClient


lowercase__ 	=     WebClient(token=os.environ['CI_SLACK_BOT_TOKEN'])







def 						__a       (     _SCREAMING_SNAKE_CASE       )     ->List[Any]:
  a__:					Optional[Any]      							=     test_results.split(' '       )

  a__:					Optional[Any]      							=     0
  a__:					Tuple      							=     0

  # When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
  # When it is too long, those signs are not present.
  a__:					Dict      							=     expressions[-2] if '=' in expressions[-1] else expressions[-1]

  for i, expression in enumerate(lowercase__       ):
    if "failed" in expression:
      failed += int(expressions[i - 1]       )
    if "passed" in expression:
      success += int(expressions[i - 1]       )

  return failed, success, time_spent







def 						__a       (     _SCREAMING_SNAKE_CASE       )     ->str:
  a__:					str      							=     {}
  a__:					Optional[int]      							=     None
  a__:					Dict      							=     False
  for line in failures_short_lines.split('\n'       ):
    if re.search(r'_ \[doctest\]'		,					lowercase__       ):
      a__:					List[Any]      							=     True
      a__:					Union[str, Any]      							=     line.split(' '       )[2]
    elif in_error and not line.split(' '       )[0].isdigit():
      a__:					Dict      							=     line
      a__:					Optional[Any]      							=     False

  return failures




class        __snake_case	:



      def __init__(						self   ,      lowercase   ,      lowercase)     ->      Tuple:





        '''simple docstring'''





        a__:					Tuple      							=     title

        a__:					Any      							=     doc_test_results['time_spent'].split(',')[0]
        a__:					List[Any]      							=     doc_test_results['success']
        a__:					Any      							=     doc_test_results['failures']
        a__:					Any      							=     self.n_success + self.n_failures

        # Failures and success of the modeling tests
        a__:					int      							=     doc_test_results



      @property
      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        a__:					Tuple      							=     [self._time_spent]
        a__:					str      							=     0

        for time in time_spent:
          a__:					List[Any]      							=     time.split(':')

          # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute.
          if len(lowercase) == 1:
            a__:					Union[str, Any]      							=     [0, 0, time_parts[0]]

          a__:					int      							=     int(time_parts[0]), int(time_parts[1]), float(time_parts[2])
          total_secs += hours * 36_00 + minutes * 60 + seconds

        a__:					Tuple      							=     total_secs // 36_00, (total_secs % 36_00) // 60, total_secs % 60
        return f'{int(lowercase)}h{int(lowercase)}m{int(lowercase)}s'



      @property
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        return {"type": "header", "text": {"type": "plain_text", "text": self.title}}



      @property
      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        return {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": f'🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.',
                "emoji": True,
            },
            "accessory": {
                "type": "button",
                "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
                "url": f'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}',
            },
        }



      @property
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        return {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": (
                    f'There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in'
                    f' {self.time}.'
                ),
                "emoji": True,
            },
            "accessory": {
                "type": "button",
                "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
                "url": f'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}',
            },
        }



      @property
      def   lowerCamelCase_       (						self)     ->      List[Any]:





        '''simple docstring'''





        a__:					str      							=     40
        a__:					List[str]      							=     {k: v['failed'] for k, v in doc_test_results.items() if isinstance(lowercase   ,      lowercase)}

        a__:					Union[str, Any]      							=     ''
        for category, failures in category_failures.items():
          if len(lowercase) == 0:
            continue

          if report != "":
            report += "\n\n"

          report += f'*{category} failures*:'.ljust(line_length // 2).rjust(line_length // 2) + "\n"
          report += "`"
          report += "`\n`".join(lowercase)
          report += "`"

        return {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": f'The following examples had failures:\n\n\n{report}\n',
            },
        }



      @property
      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        a__:					List[Any]      							=     [self.header]

        if self.n_failures > 0:
          blocks.append(self.failures)

        if self.n_failures > 0:
          blocks.extend([self.category_failures])

        if self.n_failures == 0:
          blocks.append(self.no_failures)

        return json.dumps(lowercase)



      @staticmethod
      def   lowerCamelCase_       (						)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     [
            {
                'type': 'section',
                'text': {
                    'type': 'plain_text',
                    'text': 'There was an issue running the tests.',
                },
                'accessory': {
                    'type': 'button',
                    'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True},
                    'url': f'https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}',
                },
            }
        ]

        print('Sending the following payload')
        print(json.dumps({'blocks': json.loads(lowercase)}))

        client.chat_postMessage(
            channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']   ,      text='There was an issue running the tests.'   ,      blocks=lowercase   ,      )



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





        '''simple docstring'''





        print('Sending the following payload')
        print(json.dumps({'blocks': json.loads(self.payload)}))

        a__:					List[Any]      							=     f'{self.n_failures} failures out of {self.n_tests} tests,' if self.n_failures else 'All tests passed.'

        a__:					Dict      							=     client.chat_postMessage(
            channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']   ,      blocks=self.payload   ,      text=lowercase   ,      )



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase)     ->      List[Any]:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     ''
        for key, value in failures.items():
          a__:					Tuple      							=     value[:2_00] + ' [Truncated]' if len(lowercase) > 2_50 else value
          failures_text += f'*{key}*\n_{value}_\n\n'

        a__:					Optional[Any]      							=     job_name
        a__:					str      							=     {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}}

        if job_link is not None:
          a__:					Optional[int]      							=     {
              'type': 'button',
              'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True},
              'url': job_link,
          }

        return [
            {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}},
            content,
            {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}},
        ]



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





        '''simple docstring'''





        if self.thread_ts is None:
          raise ValueError('Can only post reply if a post has been made.')

        a__:					str      							=     self.doc_test_results.pop('job_link')
        self.doc_test_results.pop('failures')
        self.doc_test_results.pop('success')
        self.doc_test_results.pop('time_spent')

        a__:					int      							=     sorted(self.doc_test_results.items()   ,      key=lambda lowercase: t[0])
        for job, job_result in sorted_dict:
          if len(job_result['failures']):
            a__:					List[str]      							=     f'*Num failures* :{len(job_result["failed"])} \n'
            a__:					Any      							=     job_result['failures']
            a__:					Any      							=     self.get_reply_blocks(lowercase   ,      lowercase   ,      lowercase   ,      text=lowercase)

            print('Sending the following reply')
            print(json.dumps({'blocks': blocks}))

            client.chat_postMessage(
                channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']   ,      text=f'Results for {job}'   ,      blocks=lowercase   ,      thread_ts=self.thread_ts['ts']   ,      )

            time.sleep(1)







def 						__a       (     )     ->List[str]:
  a__:					Optional[Any]      							=     os.environ['GITHUB_RUN_ID']
  a__:					Tuple      							=     F'https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'
  a__:					Optional[Any]      							=     requests.get(lowercase__       ).json()
  a__:					Union[str, Any]      							=     {}

  try:
    jobs.update({job['name']: job['html_url'] for job in result['jobs']}       )
    a__:					List[Any]      							=     math.ceil((result['total_count'] - 100) / 100       )

    for i in range(lowercase__       ):
      a__:					Tuple      							=     requests.get(url + F'&page={i + 2}'       ).json()
      jobs.update({job['name']: job['html_url'] for job in result['jobs']}       )

    return jobs
  except Exception as e:
    print('Unknown error, could not fetch links.'		,					lowercase__       )

  return {}







def 						__a       (     _SCREAMING_SNAKE_CASE       )     ->List[Any]:
  a__:					List[Any]      							=     {}

  if os.path.exists(lowercase__       ):
    a__:					List[Any]      							=     os.listdir(lowercase__       )
    for file in files:
      try:
        with open(os.path.join(lowercase__		,					lowercase__       )		,					encoding='utf-8'       ) as f:
          a__:					Optional[int]      							=     f.read()
      except UnicodeDecodeError as e:
        raise ValueError(F'Could not open {os.path.join(lowercase__		,					lowercase__       )}.'       ) from e

  return _artifact







def 						__a       (     )     ->Optional[Any]:




  class        __snake_case	:



        def __init__(						self   ,      lowercase)     ->      Any:





          '''simple docstring'''





          a__:					Optional[Any]      							=     name
          a__:					Dict      							=     []



        def __str__(						self)     ->      Tuple:





          '''simple docstring'''





          return self.name



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





          '''simple docstring'''





          self.paths.append({'name': self.name, 'path': path})







  a__:					Dict[str, Artifact]      							=     {}

  a__:					Optional[Any]      							=     filter(os.path.isdir		,					os.listdir()       )
  for directory in directories:
    a__:					Dict      							=     directory
    if artifact_name not in _available_artifacts:
      a__:					List[str]      							=     Artifact(lowercase__       )

      _available_artifacts[artifact_name].add_path(lowercase__       )

  return _available_artifacts


if __name__ == "__main__":
 lowercase__ 	=     get_job_links()
 lowercase__ 	=     retrieve_available_artifacts()

 lowercase__ 	=     collections.OrderedDict(
     [
         ('*.py', 'API Examples'),
         ('*.md', 'MD Examples'),
     ]
 )

 # This dict will contain all the information relative to each doc test category:
 # - failed: list of failed tests
 # - failures: dict in the format 'test': 'error_message'
 lowercase__ 	=     {
     v: {
         'failed': [],
         'failures': {},
     }
     for v in docs.values()
 }

 # Link to the GitHub Action job
 lowercase__ 	=     github_actions_job_links.get('run_doctests')

 lowercase__ 	=     available_artifacts['doc_tests_gpu_test_reports'].paths[0]
 lowercase__ 	=     retrieve_artifact(artifact_path['name'])
 if "stats" in artifact:
  lowercase__	, lowercase__	, lowercase__ 	=     handle_test_results(artifact['stats'])
  lowercase__ 	=     failed
  lowercase__ 	=     success
  lowercase__ 	=     time_spent[1:-1] + ', '

  lowercase__ 	=     extract_first_line_failure(artifact['failures_short'])
  for line in artifact["summary_short"].split('\n'):
   if re.search('FAILED', line):
    lowercase__ 	=     line.replace('FAILED ', '')
    lowercase__ 	=     line.split()[0].replace('\n', '')

    if "::" in line:
     lowercase__	, lowercase__ 	=     line.split('::')
    else:
     lowercase__	, lowercase__ 	=     line, line

    for file_regex in docs.keys():
     if fnmatch(file_path, file_regex):
      lowercase__ 	=     docs[file_regex]
      doc_test_results[category]["failed"].append(test)

      lowercase__ 	=     all_failures[test] if test in all_failures else 'N/A'
      lowercase__ 	=     failure
      break

 lowercase__ 	=     Message('🤗 Results of the doc tests.', doc_test_results)
 message.post()
 message.post_reply()






"""simple docstring"""
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowercase__ 	=     {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

lowercase__ 	=     {
    'vocab_file': {
        'unc-nlp/lxmert-base-uncased': 'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt',
    },
    'tokenizer_file': {
        'unc-nlp/lxmert-base-uncased': (
            'https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json'
        ),
    },
}

lowercase__ 	=     {
    'unc-nlp/lxmert-base-uncased': 512,
}

lowercase__ 	=     {
    'unc-nlp/lxmert-base-uncased': {'do_lower_case': True},
}




class        __snake_case	(					__lowerCAmelCase    ):
      a__				=		VOCAB_FILES_NAMES
      a__				=		PRETRAINED_VOCAB_FILES_MAP
      a__				=		PRETRAINED_INIT_CONFIGURATION
      a__				=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      a__				=		LxmertTokenizer



      def __init__(						self   ,      lowercase=None   ,      lowercase=None   ,      lowercase=True   ,      lowercase="[UNK]"   ,      lowercase="[SEP]"   ,      lowercase="[PAD]"   ,      lowercase="[CLS]"   ,      lowercase="[MASK]"   ,      lowercase=True   ,      lowercase=None   ,      **lowercase   ,      )     ->      Dict:





        '''simple docstring'''





        super().__init__(
            lowercase   ,      tokenizer_file=lowercase   ,      do_lower_case=lowercase   ,      unk_token=lowercase   ,      sep_token=lowercase   ,      pad_token=lowercase   ,      cls_token=lowercase   ,      mask_token=lowercase   ,      tokenize_chinese_chars=lowercase   ,      strip_accents=lowercase   ,      **lowercase   ,      )

        a__:					Dict      							=     json.loads(self.backend_tokenizer.normalizer.__getstate__())
        if (
            normalizer_state.get('lowercase'   ,      lowercase) != do_lower_case
            or normalizer_state.get('strip_accents'   ,      lowercase) != strip_accents
            or normalizer_state.get('handle_chinese_chars'   ,      lowercase) != tokenize_chinese_chars
        ):
          a__:					int      							=     getattr(lowercase   ,      normalizer_state.pop('type'))
          a__:					Dict      							=     do_lower_case
          a__:					Dict      							=     strip_accents
          a__:					Optional[int]      							=     tokenize_chinese_chars
          a__:					List[Any]      							=     normalizer_class(**lowercase)

        a__:					Optional[int]      							=     do_lower_case



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase=None)     ->      Tuple:





        '''simple docstring'''





        a__:					Optional[Any]      							=     [self.cls_token_id] + token_ids_a + [self.sep_token_id]

        if token_ids_a:
          output += token_ids_a + [self.sep_token_id]

        return output



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase = None)     ->      List[int]:





        '''simple docstring'''





        a__:					List[Any]      							=     [self.sep_token_id]
        a__:					List[Any]      							=     [self.cls_token_id]
        if token_ids_a is None:
          return len(cls + token_ids_a + sep) * [0]
        return len(cls + token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase = None)     ->      Tuple[str]:





        '''simple docstring'''





        a__:					List[Any]      							=     self._tokenizer.model.save(lowercase   ,      name=lowercase)
        return tuple(lowercase)


