
import torch

from transformers import AutoModel

class        _UpperCamelCase				(					torch.nn.Module  ):

			def __init__(       self :       Optional[Any]						,	snake_case_ :       int="sayef/fsner-bert-base-uncased"      ):
							super(snake_case_						,	self      ).__init__()

							UpperCamelCase_:							Optional[int]	=						AutoModel.from_pretrained(snake_case_						,	return_dict=snake_case_      )
							UpperCamelCase_:							Optional[int]	=						torch.nn.CosineSimilarity(3						,	1e-08      )
							UpperCamelCase_:							int	=						torch.nn.Softmax(dim=1      )

			def       lowerCAmelCase__	(       self :       Union[str, Any]						,	**snake_case_ :       List[Any]      ):
							return self.bert(**snake_case_      ).last_hidden_state

			def       lowerCAmelCase__	(       self :       Tuple						,	snake_case_ :       List[str]      ):
							return token_embeddings.sum(2						,	keepdim=snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       Tuple						,	snake_case_ :       Tuple						,	snake_case_ :       Optional[Any]=1      ):
							return self.softmax(T * self.cos(snake_case_						,	snake_case_      )      )




			def       lowerCAmelCase__	(       self :       str						,	snake_case_ :       Optional[Any]						,	snake_case_ :       Optional[int]      ):
							UpperCamelCase_:							Any	=						W_supports["""sizes"""].tolist()
							UpperCamelCase_:							Optional[Any]	=						W_supports["""start_token_id"""].item()
							UpperCamelCase_:							Optional[int]	=						W_supports["""end_token_id"""].item()

							del W_supports["sizes"]
							del W_supports["start_token_id"]
							del W_supports["end_token_id"]

							UpperCamelCase_:							Optional[Any]	=						self.BERT(**snake_case_      )
							UpperCamelCase_:							Any	=						self.BERT(**snake_case_      )

							UpperCamelCase_:							List[Any]	=						None
							UpperCamelCase_:							List[Any]	=						None

							UpperCamelCase_:							List[Any]	=						W_supports["""input_ids"""] == start_token_id
							UpperCamelCase_:							List[Any]	=						W_supports["""input_ids"""] == end_token_id

							for i, size in enumerate(snake_case_      ):
											if i == 0:
															UpperCamelCase_:							Dict	=						0
											else:
															UpperCamelCase_:							Tuple	=						support_sizes[i - 1]

											UpperCamelCase_:							Optional[Any]	=						S[s : s + size][start_token_masks[s : s + size]]
											UpperCamelCase_:							Dict	=						S[s : s + size][end_token_masks[s : s + size]]

											UpperCamelCase_:							Optional[int]	=						torch.matmul(q[i]						,	s_start.T      ).sum(1      ).softmax(0      )
											UpperCamelCase_:							Optional[int]	=						torch.matmul(q[i]						,	s_end.T      ).sum(1      ).softmax(0      )

											if p_starts is not None:
															UpperCamelCase_:							List[str]	=						torch.vstack((p_starts, p_start)      )
															UpperCamelCase_:							Any	=						torch.vstack((p_ends, p_end)      )
											else:
															UpperCamelCase_:							Optional[Any]	=						p_start
															UpperCamelCase_:							Any	=						p_end

							return p_starts, p_ends








# Copyright 2023 The HuggingFace 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.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase_						: List[str]         =  {
    """configuration_mgp_str""": ["""MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MgpstrConfig"""],
    """processing_mgp_str""": ["""MgpstrProcessor"""],
    """tokenization_mgp_str""": ["""MgpstrTokenizer"""],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowerCamelCase_						: Optional[Any]         =  [
							    """MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """MgpstrModel""",
							    """MgpstrPreTrainedModel""",
							    """MgpstrForSceneTextRecognition""",
							]

if TYPE_CHECKING:
							from .configuration_mgp_str import MGP_STR_PRETRAINED_CONFIG_ARCHIVE_MAP, MgpstrConfig
							from .processing_mgp_str import MgpstrProcessor
							from .tokenization_mgp_str import MgpstrTokenizer

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mgp_str import (
														    MGP_STR_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MgpstrForSceneTextRecognition,
														    MgpstrModel,
														    MgpstrPreTrainedModel,
														)
else:
							import sys

							lowerCamelCase_						: int         =  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






