


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

def 			lowerCAmelCase__					(     a__:						Optional[Any]			)    ->						List[Any]:


							'''simple docstring'''





							_UpperCAmelCase											=     test_results.split(' '			)

							_UpperCAmelCase											=     0
							_UpperCAmelCase											=     0

							# When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
							# When it is too long, those signs are not present.
							_UpperCAmelCase											=     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 			lowerCAmelCase__					(     a__:						Union[str, Any]			)    ->						List[Any]:


							'''simple docstring'''





							_UpperCAmelCase											=     {}
							_UpperCAmelCase											=     None
							_UpperCAmelCase											=     False
							for line in failures_short_lines.split('\n'			):
														if re.search(R'_ \[doctest\]'     ,    lowercase__			):
																					_UpperCAmelCase											=     True
																					_UpperCAmelCase											=     line.split(' '			)[2]
														elif in_error and not line.split(' '			)[0].isdigit():
																					_UpperCAmelCase											=     line
																					_UpperCAmelCase											=     False

							return failures

class 						__a :
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     title

									_UpperCAmelCase											=     doc_test_results["time_spent"].split(','		)[0]
									_UpperCAmelCase											=     doc_test_results["success"]
									_UpperCAmelCase											=     doc_test_results["failures"]
									_UpperCAmelCase											=     self.n_success + self.n_failures

									# Failures and success of the modeling tests
									_UpperCAmelCase											=     doc_test_results
		@property
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     [self._time_spent]
									_UpperCAmelCase											=     0

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

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

									_UpperCAmelCase											=     total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
									return f'''{int(_SCREAMING_SNAKE_CASE		)}h{int(_SCREAMING_SNAKE_CASE		)}m{int(_SCREAMING_SNAKE_CASE		)}s'''
		@property
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





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




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




									"""simple docstring"""





									_UpperCAmelCase											=     40
									_UpperCAmelCase											=     {k: v["failed"] for k, v in doc_test_results.items() if isinstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)}

									_UpperCAmelCase											=     ""
									for category, failures in category_failures.items():
																if len(_SCREAMING_SNAKE_CASE		) == 0:
																							continue

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

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

									return {
									    "type": "section",
									    "text": {
									        "type": "mrkdwn",
									        "text": f'''The following examples had failures:\n\n\n{report}\n''',
									    },
									}
		@property
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									_UpperCAmelCase											=     [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(_SCREAMING_SNAKE_CASE		)
		@staticmethod
		def       UpperCAmelCase__							(     )					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     [
									    {
									        "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(_SCREAMING_SNAKE_CASE		)}		)		)

									client.chat_postMessage(
									    channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']     ,       text='There was an issue running the tests.'     ,       blocks=_SCREAMING_SNAKE_CASE     ,       )
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     f'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else "All tests passed."

									_UpperCAmelCase											=     client.chat_postMessage(
									    channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']     ,       blocks=self.payload     ,       text=_SCREAMING_SNAKE_CASE     ,       )
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Dict:




									"""simple docstring"""





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

									_UpperCAmelCase											=     job_name
									_UpperCAmelCase											=     {"type": "section", "text": {"type": "mrkdwn", "text": text}}

									if job_link is not None:
																_UpperCAmelCase											=     {
																    "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		)					->			List[str]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     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'		)

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

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

																							time.sleep(1		)



def 			lowerCAmelCase__					(     )    ->						Tuple:


							'''simple docstring'''





							_UpperCAmelCase											=     os.environ["GITHUB_RUN_ID"]
							_UpperCAmelCase											=     F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'''
							_UpperCAmelCase											=     requests.get(lowercase__			).json()
							_UpperCAmelCase											=     {}

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

														for i in range(lowercase__			):
																					_UpperCAmelCase											=     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 			lowerCAmelCase__					(     a__:						Tuple			)    ->						List[Any]:


							'''simple docstring'''





							_UpperCAmelCase											=     {}

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

							return _artifact

def 			lowerCAmelCase__					(     )    ->						List[str]:


							'''simple docstring'''





							class 						__a :
									def __init__(     self     ,       _SCREAMING_SNAKE_CASE		)					->			Optional[int]:




																"""simple docstring"""





																_UpperCAmelCase											=     name
																_UpperCAmelCase											=     []
									def __str__(     self		)					->			Dict:




																"""simple docstring"""





																return self.name






									def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE		)					->			Optional[Any]:




																"""simple docstring"""





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



							_UpperCAmelCase											=     {}

							_UpperCAmelCase											=     filter(os.path.isdir     ,    os.listdir()			)
							for directory in directories:
														_UpperCAmelCase											=     directory
														if artifact_name not in _available_artifacts:
																					_UpperCAmelCase											=     Artifact(lowercase__			)

																					_available_artifacts[artifact_name].add_path(lowercase__			)

							return _available_artifacts


if __name__ == "__main__":
		lowerCAmelCase__    :int       =				get_job_links()
		lowerCAmelCase__    :List[Any]       =				retrieve_available_artifacts()

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

		# Link to the GitHub Action job
		lowerCAmelCase__    :int       =				github_actions_job_links.get('''run_doctests''')

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

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

								if "::" in line:
										lowerCAmelCase__     ,    lowerCAmelCase__    :Dict       =				line.split('''::''')
								else:
										lowerCAmelCase__     ,    lowerCAmelCase__    :str       =				line, line

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

												lowerCAmelCase__    :Union[str, Any]       =				all_failures[test] if test in all_failures else '''N/A'''
												lowerCAmelCase__    :Tuple       =				failure
												break

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



"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase		     =    logging.get_logger(__name__)

_UpperCamelCase		     =    {
    """facebook/dpr-ctx_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-ctx_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-multiset-base""": (
        """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json"""
    ),
}






class 			__a					(						__magic_name__       ):





       """simple docstring"""


       __UpperCamelCase					:      str    =						'dpr'






       def __init__(   self			,					snake_case=30_522			,					snake_case=768			,					snake_case=12			,					snake_case=12			,					snake_case=3_072			,					snake_case="gelu"			,					snake_case=0.1			,					snake_case=0.1			,					snake_case=512			,					snake_case=2			,					snake_case=0.02			,					snake_case=1e-12			,					snake_case=0			,					snake_case="absolute"			,					snake_case = 0			,					**snake_case			,					):





        """simple docstring"""

        super().__init__(pad_token_id=snake_case			,					**snake_case   )

        lowerCAmelCase__   :				Dict 			=				vocab_size
        lowerCAmelCase__   :				int 			=				hidden_size
        lowerCAmelCase__   :				List[str] 			=				num_hidden_layers
        lowerCAmelCase__   :				List[Any] 			=				num_attention_heads
        lowerCAmelCase__   :				Any 			=				hidden_act
        lowerCAmelCase__   :				Any 			=				intermediate_size
        lowerCAmelCase__   :				str 			=				hidden_dropout_prob
        lowerCAmelCase__   :				List[str] 			=				attention_probs_dropout_prob
        lowerCAmelCase__   :				List[Any] 			=				max_position_embeddings
        lowerCAmelCase__   :				Optional[Any] 			=				type_vocab_size
        lowerCAmelCase__   :				Tuple 			=				initializer_range
        lowerCAmelCase__   :				Dict 			=				layer_norm_eps
        lowerCAmelCase__   :				Dict 			=				projection_dim
        lowerCAmelCase__   :				Any 			=				position_embedding_type






