o
    sh                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZmZmZmZmZmZmZ d dlZd dlZd dlZd dlmZmZ d dlmZmZ d dlmZ d dlmZ e e!Z"dee#ej$ef d	efd
dZ%ded	efddZ&dee#ej$ef d	efddZ'deded	efddZ(dee#ej$ef dee#ej$ef d	efddZ)deded	efddZ*dee#ej$ef dee#ej$ef d	efddZ+deded	efddZ,dee#ej$ef dee#ej$ef d	efddZ-dee#ej$ef dee#ej$ef fddZ.dee#ej$ef dee#ej$ef d	efdd Z/dee#ej$ef dee#ej$ef fd!d"Z0d#ed$ed	efd%d&Z1d'ed	efd(d)Z2ed'ej$d*ee3 d	ej$fd+d,Z4ed'ejd*ee3 d	ejfd-d,Z4d'eej$ejf d*ee3 d	eej$ejf fd.d,Z4d/d0d1d2d3d4e)fd5ee5 d6e6d7e3d8e3d9e3d:e3d;e3d<eeegef d	eeee7e3f   fd=d>Z8d1d2d3d4e)fd'ed8e3d9e3d:e3d;e3d<eeegef d	eeee7e3f   fd?d@Z9d	eeee5ee3e7f f   fdAdBZ:d4d3dCe)fdDedEed8e3d9e3d;e3d<eeegef d	eeee5ee3e7f f   fdFdGZ;dHe5dIe5d	dfdJdKZ<dLee5e
f dMed	ee5e
f fdNdOZ=d	e5fdPdQZ>dRe5d	efdSdTZ?	U	C	V	/dwd'eejej$f dWe7dXe3d7e3d6e6d	eee3  fdYdZZ@G d[d\ d\eZAeejBfd]d^ZC				/dxd_e5d`eee6e5f  daee5 dbee5 dce6d	e6fdddeZD		/dyd_e5dfe5d`eee6e5f  daee5 dbee5 dce6d	ee5 fdgdhZE		/dyd_e5die5d`eee6e5f  daee5 dbee5 dce6d	ee5 fdjdkZFdldm ZGd	edn fdodpZHd	e6fdqdrZId	e6fdsdtZJd	e6fdudvZKdS )z    N)contextmanager)	AnyCallableDictListLiteralOptionalTypeUnionoverload)hf_hub_downloadsnapshot_download)Tensordevice)tqdm)is_torch_npu_availableareturnc                 C   s   t | ts
t| } | S )z
    Converts the input `a` to a PyTorch tensor if it is not already a tensor.

    Args:
        a (Union[list, np.ndarray, Tensor]): The input array or tensor.

    Returns:
        Tensor: The converted tensor.
    )
isinstancer   torchtensorr    r   X/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/sentence_transformers/util.py_convert_to_tensor   s   


r   c                 C   s   |   dkr| d} | S )z
    If the tensor `a` is 1-dimensional, it is unsqueezed to add a batch dimension.

    Args:
        a (Tensor): The input tensor.

    Returns:
        Tensor: The tensor with a batch dimension.
       r   )dim	unsqueezer   r   r   r   _convert_to_batch%   s   

r   c                 C   s   t | } t| } | S )z
    Converts the input data to a tensor with a batch dimension.

    Args:
        a (Union[list, np.ndarray, Tensor]): The input data to be converted.

    Returns:
        Tensor: The converted tensor with a batch dimension.
    )r   r   r   r   r   r   _convert_to_batch_tensor4   s   
r   bc                 C   s
   t | |S )  
    Computes the cosine similarity between two tensors.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Matrix with res[i][j] = cos_sim(a[i], b[j])
    )cos_simr   r    r   r   r   pytorch_cos_simC   s   
