


import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


_SCREAMING_SNAKE_CASE     :			Dict        =							pytest.mark.integration



@require_faiss
class        A     (		lowerCamelCase_	):







     '''simple docstring'''







     def 					UpperCAmelCase__       (				self    :							Tuple):
      _lowercase:       str						=				Dataset.from_dict({"filename": ["my_name-train" + "_" + str(_UpperCamelCase) for x in np.arange(30).tolist()]})
      return dset

     def 					UpperCAmelCase__       (				self    :							Union[str, Any]):
      import faiss

      _lowercase:       Any						=				self._create_dummy_dataset()
      _lowercase:       Dict						=				dset.map(
          lambda _UpperCamelCase			,				_UpperCamelCase: {"vecs": i * np.ones(5			,				dtype=np.floataa)}			,				with_indices=_UpperCamelCase			,				keep_in_memory=_UpperCamelCase)
      _lowercase:       Dict						=				dset.add_faiss_index("vecs"			,				batch_size=100			,				metric_type=faiss.METRIC_INNER_PRODUCT)
      _lowercase					,	_lowercase:       Union[str, Any]						=				dset.get_nearest_examples("vecs"			,				np.ones(5			,				dtype=np.floataa))
      self.assertEqual(examples["filename"][0]			,				"my_name-train_29")
      dset.drop_index("vecs")

     def 					UpperCAmelCase__       (				self    :							List[Any]):
      import faiss

      _lowercase:       Optional[Any]						=				self._create_dummy_dataset()
      dset.add_faiss_index_from_external_arrays(
          external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1			,				1)			,				index_name="vecs"			,				batch_size=100			,				metric_type=faiss.METRIC_INNER_PRODUCT			,				)
      _lowercase					,	_lowercase:       List[Any]						=				dset.get_nearest_examples("vecs"			,				np.ones(5			,				dtype=np.floataa))
      self.assertEqual(examples["filename"][0]			,				"my_name-train_29")

     def 					UpperCAmelCase__       (				self    :							List[str]):
      import faiss

      _lowercase:       Tuple						=				self._create_dummy_dataset()
      dset.add_faiss_index_from_external_arrays(
          external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1			,				1)			,				index_name="vecs"			,				metric_type=faiss.METRIC_INNER_PRODUCT			,				)

      # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
      # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
      # see https://bugs.python.org/issue14243 and
      # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
      with tempfile.NamedTemporaryFile(delete=_UpperCamelCase) as tmp_file:
       dset.save_faiss_index("vecs"			,				tmp_file.name)
       dset.load_faiss_index("vecs2"			,				tmp_file.name)
      os.unlink(tmp_file.name)

      _lowercase					,	_lowercase:       List[str]						=				dset.get_nearest_examples("vecs2"			,				np.ones(5			,				dtype=np.floataa))
      self.assertEqual(examples["filename"][0]			,				"my_name-train_29")

     def 					UpperCAmelCase__       (				self    :							Any):
      _lowercase:       str						=				self._create_dummy_dataset()
      dset.add_faiss_index_from_external_arrays(
          external_arrays=np.ones((30, 5)) * np.arange(30).reshape(-1			,				1)			,				index_name="vecs")
      dset.drop_index("vecs")
      self.assertRaises(_UpperCamelCase			,				partial(dset.get_nearest_examples			,				"vecs2"			,				np.ones(5			,				dtype=np.floataa)))






     def 					UpperCAmelCase__       (				self    :							str):
      from elasticsearch import Elasticsearch

      _lowercase:       Union[str, Any]						=				self._create_dummy_dataset()
      with patch("elasticsearch.Elasticsearch.search") as mocked_search, patch(
          "elasticsearch.client.IndicesClient.create") as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk") as mocked_bulk:
       _lowercase:       Any						=				{"acknowledged": True}
       mocked_bulk.return_value([(True, None)] * 30)
       _lowercase:       int						=				{"hits": {"hits": [{"_score": 1, "_id": 29}]}}
       _lowercase:       List[str]						=				Elasticsearch()

       dset.add_elasticsearch_index("filename"			,				es_client=_UpperCamelCase)
       _lowercase					,	_lowercase:       Dict						=				dset.get_nearest_examples("filename"			,				"my_name-train_29")
       self.assertEqual(examples["filename"][0]			,				"my_name-train_29")






