




import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import UperNetForSemanticSegmentation
	from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor


class    lowerCamelCase    :






			'''simple docstring'''





			def __init__( self			:   Optional[int]       ,						lowerCamelCase_			:   Optional[Any]       ,						lowerCamelCase_			:   Optional[Any]=13       ,						lowerCamelCase_			:   Union[str, Any]=32       ,						lowerCamelCase_			:   Optional[Any]=3       ,						lowerCamelCase_			:   Dict=4       ,						lowerCamelCase_			:   Optional[int]=[10, 20, 30, 40]       ,						lowerCamelCase_			:   Optional[Any]=[2, 2, 3, 2]       ,						lowerCamelCase_			:   int=True       ,						lowerCamelCase_			:   Optional[Any]=True       ,						lowerCamelCase_			:   int=37       ,						lowerCamelCase_			:   str="gelu"       ,						lowerCamelCase_			:   int=10       ,						lowerCamelCase_			:   int=0.0_2       ,						lowerCamelCase_			:   Dict=["stage2", "stage3", "stage4"]       ,						lowerCamelCase_			:   int=3       ,						lowerCamelCase_			:   Union[str, Any]=None       ,						)						->   Tuple:
								__magic_name__							:	Optional[Any]    							=							parent
								__magic_name__							:	Union[str, Any]    							=							batch_size
								__magic_name__							:	Union[str, Any]    							=							image_size
								__magic_name__							:	int    							=							num_channels
								__magic_name__							:	List[str]    							=							num_stages
								__magic_name__							:	Dict    							=							hidden_sizes
								__magic_name__							:	str    							=							depths
								__magic_name__							:	List[Any]    							=							is_training
								__magic_name__							:	int    							=							use_labels
								__magic_name__							:	Dict    							=							intermediate_size
								__magic_name__							:	Tuple    							=							hidden_act
								__magic_name__							:	List[Any]    							=							type_sequence_label_size
								__magic_name__							:	List[Any]    							=							initializer_range
								__magic_name__							:	List[str]    							=							out_features
								__magic_name__							:	str    							=							num_labels
								__magic_name__							:	List[Any]    							=							scope
								__magic_name__							:	Any    							=							num_stages

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Optional[Any]:
								__magic_name__							:	Optional[int]    							=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

								__magic_name__							:	List[Any]    							=							None
								if self.use_labels:
													__magic_name__							:	str    							=							ids_tensor([self.batch_size]       ,						self.type_sequence_label_size  )

								__magic_name__							:	List[Any]    							=							self.get_config()

								return config, pixel_values, labels

			def 							UpperCAmelCase__       ( self			:   Any  )						->   Union[str, Any]:
								return ConvNextConfig(
								    num_channels=self.num_channels       ,						num_stages=self.num_stages       ,						hidden_sizes=self.hidden_sizes       ,						depths=self.depths       ,						is_training=self.is_training       ,						intermediate_size=self.intermediate_size       ,						hidden_act=self.hidden_act       ,						out_features=self.out_features       ,						)

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Union[str, Any]:
								return UperNetConfig(
								    backbone_config=self.get_backbone_config()       ,						hidden_size=512       ,						pool_scales=[1, 2, 3, 6]       ,						use_auxiliary_head=lowerCamelCase_       ,						auxiliary_loss_weight=0.4       ,						auxiliary_in_channels=40       ,						auxiliary_channels=256       ,						auxiliary_num_convs=1       ,						auxiliary_concat_input=lowerCamelCase_       ,						loss_ignore_index=255       ,						num_labels=self.num_labels       ,						)

			def 							UpperCAmelCase__       ( self			:   str       ,						lowerCamelCase_			:   str       ,						lowerCamelCase_			:   Dict       ,						lowerCamelCase_			:   Any  )						->   List[str]:
								__magic_name__							:	Any    							=							UperNetForSemanticSegmentation(config=lowerCamelCase_  )
								model.to(lowerCamelCase_  )
								model.eval()
								__magic_name__							:	int    							=							model(lowerCamelCase_  )
								self.parent.assertEqual(
								    result.logits.shape       ,						(self.batch_size, self.num_labels, self.image_size, self.image_size)  )


			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Optional[int]:
								__magic_name__							:	Tuple    							=							self.prepare_config_and_inputs()
								(
								    (
								    __magic_name__
								)							,    (
								    __magic_name__
								)							,    (
								    __magic_name__
								)							,    
								)							:	Dict    							=							config_and_inputs
								__magic_name__							:	Tuple    							=							{'''pixel_values''': pixel_values}
								return config, inputs_dict






