# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import difflib
from collections.abc import Mapping
from typing import Any, Callable, Dict



class    lowercase_    (     __snake_case		):
							pass




def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase=None					,			__lowercase=False						)       ->       Optional[Any]:

						'''simple docstring'''



						try:
												_snake_case						:     str				=    ast.parse(__lowercase						)
						except SyntaxError as e:
												print("The code generated by the agent is not valid.\n"					,			__lowercase						)
												return
						if state is None:
												_snake_case						:     List[Any]				=    {}
						_snake_case						:     Tuple				=    None
						for idx, node in enumerate(expression.body						):
												try:
																		_snake_case						:     Optional[int]				=    evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						)
												except InterpretorError as e:
																		_snake_case						:     int				=    F"""Evaluation of the code stopped at line {idx} before the end because of the following error"""
																		if chat_mode:
																								msg += (
																								    F""". Copy paste the following error message and send it back to the agent:\nI get an error: '{e}'"""
																								)
																		else:
																								msg += F""":\n{e}"""
																		print(__lowercase						)
																		break
												if line_result is not None:
																		_snake_case						:     Any				=    line_result

						return result
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       Tuple:

						'''simple docstring'''



						if isinstance(__lowercase					,			ast.Assign						):
												# Assignement -> we evaluate the assignement which should update the state
												# We return the variable assigned as it may be used to determine the final result.
												return evaluate_assign(__lowercase					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.Call						):
												# Function call -> we return the value of the function call
												return evaluate_call(__lowercase					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.Constant						):
												# Constant -> just return the value
												return expression.value
						elif isinstance(__lowercase					,			ast.Dict						):
												# Dict -> evaluate all keys and values
												_snake_case						:     Tuple				=    [evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						) for k in expression.keys]
												_snake_case						:     Dict				=    [evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						) for v in expression.values]
												return dict(zip(__lowercase					,			__lowercase						)						)
						elif isinstance(__lowercase					,			ast.Expr						):
												# Expression -> evaluate the content
												return evaluate_ast(expression.value					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.For						):
												# For loop -> execute the loop
												return evaluate_for(__lowercase					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.FormattedValue						):
												# Formatted value (part of f-string) -> evaluate the content and return
												return evaluate_ast(expression.value					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.If						):
												# If -> execute the right branch
												return evaluate_if(__lowercase					,			__lowercase					,			__lowercase						)
						elif hasattr(__lowercase					,			"Index"						) and isinstance(__lowercase					,			ast.Index						):
												return evaluate_ast(expression.value					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.JoinedStr						):
												return "".join([str(evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						)						) for v in expression.values]						)
						elif isinstance(__lowercase					,			ast.List						):
												# List -> evaluate all elements
												return [evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						) for elt in expression.elts]
						elif isinstance(__lowercase					,			ast.Name						):
												# Name -> pick up the value in the state
												return evaluate_name(__lowercase					,			__lowercase					,			__lowercase						)
						elif isinstance(__lowercase					,			ast.Subscript						):
												# Subscript -> return the value of the indexing
												return evaluate_subscript(__lowercase					,			__lowercase					,			__lowercase						)
						else:
												# For now we refuse anything else. Let's add things as we need them.
												raise InterpretorError(F"""{expression.__class__.__name__} is not supported."""						)
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       Union[str, Any]:

						'''simple docstring'''



						_snake_case						:     List[str]				=    assign.targets
						_snake_case						:     List[Any]				=    evaluate_ast(assign.value					,			__lowercase					,			__lowercase						)

						if len(__lowercase						) == 1:
												_snake_case						:     Any				=    result
						else:
												if len(__lowercase						) != len(__lowercase						):
																		raise InterpretorError(F"""Expected {len(__lowercase						)} values but got {len(__lowercase						)}."""						)
												for var_name, r in zip(__lowercase					,			__lowercase						):
																		_snake_case						:     List[str]				=    r
						return result
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       List[str]:

						'''simple docstring'''



						if not isinstance(call.func					,			ast.Name						):
												raise InterpretorError(
												    F"""It is not permitted to evaluate other functions than the provided tools (tried to execute {call.func} of """
												    F"""type {type(call.func						)}."""						)
						_snake_case						:     Tuple				=    call.func.id
						if func_name not in tools:
												raise InterpretorError(
												    F"""It is not permitted to evaluate other functions than the provided tools (tried to execute {call.func.id})."""						)

						_snake_case						:     Any				=    tools[func_name]
						# Todo deal with args
						_snake_case						:     List[str]				=    [evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						) for arg in call.args]
						_snake_case						:     str				=    {keyword.arg: evaluate_ast(keyword.value					,			__lowercase					,			__lowercase						) for keyword in call.keywords}
						return func(*__lowercase					,			**__lowercase						)
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       Optional[int]:

						'''simple docstring'''



						_snake_case						:     str				=    evaluate_ast(subscript.slice					,			__lowercase					,			__lowercase						)
						_snake_case						:     str				=    evaluate_ast(subscript.value					,			__lowercase					,			__lowercase						)
						if isinstance(__lowercase					,			(list, tuple)						):
												return value[int(__lowercase						)]
						if index in value:
												return value[index]
						if isinstance(__lowercase					,			__lowercase						) and isinstance(__lowercase					,			__lowercase						):
												_snake_case						:     int				=    difflib.get_close_matches(__lowercase					,			list(value.keys()						)						)
												if len(__lowercase						) > 0:
																		return value[close_matches[0]]

						raise InterpretorError(F"""Could not index {value} with '{index}'."""						)
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       str:

						'''simple docstring'''



						if name.id in state:
												return state[name.id]
						_snake_case						:     Optional[Any]				=    difflib.get_close_matches(name.id					,			list(state.keys()						)						)
						if len(__lowercase						) > 0:
												return state[close_matches[0]]
						raise InterpretorError(F"""The variable `{name.id}` is not defined."""						)
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       Dict:

						'''simple docstring'''



						if len(condition.ops						) > 1:
												raise InterpretorError("Cannot evaluate conditions with multiple operators"						)

						_snake_case						:     Dict				=    evaluate_ast(condition.left					,			__lowercase					,			__lowercase						)
						_snake_case						:     int				=    condition.ops[0]
						_snake_case						:     Any				=    evaluate_ast(condition.comparators[0]					,			__lowercase					,			__lowercase						)

						if isinstance(__lowercase					,			ast.Eq						):
												return left == right
						elif isinstance(__lowercase					,			ast.NotEq						):
												return left != right
						elif isinstance(__lowercase					,			ast.Lt						):
												return left < right
						elif isinstance(__lowercase					,			ast.LtE						):
												return left <= right
						elif isinstance(__lowercase					,			ast.Gt						):
												return left > right
						elif isinstance(__lowercase					,			ast.GtE						):
												return left >= right
						elif isinstance(__lowercase					,			ast.Is						):
												return left is right
						elif isinstance(__lowercase					,			ast.IsNot						):
												return left is not right
						elif isinstance(__lowercase					,			ast.In						):
												return left in right
						elif isinstance(__lowercase					,			ast.NotIn						):
												return left not in right
						else:
												raise InterpretorError(F"""Operator not supported: {comparator}"""						)
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       List[str]:

						'''simple docstring'''



						_snake_case						:     List[str]				=    None
						if evaluate_condition(if_statement.test					,			__lowercase					,			__lowercase						):
												for line in if_statement.body:
																		_snake_case						:     List[str]				=    evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						)
																		if line_result is not None:
																								_snake_case						:     Dict				=    line_result
						else:
												for line in if_statement.orelse:
																		_snake_case						:     str				=    evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						)
																		if line_result is not None:
																								_snake_case						:     int				=    line_result
						return result

