a
    d(                     @  sL   d Z ddlmZ ddlmZ ddlmZ er8ddlmZ dddd	d
Z	dS )zThis module should not be used directly as its API is subject to change. Instead,
please use the `gr.Interface.from_pipeline()` function.    )annotations)TYPE_CHECKING)
components	pipelineszpipelines.base.Pipelinedict)pipelinereturnc              
     s\  zddl }ddl m W n. tyF } ztd|W Y d}~n
d}~0 0 t jjs^tdt|drt jj	rt
jddd	d
dt
jdd
ddd dd dnvt|drt jjrt
jddd	d
dt
jdd
ddd dd dn,t|dr<t jjr<t
jd	d
dt
jdd
ddd dd dnt|drt jjrt
jd	d
dt
jdd
ddd dd dnt|drt jjrt
jddd
dt
jdd
dd d d!d dnLt|d"r:t jjr:t
jd#d$d
d%t
jd&d
dgt
jd'd
dt
jd(d
dgd)d d*d dnt|d+rt jjrt
jd#d	d
d%t
jd,d
dd-d d.d dnt|d/rt jjrt
jd	d
dt
jdd
dd0d d1d dnNt|d2rt jjrt
jd	d
dt
jdd
dd3d d4d dnt|d5rdt jj rdt
jd	d
dt
jd6d
dd7d d8d dnt|d9rt jj!rt
jd	d
dt
jd:d
dd;d d<d dnpt|d=rt j"j#rt
jd	d
dt
jd>d
dt
j$d?d
dgt
jdd
dd@d dAd dnt|dBrjt j%j&rjt
jddCd
dt
jd&d
dgt
jdDd
ddEd dFd dnt|dGrt j'j(rt
jddd
dt
jd&d
dgt
jd(d
ddHd dId dn\t|dJrt j)j*rt
jddd
dt
jdKd
ddLd dMd dntdNt+   fdOdP}, }||dQ< |dR= |dS=  j-j.j/|dT< |S )Ua#  
    Gets the appropriate Interface kwargs for a given Hugging Face transformers.Pipeline.
    pipeline (transformers.Pipeline): the transformers.Pipeline from which to create an interface
    Returns:
    (dict): a dictionary of kwargs that can be used to construct an Interface object
    r   Nr   zAtransformers not installed. Please try `pip install transformers`z(pipeline must be a transformers.PipelineAudioClassificationPipelineZ
microphonefilepathZInputF)sourcetypelabelrenderZClass)r   r   c                 S  s   d| iS Ninputs ir   r   Y/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gradio/pipelines.py<lambda>,       z$load_from_pipeline.<locals>.<lambda>c                 S  s   dd | D S )Nc                 S  s$   i | ]}|d   dd |d qS r   z, r   scoresplit.0r   r   r   r   
<dictcomp>-   r   8load_from_pipeline.<locals>.<lambda>.<locals>.<dictcomp>r   rr   r   r   r   -   r   )r   outputs
preprocesspostprocess"AutomaticSpeechRecognitionPipelineZOutputc                 S  s   d| iS r   r   r   r   r   r   r   8   r   c                 S  s   | d S )Ntextr   r    r   r   r   r   9   r   FeatureExtractionPipelinec                 S  s   d| iS r   r   xr   r   r   r   A   r   c                 S  s   | d S )Nr   r   r    r   r   r   r   B   r   FillMaskPipelineZClassificationc                 S  s   d| iS r   r   r(   r   r   r   r   J   r   c                 S  s   dd | D S )Nc                 S  s   i | ]}|d  |d qS )Z	token_strr   r   r   r   r   r   r   K   r   r   r   r    r   r   r   r   K   r   ImageClassificationPipelinezInput Image)r   r   r   c                 S  s   d| iS NZimagesr   r   r   r   r   r   U   r   c                 S  s   dd | D S )Nc                 S  s$   i | ]}|d   dd |d qS r   r   r   r   r   r   r   V   r   r   r   r    r   r   r   r   V   r   QuestionAnsweringPipeline   Context)linesr   r   ZQuestionZAnswerZScorec                 S  s
   | |dS )N)contextquestionr   )cqr   r   r   r   d   r   c                 S  s   | d | d fS )Nanswerr   r   r    r   r   r   r   e   r   SummarizationPipelineZSummaryc                 S  s   d| iS r   r   r(   r   r   r   r   m   r   c                 S  s   | d d S )Nr   Zsummary_textr   r    r   r   r   r   n   r   TextClassificationPipelinec                 S  s   | gS Nr   r(   r   r   r   r   v   r   c                 S  s   dd | D S )Nc                 S  s$   i | ]}|d   dd |d qS r   r   r   r   r   r   r   w   r   r   r   r    r   r   r   r   w   r   TextGenerationPipelinec                 S  s   d| iS )NZtext_inputsr   r(   r   r   r   r      r   c                 S  s   | d d S Nr   Zgenerated_textr   r    r   r   r   r      r   TranslationPipelineZTranslationc                 S  s   | gS r8   r   r(   r   r   r   r      r   c                 S  s   | d d S )Nr   Ztranslation_textr   r    r   r   r   r      r   Text2TextGenerationPipelinezGenerated Textc                 S  s   | gS r8   r   r(   r   r   r   r      r   c                 S  s   | d d S r:   r   r    r   r   r   r      r   ZeroShotClassificationPipelinez&Possible class names (comma-separated)zAllow multiple true classesc                 S  s   | ||dS )N)	sequencesZcandidate_labelsZmulti_labelr   )r   r3   mr   r   r   r      s    c                   s    fddt t d D S )Nc                   s"   i | ]} d  |  d | qS )labelsZscoresr   r   r    r   r   r      s   r   r@   )rangelenr    r   r    r   r      s   
