


"""simple docstring"""







import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
from torch import nn

from ...models.controlnet import ControlNetModel, ControlNetOutput
from ...models.modeling_utils import ModelMixin
from ...utils import logging


a__    :							Tuple            =					logging.get_logger(__name__)




class 			UpperCamelCase_     (			UpperCamelCase):

	"""simple docstring"""





	def __init__(			self      :    Optional[int]      ,      UpperCAmelCase__      :    Union[List[ControlNetModel], Tuple[ControlNetModel]]       )	->     int:
						super().__init__()
						__SCREAMING_SNAKE_CASE						       =    nn.ModuleList(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    torch.FloatTensor      ,      UpperCAmelCase__      :    Union[torch.Tensor, float, int]      ,      UpperCAmelCase__      :    torch.Tensor      ,      UpperCAmelCase__      :    List[torch.tensor]      ,      UpperCAmelCase__      :    List[float]      ,      UpperCAmelCase__      :    Optional[torch.Tensor] = None      ,      UpperCAmelCase__      :    Optional[torch.Tensor] = None      ,      UpperCAmelCase__      :    Optional[torch.Tensor] = None      ,      UpperCAmelCase__      :    Optional[Dict[str, Any]] = None      ,      UpperCAmelCase__      :    bool = False      ,      UpperCAmelCase__      :    bool = True      ,      )	->     Union[ControlNetOutput, Tuple]:
						for i, (image, scale, controlnet) in enumerate(zip(UpperCAmelCase__      ,      UpperCAmelCase__      ,      self.nets       )       ):
											__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    controlnet(
											    UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      UpperCAmelCase__      ,      )

											# merge samples
											if i == 0:
																__SCREAMING_SNAKE_CASE       ,  __SCREAMING_SNAKE_CASE						       =    down_samples, mid_sample
											else:
																__SCREAMING_SNAKE_CASE						       =    [
																    samples_prev + samples_curr
																    for samples_prev, samples_curr in zip(UpperCAmelCase__      ,      UpperCAmelCase__       )
																]
																mid_block_res_sample += mid_sample

						return down_block_res_samples, mid_block_res_sample




	def   UpperCAmelCase_				(			self      :    Union[str, Any]      ,      UpperCAmelCase__      :    Union[str, os.PathLike]      ,      UpperCAmelCase__      :    bool = True      ,      UpperCAmelCase__      :    Callable = None      ,      UpperCAmelCase__      :    bool = False      ,      UpperCAmelCase__      :    Optional[str] = None      ,      )	->     int:
						__SCREAMING_SNAKE_CASE						       =    0
						__SCREAMING_SNAKE_CASE						       =    save_directory
						for controlnet in self.nets:
											controlnet.save_pretrained(
											    UpperCAmelCase__      ,      is_main_process=UpperCAmelCase__      ,      save_function=UpperCAmelCase__      ,      safe_serialization=UpperCAmelCase__      ,      variant=UpperCAmelCase__      ,      )

											idx += 1
											__SCREAMING_SNAKE_CASE						       =    model_path_to_save + F"""_{idx}"""




	@classmethod
	def   UpperCAmelCase_				(			cls      :    List[str]      ,      UpperCAmelCase__      :    Optional[Union[str, os.PathLike]]      ,      **UpperCAmelCase__      :    Optional[Any]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    0
						__SCREAMING_SNAKE_CASE						       =    []

						# load controlnet and append to list until no controlnet directory exists anymore
						# first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained`
						# second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ...
						__SCREAMING_SNAKE_CASE						       =    pretrained_model_path
						while os.path.isdir(UpperCAmelCase__       ):
											__SCREAMING_SNAKE_CASE						       =    ControlNetModel.from_pretrained(UpperCAmelCase__      ,      **UpperCAmelCase__       )
											controlnets.append(UpperCAmelCase__       )

											idx += 1
											__SCREAMING_SNAKE_CASE						       =    pretrained_model_path + F"""_{idx}"""

						logger.info(F"""{len(UpperCAmelCase__       )} controlnets loaded from {pretrained_model_path}."""       )

						if len(UpperCAmelCase__       ) == 0:
											raise ValueError(
											    F"""No ControlNets found under {os.path.dirname(UpperCAmelCase__       )}. Expected at least {pretrained_model_path + '_0'}."""       )

						return cls(UpperCAmelCase__       )




"""simple docstring"""







a__    :							Optional[Any]            =					'''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''




def        UpperCAmelCase__	():




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    input("Enter message: "       )
					__SCREAMING_SNAKE_CASE						       =    input("Enter key [alphanumeric]: "       )
					__SCREAMING_SNAKE_CASE						       =    input("Encrypt/Decrypt [e/d]: "       )

					if mode.lower().startswith("e"       ):
										__SCREAMING_SNAKE_CASE						       =    "encrypt"
										__SCREAMING_SNAKE_CASE						       =    encrypt_message(lowerCAmelCase_  ,						lowerCAmelCase_       )
					elif mode.lower().startswith("d"       ):
										__SCREAMING_SNAKE_CASE						       =    "decrypt"
										__SCREAMING_SNAKE_CASE						       =    decrypt_message(lowerCAmelCase_  ,						lowerCAmelCase_       )

					print(f"""\n{mode.title()}ed message:"""       )
					print(lowerCAmelCase_       )




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					return translate_message(lowerCAmelCase_  ,						lowerCAmelCase_  ,						"encrypt"       )




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					return translate_message(lowerCAmelCase_  ,						lowerCAmelCase_  ,						"decrypt"       )




def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    []
					__SCREAMING_SNAKE_CASE						       =    0
					__SCREAMING_SNAKE_CASE						       =    key.upper()

					for symbol in message:
										__SCREAMING_SNAKE_CASE						       =    LETTERS.find(symbol.upper()       )
										if num != -1:
															if mode == "encrypt":
																				num += LETTERS.find(key[key_index]       )
															elif mode == "decrypt":
																				num -= LETTERS.find(key[key_index]       )

															num %= len(lowerCAmelCase_       )

															if symbol.isupper():
																				translated.append(LETTERS[num]       )
															elif symbol.islower():
																				translated.append(LETTERS[num].lower()       )

															key_index += 1
															if key_index == len(lowerCAmelCase_       ):
																				__SCREAMING_SNAKE_CASE						       =    0
										else:
															translated.append(lowerCAmelCase_       )
					return "".join(lowerCAmelCase_       )


if __name__ == "__main__":
		main()
