o
    e(                     @  sF   d Z ddlmZ ddlmZ ddlmZ erddlmZ 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	pipelinespipelinepipelines.base.Pipelinereturndictc              
     s<  zddl }ddl m W n ty } ztd|d}~ww t jjs)tdt|drNt jj	rNt
jddd	d
dt
jdd
ddd dd dn1t|drst jjrst
jddd	d
dt
jdd
ddd dd dnt|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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nt|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nnt|d+r7t jjr7t
jd#d	d
d%t
jd,d
dd-d d.d dnHt|d/r\t jjr\t
jd	d
dt
jdd
dd0d d1d dn#t|d2rt jjrt
jd	d
dt
jdd
dd3d d4d dnt|d5rt jj rt
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nt|d=rt j"j#rt
jd	d
dt
jd>d
dt
j$d?d
dgt
jdd
dd@d dAd dnt|dBr%t j%j&r%t
jddCd
dt
jd&d
dgt
jdDd
ddEd dFd dnZt|dGrQt j'j(rQt
jddd
dt
jd&d
dgt
jd(d
ddHd dId dn.t|dJrvt j)j*rvt
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AudioClassificationPipeline
microphonefilepathZInputF)sourcetypelabelrenderClass)r   r   c                 S     d| iS Ninputs ir   r   @D:\Projects\ConvertPro\env\Lib\site-packages\gradio/pipelines.py<lambda>,       z$load_from_pipeline.<locals>.<lambda>c                 S     dd | D S )Nc                 S  $   i | ]}|d   dd |d qS r   z, r   scoresplit.0r   r   r   r   
<dictcomp>-      $ 8load_from_pipeline.<locals>.<lambda>.<locals>.<dictcomp>r   rr   r   r   r   -       )r   Zoutputs
preprocesspostprocess"AutomaticSpeechRecognitionPipelineOutputc                 S  r   r   r   r   r   r   r   r   8   r   c                 S     | d S )Ntextr   r'   r   r   r   r   9   r   FeatureExtractionPipelinec                 S  r   r   r   xr   r   r   r   A   r   c                 S  r.   )Nr   r   r'   r   r   r   r   B   r   FillMaskPipelineZClassificationc                 S  r   r   r   r1   r   r   r   r   J   r   c                 S  r   )Nc                 S     i | ]	}|d  |d qS )Z	token_strr   r   r"   r   r   r   r$   K       r&   r   r'   r   r   r   r   K   r)   ImageClassificationPipelinezInput Image)r   r   r   c                 S  r   NZimagesr   r   r   r   r   r   U   r   c                 S  r   )Nc                 S  r   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  
   | |dS )N)contextquestionr   )cqr   r   r   r   d      
 c                 S  s   | d | d fS )Nanswerr   r   r'   r   r   r   r   e   s    SummarizationPipelineSummaryc                 S  r   r   r   r1   r   r   r   r   m   r   c                 S     | d d S )Nr   Zsummary_textr   r'   r   r   r   r   n       TextClassificationPipelinec                 S     | gS Nr   r1   r   r   r   r   v       c                 S  r   )Nc                 S  r   r   r    r"   r   r   r   r$   w   r%   r&   r   r'   r   r   r   r   w   r)   TextGenerationPipelinec                 S  r   )NZtext_inputsr   r1   r   r   r   r      r   c                 S  rE   Nr   Zgenerated_textr   r'   r   r   r   r      rF   TranslationPipelineZTranslationc                 S  rH   rI   r   r1   r   r   r   r      rJ   c                 S  rE   )Nr   Ztranslation_textr   r'   r   r   r   r      rF   Text2TextGenerationPipelinezGenerated Textc                 S  rH   rI   r   r1   r   r   r   r      rJ   c                 S  rE   rL   r   r'   r   r   r   r      rF   ZeroShotClassificationPipelinez&Possible class names (comma-separated)zAllow multiple true classesc                 S  s   | ||dS )N)	sequencesZcandidate_labelsZmulti_labelr   )r   r?   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&   rR   )rangelenr'   r   r'   r   r      s   
 !DocumentQuestionAnsweringPipelinezInput DocumentLabelc                 S  r<   N)imager>   r   Zimgr@   r   r   r   r      rA   c                 S  r   )Nc                 S  r4   rB   r   r   r"   r   r   r   r$      r5   r&   r   r'   r   r   r   r      r)   VisualQuestionAnsweringPipelinec                 S  r<   rW   r   rY   r   r   r   r      rA   c                 S  r   )Nc                 S  r4   rZ   r   r"   r   r   r   r$      r5   r&   r   r'   r   r   r   r      r)   ImageToTextPipelineTextc                 S  r   r7   r   r   r   r   r   r      r   c                 S  rE   rL   r   r'   r   r   r   r      rF   zUnsupported pipeline type: c                    sP   d |  }t  jjjjjjfr | }n di |}d |}|S )Nr*   r+   r   )
isinstancetext_classificationrG   text2text_generationrN   rM   )paramsdataoutputr   Zpipeline_infor   r   r   fn   s   
zload_from_pipeline.<locals>.fnre   r*   r+   title)0transformersr   ImportErrorr^   baseZPipeline
ValueErrorhasattrZaudio_classificationr   r   ZAudiorV   Zautomatic_speech_recognitionr,   ZTextboxZfeature_extractionr0   Z	DataframeZ	fill_maskr3   Zimage_classificationr6   ZImageZquestion_answeringr8   r`   rC   r_   rG   Ztext_generationrK   rM   rN   Zzero_shot_classificationrO   ZCheckboxZdocument_question_answeringrU   Zvisual_question_answeringr[   Zimage_to_textr\   r   copymodel	__class____name__)r   rg   iere   Zinterface_infor   rd   r   load_from_pipeline   sN  







			rq   N)r   r   r	   r
   )
__doc__
__future__r   typingr   Zgradior   rg   r   rq   r   r   r   r   <module>   s    