r$   c                 C   s4   t | } t |}t| }t|}t||ddS )r!   r   r   )r   normalize_embeddingsr   mm	transpose)r   r    a_normb_normr   r   r   r"   Q   s
   r"   c                 C   s"   t | } t |}tt| t|S )a  
    Computes the pairwise cosine similarity cos_sim(a[i], b[i]).

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Vector with res[i] = cos_sim(a[i], b[i])
    )r   pairwise_dot_scorer%   r#   r   r   r   pairwise_cos_simd      r+   c                 C   s$   t | } t |}t| |ddS )a  
    Computes the dot-product dot_prod(a[i], b[j]) for all i and j.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Matrix with res[i][j] = dot_prod(a[i], b[j])
    r   r   )r   r   r&   r'   r#   r   r   r   	dot_scoreu   s   r-   c                 C   s    t | } t |}| | jddS )a  
    Computes the pairwise dot-product dot_prod(a[i], b[i]).

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Vector with res[i] = dot_prod(a[i], b[i])
    r   )r   sumr#   r   r   r   r*      s   r*   c                 C   "   t | } t |}tj| |dd S )a:  
    Computes the manhattan similarity (i.e., negative distance) between two tensors.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Matrix with res[i][j] = -manhattan_distance(a[i], b[j])
    g      ?pr   r   cdistr#   r   r   r   manhattan_sim   r,   r6   c                 C   s*   t | } t |}tjt| | dd S )a<  
    Computes the manhattan similarity (i.e., negative distance) between pairs of tensors.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Vector with res[i] = -manhattan_distance(a[i], b[i])
    r.   r/   )r   r   r0   absr#   r   r   r   pairwise_manhattan_sim   s   r8   c                 C   r1   )a:  
    Computes the euclidean similarity (i.e., negative distance) between two tensors.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Matrix with res[i][j] = -euclidean_distance(a[i], b[j])
    g       @r2   r4   r#   r   r   r   euclidean_sim   r,   r9   c                 C   s.   t | } t |}ttj| | d dd S )a:  
    Computes the euclidean distance (i.e., negative distance) between pairs of tensors.

    Args:
        a (Union[list, np.ndarray, Tensor]): The first tensor.
        b (Union[list, np.ndarray, Tensor]): The second tensor.

    Returns:
        Tensor: Vector with res[i] = -euclidean_distance(a[i], b[i])
       r.   r/   )r   r   sqrtr0   r#   r   r   r   pairwise_euclidean_sim   s   r<   xyc                 C   s   t | } t |}tj| ddd\}}tj|ddd\}}tj|d |d  ddd}|| ||  | }|| ||  | }tj|d |d  dddd }	tj|d |d  dddd }
||	|
  }||	|
  }tjtj||fdddd}t|S )aP  
    Computes the absolute normalized angle distance. See :class:`~sentence_transformers.losses.AnglELoss`
    or https://arxiv.org/abs/2309.12871v1 for more information.

    Args:
        x (Tensor): The first tensor.
        y (Tensor): The second tensor.

    Returns:
        Tensor: Vector with res[i] = angle_sim(a[i], b[i])
    r:   r   r/   T)r   keepdimg      ?)r   r   chunkr0   concatr7   )r=   r>   r   r    cdzreimdzdw
norm_angler   r   r   pairwise_angle_sim   s     
rJ   
embeddingsc                 C   s   t jjj| dddS )z
    Normalizes the embeddings matrix, so that each sentence embedding has unit length.

    Args:
        embeddings (Tensor): The input embeddings matrix.

    Returns:
        Tensor: The normalized embeddings matrix.
    r:   r   )r3   r   )r   nn
functional	normalize)rK   r   r   r   r%      s   
r%   truncate_dimc                 C      d S Nr   rK   rO   r   r   r   truncate_embeddings
     rS   c                 C   rP   rQ   r   rR   r   r   r   rS     rT   c                 C   s   | dd|f S )a  
    Truncates the embeddings matrix.

    Args:
        embeddings (Union[np.ndarray, torch.Tensor]): Embeddings to truncate.
        truncate_dim (Optional[int]): The dimension to truncate sentence embeddings to. `None` does no truncation.

    Example:
        >>> from sentence_transformers import SentenceTransformer
        >>> from sentence_transformers.util import truncate_embeddings
        >>> model = SentenceTransformer("tomaarsen/mpnet-base-nli-matryoshka")
        >>> embeddings = model.encode(["It's so nice outside!", "Today is a beautiful day.", "He drove to work earlier"])
        >>> embeddings.shape
        (3, 768)
        >>> model.similarity(embeddings, embeddings)
        tensor([[1.0000, 0.8100, 0.1426],
                [0.8100, 1.0000, 0.2121],
                [0.1426, 0.2121, 1.0000]])
        >>> truncated_embeddings = truncate_embeddings(embeddings, 128)
        >>> truncated_embeddings.shape
        >>> model.similarity(truncated_embeddings, truncated_embeddings)
        tensor([[1.0000, 0.8092, 0.1987],
                [0.8092, 1.0000, 0.2716],
                [0.1987, 0.2716, 1.0000]])

    Returns:
        Union[np.ndarray, torch.Tensor]: Truncated embeddings.
    .Nr   rR   r   r   r   rS     s   F    i  i i  d   	sentencesshow_progress_bar
