





'''simple docstring'''

import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length







def 							UpperCAmelCase_    (	lowerCAmelCase_							):

				"""simple docstring"""






				lowercase         = int(lowerCAmelCase_							)
				lowercase   ,							lowercase   ,							lowercase         = t // 3600, (t // 60) % 60, t % 60
				return f'{h}:{m:02d}:{s:02d}' if h != 0 else f'{m:02d}:{s:02d}'







def 							UpperCAmelCase_    (	lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_		,							lowerCAmelCase_=300							):

				"""simple docstring"""






				return f'\n    <div>\n      {prefix}\n      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>\n      {label}\n    </div>\n    '







def 							UpperCAmelCase_    (	lowerCAmelCase_							):

				"""simple docstring"""






				lowercase         = "<table border=\"1\" class=\"dataframe\">\n"
				html_code += """  <thead>\n <tr style="text-align: left;">\n"""
				for i in items[0]:
								html_code += f'      <th>{i}</th>\n'
				html_code += "    </tr>\n  </thead>\n  <tbody>\n"
				for line in items[1:]:
								html_code += "    <tr>\n"
								for elt in line:
												lowercase         = f'{elt:.6f}' if isinstance(lowerCAmelCase_		,							lowerCAmelCase_							) else str(lowerCAmelCase_							)
												html_code += f'      <td>{elt}</td>\n'
								html_code += "    </tr>\n"
				html_code += "  </tbody>\n</table><p>"
				return html_code

class       UpperCAmelCase :
		UpperCAmelCase					:	Optional[Any] 			=		5
		UpperCAmelCase					:	int 			=		0.2




		def __init__(self      :	List[str]							,			A__      :	int							,			A__      :	Optional[str] = None							,			A__      :	bool = True							,			A__      :	Optional["NotebookTrainingTracker"] = None							,			A__      :	int = 3_0_0							,			)					-> Tuple:
						lowercase         = total
						lowercase         = "" if prefix is None else prefix
						lowercase         = leave
						lowercase         = parent
						lowercase         = width
						lowercase         = None
						lowercase         = None
						lowercase         = None




		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	int							,			A__      :	bool = False							,			A__      :	str = None				)					-> Optional[Any]:
						lowercase         = value
						if comment is not None:
										lowercase         = comment
						if self.last_value is None:
										lowercase         = lowercase         = time.time()
										lowercase         = lowercase         = value
										lowercase         = lowercase         = None
										lowercase         = self.warmup
										lowercase         = 1
										self.update_bar(A__				)
						elif value <= self.last_value and not force_update:
										return
						elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for							,			self.total				):
										if self.first_calls > 0:
														self.first_calls -= 1
										lowercase         = time.time()
										lowercase         = current_time - self.start_time
										# We could have value = self.start_value if the update is called twixe with the same start value.
										if value > self.start_value:
														lowercase         = self.elapsed_time / (value - self.start_value)
										else:
														lowercase         = None
										if value >= self.total:
														lowercase         = self.total
														lowercase         = None
														if not self.leave:
																		self.close()
										elif self.average_time_per_item is not None:
														lowercase         = self.average_time_per_item * (self.total - value)
										self.update_bar(A__				)
										lowercase         = value
										lowercase         = current_time
										if self.average_time_per_item is None:
														lowercase         = 1
										else:
														lowercase         = max(int(self.update_every / self.average_time_per_item				)							,			1				)




		def 				UpperCAmelCase__   (self      :	List[Any]							,			A__      :	Dict							,			A__      :	int=None				)					-> Dict:
						lowercase         = " " * (len(str(self.total				)				) - len(str(A__				)				)) + str(A__				)
						if self.elapsed_time is None:
										lowercase         = f'[{spaced_value}/{self.total} : < :'
						elif self.predicted_remaining is None:
										lowercase         = f'[{spaced_value}/{self.total} {format_time(self.elapsed_time				)}'
						else:
										lowercase         = (
										    f'[{spaced_value}/{self.total} {format_time(self.elapsed_time				)} <'
										    f' {format_time(self.predicted_remaining				)}'
										)
										self.label += f', {1/self.average_time_per_item:.2f} it/s'
						self.label += "]" if self.comment is None or len(self.comment				) == 0 else f', {self.comment}]'
						self.display()




		def 				UpperCAmelCase__   (self      :	Dict				)					-> int:
						lowercase         = html_progress_bar(self.value							,			self.total							,			self.prefix							,			self.label							,			self.width				)
						if self.parent is not None:
										# If this is a child bar, the parent will take care of the display.
										self.parent.display()
										return
						if self.output is None:
										lowercase         = disp.display(disp.HTML(self.html_code				)							,			display_id=A__				)
						else:
										self.output.update(disp.HTML(self.html_code				)				)





		def 				UpperCAmelCase__   (self      :	Dict				)					-> Dict:
						if self.parent is None and self.output is not None:
										self.output.update(disp.HTML(""				)				)




