o
    0j                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ ddlmZ dd	lmZ d
dlmZmZ dZdZdZdZdZdddddddZdd Zdd Zdd Zdd Zdd Ze jdd ZdS )     N)Path)AnyDictOptionalType)Version)	BaseModel   )logging   )official_models   )check_backendmodel_name_to_module_nameZget_network_classZget_processor_classZ
get_configget_chat_template_pathzchat_template.jinjaz0.11.1z0.5.7)Zmin_vllm_versionZmin_sglang_version)zPaddleOCR-VL-0.9BzPaddleOCR-VL-1.5-0.9Bc                 C   s"   | t vrtd|  t| d S )NUnknown model: )ALL_MODEL_INFO
ValueErrorr   )
model_namebackend r   h/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/genai/models/__init__.py_check_model_name_and_backend-   s   r   c                 C   sv   t | | t|  }d| d|v r9|dkrdd l}|j}n|dkr)dd l}|j}nd}t|t|d| d kS dS )NZmin__versionvllmr   sglangz0.0.0F)r   r   r   __version__r   r   )r   r   Z
model_infor   Zbackend_lib_versionr   r   r   r   is_integrated_model_available4   s   
r   c              
   C   sV   t | | z	t|  }W t|S  ty* } ztdt|  dt| d|d }~ww )Nz-Could not prepare the official model for the z model with the z	 backend.)r   r   	ExceptionRuntimeErrorreprstr)r   r   	model_direr   r   r   get_model_dirK   s   

r$   c              
      s    fdd}t |   t| }ztjd| tdW n ty0 } ztd|  |d }~ww |t} dkrA|t}||fS d }||fS )Nc                    s6   t | std d|  dt| }| }|S )N`` does not have `)hasattrr   getattr)Z
getter_keygettercompr   Zmodel_moduler   r   _get_componentY   s
   

z,get_model_components.<locals>._get_component.packager   r   )	r   r   	importlibimport_module__package__ModuleNotFoundErrorr   NETWORK_CLASS_GETTER_KEYPROCESSOR_CLASS_GETTER_KEY)r   r   r,   mod_namer#   Znetwork_classZprocessor_classr   r+   r   get_model_componentsX   s   
r7   c                 C   s   t | | t| }ztjd| td}W n ty'   td|  i }Y |S w t|t	s8t
d| dt	 dt|t	}||}|S )Nz
..configs.r.   z0No default configs were found for the model '%s'r%   r&   )r   r   r0   r1   r2   r3   r
   debugr'   CONFIG_GETTER_KEYr   r(   )r   r   r6   Zconfig_moduleZdefault_configZconfig_getterr   r   r   get_default_configs   s    




r:   c                 c   s    t | | tjd|  d<}| s1t|t}| r&| r&|V  nt	d| | d V  n|V  W d    d S W d    d S W d    d S 1 sOw   Y  d S )Nz&paddlex.inference.genai.chat_templatesz.jinjazCNo chat template was found for the model '%s' with the backend '%s')
r   r0   	resourcespathexistsr   DEFAULT_CHAT_TEMPLATE_FILENAMEis_filer
   r8   )r   r   r"   Zchat_template_pathZdefault_chat_template_pathr   r   r   r      s2   



") 
contextlibr0   pathlibr   typingr   r   r   r   Zpackaging.versionr   Zpydanticr   utilsr
   Zutils.official_modelsr   r   r   r4   r5   r9   ZCHAT_TEMPLATE_PATH_GETTER_KEYr>   r   r   r   r$   r7   r:   contextmanagerr   r   r   r   r   <module>   s8   