
    .`i:                        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	 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 d d
lmZ d dlmZ d dlmZ erd dlm Z   edee          Z! edee          Z" G d de#          Z$ G d de%          Z&de'e(e'df         z  fdZ)ede'de*e+         fd            Z,ddde'e(e'df         z  fdZ-ddedd e+d!ed"e+dz  d#e.de'e!         e(e'e!         df         z  d$ede!fd%Z/ ee/          Z0ed&e!de*e+         fd'            Z1ddedd e+d!ed"e+dz  d#e.de'e!         e(e'e!         df         z  d$ede!fd(Z2efddde'e!         e(e'e!         df         z  d$ede!fd)Z3ddd*d e+d!ed"e+dz  d#e.d$ef
d+Z4 ee4          Z5ddd$efd,Z6ddd*d e+d!ed"e+dz  d#e.d$ef
d-Z7 ee7          Z8ddd$efd.Z9dddd/d e+d!ed"e+dz  d#e.d0e'e"         dz  d$efd1Z: ee:          Z;	 d3ddde'e"         dz  d$efd2Z<dS )4    N)	lru_cache)TYPE_CHECKINGAnycastget_argsget_type_hints)AutoFeatureExtractorAutoImageProcessorAutoProcessorAutoVideoProcessor)FeatureExtractionMixin)BaseImageProcessor)ProcessorMixin)BaseVideoProcessor)TypeVar)is_gguf)convert_model_repo_to_path) get_allowed_kwarg_only_overrides)ModelConfig_P)bounddefault_Vc                       e Zd ZdZdefdZdS )HashableDictz7
    A dictionary that can be hashed by lru_cache.
    returnc                 ^    t          t          |                                                     S N)hash	frozensetitemsselfs    u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/transformers_utils/processor.py__hash__zHashableDict.__hash__'   s     Idjjll++,,,    N__name__
__module____qualname____doc__intr%    r&   r$   r   r       s9         -# - - - - - -r&   r   c                       e Zd ZdZdefdZdS )HashableListz1
    A list that can be hashed by lru_cache.
    r   c                 :    t          t          |                     S r   )r   tupler"   s    r$   r%   zHashableList.__hash__0   s    E$KK   r&   Nr'   r-   r&   r$   r/   r/   +   s9         !# ! ! ! ! ! !r&   r/   processor_cls.c                     t          | t                    s| t          k    rt          j        S t          | d          r| j        S | S )Nfrom_pretrained)
isinstancer1   r   r   r4   hasattr)r2   s    r$   _get_processor_factory_fnr7   4   sI    -'' -=N+J+J,,}/00 -,,r&   
kwargs_clsr   c                     t                      }| |S t          |           }dD ]5}||v r/t          ||                   }|D ]}|                    |           6|h dz  }|S )N)text_kwargsimages_kwargsvideos_kwargsaudio_kwargs>   r:   r=   r;   r<   )setr   add)r8   dynamic_kwargskwargs_type_annotationskw_typekw_annotationskw_names         r$   ,_collect_dynamic_keys_from_processing_kwargsrE   =   s    "uuN -Z88T , ,---+,CG,LMMN) , ,""7++++WWWWNr&   model_configr   c                l   |                                  }|                    |          }t          |          }t          ||dd          }|                                D ]S\  }}t          |t                    rt          |          ||<   t          |t                    rt          |          ||<   T|S )NFT)requires_kw_onlyallow_var_kwargs)
get_multimodal_configmerge_mm_processor_kwargsr7   r   r!   r5   dictr   listr/   )	rF   r2   kwargs	mm_configmerged_kwargsfactoryallowed_kwargskeyvalues	            r$   _merge_mm_kwargsrU   N   s     2244I77??M'66G5	  N %**,, 6 6
UeT"" 	6".u"5"5N3eT"" 	6".u"5"5N3r&   Frevisiontrust_remote_coder2   processor_nameargsrW   rX   rN   c                   |d}	 t          |           } t          |t                    s|t          k    rt	          j        | g|R ||d|}n2t          |t                    r |j        | g|R ||d|}n ||i |}n(# t          $ r}|sd}t          |          ||d}~ww xY wt          ||          s"t          d| dt          |                     |S )z:Load a processor for the given model name via HuggingFace.NmainrW   rX   zFailed to load the processor. If the processor is a custom processor not yet available in the HuggingFace transformers library, consider setting `trust_remote_code=True` in LLM or using the `--trust-remote-code` flag in the CLI.z6Invalid type of HuggingFace processor. Expected type: z, but found type: )r   r5   r1   r   r   r4   
issubclass
ValueErrorRuntimeError	TypeErrortype)	rY   rW   rX   r2   rZ   rN   	processoreerr_msgs	            r$   get_processorrf   j   s    #3NCCmU++ 	7}/N/N%5  ""3	 
  II ~66 
	755  ""3	 
  II &t6v66I    ! 
	9  w''Q.G  i// 
-+- -	??- -
 
 	
 s   A:B 
B&B!!B&rc   c                 B   	 t          j        t          |           j                  j                            d          }|r|j        nd }|d t           j        fvr"t          t          |          d                   S t          |           j
        }t          j        |          }t                      }t          |                                          D ],\  }}|                    d          r|t          |          z  }-|S # t"          $ r t                      cY S w xY w)NrN   r   ProcessingKwargs)inspect	signaturerb   __call__
parametersget
annotation_emptyrE   r   r)   	importlibimport_moduler>   varsr!   endswith	Exception)rc   call_kwargscall_kwargs_annotationsmodule_namemodprocessor_kwargsnameobjs           r$   #get_processor_kwargs_from_processorr|      s0   'Y(@AALPP
 
 =H"Q+"8"8T"4*@@@
 @011!4  
 y//4K)+66C),!#YY__..  	c==!344 (FsKKL % $#   uus   A:D =BD DDc                   	 t          | |||          }t          |          		fd|                                D             }t          | f|||d|}|S )NrV   c                 $    i | ]\  }}|v	||S r-   r-   ).0kvdynamic_keyss      r$   
<dictcomp>z?cached_get_processor_without_dynamic_kwargs.<locals>.<dictcomp>   s)    PPP1!<:O:Oq!:O:O:Or&   )cached_get_processorr|   r!   )
rY   rW   rX   r2   rZ   rN   rc   filtered_kwargsfinal_processorr   s
            @r$   +cached_get_processor_without_dynamic_kwargsr      s     %+#	  I 7yAAL QPPPPPPO ++#	 
  O r&   c           	          t          | j                  r-t          | j                  r
J d            | j        }| j        }n| j        }| j        }t          |f|| j        |dt          | |fi |S )Nz^For multimodal GGUF models, the original tokenizer should be used to correctly load processor.rV   )r   model	tokenizertokenizer_revisionrW   r   rX   rU   )rF   r2   rN   r   rW   s        r$   cached_processor_from_configr      s    
 |!"" 	)<122 	
 	
:	
 	
2 &2"(6&8#	 
 <
A
A&
A
A  r&   r]   c                    	 t          |           } t          j        | g|R ||d|}n(# t          $ r}|sd}t	          |          ||d}~ww xY wt          t          |          S )zMLoad an audio feature extractor for the given model name
    via HuggingFace.r]   zFailed to load the feature extractor. If the feature extractor is a custom extractor not yet available in the HuggingFace transformers library, consider setting `trust_remote_code=True` in LLM or using the `--trust-remote-code` flag in the CLI.N)r   r	   r4   r_   r`   r   r   )rY   rW   rX   rZ   rN   feature_extractorrd   re   s           r$   get_feature_extractorr     s    3NCC0@

 
 /	
 

 
 
     ! 
	9  w''Q.G &(9:::   '* 
AA

Ac           	      b    t          | j        f| j        | j        dt	          | t
          fi |S )Nr]   )cached_get_feature_extractorr   rW   rX   rU   r	   )rF   rN   s     r$   $cached_feature_extractor_from_configr   +  sO     (&&8  <)=