batch_sizequery_chunk_sizecorpus_chunk_size	max_pairstop_kscore_functionc	           
      C   s&   | j |||dd}	t|	|||||dS )a  
    Given a list of sentences / texts, this function performs paraphrase mining. It compares all sentences against all
    other sentences and returns a list with the pairs that have the highest cosine similarity score.

    Args:
        model (SentenceTransformer): SentenceTransformer model for embedding computation
        sentences (List[str]): A list of strings (texts or sentences)
        show_progress_bar (bool, optional): Plotting of a progress bar. Defaults to False.
        batch_size (int, optional): Number of texts that are encoded simultaneously by the model. Defaults to 32.
        query_chunk_size (int, optional): Search for most similar pairs for #query_chunk_size at the same time. Decrease, to lower memory footprint (increases run-time). Defaults to 5000.
        corpus_chunk_size (int, optional): Compare a sentence simultaneously against #corpus_chunk_size other sentences. Decrease, to lower memory footprint (increases run-time). Defaults to 100000.
        max_pairs (int, optional): Maximal number of text pairs returned. Defaults to 500000.
        top_k (int, optional): For each sentence, we retrieve up to top_k other sentences. Defaults to 100.
        score_function (Callable[[Tensor, Tensor], Tensor], optional): Function for computing scores. By default, cosine similarity. Defaults to cos_sim.

    Returns:
        List[List[Union[float, int]]]: Returns a list of triplets with the format [score, id1, id2]
    T)rX   rY   convert_to_tensor)rZ   r[   r\   r]   r^   )encodeparaphrase_mining_embeddings)
modelrW   rX   rY   rZ   r[   r\   r]   r^   rK   r   r   r   paraphrase_mining4  s   rc   c              	   C   s  |d7 }t  }d}d}tdt| |D ]}	tdt| |D ]v}
|| |
|
|  | |	|	|  }tj|t|t|d dddd\}}|  }|  }tt|D ]=}t	|| D ]4\}}|
| }|	| }||kr|| | |kr|
|| | ||f |d7 }||kr| }|d }q^qVqqt }g }| s| \}}}t||g\}}||kr||f|vr|||f ||||g | rt|dd dd	}|S )
a  
    Given a list of sentences / texts, this function performs paraphrase mining. It compares all sentences against all
    other sentences and returns a list with the pairs that have the highest cosine similarity score.

    Args:
        embeddings (Tensor): A tensor with the embeddings
        query_chunk_size (int): Search for most similar pairs for #query_chunk_size at the same time. Decrease, to lower memory footprint (increases run-time).
        corpus_chunk_size (int): Compare a sentence simultaneously against #corpus_chunk_size other sentences. Decrease, to lower memory footprint (increases run-time).
        max_pairs (int): Maximal number of text pairs returned.
        top_k (int): For each sentence, we retrieve up to top_k other sentences
        score_function (Callable[[Tensor, Tensor], Tensor]): Function for computing scores. By default, cosine similarity.

    Returns:
        List[List[Union[float, int]]]: Returns a list of triplets with the format [score, id1, id2]
    r   r.   r   TFr   largestsortedc                 S      | d S )Nr   r   r=   r   r   r   <lambda>      z.paraphrase_mining_embeddings.<locals>.<lambda>keyreverse)queuePriorityQueuerangelenr   topkmincputolist	enumerateputgetsetemptyrf   addappend)rK   rZ   r[   r\   r]   r^   pairs	min_score	num_addedcorpus_start_idxquery_start_idxscoresscores_top_k_valuesscores_top_k_idx	query_itr	top_k_idx
corpus_itrijentryadded_pairs
pairs_listscoresorted_isorted_jr   r   r   ra   a  sP   
	ra   c                  O   s   t | i |S )z8This function is deprecated. Use semantic_search instead)semantic_search)argskwargsr   r   r   information_retrieval  s   r   
   query_embeddingscorpus_embeddingsc              	   C   s  t | tjtjfrt| } n
t | trt| } t| j	dkr%| 
d} t |tjtjfr4t|}n
t |tr>t|}|j| jkrJ| |j} dd tt| D }tdt| |D ]y}tdt||D ]n}|| |||  ||||  }	tj|	t|t|	d dddd\}
}|
  }