def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       Union[str, Any]:

						'''simple docstring'''



						_snake_case						:     Tuple				=    None
						_snake_case						:     int				=    evaluate_ast(for_loop.iter					,			__lowercase					,			__lowercase						)
						for counter in iterator:
												_snake_case						:     List[Any]				=    counter
												for expression in for_loop.body:
																		_snake_case						:     Dict				=    evaluate_ast(__lowercase					,			__lowercase					,			__lowercase						)
																		if line_result is not None:
																								_snake_case						:     str				=    line_result
						return result


import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        ErnieMForInformationExtraction,
        ErnieMForMultipleChoice,
        ErnieMForQuestionAnswering,
        ErnieMForSequenceClassification,
        ErnieMForTokenClassification,
        ErnieMModel,
    )
    from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST







class  __lowerCamelCase				:







      def __init__(     self							,  lowerCamelCase							,  lowerCamelCase=13							,  lowerCamelCase=7							,  lowerCamelCase=True							,  lowerCamelCase=True							,  lowerCamelCase=True							,  lowerCamelCase=99							,  lowerCamelCase=32							,  lowerCamelCase=5							,  lowerCamelCase=4							,  lowerCamelCase=37							,  lowerCamelCase="gelu"							,  lowerCamelCase=0.1							,  lowerCamelCase=0.1							,  lowerCamelCase=512							,  lowerCamelCase=16							,  lowerCamelCase=2							,  lowerCamelCase=0.02							,  lowerCamelCase=3							,  lowerCamelCase=4							,  lowerCamelCase=None							,  )						->       Optional[int]:
        snake_case_       						=      parent
        snake_case_       						=      batch_size
        snake_case_       						=      seq_length
        snake_case_       						=      is_training
        snake_case_       						=      use_input_mask
        snake_case_       						=      use_labels
        snake_case_       						=      vocab_size
        snake_case_       						=      hidden_size
        snake_case_       						=      num_hidden_layers
        snake_case_       						=      num_attention_heads
        snake_case_       						=      intermediate_size
        snake_case_       						=      hidden_act
        snake_case_       						=      hidden_dropout_prob
        snake_case_       						=      attention_probs_dropout_prob
        snake_case_       						=      max_position_embeddings
        snake_case_       						=      type_vocab_size
        snake_case_       						=      type_sequence_label_size
        snake_case_       						=      initializer_range
        snake_case_       						=      num_labels
        snake_case_       						=      num_choices
        snake_case_       						=      scope







      def 	lowerCAmelCase_    (     self   )						->       Optional[Any]:
        snake_case_       						=      ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size   )

        snake_case_       						=      None
        if self.use_input_mask:
          snake_case_       						=      random_attention_mask([self.batch_size, self.seq_length]   )

        snake_case_       						=      None
        snake_case_       						=      None
        snake_case_       						=      None
        if self.use_labels:
          snake_case_       						=      ids_tensor([self.batch_size]							,  self.type_sequence_label_size   )
          snake_case_       						=      ids_tensor([self.batch_size, self.seq_length]							,  self.num_labels   )
          snake_case_       						=      ids_tensor([self.batch_size]							,  self.num_choices   )

        snake_case_       						=      self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







      def 	lowerCAmelCase_    (     self   )						->       Optional[Any]:
        snake_case_       						=      ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size   )

        snake_case_       						=      None
        if self.use_input_mask:
          snake_case_       						=      random_attention_mask([self.batch_size, self.seq_length]   )
        snake_case_       						=      self.get_config()

        return config, input_ids, input_mask







      def 	lowerCAmelCase_    (     self   )						->       Optional[int]:
        return ErnieMConfig(
            vocab_size=self.vocab_size							,  hidden_size=self.hidden_size							,  num_hidden_layers=self.num_hidden_layers							,  num_attention_heads=self.num_attention_heads							,  intermediate_size=self.intermediate_size							,  hidden_act=self.hidden_act							,  hidden_dropout_prob=self.hidden_dropout_prob							,  attention_probs_dropout_prob=self.attention_probs_dropout_prob							,  max_position_embeddings=self.max_position_embeddings							,  type_vocab_size=self.type_vocab_size							,  initializer_range=self.initializer_range							,  )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Union[str, Any]:
        snake_case_       						=      ErnieMModel(config=lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        snake_case_       						=      model(lowerCamelCase							,  return_dict=lowerCamelCase   )
        self.parent.assertEqual(result.last_hidden_state.shape							,  (self.batch_size, self.seq_length, self.hidden_size)   )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Tuple:
        snake_case_       						=      ErnieMForQuestionAnswering(config=lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        snake_case_       						=      model(
            lowerCamelCase							,  attention_mask=lowerCamelCase							,  start_positions=lowerCamelCase							,  end_positions=lowerCamelCase							,  )
        self.parent.assertEqual(result.start_logits.shape							,  (self.batch_size, self.seq_length)   )
        self.parent.assertEqual(result.end_logits.shape							,  (self.batch_size, self.seq_length)   )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Tuple:
        snake_case_       						=      ErnieMForInformationExtraction(config=lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        snake_case_       						=      torch.ones_like(lowerCamelCase							,  dtype=torch.floataa   )
        snake_case_       						=      model(
            lowerCamelCase							,  attention_mask=lowerCamelCase							,  start_positions=lowerCamelCase							,  end_positions=lowerCamelCase							,  )
        self.parent.assertEqual(result.start_logits.shape							,  (self.batch_size, self.seq_length)   )
        self.parent.assertEqual(result.end_logits.shape							,  (self.batch_size, self.seq_length)   )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Optional[Any]:
        snake_case_       						=      self.num_labels
        snake_case_       						=      ErnieMForSequenceClassification(lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        snake_case_       						=      model(lowerCamelCase							,  attention_mask=lowerCamelCase							,  labels=lowerCamelCase   )
        self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels)   )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Union[str, Any]:
        snake_case_       						=      self.num_labels
        snake_case_       						=      ErnieMForTokenClassification(config=lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        input_ids.to(lowerCamelCase   )
        input_mask.to(lowerCamelCase   )
        token_labels.to(lowerCamelCase   )

        snake_case_       						=      model(lowerCamelCase							,  attention_mask=lowerCamelCase							,  labels=lowerCamelCase   )

        self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.seq_length, self.num_labels)   )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Any:
        snake_case_       						=      self.num_choices
        snake_case_       						=      ErnieMForMultipleChoice(config=lowerCamelCase   )
        model.to(lowerCamelCase   )
        model.eval()
        snake_case_       						=      input_ids.unsqueeze(1   ).expand(-1							,  self.num_choices							,  -1   ).contiguous()
        snake_case_       						=      input_mask.unsqueeze(1   ).expand(-1							,  self.num_choices							,  -1   ).contiguous()
        snake_case_       						=      model(
            lowerCamelCase							,  attention_mask=lowerCamelCase							,  labels=lowerCamelCase							,  )
        self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_choices)   )







      def 	lowerCAmelCase_    (     self   )						->       str:
        snake_case_       						=      self.prepare_config_and_inputs()
        (
            (
            snake_case_
        )						,				(
            snake_case_
        )						,				(
            snake_case_
        )						,				(
            snake_case_
        )						,				(
            snake_case_
        )						,				(
            snake_case_
        )						,				
        )       						=      config_and_inputs
        snake_case_       						=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class  __lowerCamelCase				(	__snake_case  ,							__snake_case  ,							unittest.TestCase	):
      lowerCamelCase_	:      Optional[int]          =					(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      lowerCamelCase_	:      List[str]          =					()
      lowerCamelCase_	:      Optional[int]          =					(
          {
              'feature-extraction': ErnieMModel,
              'question-answering': ErnieMForQuestionAnswering,
              'text-classification': ErnieMForSequenceClassification,
              'token-classification': ErnieMForTokenClassification,
              'zero-shot': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      lowerCamelCase_	:      List[Any]          =					False







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       Tuple:
        if pipeline_test_casse_name == "QAPipelineTests":
          return True

        return False







      def 	lowerCAmelCase_    (     self   )						->       Tuple:
        snake_case_       						=      ErnieMModelTester(self   )
        snake_case_       						=      ConfigTester(self							,  config_class=lowerCamelCase							,  hidden_size=37   )







      def 	lowerCAmelCase_    (     self   )						->       Any:
        self.config_tester.run_common_tests()







      def 	lowerCAmelCase_    (     self   )						->       Dict:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       int:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          snake_case_       						=      type
          self.model_tester.create_and_check_model(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       Dict:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       Optional[Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       List[Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       List[Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       Union[str, Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCamelCase   )







      @slow
      def 	lowerCAmelCase_    (     self   )						->       int:
        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          snake_case_       						=      ErnieMModel.from_pretrained(lowerCamelCase   )
          self.assertIsNotNone(lowerCamelCase   )







@require_torch
class  __lowerCamelCase				(	unittest.TestCase	):







      @slow
      def 	lowerCAmelCase_    (     self   )						->       List[Any]:
        snake_case_       						=      ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""   )
        model.eval()
        snake_case_       						=      torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        snake_case_       						=      model(lowerCamelCase   )[0]

        # TODO Replace vocab size
        snake_case_       						=      768

        snake_case_       						=      torch.Size((1, 6, hidden_size)   )
        self.assertEqual(output.shape							,  lowerCamelCase   )

        snake_case_       						=      torch.tensor(
            [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]   )

        self.assertTrue(torch.allclose(output[:, :3, :3]							,  lowerCamelCase							,  atol=1e-3   )   )