class       UpperCAmelCase (						_lowercase  ):




		def __init__(self      :	int							,			A__      :	Optional[Any]							,			A__      :	List[str]=None				)					-> str:
						super().__init__(A__				)
						lowercase         = None if column_names is None else [column_names]
						lowercase         = None




		def 				UpperCAmelCase__   (self      :	Any				)					-> Optional[Any]:
						lowercase         = html_progress_bar(self.value							,			self.total							,			self.prefix							,			self.label							,			self.width				)
						if self.inner_table is not None:
										self.html_code += text_to_html_table(self.inner_table				)
						if self.child_bar is not None:
										self.html_code += self.child_bar.html_code
						if self.output is None:
										lowercase         = disp.display(disp.HTML(self.html_code				)							,			display_id=A__				)
						else:
										self.output.update(disp.HTML(self.html_code				)				)




		def 				UpperCAmelCase__   (self      :	Dict							,			A__      :	List[str]				)					-> Dict:
						if self.inner_table is None:
										lowercase         = [list(values.keys()				), list(values.values()				)]
						else:
										lowercase         = self.inner_table[0]
										if len(self.inner_table				) == 1:
														# We give a chance to update the column names at the first iteration
														for key in values.keys():
																		if key not in columns:
																						columns.append(A__				)
														lowercase         = columns
										self.inner_table.append([values[c] for c in columns]				)




		def 				UpperCAmelCase__   (self      :	int							,			A__      :	Optional[Any]							,			A__      :	str=None							,			A__      :	Dict=3_0_0				)					-> List[Any]:
						lowercase         = NotebookProgressBar(A__							,			prefix=A__							,			parent=self							,			width=A__				)
						return self.child_bar





		def 				UpperCAmelCase__   (self      :	int				)					-> Union[str, Any]:
						lowercase         = None
						self.display()




class       UpperCAmelCase (						_lowercase  ):




		def __init__(self      :	Optional[Any]				)					-> Optional[int]:
						lowercase         = None
						lowercase         = None
						lowercase         = False




		def 				UpperCAmelCase__   (self      :	int							,			A__      :	Optional[int]							,			A__      :	Dict							,			A__      :	List[Any]							,			**A__      :	str				)					-> Tuple:
						lowercase         = "Epoch" if args.evaluation_strategy == IntervalStrategy.EPOCH else "Step"
						lowercase         = 0
						lowercase         = 0
						lowercase         = [self.first_column] + ["Training Loss"]
						if args.evaluation_strategy != IntervalStrategy.NO:
										column_names.append("Validation Loss"				)
						lowercase         = NotebookTrainingTracker(state.max_steps							,			A__				)




		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	Optional[Any]							,			A__      :	Dict							,			A__      :	List[str]							,			**A__      :	Tuple				)					-> int:
						lowercase         = int(state.epoch				) if int(state.epoch				) == state.epoch else f'{state.epoch:.2f}'
						self.training_tracker.update(
						    state.global_step + 1							,			comment=f'Epoch {epoch}/{state.num_train_epochs}'							,			force_update=self._force_next_update							,			)
						lowercase         = False




		def 				UpperCAmelCase__   (self      :	List[Any]							,			A__      :	List[str]							,			A__      :	int							,			A__      :	Optional[Any]							,			A__      :	str=None							,			**A__      :	str				)					-> Optional[int]:
						if not has_length(A__				):
										return
						if self.prediction_bar is None:
										if self.training_tracker is not None:
														lowercase         = self.training_tracker.add_child(len(A__				)				)
										else:
														lowercase         = NotebookProgressBar(len(A__				)				)
										self.prediction_bar.update(1				)
						else:
										self.prediction_bar.update(self.prediction_bar.value + 1				)




		def 				UpperCAmelCase__   (self      :	List[Any]							,			A__      :	str							,			A__      :	Tuple							,			A__      :	Tuple							,			**A__      :	Dict				)					-> Dict:
						if self.prediction_bar is not None:
										self.prediction_bar.close()
						lowercase         = None




		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	Optional[Any]							,			A__      :	List[Any]							,			A__      :	Union[str, Any]							,			A__      :	Dict=None							,			**A__      :	str				)					-> Union[str, Any]:
						# Only for when there is no evaluation
						if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
										lowercase         = {"Training Loss": logs["loss"]}
										# First column is necessarily Step sine we're not in epoch eval strategy
										lowercase         = state.global_step
										self.training_tracker.write_line(A__				)




		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	int							,			A__      :	Union[str, Any]							,			A__      :	Tuple							,			A__      :	Dict=None							,			**A__      :	Tuple				)					-> List[Any]:
						if self.training_tracker is not None:
										lowercase         = {"Training Loss": "No log", "Validation Loss": "No log"}
										for log in reversed(state.log_history				):
														if "loss" in log:
																		lowercase         = log["loss"]
																		break

										if self.first_column == "Epoch":
														lowercase         = int(state.epoch				)
										else:
														lowercase         = state.global_step
										lowercase         = "eval"
										for k in metrics:
														if k.endswith("_loss"				):
																		lowercase         = re.sub(R"\_loss$"							,			""							,			A__				)
										lowercase         = metrics.pop("total_flos"							,			A__				)
										lowercase         = metrics.pop("epoch"							,			A__				)
										lowercase         = metrics.pop(f'{metric_key_prefix}_runtime'							,			A__				)
										lowercase         = metrics.pop(f'{metric_key_prefix}_samples_per_second'							,			A__				)
										lowercase         = metrics.pop(f'{metric_key_prefix}_steps_per_second'							,			A__				)
										lowercase         = metrics.pop(f'{metric_key_prefix}_jit_compilation_time'							,			A__				)
										for k, v in metrics.items():
														if k == f'{metric_key_prefix}_loss':
																		lowercase         = v
														else:
																		lowercase         = k.split("_"				)
																		lowercase         = " ".join([part.capitalize() for part in splits[1:]]				)
																		lowercase         = v
										self.training_tracker.write_line(A__				)
										self.training_tracker.remove_child()
										lowercase         = None
										# Evaluation takes a long time so we should force the next update.
										lowercase         = True





		def 				UpperCAmelCase__   (self      :	Optional[Any]							,			A__      :	Optional[int]							,			A__      :	Union[str, Any]							,			A__      :	List[Any]							,			**A__      :	List[str]				)					-> Dict:
						self.training_tracker.update(
						    state.global_step							,			comment=f'Epoch {int(state.epoch				)}/{state.num_train_epochs}'							,			force_update=A__				)
						lowercase         = None