|  }tt|	D ]5}t|| |
| D ])\}}|| }|| }t|| |k rt|| ||f qt|| ||f qqqgq]tt|D ]+}tt|| D ]}|| | \}}||d|| |< qt|| d	d
 dd||< q|S )a  
    This function performs a cosine similarity search between a list of query embeddings  and a list of corpus embeddings.
    It can be used for Information Retrieval / Semantic Search for corpora up to about 1 Million entries.

    Args:
        query_embeddings (Tensor): A 2 dimensional tensor with the query embeddings.
        corpus_embeddings (Tensor): A 2 dimensional tensor with the corpus embeddings.
        query_chunk_size (int, optional): Process 100 queries simultaneously. Increasing that value increases the speed, but requires more memory. Defaults to 100.
        corpus_chunk_size (int, optional): Scans the corpus 100k entries at a time. Increasing that value increases the speed, but requires more memory. Defaults to 500000.
        top_k (int, optional): Retrieve top k matching entries. Defaults to 10.
        score_function (Callable[[Tensor, Tensor], Tensor], optional): Function for computing scores. By default, cosine similarity.

    Returns:
        List[List[Dict[str, Union[int, float]]]]: A list with one entry for each query. Each entry is a list of dictionaries with the keys 'corpus_id' and 'score', sorted by decreasing cosine similarity scores.
    r   r   c                 S   s   g | ]}g qS r   r   ).0_r   r   r   
<listcomp>  s    z#semantic_search.<locals>.<listcomp>TFrd   )	corpus_idr   c                 S   rg   )Nr   r   rh   r   r   r   ri     rj   z!semantic_search.<locals>.<lambda>rk   )r   npndarraygenericr   
from_numpyliststackrq   shaper   r   torp   rr   rs   rt   ru   zipheapqheappushheappushpoprf   )r   r   rZ   r[   r]   r^   queries_result_listr   r   
cos_scorescos_scores_top_k_valuescos_scores_top_k_idxr   sub_corpus_idr   r   query_iddoc_itrr   r   r   r     sT   





r   urlpathc           	      C   s  t j|dkrt jt j|dd tj| dd}|jdkr1td| |jt	j
d |  dS |d	 }t|d
6}|jd}|durIt|nd}td|dd}|jddD ]}|rh|t| || qXW d   n1 ssw   Y  t || |  dS )a  
    Downloads a URL to a given path on disk.

    Args:
        url (str): The URL to download.
        path (str): The path to save the downloaded file.

    Raises:
        requests.HTTPError: If the HTTP request returns a non-200 status code.

    Returns:
        None
     T)exist_ok)stream   z1Exception when trying to download {}. Response {})fileN_partwbzContent-LengthB)unittotal
unit_scale   )
chunk_size)osr   dirnamemakedirsrequestsrx   status_codeprintformatsysstderrraise_for_statusopenheadersintr   iter_contentupdaterq   writerenameclose)	r   r   reqdownload_filepathfile_binarycontent_lengthr   progressr@   r   r   r   http_get  s*   

	r   batchtarget_devicec                 C   s.   | D ]}t | | tr| | || |< q| S )au  
    Send a PyTorch batch (i.e., a dictionary of string keys to Tensors) to a device (e.g. "cpu", "cuda", "mps").

    Args:
        batch (Dict[str, Tensor]): The batch to send to the device.
        target_device (torch.device): The target device (e.g. "cpu", "cuda", "mps").

    Returns:
        Dict[str, Tensor]: The batch with tensors sent to the target device.
    )r   r   r   )r   r   rl   r   r   r   batch_to_device$  s
   r   c                 C   s4   | j j}|du s|tj jkr| j jS |d | j j S )a  
    Gives a full name (package_name.class_name) for a class / object in Python. Will
    be used to load the correct classes from JSON files

    Args:
        o: The object for which to get the full name.

    Returns:
        str: The full name of the object.

    Example:
        >>> from sentence_transformers.losses import MultipleNegativesRankingLoss
        >>> from sentence_transformers import SentenceTransformer
        >>> from sentence_transformers.util import fullname
        >>> model = SentenceTransformer('all-MiniLM-L6-v2')
        >>> loss = MultipleNegativesRankingLoss(model)
        >>> fullname(loss)
        'sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss'
    N.)	__class__
