



import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, 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.models.esm.modeling_esmfold import EsmForProteinFolding

class     _A       :



       def __init__(		self							:     Any     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     int=13     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=7     ,		__SCREAMING_SNAKE_CASE							:     Dict=False     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=True     ,		__SCREAMING_SNAKE_CASE							:     Tuple=False     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=False     ,		__SCREAMING_SNAKE_CASE							:     str=19     ,		__SCREAMING_SNAKE_CASE							:     str=32     ,		__SCREAMING_SNAKE_CASE							:     List[str]=5     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=4     ,		__SCREAMING_SNAKE_CASE							:     Dict=37     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]="gelu"     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.1     ,		__SCREAMING_SNAKE_CASE							:     str=0.1     ,		__SCREAMING_SNAKE_CASE							:     str=512     ,		__SCREAMING_SNAKE_CASE							:     Dict=16     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=2     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=0.02     ,		__SCREAMING_SNAKE_CASE							:     int=3     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=4     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=None     ,		):


           '''simple docstring'''

           __a						       =				parent
           __a						       =				batch_size
           __a						       =				seq_length
           __a						       =				is_training
           __a						       =				use_input_mask
           __a						       =				use_token_type_ids
           __a						       =				use_labels
           __a						       =				vocab_size
           __a						       =				hidden_size
           __a						       =				num_hidden_layers
           __a						       =				num_attention_heads
           __a						       =				intermediate_size
           __a						       =				hidden_act
           __a						       =				hidden_dropout_prob
           __a						       =				attention_probs_dropout_prob
           __a						       =				max_position_embeddings
           __a						       =				type_vocab_size
           __a						       =				type_sequence_label_size
           __a						       =				initializer_range
           __a						       =				num_labels
           __a						       =				num_choices
           __a						       =				scope



       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           __a						       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size)

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

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

           __a						       =				self.get_config()

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



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           __a						       =				EsmConfig(
               vocab_size=33     ,		hidden_size=self.hidden_size     ,		pad_token_id=1     ,		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     ,		is_folding_model=__SCREAMING_SNAKE_CASE     ,		esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False}     ,		)
           return config



       def 		_lowerCamelCase			(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     Dict     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     List[str]     ,		__SCREAMING_SNAKE_CASE							:     str     ,		__SCREAMING_SNAKE_CASE							:     Any     ,		__SCREAMING_SNAKE_CASE							:     List[str]):


           '''simple docstring'''

           __a						       =				EsmForProteinFolding(config=__SCREAMING_SNAKE_CASE).float()
           model.to(__SCREAMING_SNAKE_CASE)
           model.eval()
           __a						       =				model(__SCREAMING_SNAKE_CASE     ,		attention_mask=__SCREAMING_SNAKE_CASE)
           __a						       =				model(__SCREAMING_SNAKE_CASE)
           __a						       =				model(__SCREAMING_SNAKE_CASE)

           self.parent.assertEqual(result.positions.shape     ,		(8, self.batch_size, self.seq_length, 14, 3))
           self.parent.assertEqual(result.angles.shape     ,		(8, self.batch_size, self.seq_length, 7, 2))





       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           __a						       =				self.prepare_config_and_inputs()
           (
               (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   (
               __a
           )       ,   
           )						       =				config_and_inputs
           __a						       =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict


@require_torch
class     _A       (	__UpperCAmelCase      ,__UpperCAmelCase      ,unittest.TestCase    ):
       UpperCamelCase__  :    Tuple											=						False

       UpperCamelCase__  :    Optional[Any]											=						(EsmForProteinFolding,) if is_torch_available() else ()
       UpperCamelCase__  :    List[str]											=						()
       UpperCamelCase__  :    str											=						{} if is_torch_available() else {}
       UpperCamelCase__  :    Optional[Any]											=						False



       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           __a						       =				EsmFoldModelTester(self)
           __a						       =				ConfigTester(self     ,		config_class=__SCREAMING_SNAKE_CASE     ,		hidden_size=37)



       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)



       @unittest.skip('''Does not support attention outputs''')
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           pass



       @unittest.skip
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           pass



       @unittest.skip('''Esm does not support embedding resizing''')
       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           pass



       @unittest.skip('''Esm does not support embedding resizing''')
       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support passing input embeds!''')
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support head pruning.''')
       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support head pruning.''')
       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support head pruning.''')
       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support head pruning.''')
       def 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support head pruning.''')
       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not output hidden states in the normal way.''')
       def 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMfold does not output hidden states in the normal way.''')
       def 		_lowerCamelCase			(		self							:     Union[str, Any]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold only has one output format.''')
       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           pass



       @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality''')
       def 		_lowerCamelCase			(		self							:     Union[str, Any]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold does not support input chunking.''')
       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.''')
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''')
       def 		_lowerCamelCase			(		self							:     int):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''')
       def 		_lowerCamelCase			(		self							:     List[str]):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold doesn\'t support torchscript compilation.''')
       def 		_lowerCamelCase			(		self							:     Any):


           '''simple docstring'''

           pass



       @unittest.skip('''ESMFold doesn\'t support data parallel.''')
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           pass





       @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

           pass