@require_torch
class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase				,unittest.TestCase						):






			'''simple docstring'''





			UpperCamelCase__								=(UperNetForSemanticSegmentation,) if is_torch_available() else ()
			UpperCamelCase__								={'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False
			UpperCamelCase__								=False

			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Any:
								__magic_name__							:	Dict    							=							UperNetModelTester(self  )
								__magic_name__							:	int    							=							ConfigTester(self       ,						config_class=lowerCamelCase_       ,						has_text_modality=lowerCamelCase_       ,						hidden_size=37  )

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Union[str, Any]:
								self.create_and_test_config_common_properties()
								self.config_tester.create_and_test_config_to_json_string()
								self.config_tester.create_and_test_config_to_json_file()
								self.config_tester.create_and_test_config_from_and_save_pretrained()
								self.config_tester.create_and_test_config_with_num_labels()
								self.config_tester.check_config_can_be_init_without_params()
								self.config_tester.check_config_arguments_init()

			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Union[str, Any]:
								return

			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Dict:
								__magic_name__							,    __magic_name__							:	Optional[int]    							=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__							:	List[Any]    							=							model_class(lowerCamelCase_  )
													__magic_name__							:	List[str]    							=							inspect.signature(model.forward  )
													# signature.parameters is an OrderedDict => so arg_names order is deterministic
													__magic_name__							:	Optional[int]    							=							[*signature.parameters.keys()]

													__magic_name__							:	int    							=							['''pixel_values''']
													self.assertListEqual(arg_names[:1]       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   List[Any]:
								__magic_name__							:	Union[str, Any]    							=							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase_  )

			@unittest.skip(reason='''UperNet does not use inputs_embeds'''  )
			def 							UpperCAmelCase__       ( self			:   int  )						->   List[str]:
								pass

			@unittest.skip(reason='''UperNet does not support input and output embeddings'''  )
			def 							UpperCAmelCase__       ( self			:   str  )						->   List[Any]:
								pass

			@unittest.skip(reason='''UperNet does not have a base model'''  )
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Optional[Any]:
								pass

			@unittest.skip(reason='''UperNet does not have a base model'''  )
			def 							UpperCAmelCase__       ( self			:   int  )						->   List[Any]:
								pass

			@require_torch_multi_gpu
			@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''  )
			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   int:
								pass

			@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   List[str]:
								pass

			def 							UpperCAmelCase__       ( self			:   Any  )						->   Any:
								def check_hidden_states_output(lowerCamelCase_			:   List[str]       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   Union[str, Any]  ):
													__magic_name__							:	Optional[Any]    							=							model_class(lowerCamelCase_  )
													model.to(lowerCamelCase_  )
													model.eval()

													with torch.no_grad():
																		__magic_name__							:	Tuple    							=							model(**self._prepare_for_class(lowerCamelCase_       ,						lowerCamelCase_  )  )

													__magic_name__							:	List[str]    							=							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													__magic_name__							:	Any    							=							self.model_tester.num_stages
													self.assertEqual(len(lowerCamelCase_  )       ,						expected_num_stages + 1  )

													# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
													self.assertListEqual(
													    list(hidden_states[0].shape[-2:]  )       ,						[self.model_tester.image_size // 4, self.model_tester.image_size // 4]       ,						)

								__magic_name__							,    __magic_name__							:	Union[str, Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
													__magic_name__							:	int    							=							True
													check_hidden_states_output(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													__magic_name__							:	str    							=							True

													check_hidden_states_output(lowerCamelCase_       ,						lowerCamelCase_       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   List[Any]:
								__magic_name__							,    __magic_name__							:	List[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()

								__magic_name__							:	str    							=							_config_zero_init(lowerCamelCase_  )
								__magic_name__							:	List[Any]    							=							_config_zero_init(configs_no_init.backbone_config  )
								for model_class in self.all_model_classes:
													__magic_name__							:	List[Any]    							=							model_class(config=lowerCamelCase_  )
													for name, param in model.named_parameters():
																		if param.requires_grad:
																							self.assertIn(
																							    ((param.data.mean() * 1E9).round() / 1E9).item()       ,						[0.0, 1.0]       ,						msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''       ,						)

			@unittest.skip(reason='''UperNet does not have tied weights'''  )
			def 							UpperCAmelCase__       ( self			:   Any  )						->   List[str]:
								pass


			@slow
			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Union[str, Any]:
								for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__magic_name__							:	List[Any]    							=							UperNetForSemanticSegmentation.from_pretrained(lowerCamelCase_  )
													self.assertIsNotNone(lowerCamelCase_  )







def 		lowercase__   (		):





					'''simple docstring'''






					__magic_name__							:	Optional[Any]    							=							hf_hub_download(
					    repo_id='''hf-internal-testing/fixtures_ade20k'''							,repo_type='''dataset'''							,filename='''ADE_val_00000001.jpg'''				)
					__magic_name__							:	List[str]    							=							Image.open(__A				).convert('''RGB'''				)
					return image


@require_torch
@require_vision
@slow
class    lowerCamelCase    (		unittest.TestCase						):






			'''simple docstring'''





			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   List[Any]:
								__magic_name__							:	Optional[int]    							=							AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''  )
								__magic_name__							:	str    							=							UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''  ).to(lowerCamelCase_  )

								__magic_name__							:	List[Any]    							=							prepare_img()
								__magic_name__							:	Union[str, Any]    							=							processor(images=lowerCamelCase_       ,						return_tensors='''pt'''  ).to(lowerCamelCase_  )

								with torch.no_grad():
													__magic_name__							:	int    							=							model(**lowerCamelCase_  )

								__magic_name__							:	str    							=							torch.Size((1, model.config.num_labels, 512, 512)  )
								self.assertEqual(outputs.logits.shape       ,						lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							torch.tensor(
								    [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]]  ).to(lowerCamelCase_  )
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,						lowerCamelCase_       ,						atol=1E-4  )  )


			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   str:
								__magic_name__							:	List[str]    							=							AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''  )
								__magic_name__							:	Optional[int]    							=							UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''  ).to(lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							prepare_img()
								__magic_name__							:	List[str]    							=							processor(images=lowerCamelCase_       ,						return_tensors='''pt'''  ).to(lowerCamelCase_  )

								with torch.no_grad():
													__magic_name__							:	Optional[Any]    							=							model(**lowerCamelCase_  )

								__magic_name__							:	Optional[int]    							=							torch.Size((1, model.config.num_labels, 512, 512)  )
								self.assertEqual(outputs.logits.shape       ,						lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							torch.tensor(
								    [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]]  ).to(lowerCamelCase_  )
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]       ,						lowerCamelCase_       ,						atol=1E-4  )  )









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   :						Dict				=			WebClient(token=os.environ['''CI_SLACK_BOT_TOKEN'''])







