o
    8wi                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ e	r)dgZdee defd	d
Zdee deeef fddZdee dee defddZdededefddZdee dee dedefddZdee ddddfddZdS )    N)Optional)module_available)_IS_WINDOWS)_PATH_determine_model_foldertextreturnc                 C   s&   t | tsdS d}tt||  S )a*  Check if a string equals 'registry' or starts with 'registry:'.

    Args:
        text: The string to check

    >>> _is_registry("registry")
    True
    >>> _is_registry("REGISTRY:model-name")
    True
    >>> _is_registry("something_registry")
    False
    >>> _is_registry("")
    False

    Fz^registry(:.*|$))
isinstancestrboolrematchlower)r   pattern r   g/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pytorch_lightning/utilities/model_registry.py_is_registry   s   
r   	ckpt_pathc                 C   s   | rt | std|  t| d}d\}}t|dkr*|d  dkr*|d }t|dkr@|d  dkr@|d }||fS t|dkrR|d  dkrR|d }||fS )	a  Parse the model version from a registry path.

    Args:
        ckpt_path: The checkpoint path

    Returns:
        string name and version of the model

    >>> _parse_registry_model_version("registry:model-name:version:1.0")
    ('model-name', '1.0')
    >>> _parse_registry_model_version("registry:model-name")
    ('model-name', '')
    >>> _parse_registry_model_version("registry:VERSION:v2")
    ('', 'v2')

    zInvalid registry path: :) r         version      )r   
ValueErrorr
   splitlenr   )r   parts
model_namer   r   r   r   _parse_registry_model_version5   s   r    default_model_registryc                 C   sP   t | \}}|s|r|}|std|  d|}||r#d| 7 }|S d7 }|S )a  Determine the model name from the checkpoint path.

    Args:
        ckpt_path: The checkpoint path
        default_model_registry: The default model registry

    Returns:
        string name of the model with optional version

    >>> _determine_model_name("registry:model-name:version:1.0", "default-model")
    'model-name:1.0'
    >>> _determine_model_name("registry:model-name", "default-model")
    'model-name'
    >>> _determine_model_name("registry:version:v2", "default-model")
    'default-model:v2'

    Invalid model registry: ''r   r   )r    r   )r   r!   r   model_versionmodel_registryr   r   r   _determine_model_nameY   s   r&   r   default_root_dirc                 C   s>   | s
t d|  d| dd} | dd} tj|| }|S )a  Determine the local model folder based on the model registry.

    Args:
        model_name: The model name
        default_root_dir: The default root directory

    Returns:
        string path to the local model folder

    >>> _determine_model_folder("model-name", "/path/to/root")
    '/path/to/root/model-name'
    >>> _determine_model_folder("model-name:1.0", "/path/to/root")
    '/path/to/root/model-name_1.0'

    r"   r#   /_r   )r   replaceospathjoin)r   r'   local_model_dirr   r   r   r   w   s   c                 C   sL   t | |}t||}dd t|D }|std| tj||d S )z+Find the local checkpoint path for a model.c                 S   s   g | ]	}| d r|qS )z.ckpt)endswith).0fnr   r   r   
<listcomp>   s    z.find_model_local_ckpt_path.<locals>.<listcomp>z%Parsing files from downloaded model: r   )r&   r   r+   listdirRuntimeErrorr,   r-   )r   r!   r'   r%   r.   folder_filesr   r   r   find_model_local_ckpt_path   s   

r6   trainerz
pl.Trainerc                 C   sl   |j dkr.tdstdddlm} t| |j}t||j}|||d}|s.t	d| |j
d dS )	z3Download a model from the Lightning Model Registry.r   	litmodelszYThe `litmodels` package is not installed. Please install it with `pip install litmodels`.)download_model)download_dirzDownload model failed - download_model_from_registryN)
local_rankr   ImportErrorr8   r9   r&   _model_registryr   r'   r4   strategybarrier)r   r7   r9   r%   r.   model_filesr   r   r   r;      s   
r;   )r+   r   typingr   lightning_utilitiesr   pytorch_lightningpl"lightning_fabric.utilities.importsr    lightning_fabric.utilities.typesr   __doctest_skip__r   r   tupler
   r    r&   r   r6   r;   r   r   r   r   <module>   s.   $