'''simple docstring'''

import sacrebleu as scb
from packaging import version
from sacrebleu import CHRF

import datasets


__lowerCamelCase : Any											=			"\\n@inproceedings{popovic-2015-chrf,\n    title = \"chr{F}: character n-gram {F}-score for automatic {MT} evaluation\",\n    author = \"Popovi{\'c}, Maja\",\n    booktitle = \"Proceedings of the Tenth Workshop on Statistical Machine Translation\",\n    month = sep,\n    year = \"2015\",\n    address = \"Lisbon, Portugal\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://aclanthology.org/W15-3049\",\n    doi = \"10.18653/v1/W15-3049\",\n    pages = \"392--395\",\n}\n@inproceedings{popovic-2017-chrf,\n    title = \"chr{F}++: words helping character n-grams\",\n    author = \"Popovi{\'c}, Maja\",\n    booktitle = \"Proceedings of the Second Conference on Machine Translation\",\n    month = sep,\n    year = \"2017\",\n    address = \"Copenhagen, Denmark\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://aclanthology.org/W17-4770\",\n    doi = \"10.18653/v1/W17-4770\",\n    pages = \"612--618\",\n}\n@inproceedings{post-2018-call,\n    title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n    author = \"Post, Matt\",\n    booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n    month = oct,\n    year = \"2018\",\n    address = \"Belgium, Brussels\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://www.aclweb.org/anthology/W18-6319\",\n    pages = \"186--191\",\n}\n"

__lowerCamelCase : Dict											=			"\\nChrF and ChrF++ are two MT evaluation metrics. They both use the F-score statistic for character n-gram matches,\nand ChrF++ adds word n-grams as well which correlates more strongly with direct assessment. We use the implementation\nthat is already present in sacrebleu.\n\nThe implementation here is slightly different from sacrebleu in terms of the required input format. The length of\nthe references and hypotheses lists need to be the same, so you may need to transpose your references compared to\nsacrebleu's required input format. See https://github.com/huggingface/datasets/issues/3154#issuecomment-950746534\n\nSee the README.md file at https://github.com/mjpost/sacreBLEU#chrf--chrf for more information.\n"