!DocumentQuestionAnsweringPipelinezInput DocumentLabelc                 S  s
   | |dS N)imager2   r   imgr4   r   r   r   r      r   c                 S  s   dd | D S )Nc                 S  s   i | ]}|d  |d qS r5   r   r   r   r   r   r   r      r   r   r   r    r   r   r   r      r   VisualQuestionAnsweringPipelinec                 S  s
   | |dS rE   r   rG   r   r   r   r      r   c                 S  s   dd | D S )Nc                 S  s   i | ]}|d  |d qS rI   r   r   r   r   r   r      r   r   r   r    r   r   r   r      r   ImageToTextPipelineTextc                 S  s   d| iS r,   r   r   r   r   r   r      r   c                 S  s   | d d S r:   r   r    r   r   r   r      r   zUnsupported pipeline type: c                    sP   d |  }t  jjjjjjfr2 | }n f i |}d |}|S )Nr#   r$   )
isinstancetext_classificationr7   text2text_generationr<   r;   )paramsdataoutputr   Zpipeline_infor   r   r   fn   s    
zload_from_pipeline.<locals>.fnrT   r#   r$   title)0transformersr   ImportErrorrM   baseZPipeline
ValueErrorhasattrZaudio_classificationr
   r   ZAudiorD   Zautomatic_speech_recognitionr%   ZTextboxZfeature_extractionr'   Z	DataframeZ	fill_maskr*   Zimage_classificationr+   ZImageZquestion_answeringr-   rO   r6   rN   r7   Ztext_generationr9   r;   r<   Zzero_shot_classificationr=   ZCheckboxZdocument_question_answeringrC   Zvisual_question_answeringrJ   Zimage_to_textrK   r   copymodel	__class____name__)r   rV   ZierT   Zinterface_infor   rS   r   load_from_pipeline   sJ   











			r_   N)
__doc__
__future__r   typingr   Zgradior   rV   r   r_   r   r   r   r   <module>   s   