__module__str__name__)omoduler   r   r   fullname5  s   r   dotted_pathc                 C   s   z
|  dd\}}W n ty   d|  }t|w zt| }W n ty/   t|}Y nw zt||W S  tyG   d||f }t|w )a:  
    Import a dotted module path and return the attribute/class designated by the
    last name in the path. Raise ImportError if the import failed.

    Args:
        dotted_path (str): The dotted module path.

    Returns:
        Any: The attribute/class designated by the last name in the path.

    Raises:
        ImportError: If the import failed.

    Example:
        >>> import_from_string('sentence_transformers.losses.MultipleNegativesRankingLoss')
        <class 'sentence_transformers.losses.MultipleNegativesRankingLoss.MultipleNegativesRankingLoss'>
    r   r   z"%s doesn't look like a module pathz2Module "%s" does not define a "%s" attribute/class)rsplit
ValueErrorImportError	importlibimport_module	ExceptiongetattrAttributeError)r   module_path
class_namemsgr   r   r   r   import_from_stringQ  s"   r         ?r   	thresholdmin_community_sizec                 C   sX  t | tjst| } tj|| jd}t| } g }t|t| }ttd| dt| }t	t
dt| |d| dD ]}| |||  | j }| jjdv r||k}	|	d}
|
|k}| s_q:|
| }
|| }|
 }|j|d	d
\}}t|
|D ]\}}||d|   qyq:|j|d	d
\}}t
t|D ]L}|| d |kr|| j|d	d
\}}|d |kr|t| k rtd| t| }|| j|d	d
\}}|d |kr|t| k s||||k   qq:t|dd d	d}g }t }t|D ]'\}}g }|D ]}||vr|| qt||kr || || qt|dd d	d}|S )a  
    Function for Fast Community Detection.

    Finds in the embeddings all communities, i.e. embeddings that are close (closer than threshold).
    Returns only communities that are larger than min_community_size. The communities are returned
    in decreasing order. The first element in each list is the central point in the community.

    Args:
        embeddings (torch.Tensor or numpy.ndarray): The input embeddings.
        threshold (float): The threshold for determining if two embeddings are close. Defaults to 0.75.
        min_community_size (int): The minimum size of a community to be considered. Defaults to 10.
        batch_size (int): The batch size for computing cosine similarity scores. Defaults to 1024.
        show_progress_bar (bool): Whether to show a progress bar during computation. Defaults to False.

    Returns:
        List[List[int]]: A list of communities, where each community is represented as a list of indices.
    )r   r:   2   r   zFinding clusters)descdisable)cudanpur   T)kre   Nr.   c                 S      t | S rQ   rq   rh   r   r   r   ri     rj   z%community_detection.<locals>.<lambda>rk   c                 S   r   rQ   r   rh   r   r   r   ri     rj   )r   r   r   r   r   r%   rs   rq   maxr   rp   Ttyper0   anyrr   r   r|   ru   rf   ry   rv   r   )rK   r   r   rY   rX   extracted_communitiessort_max_size	start_idxr   threshold_maskrow_wise_countlarge_enough_maskr   r   top_k_indicescountindicestop_k_valuesr   top_val_largetop_idx_largeunique_communitiesextracted_ids
cluster_id	communitynon_overlapped_communityidxr   r   r   community_detectionu  sd   






r  c                       s6   e Zd ZdZ fddZdeddf fddZ  ZS )	disabled_tqdmz
    Class to override `disable` argument in case progress bars are globally disabled.

    Taken from https://github.com/tqdm/tqdm/issues/619#issuecomment-619639324.
    c                    s   d|d< t  j|i | d S )NTr   )super__init__)selfr   r   r   r   r   r    s   zdisabled_tqdm.__init__attrr   Nc                    s2   z	t  | W dS  ty   |dkr Y dS w )zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)r  __delattr__r   )r  r  r  r   r   r    s   zdisabled_tqdm.__delattr__)r   r   __qualname____doc__r  r   r  __classcell__r   r   r  r   r    s    r  c              	   c   s:    t jjj}t |  zdV  W t | dS t | w )z
    A context manager that will prevent any logging messages
    triggered during the body from being processed.

    Args:
        highest_level: the maximum logging level allowed.
    N)loggingrootmanagerr   )highest_levelprevious_levelr   r   r   disable_logging  s   


