
    rh3                       d dl m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 d dl	m
Z
mZmZ d dlZd dlmZ e
r
d dlmZmZmZ  ej&                  e      ZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)    )annotationsN)fnmatch)Path)TYPE_CHECKINGAnyCallable)list_repo_filesCrossEncoderSentenceTransformerSparseEncoderc                     d fd}|S )a  
    Wraps the save_pretrained method of a model to save to a subfolder.

    Args:
        _save_pretrained_fn: The original save_pretrained function
        subfolder: The subfolder to save to

    Returns:
        A wrapped function that saves to the specified subfolder
    c                t    t        j                  t        |       z  d        t        |       z  fi |S )NT)exist_ok)osmakedirsr   )save_directorykwargs_save_pretrained_fn	subfolders     v/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/sentence_transformers/backend/utils.pywrapperz)_save_pretrained_wrapper.<locals>.wrapper    s5    
D(94tD"4#7)#CNvNN    )r   z
str | PathreturnNone )r   r   r   s   `` r   _save_pretrained_wrapperr      s    O Nr   c                &   |j                  dd      }|r||fS |j                         }|j                  d|      }|j                  dd      }	|	rt        |	|      j	                         nt        |      j	                         }
|	rt        |	||      j	                         nt        ||      j	                         }|	r|	 d| nd| }|r<| j                  |      D cg c]!  }|j                  |       j	                         # }}nYt        | j	                         d|j                  dd      |j                  d	d      
      }|D cg c]  }t        ||      s| }}|
|v }|sn||v }|rht        |      dkD  r4d|vr0t        j                  d| d| j	                         d| d|d	       |	rt        |	|      j	                         n||d<   ||d<   || }t        |      j                  }t        |      dkD  r8|d   |d<   t        |j                  dd      g|dd  j	                         |d<   |rQt        j                  d|d| j	                         d| d       |r"t        j                  d| d| d|d    d       ||fS c c}w c c}w )a(  
    Determines whether the model should be exported to the backend, or if it can be loaded directly.
    Also update the `file_name` and `subfolder` model_kwargs if necessary.

    These are the cases:

    1. If export is set in model_kwargs, just return export
    2. If `<subfolder>/<file_name>` exists; set export to False
    3. If `<backend>/<file_name>` exists; set export to False and set subfolder to the backend (e.g. "onnx")
    4. If `<file_name>` contains a folder, add those folders to the subfolder and set the file_name to the last part

    We will warn if:

    1. The expected file does not exist in the model directory given the optional file_name and subfolder.
        If there are valid files for this backend, but they're don't align with file_name, then we give a useful warning.
    2. Multiple files are found in the model directory that match the target file name and the user did not
        specify the desired file name via `model_kwargs={"file_name": "<file_name>"}`

    Args:
        load_path: The model repository or directory, as a Path instance
        is_local: Whether the model is local or remote, i.e. whether load_path is a local directory
        model_kwargs: The model_kwargs dictionary. Notable keys are "export", "file_name", and "subfolder"
        target_file_name: The expected file name in the model directory, e.g. "model.onnx" or "openvino_model.xml"
        target_file_glob: The glob pattern to match the target file name, e.g. "*.onnx" or "openvino*.xml"
        backend_name: The human-readable name of the backend for use in warnings, e.g. "ONNX" or "OpenVINO"

    Returns:
        Tuple[bool, dict[str, Any]]: A tuple of the export boolean and the updated model_kwargs dictionary.
            Notable keys in model_kwargs are "export", "file_name", and "subfolder".
    exportN	file_namer   z/**/z**/modelrevisiontoken)	repo_typer"   r#      z	Multiple z files found in z: z, defaulting to zW. Please specify the desired file name via `model_kwargs={"file_name": "<file_name>"}`. zNo z
 found in z. Exporting the model to .z#If you intended to load one of the  zN files, please specify the desired file name via `model_kwargs={"file_name": "r   z"}`.)poplowergetr   as_posixglobrelative_tor	   r   lenloggerwarningparts)	load_pathis_localmodel_kwargstarget_file_nametarget_file_globbackend_namer   backendr    r   primary_full_pathsecondary_full_pathglob_patternpathmodel_file_names	all_filesfnamemodel_foundfile_name_partss                      r   backend_should_exportrD   '   s   L h-F|##  "G  .>?I  d3IAJY	2;;=PTU^P_PhPhPj:CY+446gW`IaIjIjIl  <Ei[%5$67CP`OaJbL OX~~^jOkltD,,Y7@@Bll# !%%j$7""7D1	
	 09YeGE<<XEYY
 $'77K)-==#$q([-L~-=i>P>P>R=UUWXhWiiy  {N  zQ Qn o PYY(@(I(I(K^eL%(1L%~  9o++O
?a$3B$7[!$()9)9+r)J$b_]`^`Ma$b$k$k$m[!YMI4F4F4H3KKdeqdrrstuNN56F5Gq WZZjklZmYnnsu
 <U m Zs   &J	JJc                d    d| d}|r