H
H
H
H	  r&   c                    	 t          |           } t          j        | g|R ||d|}n(# t          $ r}|sd}t	          |          ||d}~ww xY wt          t          |          S )zALoad an image processor for the given model name via HuggingFace.r]   zFailed to load the image processor. If the image processor is a custom processor not yet available in the HuggingFace transformers library, consider setting `trust_remote_code=True` in LLM or using the `--trust-remote-code` flag in the CLI.N)r   r
   r4   r_   r`   r   r   )rY   rW   rX   rZ   rN   rc   rd   re   s           r$   get_image_processorr   7  s    3NCC&6

 
 /	
 

 
 
		     ! 
	9  w''Q.G  "I...r   c           	          t          | j                  r-t          | j                  r
J d            | j        }| j        }n| j        }| j        }t          |f|| j        dt          | t          fi |S )NzdFor multimodal GGUF models, the original tokenizer should be used to correctly load image processor.r]   )	r   r   r   r   rW   cached_get_image_processorrX   rU   r
   )rF   rN   r   rW   s       r$   "cached_image_processor_from_configr   ^  s     |!"" 	)<122 	
 	
@	
 	
2 &2"(%&8  <);
F
Fv
F
F	  r&   rW   rX   processor_cls_overridesr   c                    	 t          |           } |pt          } |j        | g|R ||d|}n(# t          $ r}|sd}	t	          |	          ||d}~ww xY wt          t          |          S )z@Load a video processor for the given model name via HuggingFace.r]   zFailed to load the video processor. If the video processor is a custom processor not yet available in the HuggingFace transformers library, consider setting `trust_remote_code=True` in LLM or using the `--trust-remote-code` flag in the CLI.N)r   r   r4   r_   r`   r   r   )
rY   rW   rX   r   rZ   rN   r2   rc   rd   re   s
             r$   get_video_processorr   t  s    3NCC/E3E1M1

 
 /	
 

 
 
		     ! 
	9  w''Q.G  "I...s   ,/ 