r  model_name_or_pathtokencache_folderrevisionlocal_files_onlyc              	   C   s   t t| d||||dS )a}  
    Checks if the given model name or path corresponds to a SentenceTransformer model.

    Args:
        model_name_or_path (str): The name or path of the model.
        token (Optional[Union[bool, str]]): The token to be used for authentication. Defaults to None.
        cache_folder (Optional[str]): The folder to cache the model files. Defaults to None.
        revision (Optional[str]): The revision of the model. Defaults to None.
        local_files_only (bool): Whether to only use local files for the model. Defaults to False.

    Returns:
        bool: True if the model is a SentenceTransformer model, False otherwise.
    zmodules.json)r#  r$  )boolload_file_path)r   r!  r"  r#  r$  r   r   r   is_sentence_transformer_model  s   r'  filenamec              	   C   sL   t j| |}t j|r|S zt| ||d|||dW S  ty%   Y dS w )a  
    Loads a file from a local or remote location.

    Args:
        model_name_or_path (str): The model name or path.
        filename (str): The name of the file to load.
        token (Optional[Union[bool, str]]): The token to access the remote file (if applicable).
        cache_folder (Optional[str]): The folder to cache the downloaded file (if applicable).
        revision (Optional[str], optional): The revision of the file (if applicable). Defaults to None.
        local_files_only (bool, optional): Whether to only consider local files. Defaults to False.

    Returns:
        Optional[str]: The path to the loaded file, or None if the file could not be found or loaded.
    sentence-transformers)r(  r#  library_namer!  	cache_dirr$  N)r   r   joinexistsr   r   )r   r(  r!  r"  r#  r$  	file_pathr   r   r   r&  (  s    	r&  	directoryc           	   	   C   s   t j| |}t j|r|S | || dd|||td}z	tdi |}W n ty:   d|d< tdi |}Y nw t j||S )af  
    Loads the directory path for a given model name or path.

    Args:
        model_name_or_path (str): The name or path of the model.
        directory (str): The directory to load.
        token (Optional[Union[bool, str]]): The token for authentication.
        cache_folder (Optional[str]): The folder to cache the downloaded files.
        revision (Optional[str], optional): The revision of the model. Defaults to None.
        local_files_only (bool, optional): Whether to only use local files. Defaults to False.

    Returns:
        Optional[str]: The directory path if it exists, otherwise None.
    z/**r)  )repo_idr#  allow_patternsr*  r!  r+  r$  
tqdm_classTr$  Nr   )r   r   r,  r-  r  r   r   )	r   r/  r!  r"  r#  r$  dir_pathdownload_kwargs	repo_pathr   r   r   load_dir_pathR  s&   r6  c                    s   t   fdd}|S )Nc                    sn   | dd }|rd|vrtd ||d< t|dkr,g |d d d |dd  R } | g|R i |S )N	repo_namer0  zfProviding a `repo_name` keyword argument to `save_to_hub` is deprecated, please use `repo_id` instead.r:   )poploggerwarningrq   )r  r   r   r7  funcr   r   wrapper  s   "z+save_to_hub_args_decorator.<locals>.wrapper)	functoolswraps)r<  r=  r   r;  r   save_to_hub_args_decorator  s   r@  )mpsr   r   hpurt   c                  C   sZ   t j rdS t jj rdS t rdS tjddur+ddl	m   m
}  |  r+dS dS )	aH  
    Returns the name of the device where this module is running on.

    It's a simple implementation that doesn't cover cases when more powerful GPUs are available and
    not a primary device ('cuda:0') or MPS device is available, but not configured properly.

    Returns:
        str: Device name, like 'cuda' or 'cpu'
    r   rA  r   habana_frameworksNr   rB  rt   )r   r   is_availablebackendsrA  r   r   util	find_spechabana_frameworks.torch.hpurB  )hthpur   r   r   get_device_name  s   

rJ  c                   C      t jdduS )z>
    Returns True if the accelerate library is available.
    
accelerateNr   rF  rG  r   r   r   r   is_accelerate_available     rN  c                   C   rK  )z<
    Returns True if the datasets library is available.
    datasetsNrM  r   r   r   r   is_datasets_available  rO  rQ  c                   C   s   t  ot S )zd
    Returns True if we have the required dependencies for training Sentence Transformer models
    )rN  rQ  r   r   r   r   is_training_available  s   rR  )r   r   r   F)NNNF)NF)Lr>  r   r   r  r   rn   r   
contextlibr   typingr   r   r   r   r   r   r	   r
   r   numpyr   r   r   huggingface_hubr   r   r   r   tqdm.autonotebookr   transformersr   	getLoggerr   r9  r   r   r   r   r   r$   r"   r+   r-   r*   r6   r8   r9   r<   rJ   r%   r   rS   r   r%  floatrc   ra   r   r   r   r   r   r   r  r  CRITICALr  r'  r&  r6  r@  rJ  rN  rQ  rR  r   r   r   r   <module>   sx   ,
...*.*"  
%	

/
&I
P&%&

k
%
/
/