





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


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




def  __UpperCAmelCase(   lowercase_   ):
       _lowerCamelCase				:  Any						=		test_results.split(''' '''   )

       _lowerCamelCase				:  Dict						=		0
       _lowerCamelCase				:  List[str]						=		0

       # When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
       # When it is too long, those signs are not present.
       _lowerCamelCase				:  Union[str, Any]						=		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  __UpperCAmelCase(   lowercase_   ):
       _lowerCamelCase				:  Dict						=		{}
       _lowerCamelCase				:  int						=		None
       _lowerCamelCase				:  Any						=		False
       for line in failures_short_lines.split('''\n'''   ):
              if re.search(r'''_ \[doctest\]'''			,     lowercase_   ):
                     _lowerCamelCase				:  Optional[int]						=		True
                     _lowerCamelCase				:  Dict						=		line.split(''' '''   )[2]
              elif in_error and not line.split(''' '''   )[0].isdigit():
                     _lowerCamelCase				:  Dict						=		line
                     _lowerCamelCase				:  Any						=		False

       return failures







class       __A							:




      """simple docstring"""







      def __init__(		self   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Union[str, Any]						=		title

             _lowerCamelCase				:  Tuple						=		doc_test_results['''time_spent'''].split(''',''')[0]
             _lowerCamelCase				:  int						=		doc_test_results['''success''']
             _lowerCamelCase				:  List[str]						=		doc_test_results['''failures''']
             _lowerCamelCase				:  Dict						=		self.n_success + self.n_failures

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


      @property
      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		[self._time_spent]
             _lowerCamelCase				:  List[Any]						=		0

             for time in time_spent:
                    _lowerCamelCase				:  Optional[int]						=		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(a__) == 1:
                           _lowerCamelCase				:  Any						=		[0, 0, time_parts[0]]

                    _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  Union[str, Any]						=		int(time_parts[0]), int(time_parts[1]), float(time_parts[2])
                    total_secs += hours * 3600 + minutes * 60 + seconds

             _lowerCamelCase,	_lowerCamelCase,	_lowerCamelCase				:  Any						=		total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
             return F"""{int(a__)}h{int(a__)}m{int(a__)}s"""


      @property
      def 					__snake_case      (		self):



             """simple docstring"""





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


      @property
      def 					__snake_case      (		self):



             """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 					__snake_case      (		self):



             """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 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  Dict						=		''''''
             for category, failures in category_failures.items():
                    if len(a__) == 0:
                           continue

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

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

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


      @property
      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		[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(a__)


      @staticmethod
      def 					__snake_case      (		):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		[
                 {
                     '''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(a__)}))

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


      def 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  Any						=		F"""{self.n_failures} failures out of {self.n_tests} tests,""" if self.n_failures else '''All tests passed.'''

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


      def 					__snake_case      (		self   ,				a__   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		''''''
             for key, value in failures.items():
                    _lowerCamelCase				:  int						=		value[:200] + ''' [Truncated]''' if len(a__) > 250 else value
                    failures_text += F"""*{key}*\n_{value}_\n\n"""

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

             if job_link is not None:
                    _lowerCamelCase				:  Dict						=		{
                        '''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 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  Optional[int]						=		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''')

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

                           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=a__   ,				thread_ts=self.thread_ts['''ts''']   ,				)

                           time.sleep(1)




def  __UpperCAmelCase(   ):
       _lowerCamelCase				:  Any						=		os.environ['''GITHUB_RUN_ID''']
       _lowerCamelCase				:  Any						=		F"""https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100"""
       _lowerCamelCase				:  int						=		requests.get(lowercase_   ).json()
       _lowerCamelCase				:  Optional[Any]						=		{}

       try:
              jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']}   )
              _lowerCamelCase				:  int						=		math.ceil((result['''total_count'''] - 1_00) / 1_00   )

              for i in range(lowercase_   ):
                     _lowerCamelCase				:  str						=		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  __UpperCAmelCase(   lowercase_   ):
       _lowerCamelCase				:  int						=		{}

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

       return _artifact




def  __UpperCAmelCase(   ):







       class       __A							:




             """simple docstring"""







             def __init__(		self   ,				a__):



                    """simple docstring"""





                    _lowerCamelCase				:  Union[str, Any]						=		name
                    _lowerCamelCase				:  str						=		[]


             def __str__(		self):



                    """simple docstring"""





                    return self.name



             def 					__snake_case      (		self   ,				a__):



                    """simple docstring"""





                    self.paths.append({'''name''': self.name, '''path''': path})
       _lowerCamelCase				:  Dict[str, Artifact]						=		{}

       _lowerCamelCase				:  Tuple						=		filter(os.path.isdir			,     os.listdir()   )
       for directory in directories:
              _lowerCamelCase				:  List[str]						=		directory
              if artifact_name not in _available_artifacts:
                     _lowerCamelCase				:  str						=		Artifact(lowercase_   )

                     _available_artifacts[artifact_name].add_path(lowercase_   )

       return _available_artifacts


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

       _lowerCamelCase      							=   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'
       _lowerCamelCase      							=   {
           v: {
               'failed': [],
               'failures': {},
           }
           for v in docs.values()
       }

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

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

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

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

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

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

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








import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
       from PIL import Image

       from transformers import (
           AutoProcessor,
           BertTokenizerFast,
           BlipImageProcessor,
           GPTaTokenizer,
           InstructBlipProcessor,
           PreTrainedTokenizerFast,
       )







@require_vision
class       __A							(   unittest.TestCase	):




      """simple docstring"""







      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		tempfile.mkdtemp()

             _lowerCamelCase				:  List[str]						=		BlipImageProcessor()
             _lowerCamelCase				:  Union[str, Any]						=		GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''')
             _lowerCamelCase				:  Any						=		BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''')

             _lowerCamelCase				:  Any						=		InstructBlipProcessor(a__   ,				a__   ,				a__)

             processor.save_pretrained(self.tmpdirname)


      def 					__snake_case      (		self   ,				**a__):



             """simple docstring"""





             return AutoProcessor.from_pretrained(self.tmpdirname   ,				**a__).tokenizer


      def 					__snake_case      (		self   ,				**a__):



             """simple docstring"""





             return AutoProcessor.from_pretrained(self.tmpdirname   ,				**a__).image_processor


      def 					__snake_case      (		self   ,				**a__):



             """simple docstring"""





             return AutoProcessor.from_pretrained(self.tmpdirname   ,				**a__).qformer_tokenizer


      def 					__snake_case      (		self):



             """simple docstring"""





             shutil.rmtree(self.tmpdirname)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		[np.random.randint(255   ,				size=(3, 30, 400)   ,				dtype=np.uinta)]

             _lowerCamelCase				:  List[str]						=		[Image.fromarray(np.moveaxis(a__   ,				0   ,				-1)) for x in image_inputs]

             return image_inputs


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		InstructBlipProcessor(
                 tokenizer=self.get_tokenizer()   ,				image_processor=self.get_image_processor()   ,				qformer_tokenizer=self.get_qformer_tokenizer()   ,				)
             processor.save_pretrained(self.tmpdirname)

             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer(bos_token='''(BOS)'''   ,				eos_token='''(EOS)''')
             _lowerCamelCase				:  str						=		self.get_image_processor(do_normalize=a__   ,				padding_value=1.0)

             _lowerCamelCase				:  Tuple						=		InstructBlipProcessor.from_pretrained(
                 self.tmpdirname   ,				bos_token='''(BOS)'''   ,				eos_token='''(EOS)'''   ,				do_normalize=a__   ,				padding_value=1.0)

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

             self.assertEqual(processor.image_processor.to_json_string()   ,				image_processor_add_kwargs.to_json_string())
             self.assertIsInstance(processor.image_processor   ,				a__)
             self.assertIsInstance(processor.qformer_tokenizer   ,				a__)


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		self.get_image_processor()
             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer()
             _lowerCamelCase				:  Any						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Dict						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		self.prepare_image_inputs()

             _lowerCamelCase				:  List[str]						=		image_processor(a__   ,				return_tensors='''np''')
             _lowerCamelCase				:  List[Any]						=		processor(images=a__   ,				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 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		self.get_image_processor()
             _lowerCamelCase				:  Dict						=		self.get_tokenizer()
             _lowerCamelCase				:  int						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Tuple						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		'''lower newer'''

             _lowerCamelCase				:  Any						=		processor(text=a__)

             _lowerCamelCase				:  Optional[int]						=		tokenizer(a__   ,				return_token_type_ids=a__)
             _lowerCamelCase				:  Optional[Any]						=		qformer_tokenizer(a__   ,				return_token_type_ids=a__)

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

             for key in encoded_tokens_qformer.keys():
                    self.assertListEqual(encoded_tokens_qformer[key]   ,				encoded_processor['''qformer_''' + key])


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		self.get_image_processor()
             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer()
             _lowerCamelCase				:  Optional[Any]						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  List[Any]						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		'''lower newer'''
             _lowerCamelCase				:  Tuple						=		self.prepare_image_inputs()

             _lowerCamelCase				:  Tuple						=		processor(text=a__   ,				images=a__)

             self.assertListEqual(
                 list(inputs.keys())   ,				['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values''']   ,				)

             # test if it raises when no input is passed
             with pytest.raises(a__):
                    processor()


      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  int						=		self.get_image_processor()
             _lowerCamelCase				:  List[Any]						=		self.get_tokenizer()
             _lowerCamelCase				:  List[Any]						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Optional[Any]						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

             _lowerCamelCase				:  Tuple						=		processor.batch_decode(a__)
             _lowerCamelCase				:  str						=		tokenizer.batch_decode(a__)

             self.assertListEqual(a__   ,				a__)



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		self.get_image_processor()
             _lowerCamelCase				:  Dict						=		self.get_tokenizer()
             _lowerCamelCase				:  str						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  str						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  str						=		'''lower newer'''
             _lowerCamelCase				:  str						=		self.prepare_image_inputs()

             _lowerCamelCase				:  List[Any]						=		processor(text=a__   ,				images=a__)

             self.assertListEqual(
                 list(inputs.keys())   ,				['''input_ids''', '''attention_mask''', '''qformer_input_ids''', '''qformer_attention_mask''', '''pixel_values''']   ,				)