AAAc           	      d    t          | j        f| j        | j        |dt	          | t
          fi |S )Nr   )cached_get_video_processorr   rW   rX   rU   r   )rF   r2   rN   s      r$   "cached_video_processor_from_configr     sR    
 &&&8 -	 
 <);
F
Fv
F
F  r&   r   )=rp   ri   	functoolsr   typingr   r   r   r   r   transformersr	   r
   r   r   %transformers.feature_extraction_utilsr   #transformers.image_processing_utilsr   transformers.processing_utilsr   #transformers.video_processing_utilsr   typing_extensionsr   "vllm.transformers_utils.gguf_utilsr   vllm.transformers_utils.utilsr   vllm.utils.func_utilsr   vllm.configr   r   r   rL   r   rM   r/   rb   r1   r7   r>   strrE   rU   boolrf   r   r|   r   r   r   r   r   r   r   r   r   r   r   r-   r&   r$   <module>r      s              E E E E E E E E E E E E E E            I H H H H H B B B B B B 8 8 8 8 8 8 B B B B B B % % % % % % 6 6 6 6 6 6 D D D D D D B B B B B B (''''''WT@@@WT+5GHHH- - - - -4 - - -! ! ! ! !4 ! ! !TE$)4D-D     T c#h     %c	**   >  #5C7 7 777 Dj7 	7
 8eDHcM227 7 7 7 7 7t !y//  2 #c(    H  #5C   Dj 	
 8eDHcM22     H 6D 8eDHcM22  	   8  #	!; !; !;!;!; Dj!; 	!;
 !; !; !; !;H  )y)>?? 			 	 	 	  #	!/ !/ !/!/!/ Dj!/ 	!/
 !/ !/ !/ !/H 'Y':;;    2  #/3#/ #/ #/#/#/ Dj#/ 	#/
 ""X_#/ #/ #/ #/ #/L 'Y':;; 
 &* 8d?      r&   