
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


UpperCamelCase_   	=	WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"])






def 		_UpperCAmelCase   (	UpperCamelCase:     int  ):






	"""simple docstring"""


	__lowerCAmelCase         =							test_results.split(" "  )

	__lowerCAmelCase         =							0
	__lowerCAmelCase         =							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         =							expressions[-2] if "=" in expressions[-1] else expressions[-1]

	for i, expression in enumerate(UpperCamelCase  ):
		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   (	UpperCamelCase:     Optional[int]  ):






	"""simple docstring"""


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

	return failures

class       a		:







							def __init__(						self  :  Optional[int]      ,				snake_case__  :  str      ,				snake_case__  :  Dict ):
								"""simple docstring"""







								__lowerCAmelCase         =							title

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

								# Failures and success of the modeling tests
								__lowerCAmelCase         =							doc_test_results







							@property
							def       UpperCAmelCase__					(						self  :  int ):
								"""simple docstring"""







								__lowerCAmelCase         =							[self._time_spent]
								__lowerCAmelCase         =							0

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

									__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							int(time_parts[0] ), int(time_parts[1] ), float(time_parts[2] )
									total_secs += hours * 3_600 + minutes * 60 + seconds

								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							total_secs // 3_600, (total_secs % 3_600) // 60, total_secs % 60
								return F"{int(snake_case__ )}h{int(snake_case__ )}m{int(snake_case__ )}s"







							@property
							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""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  :  List[Any] ):
								"""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  :  int ):
								"""simple docstring"""







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

								__lowerCAmelCase         =							""
								for category, failures in category_failures.items():
									if len(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(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  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							[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(snake_case__ )







							@staticmethod
							def       UpperCAmelCase__					(						):
								"""simple docstring"""







								__lowerCAmelCase         =							[
								    {
								        "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(snake_case__ )} ) )

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







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







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

								__lowerCAmelCase         =							F"{self.n_failures} failures out of {self.n_tests} tests," if self.n_failures else "All tests passed."

								__lowerCAmelCase         =							client.chat_postMessage(
								    channel=os.environ["CI_SLACK_CHANNEL_ID_DAILY"]      ,				blocks=self.payload      ,				text=snake_case__      ,				)







							def       UpperCAmelCase__					(						self  :  List[Any]      ,				snake_case__  :  List[str]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  List[str] ):
								"""simple docstring"""







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

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

								if job_link is not None:
									__lowerCAmelCase         =							{
									    "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  :  Union[str, Any] ):
								"""simple docstring"""







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

								__lowerCAmelCase         =							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         =							sorted(self.doc_test_results.items()      ,				key=lambda snake_case__ : t[0] )
								for job, job_result in sorted_dict:
									if len(job_result["failures"] ):
										__lowerCAmelCase         =							F"*Num failures* :{len(job_result['failed'] )} \n"
										__lowerCAmelCase         =							job_result["failures"]
										__lowerCAmelCase         =							self.get_reply_blocks(snake_case__      ,				snake_case__      ,				snake_case__      ,				text=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=snake_case__      ,				thread_ts=self.thread_ts["ts"]      ,				)

										time.sleep(1 )






def 		_UpperCAmelCase   (	):






	"""simple docstring"""


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

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

		for i in range(UpperCamelCase  ):
			__lowerCAmelCase         =							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."			,							UpperCamelCase  )

	return {}






def 		_UpperCAmelCase   (	UpperCamelCase:     str  ):






	"""simple docstring"""


	__lowerCAmelCase         =							{}

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

	return _artifact






def 		_UpperCAmelCase   (	):






	"""simple docstring"""


	class       a		:







								def __init__(						self  :  Union[str, Any]      ,				snake_case__  :  str ):
									"""simple docstring"""







									__lowerCAmelCase         =							name
									__lowerCAmelCase         =							[]







								def __str__(						self  :  str ):
									"""simple docstring"""







									return self.name







								def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				snake_case__  :  str ):
									"""simple docstring"""







									self.paths.append({"name": self.name, "path": path} )
	__lowerCAmelCase         =							{}

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

			_available_artifacts[artifact_name].add_path(UpperCamelCase  )

	return _available_artifacts


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

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

					# Link to the GitHub Action job
					UpperCamelCase_   	=	github_actions_job_links.get("run_doctests")

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

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

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

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

																														UpperCamelCase_   	=	all_failures[test] if test in all_failures else "N/A"
																														UpperCamelCase_   	=	failure
																														break

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








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


UpperCamelCase_   	=	logging.get_logger(__name__)

UpperCamelCase_   	=	{
    "speechbrain/m-ctc-t-large": "https://huggingface.co/speechbrain/m-ctc-t-large/resolve/main/config.json",
    # See all M-CTC-T models at https://huggingface.co/models?filter=mctct
}

class       a		(   __UpperCAmelCase					):
							lowercase_		:						Union[str, Any]   							=  'mctct'







							def __init__(						self  :  Dict      ,				snake_case__  :  Any=8_065      ,				snake_case__  :  Tuple=1_536      ,				snake_case__  :  Union[str, Any]=36      ,				snake_case__  :  List[Any]=6_144      ,				snake_case__  :  Tuple=4      ,				snake_case__  :  List[str]=384      ,				snake_case__  :  List[Any]=920      ,				snake_case__  :  Any=1E-5      ,				snake_case__  :  List[Any]=0.3      ,				snake_case__  :  List[str]="relu"      ,				snake_case__  :  List[Any]=0.0_2      ,				snake_case__  :  Optional[Any]=0.3      ,				snake_case__  :  List[str]=0.3      ,				snake_case__  :  List[Any]=1      ,				snake_case__  :  Optional[int]=0      ,				snake_case__  :  Any=2      ,				snake_case__  :  Any=1      ,				snake_case__  :  List[str]=0.3      ,				snake_case__  :  Tuple=1      ,				snake_case__  :  Union[str, Any]=(7,)      ,				snake_case__  :  List[Any]=(3,)      ,				snake_case__  :  List[Any]=80      ,				snake_case__  :  Optional[int]=1      ,				snake_case__  :  List[str]=None      ,				snake_case__  :  int="sum"      ,				snake_case__  :  Dict=False      ,				**snake_case__  :  List[str]      ,				):
								"""simple docstring"""







								super().__init__(**snake_case__      ,				pad_token_id=snake_case__      ,				bos_token_id=snake_case__      ,				eos_token_id=snake_case__ )
								__lowerCAmelCase         =							vocab_size
								__lowerCAmelCase         =							hidden_size
								__lowerCAmelCase         =							num_hidden_layers
								__lowerCAmelCase         =							intermediate_size
								__lowerCAmelCase         =							num_attention_heads
								__lowerCAmelCase         =							attention_head_dim
								__lowerCAmelCase         =							max_position_embeddings
								__lowerCAmelCase         =							layer_norm_eps
								__lowerCAmelCase         =							layerdrop
								__lowerCAmelCase         =							hidden_act
								__lowerCAmelCase         =							initializer_range
								__lowerCAmelCase         =							hidden_dropout_prob
								__lowerCAmelCase         =							attention_probs_dropout_prob
								__lowerCAmelCase         =							pad_token_id
								__lowerCAmelCase         =							bos_token_id
								__lowerCAmelCase         =							eos_token_id
								__lowerCAmelCase         =							conv_glu_dim
								__lowerCAmelCase         =							conv_dropout
								__lowerCAmelCase         =							num_conv_layers
								__lowerCAmelCase         =							input_feat_per_channel
								__lowerCAmelCase         =							input_channels
								__lowerCAmelCase         =							conv_channels
								__lowerCAmelCase         =							ctc_loss_reduction
								__lowerCAmelCase         =							ctc_zero_infinity

								# prevents config testing fail with exporting to json
								__lowerCAmelCase         =							list(snake_case__ )
								__lowerCAmelCase         =							list(snake_case__ )

								if len(self.conv_kernel ) != self.num_conv_layers:
									raise ValueError(
									    "Configuration for convolutional module is incorrect. "
									    "It is required that `len(config.conv_kernel)` == `config.num_conv_layers` "
									    F"but is `len(config.conv_kernel) = {len(self.conv_kernel )}`, "
									    F"`config.num_conv_layers = {self.num_conv_layers}`." )






