o
    o i                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlZd dlm	Z	m
Z
 d dlmZmZmZmZ daG dd de
Zedd	d
 Zddee fddZdd Zdd Zejdd ZdS )    N)CallableOptional)CacheDisk)ENOVALUNKNOWNargs_to_key	full_nameTc                       sR   e Zd Zd fdd	Z fddZ fddZef fdd		Z fd
dZ  Z	S )CloudpickleDisk   c                    s   || _ t j|fi | d S N)compress_levelsuper__init__)self	directoryr   kwargs	__class__ \/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/outlines/caching.pyr      s   zCloudpickleDisk.__init__c                    s   t |}t |S r   )cloudpickledumpsr   put)r   keydatar   r   r   r      s   
zCloudpickleDisk.putc                    s   t  ||}t|S r   )r   getr   loads)r   r   rawr   r   r   r   r      s   
zCloudpickleDisk.getc                    s    |st |}t j|||dS )N)r   )r   r   r   store)r   valuereadr   r   r   r   r      s   
zCloudpickleDisk.storec                    s$   t  ||||}|st|}|S r   )r   fetchr   r   )r   modefilenamer    r!   r   r   r   r   r"       s   
zCloudpickleDisk.fetch)r   )
__name__
__module____qualname__r   r   r   r   r   r"   __classcell__r   r   r   r   r
      s    r
   r   c                  C   s^   ddl m}  tjd}tjd| d}t|ddtd}| |dkr)|	  | |d< |S )	a  Get the context object that contains previously-computed return values.

    The cache is used to avoid unnecessary computations and API calls, which can
    be long and expensive for large models.

    The cache directory defaults to `HOMEDIR/.cache/outlines`, but this choice
    can be overridden by the user by setting the value of the `OUTLINES_CACHE_DIR`
    environment variable.

    r   )__version__~ZOUTLINES_CACHE_DIRz/.cache/outlinesnone)Zeviction_policyZ
cull_limitZdiskr)   )
Zoutlines._versionr)   ospath
expanduserenvironr   r   r
   clear)Zoutlines_versionZhome_dir	cache_dirmemoryr   r   r   	get_cache'   s   r3   Fr   expirec                    s   dt f fdd}|S )a4  Caching decorator for memoizing function calls.

    The cache key is created based on the values returned by the key_function callable
    if provided or based on the arguments of the decorated function directly otherwise

    This is based on `diskcache`'s `memoize`.

    Parameters
    ----------
    expire
        Seconds until arguments expire.
    typed
        Cache different types separately.
    ignore
        Positional or keyword arguments to ignore.

    Returns
    -------
        A decorator function that can be applied to other functions.
    cached_functionc                    sb   t  }tf trfddnfdd fdd}|_|__S )Nc                     sp   t s | i |I d H S j| i |}jj|tdd}|tu r6 | i |I d H }jj||dd |S NT)defaultretry)r8   _caching_enabled__cache_key__
__memory__r   r   setargsr   	cache_keyresultr5   r4   wrapperr   r   rC   c   s   z)cache.<locals>.decorator.<locals>.wrapperc                     sb   t s	 | i |S j| i |}jj|tdd}|tu r/ | i |}jj||dd |S r6   r9   r>   rB   r   r   rC   r   s   c                     s   t  | |S )z,Make key for cache given function arguments.)r   )r?   r   )baseignoretypedr   r   r;      s   z/cache.<locals>.decorator.<locals>.__cache_key__)r3   r	   asyncioiscoroutinefunctionr;   r<   __wrapped__)r5   r2   r;   r4   rE   rF   )rD   r5   rC   r   	decorator\   s   

zcache.<locals>.decorator)r   )r4   rF   rE   rK   r   rJ   r   cacheF   s   -rL   c                   C   s   da dS )au  Disable the cache for this session.

    Generative models output different results each time they are called when
    sampling. This can be a desirable property for some workflows, in which case
    one can call `outlines.call.disable` to disable the cache for the session.

    This function does not delete the cache, call `outlines.cache.clear`
    instead. It also does not overwrite the cache with the values returned
    during the session.

    Example
    -------

    `outlines.cache.disable` should be called right after importing outlines:

    >>> import outlines.caching as cache
    >>> cache.disable_cache()

    FNr:   r   r   r   r   disable_cache   s   rN   c                  C   s   t  } |   dS )zErase the cache completely.N)r3   r0   )r2   r   r   r   clear_cache   s   rO   c                  c   s"    t } da zd V  W | a d S | a w )NFrM   )Zoriginal_stater   r   r   cache_disabled   s   rP   )NFr   )rG   
contextlib	functoolsr,   typingr   r   r   Z	diskcacher   r   Zdiskcache.corer   r   r   r	   r:   r
   	lru_cacher3   floatrL   rN   rO   contextmanagerrP   r   r   r   r   <module>   s"    
F