
"""simple docstring"""





import numpy as np
from PIL import Image



def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case	)							->		np.ndarray:


					"""simple docstring"""





					_UpperCamelCase							 =  np.array(__snake_case	)
					if arr.shape[0] != arr.shape[1]:
										raise ValueError('''The input array is not a square matrix'''	)
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0

					# compute the shape of the output matrix
					_UpperCamelCase							 =  (arr.shape[0] - size) // stride + 1
					# initialize the output matrix with zeros of shape maxpool_shape
					_UpperCamelCase							 =  np.zeros((maxpool_shape, maxpool_shape)	)

					while i < arr.shape[0]:
										if i + size > arr.shape[0]:
															# if the end of the matrix is reached, break
															break
										while j < arr.shape[1]:
															# if the end of the matrix is reached, break
															if j + size > arr.shape[1]:
																				break
															# compute the maximum of the pooling matrix
															_UpperCamelCase							 =  np.max(arr[i : i + size, j : j + size]	)
															# shift the pooling matrix by stride of column pixels
															j += stride
															mat_j += 1

										# shift the pooling matrix by stride of row pixels
										i += stride
										mat_i += 1

										# reset the column index to 0
										_UpperCamelCase							 =  0
										_UpperCamelCase							 =  0

					return updated_arr



def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case	)							->		np.ndarray:


					"""simple docstring"""





					_UpperCamelCase							 =  np.array(__snake_case	)
					if arr.shape[0] != arr.shape[1]:
										raise ValueError('''The input array is not a square matrix'''	)
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0
					_UpperCamelCase							 =  0

					# compute the shape of the output matrix
					_UpperCamelCase							 =  (arr.shape[0] - size) // stride + 1
					# initialize the output matrix with zeros of shape avgpool_shape
					_UpperCamelCase							 =  np.zeros((avgpool_shape, avgpool_shape)	)

					while i < arr.shape[0]:
										# if the end of the matrix is reached, break
										if i + size > arr.shape[0]:
															break
										while j < arr.shape[1]:
															# if the end of the matrix is reached, break
															if j + size > arr.shape[1]:
																				break
															# compute the average of the pooling matrix
															_UpperCamelCase							 =  int(np.average(arr[i : i + size, j : j + size]	)	)
															# shift the pooling matrix by stride of column pixels
															j += stride
															mat_j += 1

										# shift the pooling matrix by stride of row pixels
										i += stride
										mat_i += 1
										# reset the column index to 0
										_UpperCamelCase							 =  0
										_UpperCamelCase							 =  0

					return updated_arr


# Main Function
if __name__ == "__main__":
				from doctest import testmod

				testmod(name="""avgpooling""", verbose=True)

				# Loading the image
				_a														=		Image.open("""path_to_image""")

				# Converting the image to numpy array and maxpooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

				# Converting the image to numpy array and averagepooling, displaying the result
				# Ensure that the image is a square matrix

				Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()








"""simple docstring"""





import torch

from transformers import CamembertForMaskedLM, CamembertTokenizer



def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case,     __snake_case=5	)							->		Union[str, Any]:


					"""simple docstring"""





					assert masked_input.count('''<mask>'''	) == 1
					_UpperCamelCase							 =  torch.tensor(tokenizer.encode(__snake_case,     add_special_tokens=__snake_case	)	).unsqueeze(0	)  # Batch size 1
					_UpperCamelCase							 =  model(__snake_case	)[0]  # The last hidden-state is the first element of the output tuple
					_UpperCamelCase							 =  (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item()
					_UpperCamelCase							 =  logits[0, masked_index, :]
					_UpperCamelCase							 =  logits.softmax(dim=0	)
					_UpperCamelCase ,  _UpperCamelCase							 =  prob.topk(k=__snake_case,     dim=0	)
					_UpperCamelCase							 =  ''' '''.join(
					    [tokenizer.convert_ids_to_tokens(indices[i].item()	) for i in range(len(__snake_case	)	)]	)
					_UpperCamelCase							 =  tokenizer.mask_token
					_UpperCamelCase							 =  []
					for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(''' '''	)	):
										_UpperCamelCase							 =  predicted_token_bpe.replace('''\u2581''',     ''' '''	)
										if " {0}".format(__snake_case	) in masked_input:
															topk_filled_outputs.append(
															    (
															        masked_input.replace(''' {0}'''.format(__snake_case	),     __snake_case	),
															        values[index].item(),
															        predicted_token,
															    )	)
										else:
															topk_filled_outputs.append(
															    (
															        masked_input.replace(__snake_case,     __snake_case	),
															        values[index].item(),
															        predicted_token,
															    )	)
					return topk_filled_outputs


_a														=		CamembertTokenizer.from_pretrained("""camembert-base""")
_a														=		CamembertForMaskedLM.from_pretrained("""camembert-base""")
model.eval()

_a														=		"""Le camembert est <mask> :)"""
print(fill_mask(masked_input, model, tokenizer, topk=3))