@require_faiss
class        A     (		lowerCamelCase_	):







     '''simple docstring'''







     def 					UpperCAmelCase__       (				self    :							List[Any]):
      import faiss

      _lowercase:       Optional[int]						=				FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT)

      # add vectors
      index.add_vectors(np.eye(5			,				dtype=np.floataa))
      self.assertIsNotNone(index.faiss_index)
      self.assertEqual(index.faiss_index.ntotal			,				5)
      index.add_vectors(np.zeros((5, 5)			,				dtype=np.floataa))
      self.assertEqual(index.faiss_index.ntotal			,				10)

      # single query
      _lowercase:       Optional[int]						=				np.zeros(5			,				dtype=np.floataa)
      _lowercase:       Optional[Any]						=				1
      _lowercase					,	_lowercase:       Union[str, Any]						=				index.search(_UpperCamelCase)
      self.assertRaises(_UpperCamelCase			,				index.search			,				query.reshape(-1			,				1))
      self.assertGreater(scores[0]			,				0)
      self.assertEqual(indices[0]			,				1)

      # batched queries
      _lowercase:       Tuple						=				np.eye(5			,				dtype=np.floataa)[::-1]
      _lowercase					,	_lowercase:       Union[str, Any]						=				index.search_batch(_UpperCamelCase)
      self.assertRaises(_UpperCamelCase			,				index.search_batch			,				queries[0])
      _lowercase:       Optional[int]						=				[scores[0] for scores in total_scores]
      _lowercase:       Optional[Any]						=				[indices[0] for indices in total_indices]
      self.assertGreater(np.min(_UpperCamelCase)			,				0)
      self.assertListEqual([4, 3, 2, 1, 0]			,				_UpperCamelCase)

     def 					UpperCAmelCase__       (				self    :							Any):
      import faiss

      _lowercase:       Optional[Any]						=				FaissIndex(string_factory="Flat")
      index.add_vectors(np.eye(5			,				dtype=np.floataa))
      self.assertIsInstance(index.faiss_index			,				faiss.IndexFlat)
      _lowercase:       Tuple						=				FaissIndex(string_factory="LSH")
      index.add_vectors(np.eye(5			,				dtype=np.floataa))
      self.assertIsInstance(index.faiss_index			,				faiss.IndexLSH)
      with self.assertRaises(_UpperCamelCase):
       _lowercase:       Optional[int]						=				FaissIndex(string_factory="Flat"			,				custom_index=faiss.IndexFlat(5))

     def 					UpperCAmelCase__       (				self    :							Tuple):
      import faiss

      _lowercase:       Dict						=				faiss.IndexFlat(5)
      _lowercase:       str						=				FaissIndex(custom_index=_UpperCamelCase)
      index.add_vectors(np.eye(5			,				dtype=np.floataa))
      self.assertIsInstance(index.faiss_index			,				faiss.IndexFlat)






     def 					UpperCAmelCase__       (				self    :							Optional[int]):
      import faiss

      _lowercase:       Tuple						=				FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT)
      index.add_vectors(np.eye(5			,				dtype=np.floataa))

      # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
      # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
      # see https://bugs.python.org/issue14243 and
      # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
      with tempfile.NamedTemporaryFile(delete=_UpperCamelCase) as tmp_file:
       index.save(tmp_file.name)
       _lowercase:       Tuple						=				FaissIndex.load(tmp_file.name)
      os.unlink(tmp_file.name)

      _lowercase:       Union[str, Any]						=				np.zeros(5			,				dtype=np.floataa)
      _lowercase:       Union[str, Any]						=				1
      _lowercase					,	_lowercase:       Optional[Any]						=				index.search(_UpperCamelCase)
      self.assertGreater(scores[0]			,				0)
      self.assertEqual(indices[0]			,				1)






@require_faiss
def 							__lowerCAmelCase      (			__magic_name__      ):
 import faiss

 _lowercase:       List[Any]						=				FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT      )
 index.add_vectors(np.eye(5 ,   dtype=np.floataa      )      )

 _lowercase:       Any						=				"index.faiss"
 _lowercase:       Dict						=				f"mock://{index_name}"
 index.save(lowerCAmelCase__ ,   storage_options=mockfs.storage_options      )
 _lowercase:       Optional[int]						=				FaissIndex.load(lowerCAmelCase__ ,   storage_options=mockfs.storage_options      )

 _lowercase:       List[Any]						=				np.zeros(5 ,   dtype=np.floataa      )
 _lowercase:       List[Any]						=				1
 _lowercase					,	_lowercase:       Any						=				index.search(lowerCAmelCase__      )
 assert scores[0] > 0
 assert indices[0] == 1





