o
     
j$                     @   s2  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
m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 d d
lmZmZmZmZ G dd deZG dd deZG dd deZdedefddZ eddde!eedB f fddZ"edddededdfddZ#G dd deZ$dS )    N)	lru_cache)Path)Any)urlparse
urlunparse)	constants)InferenceProviderMapping)	MimeBytesRequestParameters_b64_encode_bytes_to_dict_open_as_mime_bytes)TaskProviderHelperfilter_none)build_hf_headersget_session	get_tokenhf_raise_for_statusc                	       s   e Zd ZdZdef fddZdedB defdd	Zd
edB defddZdededefddZ	de
dedededB fddZ  ZS )HFInferenceTaskz&Base class for HF Inference API tasks.taskc                    s    t  jdtjjdd|d d S )Nhf-inferenceprovider)r   base_urlr   )super__init__r   INFERENCE_PROXY_TEMPLATEformat)selfr   	__class__ d/home/kuhnn/.local/lib/python3.10/site-packages/huggingface_hub/inference/_providers/hf_inference.pyr      s
   
zHFInferenceTask.__init__api_keyNreturnc                 C   s
   |pt  S N)r   )r   r#   r!   r!   r"   _prepare_api_key   s   
z HFInferenceTask._prepare_api_keymodelc                 C   sz   |d ur| drtd||| jddS |d ur|nt | j}|d u r-td| j dt|| j td||| jddS )Nzhttp://zhttps://r   live)r   
providerIdhf_model_idr   statuszTask z has no recommended model for HF Inference. Please specify a model explicitly. Visit https://huggingface.co/tasks for more info.)
startswithr   r   _fetch_recommended_modelsget
ValueError_check_supported_task)r   r'   model_idr!   r!   r"   _prepare_mapping_info"   s   z%HFInferenceTask._prepare_mapping_infomapped_modelc                 C   s@   | dr|S | jdv r| j d| d| j S | j d| S )Nr(   feature-extractionzsentence-similarity/models/z
/pipeline/)r-   r   r   )r   r#   r4   r!   r!   r"   _prepare_url2   s   

zHFInferenceTask._prepare_urlinputs
parametersprovider_mapping_infoc                 C   sL   t |trtd| j dt |trtd| j d| dt||dS )N!Unexpected binary input for task .Unexpected path input for task  (got )r9   r:   
isinstancebytesr0   r   r   r   r   r9   r:   r;   r!   r!   r"   _prepare_payload_as_dict>   s
   

z(HFInferenceTask._prepare_payload_as_dict)__name__
__module____qualname____doc__strr   r&   r   r3   r8   r   dictrF   __classcell__r!   r!   r   r"   r      s    r   c                   @   sP   e Zd ZdededededB fddZdededededB dedB f
d	d
ZdS )HFInferenceBinaryInputTaskr9   r:   r;   r$   Nc                 C   s   d S r%   r!   rE   r!   r!   r"   rF   I   s   z3HFInferenceBinaryInputTask._prepare_payload_as_dictextra_payloadc                 C   s   t |}|pi }t|dkpt|dk}t|ttfs't|ts'td| |s-t|S tt	
t||d|dddS )Nr   z5Expected binary inputs or a local path or a URL. Got rA   zutf-8zapplication/json)	mime_type)r   lenrC   rD   r   rK   r0   r   r	   jsondumpsr   encode)r   r9   r:   r;   rO   has_parametersr!   r!   r"   _prepare_payload_as_bytesN   s   z4HFInferenceBinaryInputTask._prepare_payload_as_bytes)	rG   rH   rI   r   rL   r   rF   r	   rV   r!   r!   r!   r"   rN   H   s*    
rN   c                	       sP   e Zd Z fddZdededededB fdd	Zd
ededefddZ	  Z
S )HFInferenceConversationalc                       t  d d S )Nconversationalr   r   r   r   r!   r"   r   i      z"HFInferenceConversational.__init__r9   r:   r;   r$   Nc                 C   s|   t |}|j}|dp|}|d u s|drd}|d}t|tr5|ddkr5d|d d d	|d< i |||d
S )Nr'   r(   dummyresponse_formattypejson_schemajson_objectschema)r_   value)r'   messages)r   provider_idr/   r-   rC   rL   )r   r9   r:   r;   payloadr4   payload_modelr^   r!   r!   r"   rF   l   s   


z2HFInferenceConversational._prepare_payload_as_dictr#   r4   c                 C   s.   | dr|ntjjdd d| }t|S )Nr(   r   r   r7   )r-   r   r   r   _build_chat_completion_url)r   r#   r4   r   r!   r!   r"   r8   ~   s
   z&HFInferenceConversational._prepare_url)rG   rH   rI   r   r   rL   r   rF   rK   r8   rM   r!   r!   r   r"   rW   h   s    
rW   	model_urlr$   c                 C   s`   t | }|jd}|dr| S |dr|d }n	|s d}n|d }|j|d}tt|S )N/z/chat/completionsz/v1z/v1/chat/completions)path)r   rk   rstripendswith_replacerK   r   )ri   parsedrk   new_path
new_parsedr!   r!   r"   rh      s   


rh      )maxsizec                  C   s8   t  jtj dt d} t|  dd |   D S )Nz
/api/tasks)headersc                 S   s$   i | ]\}}|t t|d  dqS )widgetModelsN)nextiter).0r   detailsr!   r!   r"   
<dictcomp>   s   $ z-_fetch_recommended_models.<locals>.<dictcomp>)r   r/   r   ENDPOINTr   r   rR   items)responser!   r!   r"   r.      s   r.   r'   r   c              
   C   s   ddl m} | | }|j}|jpg }d|v }|dv r5|dkr5|r$d S |dkr*d S td|  d| d|d	krJ|dkr?d S td|  d| d|d
krZ|rV|dkrVd S td|dv rh|dv rh||v rhd S ||kr}td|  d| d| d| d	d S )Nr   )HfApirY   )text-generationrY   r   zModel 'z' doesn't support task 'z'.ztext2text-generationzimage-text-to-textz<Non-conversational image-text-to-text task is not supported.r5   z'. Supported tasks: 'z	', got: '')huggingface_hub.hf_apir~   
model_infopipeline_tagtagsr0   )r'   r   r~   r   r   r   is_conversationalr!   r!   r"   r1      s:   
r1   c                	       sZ   e Zd Z fddZdededededB fdd	Zdd
eeB de	dB defddZ
  ZS ) HFInferenceFeatureExtractionTaskc                    rX   )Nr6   rZ   r[   r   r!   r"   r      r\   z)HFInferenceFeatureExtractionTask.__init__r9   r:   r;   r$   Nc                 C   sN   t |trtd| j dt |trtd| j d| dd|it|S )Nr<   r=   r>   r?   r@   r9   rB   rE   r!   r!   r"   rF      s
   

z9HFInferenceFeatureExtractionTask._prepare_payload_as_dictr}   request_paramsc                 C   s   t |tr	t|S |S r%   )rC   rD   r   )r   r}   r   r!   r!   r"   get_response   s   
z-HFInferenceFeatureExtractionTask.get_responser%   )rG   rH   rI   r   r   rL   r   rF   rD   r
   r   rM   r!   r!   r   r"   r      s    
(r   )%rR   	functoolsr   pathlibr   typingr   urllib.parser   r   huggingface_hubr   r   r   !huggingface_hub.inference._commonr	   r
   r   r   r   ,huggingface_hub.inference._providers._commonr   r   huggingface_hub.utilsr   r   r   r   r   rN   rW   rK   rh   rL   r.   r1   r   r!   r!   r!   r"   <module>   s&    4 +