def 		lowercase__   (		__A:	List[Any]				):





					'''simple docstring'''






					__magic_name__							:	int    							=							test_results.split(''' '''				)

					__magic_name__							:	Optional[Any]    							=							0
					__magic_name__							:	int    							=							0

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

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

					return failed, success, time_spent







def 		lowercase__   (		__A:	Dict				):





					'''simple docstring'''






					__magic_name__							:	Tuple    							=							{}
					__magic_name__							:	List[Any]    							=							None
					__magic_name__							:	int    							=							False
					for line in failures_short_lines.split('''\n'''				):
										if re.search(r'''_ \[doctest\]'''							,__A				):
															__magic_name__							:	Dict    							=							True
															__magic_name__							:	Any    							=							line.split(''' '''				)[2]
										elif in_error and not line.split(''' '''				)[0].isdigit():
															__magic_name__							:	List[Any]    							=							line
															__magic_name__							:	List[Any]    							=							False

					return failures


class    lowerCamelCase    :






			'''simple docstring'''





			def __init__( self			:   Union[str, Any]       ,						lowerCamelCase_			:   str       ,						lowerCamelCase_			:   Dict  )						->   List[str]:
								__magic_name__							:	Union[str, Any]    							=							title

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

								# Failures and success of the modeling tests
								__magic_name__							:	List[Any]    							=							doc_test_results

			@property
			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   str:
								__magic_name__							:	Union[str, Any]    							=							[self._time_spent]
								__magic_name__							:	str    							=							0

								for time in time_spent:
													__magic_name__							:	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(lowerCamelCase_  ) == 1:
																		__magic_name__							:	str    							=							[0, 0, time_parts[0]]

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

								__magic_name__							,    __magic_name__							,    __magic_name__							:	Dict    							=							total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
								return F'''{int(lowerCamelCase_  )}h{int(lowerCamelCase_  )}m{int(lowerCamelCase_  )}s'''

			@property
			def 							UpperCAmelCase__       ( self			:   Dict  )						->   Dict:
								return {"type": "header", "text": {"type": "plain_text", "text": self.title}}

			@property
			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Dict:
								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 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Dict:
								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 							UpperCAmelCase__       ( self			:   Dict  )						->   Dict:
								__magic_name__							:	List[Any]    							=							40
								__magic_name__							:	Optional[Any]    							=							{k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(lowerCamelCase_       ,						lowerCamelCase_  )}

								__magic_name__							:	Any    							=							''''''
								for category, failures in category_failures.items():
													if len(lowerCamelCase_  ) == 0:
																		continue

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

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

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

			@property
			def 							UpperCAmelCase__       ( self			:   Dict  )						->   str:
								__magic_name__							:	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(lowerCamelCase_  )

			@staticmethod
			def 							UpperCAmelCase__       ( )						->   List[Any]:
								__magic_name__							:	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(lowerCamelCase_  )}  )  )

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

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Union[str, Any]:
								print('''Sending the following payload'''  )
								print(json.dumps({'''blocks''': json.loads(self.payload  )}  )  )

								__magic_name__							:	Tuple    							=							F'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else '''All tests passed.'''

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

			def 							UpperCAmelCase__       ( self			:   Tuple       ,						lowerCamelCase_			:   List[Any]       ,						lowerCamelCase_			:   Optional[int]       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   Any  )						->   Optional[Any]:
								__magic_name__							:	Optional[Any]    							=							''''''
								for key, value in failures.items():
													__magic_name__							:	int    							=							value[:200] + ''' [Truncated]''' if len(lowerCamelCase_  ) > 250 else value
													failures_text += F'''*{key}*\n_{value}_\n\n'''

								__magic_name__							:	Dict    							=							job_name
								__magic_name__							:	Tuple    							=							{'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}}

								if job_link is not None:
													__magic_name__							:	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 							UpperCAmelCase__       ( self			:   Any  )						->   Tuple:
								if self.thread_ts is None:
													raise ValueError('''Can only post reply if a post has been made.'''  )

								__magic_name__							:	List[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'''  )

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

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

																		time.sleep(1  )







