"""simple docstring"""






import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
						from transformers.pipelines.conversational import Conversation

lowerCAmelCase__    :    int			=      logging.get_logger(__name__)

lowerCAmelCase__    :    int			=      {
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

lowerCAmelCase__    :    Tuple			=      {
    'vocab_file': {
        'gpt2': 'https://huggingface.co/gpt2/resolve/main/vocab.json',
        'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/vocab.json',
        'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/vocab.json',
        'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/vocab.json',
        'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/vocab.json',
    },
    'merges_file': {
        'gpt2': 'https://huggingface.co/gpt2/resolve/main/merges.txt',
        'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/merges.txt',
        'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/merges.txt',
        'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/merges.txt',
        'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/merges.txt',
    },
}

lowerCAmelCase__    :    Any			=      {
    'gpt2': 1_024,
    'gpt2-medium': 1_024,
    'gpt2-large': 1_024,
    'gpt2-xl': 1_024,
    'distilgpt2': 1_024,
}



@lru_cache()
def     a_		(   ):
				UpperCAmelCase__			 =				(
				    list(range(ord('!'			)   ,     ord('~'			) + 1			)			) + list(range(ord('¡'			)   ,     ord('¬'			) + 1			)			) + list(range(ord('®'			)   ,     ord('ÿ'			) + 1			)			)
				)
				UpperCAmelCase__			 =				bs[:]
				UpperCAmelCase__			 =				0
				for b in range(2**8			):
								if b not in bs:
												bs.append(lowerCamelCase			)
												cs.append(2**8 + n			)
												n += 1
				UpperCAmelCase__			 =				[chr(lowerCamelCase			) for n in cs]
				return dict(zip(lowerCamelCase   ,     lowerCamelCase			)			)



def     a_		(   lowerCamelCase			):
				UpperCAmelCase__			 =				set()
				UpperCAmelCase__			 =				word[0]
				for char in word[1:]:
								pairs.add((prev_char, char)			)
								UpperCAmelCase__			 =				char
				return pairs







class 							snake_case			(					__UpperCAmelCase							):




					"""simple docstring"""

					snake_case__							       =					VOCAB_FILES_NAMES
					snake_case__							       =					PRETRAINED_VOCAB_FILES_MAP
					snake_case__							       =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					snake_case__							       =					["input_ids", "attention_mask"]







					def __init__(							self  :      Dict       ,lowerCamelCase__  :      Union[str, Any]       ,lowerCamelCase__  :      Dict       ,lowerCamelCase__  :      Dict="replace"       ,lowerCamelCase__  :      List[Any]="<|endoftext|>"       ,lowerCamelCase__  :      Optional[int]="<|endoftext|>"       ,lowerCamelCase__  :      Optional[Any]="<|endoftext|>"       ,lowerCamelCase__  :      Any=None       ,lowerCamelCase__  :      List[Any]=False       ,lowerCamelCase__  :      Optional[int]=False       ,**lowerCamelCase__  :      List[Any]       ,):
									UpperCAmelCase__			 =				AddedToken(lowerCamelCase__       ,lstrip=lowerCamelCase__       ,rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__       ,lowerCamelCase__     ) else bos_token
									UpperCAmelCase__			 =				AddedToken(lowerCamelCase__       ,lstrip=lowerCamelCase__       ,rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__       ,lowerCamelCase__     ) else eos_token
									UpperCAmelCase__			 =				AddedToken(lowerCamelCase__       ,lstrip=lowerCamelCase__       ,rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__       ,lowerCamelCase__     ) else unk_token
									UpperCAmelCase__			 =				AddedToken(lowerCamelCase__       ,lstrip=lowerCamelCase__       ,rstrip=lowerCamelCase__     ) if isinstance(lowerCamelCase__       ,lowerCamelCase__     ) else pad_token
									super().__init__(
									    errors=lowerCamelCase__       ,unk_token=lowerCamelCase__       ,bos_token=lowerCamelCase__       ,eos_token=lowerCamelCase__       ,pad_token=lowerCamelCase__       ,add_prefix_space=lowerCamelCase__       ,add_bos_token=lowerCamelCase__       ,**lowerCamelCase__       ,)
									UpperCAmelCase__			 =				add_bos_token

									with open(lowerCamelCase__       ,encoding='utf-8'     ) as vocab_handle:
													UpperCAmelCase__			 =				json.load(lowerCamelCase__     )
									UpperCAmelCase__			 =				{v: k for k, v in self.encoder.items()}
									UpperCAmelCase__			 =				errors  # how to handle errors in decoding
									UpperCAmelCase__			 =				bytes_to_unicode()
									UpperCAmelCase__			 =				{v: k for k, v in self.byte_encoder.items()}
									with open(lowerCamelCase__       ,encoding='utf-8'     ) as merges_handle:
													UpperCAmelCase__			 =				merges_handle.read().split('\n'     )[1:-1]
									UpperCAmelCase__			 =				[tuple(merge.split()     ) for merge in bpe_merges]
									UpperCAmelCase__			 =				dict(zip(lowerCamelCase__       ,range(len(lowerCamelCase__     )     )     )     )
									UpperCAmelCase__			 =				{}
									UpperCAmelCase__			 =				add_prefix_space

									# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
									UpperCAmelCase__			 =				re.compile(R'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'     )







					@property
					def 						__lowerCAmelCase		(							self  :      str     ):
									return len(self.encoder     )







					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									return dict(self.encoder       ,**self.added_tokens_encoder     )







					def 						__lowerCAmelCase		(							self  :      Tuple       ,lowerCamelCase__  :      int     ):
									if token in self.cache:
													return self.cache[token]
									UpperCAmelCase__			 =				tuple(lowerCamelCase__     )
									UpperCAmelCase__			 =				get_pairs(lowerCamelCase__     )

									if not pairs:
													return token

									while True:
													UpperCAmelCase__			 =				min(lowerCamelCase__       ,key=lambda lowerCamelCase__     : self.bpe_ranks.get(lowerCamelCase__       ,float('inf'     )     )     )
													if bigram not in self.bpe_ranks:
																	break
													UpperCAmelCase__ ,      UpperCAmelCase__			 =				bigram
													UpperCAmelCase__			 =				[]
													UpperCAmelCase__			 =				0
													while i < len(lowerCamelCase__     ):
																	try:
																					UpperCAmelCase__			 =				word.index(lowerCamelCase__       ,lowerCamelCase__     )
																	except ValueError:
																					new_word.extend(word[i:]     )
																					break
																	else:
																					new_word.extend(word[i:j]     )
																					UpperCAmelCase__			 =				j

																	if word[i] == first and i < len(lowerCamelCase__     ) - 1 and word[i + 1] == second:
																					new_word.append(first + second     )
																					i += 2
																	else:
																					new_word.append(word[i]     )
																					i += 1
													UpperCAmelCase__			 =				tuple(lowerCamelCase__     )
													UpperCAmelCase__			 =				new_word
													if len(lowerCamelCase__     ) == 1:
																	break
													else:
																	UpperCAmelCase__			 =				get_pairs(lowerCamelCase__     )
									UpperCAmelCase__			 =				' '.join(lowerCamelCase__     )
									UpperCAmelCase__			 =				word
									return word







					def 						__lowerCAmelCase		(							self  :      List[Any]       ,lowerCamelCase__  :      Dict       ,lowerCamelCase__  :      List[str]=None     ):
									if self.add_bos_token:
													UpperCAmelCase__			 =				[self.bos_token_id]
									else:
													UpperCAmelCase__			 =				[]

									UpperCAmelCase__			 =				bos_token_ids + token_ids_a

									if token_ids_a is None:
													return output

									return output + bos_token_ids + token_ids_a







					def 						__lowerCAmelCase		(							self  :      Optional[Any]       ,lowerCamelCase__  :      List[int]       ,lowerCamelCase__  :      Optional[List[int]] = None       ,lowerCamelCase__  :      bool = False     ):
									if already_has_special_tokens:
													return super().get_special_tokens_mask(
													    token_ids_a=lowerCamelCase__       ,token_ids_a=lowerCamelCase__       ,already_has_special_tokens=lowerCamelCase__     )

									if not self.add_bos_token:
													return super().get_special_tokens_mask(
													    token_ids_a=lowerCamelCase__       ,token_ids_a=lowerCamelCase__       ,already_has_special_tokens=lowerCamelCase__     )

									if token_ids_a is None:
													return [1] + ([0] * len(lowerCamelCase__     ))
									return [1] + ([0] * len(lowerCamelCase__     )) + [1] + ([0] * len(lowerCamelCase__     ))







					def 						__lowerCAmelCase		(							self  :      List[str]       ,lowerCamelCase__  :      Optional[Any]     ):
									UpperCAmelCase__			 =				[]
									for token in re.findall(self.pat       ,lowerCamelCase__     ):
													UpperCAmelCase__			 =				''.join(
													    self.byte_encoder[b] for b in token.encode('utf-8'     )     )  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
													bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCamelCase__     ).split(' '     )     )
									return bpe_tokens







					def 						__lowerCAmelCase		(							self  :      Any       ,lowerCamelCase__  :      Tuple     ):
									return self.encoder.get(lowerCamelCase__       ,self.encoder.get(self.unk_token     )     )







					def 						__lowerCAmelCase		(							self  :      str       ,lowerCamelCase__  :      Optional[int]     ):
									return self.decoder.get(lowerCamelCase__     )







					def 						__lowerCAmelCase		(							self  :      int       ,lowerCamelCase__  :      Optional[Any]     ):
									UpperCAmelCase__			 =				''.join(lowerCamelCase__     )
									UpperCAmelCase__			 =				bytearray([self.byte_decoder[c] for c in text]     ).decode('utf-8'       ,errors=self.errors     )
									return text







					def 						__lowerCAmelCase		(							self  :      Any       ,lowerCamelCase__  :      str       ,lowerCamelCase__  :      Optional[str] = None     ):
									if not os.path.isdir(lowerCamelCase__     ):
													logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''     )
													return
									UpperCAmelCase__			 =				os.path.join(
									    lowerCamelCase__       ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']     )
									UpperCAmelCase__			 =				os.path.join(
									    lowerCamelCase__       ,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']     )

									with open(lowerCamelCase__       ,'w'       ,encoding='utf-8'     ) as f:
													f.write(json.dumps(self.encoder       ,indent=2       ,sort_keys=lowerCamelCase__       ,ensure_ascii=lowerCamelCase__     ) + '\n'     )

									UpperCAmelCase__			 =				0
									with open(lowerCamelCase__       ,'w'       ,encoding='utf-8'     ) as writer:
													writer.write('#version: 0.2\n'     )
													for bpe_tokens, token_index in sorted(self.bpe_ranks.items()       ,key=lambda lowerCamelCase__     : kv[1]     ):
																	if index != token_index:
																					logger.warning(
																					    f'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'''
																					    ' Please check that the tokenizer is not corrupted!'     )
																					UpperCAmelCase__			 =				token_index
																	writer.write(' '.join(lowerCamelCase__     ) + '\n'     )
																	index += 1

									return vocab_file, merge_file







					def 						__lowerCAmelCase		(							self  :      Tuple       ,lowerCamelCase__  :      Optional[int]       ,lowerCamelCase__  :      Dict=False       ,**lowerCamelCase__  :      int     ):
									UpperCAmelCase__			 =				kwargs.pop('add_prefix_space'       ,self.add_prefix_space     )
									if is_split_into_words or add_prefix_space:
													UpperCAmelCase__			 =				' ' + text
									return (text, kwargs)







					def 						__lowerCAmelCase		(							self  :      Optional[int]       ,lowerCamelCase__  :      "Conversation"     ):
									UpperCAmelCase__			 =				[]
									for is_user, text in conversation.iter_texts():
													input_ids.extend(self.encode(lowerCamelCase__       ,add_special_tokens=lowerCamelCase__     ) + [self.eos_token_id]     )
									if len(lowerCamelCase__     ) > self.model_max_length:
													UpperCAmelCase__			 =				input_ids[-self.model_max_length :]
									return input_ids




"""simple docstring"""






import requests

try:
						from django.contrib.auth import authenticate, login
						from django.shortcuts import redirect, render
except ImportError:
						lowerCAmelCase__    :    Union[str, Any]			=      print



def     a_		(   lowerCamelCase			):
				# Enter your recaptcha secret key here
				UpperCAmelCase__			 =				'secretKey'
				UpperCAmelCase__			 =				'https://www.google.com/recaptcha/api/siteverify'

				# when method is not POST, direct user to login page
				if request.method != "POST":
								return render(lowerCamelCase   ,     'login.html'			)

				# from the frontend, get username, password, and client_key
				UpperCAmelCase__			 =				request.POST.get('username'			)
				UpperCAmelCase__			 =				request.POST.get('password'			)
				UpperCAmelCase__			 =				request.POST.get('g-recaptcha-response'			)

				# post recaptcha response to Google's recaptcha api
				UpperCAmelCase__			 =				requests.post(lowerCamelCase   ,     data={'secret': secret_key, 'response': client_key}			)
				# if the recaptcha api verified our keys
				if response.json().get('success'   ,     lowerCamelCase			):
								# authenticate the user
								UpperCAmelCase__			 =				authenticate(lowerCamelCase   ,     username=lowerCamelCase   ,     password=lowerCamelCase			)
								if user_in_database:
												login(lowerCamelCase   ,     lowerCamelCase			)
												return redirect('/your-webpage'			)
				return render(lowerCamelCase   ,     'login.html'			)