|d| dz  }n	|d| dz  }t         j                  |       y)z
    Warns the user to save the model if they just exported it.

    Args:
        model_name_or_path: The model name or path
        is_local: Whether the model is local
        backend_name: The name of the backend (ONNX or OpenVINO)
    zSaving the exported zA model is heavily recommended to avoid having to export it again.z# Do so with `model.save_pretrained(z)`.z Do so with `model.push_to_hub(z, create_pr=True)`.N)r1   r2   )model_name_or_pathr5   r9   to_logs       r   backend_warn_to_saverH      sO     $L>1rsF78J7MSQQ34F3II\]]
NN6r   c	                   ddl m}	m}
m} |dk(  rd| d}n|dk(  rd| d}t	        j
                         5 } | |       |dk(  ret        |      |z  }t        j                  |d	z  |z         t        j                  |d
z  ||z  j                  d             |j                         }|dk(  rZt        |      |z  }|j                  dd       t        |      z  }||z  }t        j                  ||       |j                         }|rd}|rt        |      j                  dd      j                  dd      j                  dd      }t        ||      rd| d| d| d| d| d d}nE|t        ||
      rd| d| d| d| d| d d}n!t        ||	      rd| d| d| d| d| d d}t        j                   |||dd | d!||"       nt        |      |z  }|j                  dd       t        |      z  }||z  }t        j"                  ||       |dk(  rPt        |      |z  j                  d      }t        |      |z  j                  d      }t        j"                  ||       d d d        y # 1 sw Y   y xY w)#Nr   r
   onnxmodel_z.onnxopenvinoopenvino_model_z.xmlzopenvino_model.xmlzopenvino_model.binz.binT)parentsr   r'   (z(
	z, z,
	)z
)zGHello!

*This pull request has been automatically generated from the [`zT`](https://sbert.net/docs/package_reference/util.html#sentence_transformers.backend.zI) function from the Sentence Transformers library.*

## Config
```python
a  
```

## Tip:
Consider testing this pull request before merging by loading the model from this PR with the `revision` argument:
```python
from sentence_transformers import SparseEncoder

# TODO: Fill in the PR number
pr_number = 2
model = SparseEncoder(
    "z5",
    revision=f"refs/pr/{pr_number}",
    backend="z#",
    model_kwargs={"file_name": "a  "},
)

# Verify that everything works as expected
embeddings = model.encode(["The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium."])
print(embeddings.shape)

similarities = model.similarity(embeddings, embeddings)
print(similarities)
```
a  
```

## Tip:
Consider testing this pull request before merging by loading the model from this PR with the `revision` argument:
```python
from sentence_transformers import SentenceTransformer

# TODO: Fill in the PR number
pr_number = 2
model = SentenceTransformer(
    "a  
```

## Tip:
Consider testing this pull request before merging by loading the model from this PR with the `revision` argument:
```python
from sentence_transformers import CrossEncoder

# TODO: Fill in the PR number
pr_number = 2
model = CrossEncoder(
    "a
  "},
)

# Verify that everything works as expected
query = "Which planet is known as the Red Planet?"
passages = [
	"Venus is often called Earth's twin because of its similar size and proximity.",
	"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
	"Jupiter, the largest planet in our solar system, has a prominent red spot.",
	"Saturn, famous for its rings, is sometimes mistaken for the Red Planet."
]

scores = model.predict([(query, passage) for passage in passages])
print(scores)
```
r!   zAdd exported z model )folder_pathpath_in_reporepo_idr$   commit_messagecommit_description	create_pr)sentence_transformersr   r   r   tempfileTemporaryDirectoryr   shutilmovewith_suffixr-   mkdirreprreplace
isinstancehuggingface_hubupload_foldercopy)export_functionexport_function_nameconfigrF   push_to_hubrV   file_suffixr:   r!   r   r   r   r    save_dirdst_dirsourcedestinationrU   opt_config_string
bin_sourcebin_destinations                        r   save_or_push_to_hub_modelrp      sx    WV&[M/		J	%k]$7			$	$	& Y9(! j H~/HKK#77I9MNKK#77(Y:N9[9[\b9cd((*H f8nw.GMM$M6(^i/F!I-KKK,'')H!#$(L$8$8g$F$N$NtU\$]$e$efikp$q!e]3.@ AU?U  Vj  k  j@ @     Y ""+ 
-+*&@ ]j8K&L.@ AU?U  Vj  k  j@ @     Y ""+ 
-+*&@  |4.@ AU?U  Vj  k  j@ @     Y ""+ -+$*&L ))$$*!!.wiwymL#5# -.8GMM$M6(^i/F!I-KKK, *$"8ny8EEfM
#'=9#<"I"I&"QJ8sY9 Y9 Y9s   H<I;;J)r   r   r   strr   zCallable[..., None])r4   r   r5   boolr6   zdict[str, Any]r7   rq   r8   rq   r9   rq   r   ztuple[bool, dict[str, Any]])rF   rq   r5   rr   r9   rq   r   r   )FFNrJ   N)rd   r   re   rq   rF   rq   rg   rr   rV   rr   rh   z
str | Noner:   rq   r!   z9SentenceTransformer | SparseEncoder | CrossEncoder | None)
__future__r   loggingr   rZ   rX   r   pathlibr   typingr   r   r   ra   r	   rW   r   r   r   	getLogger__name__r1   r   rD   rH   rp   r   r   r   <module>ry      s   "  	     / /  +VV			8	$&_ _ _  !_  	_ 
 _  _  !_ D, "GKk9k9k9 	k9
 k9 k9 k9 k9 Ek9r   