





import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test






class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			@parameterized.expand([(None,), ('''foo.json''',)]							)
			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig(
				    do_sample=__lowerCAmelCase ,						temperature=0.7 ,						length_penalty=1.0 ,						bad_words_ids=[[1, 2, 3], [4, 5]] ,						)
				with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(__lowerCAmelCase ,						config_name=__lowerCAmelCase							)
					lowerCamelCase__              =	GenerationConfig.from_pretrained(__lowerCAmelCase ,						config_name=__lowerCAmelCase							)

				# Checks parameters that were specified
				self.assertEqual(loaded_config.do_sample ,						__lowerCAmelCase							)
				self.assertEqual(loaded_config.temperature ,						0.7							)
				self.assertEqual(loaded_config.length_penalty ,						1.0							)
				self.assertEqual(loaded_config.bad_words_ids ,						[[1, 2, 3], [4, 5]]							)

				# Checks parameters that were not specified (defaults)
				self.assertEqual(loaded_config.top_k ,						5_0							)
				self.assertEqual(loaded_config.max_length ,						2_0							)
				self.assertEqual(loaded_config.max_time ,						__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	AutoConfig.from_pretrained('''gpt2'''							)
				lowerCamelCase__              =	GenerationConfig.from_model_config(__lowerCAmelCase							)
				lowerCamelCase__              =	GenerationConfig()

				# The generation config has loaded a few non-default parameters from the model config
				self.assertNotEqual(__lowerCAmelCase ,						__lowerCAmelCase							)

				# One of those parameters is eos_token_id -- check if it matches
				self.assertNotEqual(generation_config_from_model.eos_token_id ,						default_generation_config.eos_token_id							)
				self.assertEqual(generation_config_from_model.eos_token_id ,						model_config.eos_token_id							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig()
				lowerCamelCase__              =	{
				    '''max_new_tokens''': 1_0_2_4,
				    '''foo''': '''bar''',
				}
				lowerCamelCase__              =	copy.deepcopy(__lowerCAmelCase							)
				lowerCamelCase__              =	generation_config.update(**__lowerCAmelCase							)

				# update_kwargs was not modified (no side effects)
				self.assertEqual(__lowerCAmelCase ,						__lowerCAmelCase							)

				# update_kwargs was used to update the config on valid attributes
				self.assertEqual(generation_config.max_new_tokens ,						1_0_2_4							)

				# `.update()` returns a dictionary of unused kwargs
				self.assertEqual(__lowerCAmelCase ,						{'''foo''': '''bar'''}							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig()
				lowerCamelCase__              =	'''bar'''

				with tempfile.TemporaryDirectory('''test-generation-config'''							) as tmp_dir:
					generation_config.save_pretrained(__lowerCAmelCase							)

					lowerCamelCase__              =	GenerationConfig.from_pretrained(__lowerCAmelCase							)
				# update_kwargs was used to update the config on valid attributes
				self.assertEqual(new_config.foo ,						'''bar'''							)

				lowerCamelCase__              =	GenerationConfig.from_model_config(__lowerCAmelCase							)
				assert not hasattr(__lowerCAmelCase ,						'''foo'''							)  # no new kwargs should be initialized if from config





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig()
				self.assertEqual(default_config.temperature ,						1.0							)
				self.assertEqual(default_config.do_sample ,						__lowerCAmelCase							)
				self.assertEqual(default_config.num_beams ,						1							)

				lowerCamelCase__              =	GenerationConfig(
				    do_sample=__lowerCAmelCase ,						temperature=0.7 ,						length_penalty=1.0 ,						bad_words_ids=[[1, 2, 3], [4, 5]] ,						)
				self.assertEqual(config.temperature ,						0.7							)
				self.assertEqual(config.do_sample ,						__lowerCAmelCase							)
				self.assertEqual(config.num_beams ,						1							)

				with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(__lowerCAmelCase							)
					lowerCamelCase__              =	GenerationConfig.from_pretrained(__lowerCAmelCase ,						temperature=1.0							)

				self.assertEqual(loaded_config.temperature ,						1.0							)
				self.assertEqual(loaded_config.do_sample ,						__lowerCAmelCase							)
				self.assertEqual(loaded_config.num_beams ,						1							)  # default value






@is_staging_test
class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			@classmethod
			def 	__lowerCamelCase       (					cls							):



				'''simple docstring'''







				lowerCamelCase__              =	TOKEN
				HfFolder.save_token(__lowerCAmelCase							)





			@classmethod
			def 	__lowerCamelCase       (					cls							):



				'''simple docstring'''







				try:
					delete_repo(token=cls._token ,						repo_id='''test-generation-config'''							)
				except HTTPError:
					pass

				try:
					delete_repo(token=cls._token ,						repo_id='''valid_org/test-generation-config-org'''							)
				except HTTPError:
					pass





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig(
				    do_sample=__lowerCAmelCase ,						temperature=0.7 ,						length_penalty=1.0 ,						)
				config.push_to_hub('''test-generation-config''' ,						use_auth_token=self._token							)

				lowerCamelCase__              =	GenerationConfig.from_pretrained(F'{USER}/test-generation-config'							)
				for k, v in config.to_dict().items():
					if k != "transformers_version":
						self.assertEqual(__lowerCAmelCase ,						getattr(__lowerCAmelCase ,						__lowerCAmelCase							)							)

        # Reset repo
				delete_repo(token=self._token ,						repo_id='''test-generation-config'''							)

				# Push to hub via save_pretrained
				with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(
					    __lowerCAmelCase ,						repo_id='''test-generation-config''' ,						push_to_hub=__lowerCAmelCase ,						use_auth_token=self._token							)

				lowerCamelCase__              =	GenerationConfig.from_pretrained(F'{USER}/test-generation-config'							)
				for k, v in config.to_dict().items():
					if k != "transformers_version":
						self.assertEqual(__lowerCAmelCase ,						getattr(__lowerCAmelCase ,						__lowerCAmelCase							)							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	GenerationConfig(
				    do_sample=__lowerCAmelCase ,						temperature=0.7 ,						length_penalty=1.0 ,						)
				config.push_to_hub('''valid_org/test-generation-config-org''' ,						use_auth_token=self._token							)

				lowerCamelCase__              =	GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''							)
				for k, v in config.to_dict().items():
					if k != "transformers_version":
						self.assertEqual(__lowerCAmelCase ,						getattr(__lowerCAmelCase ,						__lowerCAmelCase							)							)

        # Reset repo
				delete_repo(token=self._token ,						repo_id='''valid_org/test-generation-config-org'''							)

				# Push to hub via save_pretrained
				with tempfile.TemporaryDirectory() as tmp_dir:
					config.save_pretrained(
					    __lowerCAmelCase ,						repo_id='''valid_org/test-generation-config-org''' ,						push_to_hub=__lowerCAmelCase ,						use_auth_token=self._token							)

				lowerCamelCase__              =	GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''							)
				for k, v in config.to_dict().items():
					if k != "transformers_version":
						self.assertEqual(__lowerCAmelCase ,						getattr(__lowerCAmelCase ,						__lowerCAmelCase							)							)










from __future__ import annotations





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case							)       ->   float:







	'''simple docstring'''







	if days_between_payments <= 0:
		raise ValueError('''days_between_payments must be > 0'''							)
	if daily_interest_rate < 0:
		raise ValueError('''daily_interest_rate must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)
	return principal * daily_interest_rate * days_between_payments





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,)       ->   float:







	'''simple docstring'''







	if number_of_compounding_periods <= 0:
		raise ValueError('''number_of_compounding_periods must be > 0'''							)
	if nominal_annual_interest_rate_percentage < 0:
		raise ValueError('''nominal_annual_interest_rate_percentage must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)

	return principal * (
	    (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods
	    - 1
	)





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,)       ->   float:







	'''simple docstring'''







	if number_of_years <= 0:
		raise ValueError('''number_of_years must be > 0'''							)
	if nominal_annual_percentage_rate < 0:
		raise ValueError('''nominal_annual_percentage_rate must be >= 0'''							)
	if principal <= 0:
		raise ValueError('''principal must be > 0'''							)

	return compound_interest(
	    __snake_case ,nominal_annual_percentage_rate / 365 ,number_of_years * 365							)


if __name__ == "__main__":
			import doctest

			doctest.testmod()