@require_elasticsearch
class        A     (		lowerCamelCase_	):







     '''simple docstring'''







     def 					UpperCAmelCase__       (				self    :							List[Any]):
      from elasticsearch import Elasticsearch

      with patch("elasticsearch.Elasticsearch.search") as mocked_search, patch(
          "elasticsearch.client.IndicesClient.create") as mocked_index_create, patch("elasticsearch.helpers.streaming_bulk") as mocked_bulk:
       _lowercase:       Any						=				Elasticsearch()
       _lowercase:       Union[str, Any]						=				{"acknowledged": True}
       _lowercase:       Tuple						=				ElasticSearchIndex(es_client=_UpperCamelCase)
       mocked_bulk.return_value([(True, None)] * 3)
       index.add_documents(["foo", "bar", "foobar"])

       # single query
       _lowercase:       Tuple						=				"foo"
       _lowercase:       Union[str, Any]						=				{"hits": {"hits": [{"_score": 1, "_id": 0}]}}
       _lowercase					,	_lowercase:       Dict						=				index.search(_UpperCamelCase)
       self.assertEqual(scores[0]			,				1)
       self.assertEqual(indices[0]			,				0)

       # single query with timeout
       _lowercase:       Tuple						=				"foo"
       _lowercase:       Union[str, Any]						=				{"hits": {"hits": [{"_score": 1, "_id": 0}]}}
       _lowercase					,	_lowercase:       Dict						=				index.search(_UpperCamelCase			,				request_timeout=30)
       self.assertEqual(scores[0]			,				1)
       self.assertEqual(indices[0]			,				0)

       # batched queries
       _lowercase:       int						=				["foo", "bar", "foobar"]
       _lowercase:       List[str]						=				{"hits": {"hits": [{"_score": 1, "_id": 1}]}}
       _lowercase					,	_lowercase:       Dict						=				index.search_batch(_UpperCamelCase)
       _lowercase:       Optional[int]						=				[scores[0] for scores in total_scores]
       _lowercase:       Optional[int]						=				[indices[0] for indices in total_indices]
       self.assertGreater(np.min(_UpperCamelCase)			,				0)
       self.assertListEqual([1, 1, 1]			,				_UpperCamelCase)

       # batched queries with timeout
       _lowercase:       Dict						=				["foo", "bar", "foobar"]
       _lowercase:       Tuple						=				{"hits": {"hits": [{"_score": 1, "_id": 1}]}}
       _lowercase					,	_lowercase:       str						=				index.search_batch(_UpperCamelCase			,				request_timeout=30)
       _lowercase:       Any						=				[scores[0] for scores in total_scores]
       _lowercase:       Tuple						=				[indices[0] for indices in total_indices]
       self.assertGreater(np.min(_UpperCamelCase)			,				0)
       self.assertListEqual([1, 1, 1]			,				_UpperCamelCase)



"""simple docstring"""


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  __lowerCamelCase  ( unittest.TestCase			):




				@parameterized.expand([(None,), ('''foo.json''',)]   )
				def        UpperCAmelCase__						(				self							,						UpperCAmelCase   ):
							lowerCamelCase_					=  GenerationConfig(
							    do_sample=UpperCAmelCase							,						temperature=0.7							,						length_penalty=1.0							,						bad_words_ids=[[1, 2, 3], [4, 5]]							,						)
							with tempfile.TemporaryDirectory() as tmp_dir:
										config.save_pretrained(UpperCAmelCase							,						config_name=UpperCAmelCase   )
										lowerCamelCase_					=  GenerationConfig.from_pretrained(UpperCAmelCase							,						config_name=UpperCAmelCase   )

							# Checks parameters that were specified
							self.assertEqual(loaded_config.do_sample							,						UpperCAmelCase   )
							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							,						50   )
							self.assertEqual(loaded_config.max_length							,						20   )
							self.assertEqual(loaded_config.max_time							,						UpperCAmelCase   )




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  AutoConfig.from_pretrained('''gpt2'''   )
							lowerCamelCase_					=  GenerationConfig.from_model_config(UpperCAmelCase   )
							lowerCamelCase_					=  GenerationConfig()

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

							# 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        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  GenerationConfig()
							lowerCamelCase_					=  {
							    '''max_new_tokens''': 1024,
							    '''foo''': '''bar''',
							}
							lowerCamelCase_					=  copy.deepcopy(UpperCAmelCase   )
							lowerCamelCase_					=  generation_config.update(**UpperCAmelCase   )

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

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

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




				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  GenerationConfig()
							lowerCamelCase_					=  '''bar'''

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

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

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





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  GenerationConfig()
							self.assertEqual(default_config.temperature							,						1.0   )
							self.assertEqual(default_config.do_sample							,						UpperCAmelCase   )
							self.assertEqual(default_config.num_beams							,						1   )

							lowerCamelCase_					=  GenerationConfig(
							    do_sample=UpperCAmelCase							,						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							,						UpperCAmelCase   )
							self.assertEqual(config.num_beams							,						1   )

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

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




@is_staging_test
class  __lowerCamelCase  ( unittest.TestCase			):




				@classmethod
				def        UpperCAmelCase__						(				cls   ):
							lowerCamelCase_					=  TOKEN
							HfFolder.save_token(UpperCAmelCase   )




				@classmethod
				def        UpperCAmelCase__						(				cls   ):
							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        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  GenerationConfig(
							    do_sample=UpperCAmelCase							,						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(UpperCAmelCase							,						getattr(UpperCAmelCase							,						UpperCAmelCase   )   )

        # 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(
										    UpperCAmelCase							,						repo_id='''test-generation-config'''							,						push_to_hub=UpperCAmelCase							,						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(UpperCAmelCase							,						getattr(UpperCAmelCase							,						UpperCAmelCase   )   )





				def        UpperCAmelCase__						(				self   ):
							lowerCamelCase_					=  GenerationConfig(
							    do_sample=UpperCAmelCase							,						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(UpperCAmelCase							,						getattr(UpperCAmelCase							,						UpperCAmelCase   )   )

        # 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(
										    UpperCAmelCase							,						repo_id='''valid_org/test-generation-config-org'''							,						push_to_hub=UpperCAmelCase							,						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(UpperCAmelCase							,						getattr(UpperCAmelCase							,						UpperCAmelCase   )   )