__lowerCamelCase : List[Any]											=			"\nProduces ChrF(++) scores for hypotheses given reference translations.\n\nArgs:\n    predictions (list of str): The predicted sentences.\n    references (list of list of str): The references. There should be one reference sub-list for each prediction sentence.\n    char_order (int): Character n-gram order. Defaults to `6`.\n    word_order (int): Word n-gram order. If equals to `2`, the metric is referred to as chrF++. Defaults to `0`.\n    beta (int): Determine the importance of recall w.r.t precision. Defaults to `2`.\n    lowercase (bool): if `True`, enables case-insensitivity. Defaults to `False`.\n    whitespace (bool): If `True`, include whitespaces when extracting character n-grams.\n    eps_smoothing (bool): If `True`, applies epsilon smoothing similar\n    to reference chrF++.py, NLTK and Moses implementations. If `False`,\n    it takes into account effective match order similar to sacreBLEU < 2.0.0. Defaults to `False`.\n\nReturns:\n    'score' (float): The chrF (chrF++) score,\n    'char_order' (int): The character n-gram order,\n    'word_order' (int): The word n-gram order. If equals to 2, the metric is referred to as chrF++,\n    'beta' (int): Determine the importance of recall w.r.t precision\n\nExamples:\n    Example 1--a simple example of calculating chrF:\n        >>> prediction = [\"The relationship between cats and dogs is not exactly friendly.\", \"a good bookshop is just a genteel black hole that knows how to read.\"]\n        >>> reference = [[\"The relationship between dogs and cats is not exactly friendly.\"], [\"A good bookshop is just a genteel Black Hole that knows how to read.\"]]\n        >>> chrf = datasets.load_metric(\"chrf\")\n        >>> results = chrf.compute(predictions=prediction, references=reference)\n        >>> print(results)\n        {'score': 84.64214891738334, 'char_order': 6, 'word_order': 0, 'beta': 2}\n\n    Example 2--the same example, but with the argument word_order=2, to calculate chrF++ instead of chrF:\n        >>> prediction = [\"The relationship between cats and dogs is not exactly friendly.\", \"a good bookshop is just a genteel black hole that knows how to read.\"]\n        >>> reference = [[\"The relationship between dogs and cats is not exactly friendly.\"], [\"A good bookshop is just a genteel Black Hole that knows how to read.\"]]\n        >>> chrf = datasets.load_metric(\"chrf\")\n        >>> results = chrf.compute(predictions=prediction,\n        ...                         references=reference,\n        ...                         word_order=2)\n        >>> print(results)\n        {'score': 82.87263732906315, 'char_order': 6, 'word_order': 2, 'beta': 2}\n\n    Example 3--the same chrF++ example as above, but with `lowercase=True` to normalize all case:\n        >>> prediction = [\"The relationship between cats and dogs is not exactly friendly.\", \"a good bookshop is just a genteel black hole that knows how to read.\"]\n        >>> reference = [[\"The relationship between dogs and cats is not exactly friendly.\"], [\"A good bookshop is just a genteel Black Hole that knows how to read.\"]]\n        >>> chrf = datasets.load_metric(\"chrf\")\n        >>> results = chrf.compute(predictions=prediction,\n        ...                         references=reference,\n        ...                         word_order=2,\n        ...                         lowercase=True)\n        >>> print(results)\n        {'score': 92.12853119829202, 'char_order': 6, 'word_order': 2, 'beta': 2}\n"

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,       _KWARGS_DESCRIPTION  )
class       UpperCAmelCase (						datasets.Metric  ):




		def 				UpperCAmelCase__   (self      :	List[str]				)					-> Union[str, Any]:
						if version.parse(scb.__version__				) < version.parse("1.4.12"				):
										raise ImportWarning(
										    "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n"
										    "You can install it with `pip install \"sacrebleu>=1.4.12\"`."				)
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,			citation=_CITATION							,			homepage="https://github.com/mjpost/sacreBLEU#chrf--chrf"							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
						        {
						            "predictions": datasets.Value("string"							,			id="sequence"				),
						            "references": datasets.Sequence(datasets.Value("string"							,			id="sequence"				)							,			id="references"				),
						        }				)							,			codebase_urls=["https://github.com/mjpost/sacreBLEU#chrf--chrf"]							,			reference_urls=[
						        "https://github.com/m-popovic/chrF",
						    ]							,			)





		def 				UpperCAmelCase__   (self      :	Tuple							,			A__      :	Dict							,			A__      :	Tuple							,			A__      :	int = CHRF.CHAR_ORDER							,			A__      :	int = CHRF.WORD_ORDER							,			A__      :	int = CHRF.BETA							,			A__      :	bool = False							,			A__      :	bool = False							,			A__      :	bool = False							,			)					-> List[str]:
						lowercase         = len(references[0]				)
						if any(len(A__				) != references_per_prediction for refs in references				):
										raise ValueError("Sacrebleu requires the same number of references for each prediction"				)
						lowercase         = [[refs[i] for refs in references] for i in range(A__				)]

						lowercase         = CHRF(A__							,			A__							,			A__							,			A__							,			A__							,			A__				)
						lowercase         = sb_chrf.corpus_score(A__							,			A__				)

						return {
						    "score": output.score,
						    "char_order": output.char_order,
						    "word_order": output.word_order,
						    "beta": output.beta,
						}