def 		lowercase__   (		):





					'''simple docstring'''






					__magic_name__							:	List[Any]    							=							os.environ['''GITHUB_RUN_ID''']
					__magic_name__							:	Optional[int]    							=							F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'''
					__magic_name__							:	Any    							=							requests.get(__A				).json()
					__magic_name__							:	List[str]    							=							{}

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

										for i in range(__A				):
															__magic_name__							:	Optional[Any]    							=							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.'''							,__A				)

					return {}







def 		lowercase__   (		__A:	str				):





					'''simple docstring'''






					__magic_name__							:	List[str]    							=							{}

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

					return _artifact







def 		lowercase__   (		):





					'''simple docstring'''






					class    lowerCamelCase    :






								'''simple docstring'''





								def __init__( self			:   Optional[Any]       ,						lowerCamelCase_			:   str  )						->   int:
													__magic_name__							:	int    							=							name
													__magic_name__							:	Dict    							=							[]

								def __str__( self			:   List[str]  )						->   Union[str, Any]:
													return self.name


								def 							UpperCAmelCase__       ( self			:   List[Any]       ,						lowerCamelCase_			:   str  )						->   Optional[Any]:
													self.paths.append({'''name''': self.name, '''path''': path}  )






					__magic_name__							:	Dict[str, Artifact]    							=							{}

					__magic_name__							:	Tuple    							=							filter(os.path.isdir							,os.listdir()				)
					for directory in directories:
										__magic_name__							:	int    							=							directory
										if artifact_name not in _available_artifacts:
															__magic_name__							:	List[Any]    							=							Artifact(__A				)

															_available_artifacts[artifact_name].add_path(__A				)

					return _available_artifacts


if __name__ == "__main__":
	__lowerCamelCase   :						Optional[int]				=			get_job_links()
	__lowerCamelCase   :						Tuple				=			retrieve_available_artifacts()

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

	# Link to the GitHub Action job
	__lowerCamelCase   :						Optional[int]				=			github_actions_job_links.get('''run_doctests''')

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

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

				if "::" in line:
					__lowerCamelCase	,		__lowerCamelCase   :						List[str]				=			line.split('''::''')
				else:
					__lowerCamelCase	,		__lowerCamelCase   :						Optional[int]				=			line, line

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

						__lowerCamelCase   :						List[Any]				=			all_failures[test] if test in all_failures else '''N/A'''
						__lowerCamelCase   :						int				=			failure
						break

	__lowerCamelCase   :						List[Any]				=			Message('''🤗 Results of the doc tests.''', doc_test_results)
	message.post()
	message.post_reply()