@require_torch
class     _A       (	__UpperCAmelCase    ):



       @slow
       def 		_lowerCamelCase			(		self							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1''').float()
           model.eval()
           __a						       =				torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]])
           __a						       =				model(__SCREAMING_SNAKE_CASE)['''positions''']
           __a						       =				torch.tensor([2.58_28, 0.79_93, -10.93_34]     ,		dtype=torch.floataa)
           self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]     ,		__SCREAMING_SNAKE_CASE     ,		atol=1E-4))










from json import JSONDecodeError  # Workaround for requests.exceptions.JSONDecodeError

import requests





def      __snake_case	(			_UpperCAmelCase = "isbn/0140328726"				):
    __a						       =				olid.strip().strip('''/'''				)  # Remove leading/trailing whitespace & slashes
    if new_olid.count('''/'''				) != 1:
        __a						       =				f'{olid} is not a valid Open Library olid'
        raise ValueError(_UpperCAmelCase				)
    return requests.get(f'https://openlibrary.org/{new_olid}.json'				).json()





def      __snake_case	(			_UpperCAmelCase				):
    __a						       =				{
        '''title''': '''Title''',
        '''publish_date''': '''Publish date''',
        '''authors''': '''Authors''',
        '''number_of_pages''': '''Number of pages:''',
        '''first_sentence''': '''First sentence''',
        '''isbn_10''': '''ISBN (10)''',
        '''isbn_13''': '''ISBN (13)''',
    }
    __a						       =				{better_key: ol_book_data[key] for key, better_key in desired_keys.items()}
    __a						       =				[
        get_openlibrary_data(author['''key''']				)['''name'''] for author in data['''Authors''']
    ]
    __a						       =				data['''First sentence''']['''value''']
    for key, value in data.items():
        if isinstance(_UpperCAmelCase   , _UpperCAmelCase				):
            __a						       =				''', '''.join(_UpperCAmelCase				)
    return data


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 while True:
  __snake_case						:List[Any]      		=     input('''\nEnter the ISBN code to search (or \'quit\' to stop): ''').strip()
  if isbn.lower() in ("", "q", "quit", "exit", "stop"):
   break

  if len(isbn) not in (10, 13) or not isbn.isdigit():
   print(f'Sorry, {isbn} is not a valid ISBN.  Please, input a valid ISBN.')
   continue

  print(f'\nSearching Open Library for ISBN: {isbn}...\n')

  try:
   __snake_case						:Optional[Any]      		=     summarize_book(get_openlibrary_data(f'isbn/{isbn}'))
   print('''\n'''.join(f'{key}: {value}' for key, value in book_summary.items()))
  except JSONDecodeError:  # Workaround for requests.exceptions.RequestException:
   print(f'Sorry, there are no results for ISBN: {isbn}.')





