
    l0jY                    r   d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	Z	ddl
mZ ddlmZmZ ddl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 ddlmZmZmZmZmZmZ ddl m!Z!m"Z" erddl#m$Z$ ddl%m&Z&  ed            e            	 	 	 	 d(d)d            Z'	 	 	 d*d+dZ(d Z)d Z*d,dZ+d-d Z,d.d"Z-d/d'Z.dS )0zThis module should not be used directly as its API is subject to change. Instead,
use the `gr.Blocks.load()` or `gr.load()` functions.    )annotationsN)TYPE_CHECKINGCallable)Client)documentset_documentation_group)
componentsutils)Context)warn_deprecation)ErrorTooManyRequestsError)cols_to_rowsencode_to_base64get_tabular_examplespostprocess_labelrows_to_colsstreamline_spaces_interface)extract_base64_data	to_binary)Blocks)	Interfacehelpersnamestrsrc
str | Noneapi_keyhf_tokenaliasreturnr   c                N    ||rt          d           |}t          d| |||d|S )a  
    Method that constructs a Blocks from a Hugging Face repo. Can accept
    model repos (if src is "models") or Space repos (if src is "spaces"). The input
    and output components are automatically loaded from the repo.
    Parameters:
        name: the name of the model (e.g. "gpt2" or "facebook/bart-base") or space (e.g. "flax-community/spanish-gpt2"), can include the `src` as prefix (e.g. "models/facebook/bart-base")
        src: the source of the model: `models` or `spaces` (or leave empty if source is provided as a prefix in `name`)
        api_key: Deprecated. Please use the `hf_token` parameter instead.
        hf_token: optional access token for loading private Hugging Face Hub models or spaces. Find your token here: https://huggingface.co/settings/tokens.  Warning: only provide this if you are loading a trusted private Space as it can be read by the Space you are loading.
        alias: optional string used as the name of the loaded model instead of the default name (only applies if loading a Space running Gradio 2.x)
    Returns:
        a Gradio Blocks object for the given model
    Example:
        import gradio as gr
        demo = gr.load("gradio/question-answering", src="spaces")
        demo.launch()
    Nz^The `api_key` parameter will be deprecated. Please use the `hf_token` parameter going forward.)r   r   r   r     )r   load_blocks_from_repo)r   r   r   r   r    kwargss         R/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/gradio/external.pyloadr'   &   s[    4 GA	
 	
 	
   sXU >D      c                   |W|                      d          }t          |          dk    s
J d            |d         }d                    |dd                   } t          t          t          d}|                                |v sJ d|                                             |<t          j        $t          j        |k    rt          j
        d           |t          _         ||         | ||fi |}|S )	zNCreates and returns a Blocks instance from a Hugging Face model or Space repo.N/   zYEither `src` parameter must be provided, or `name` must be formatted as {src}/{repo name}r   )huggingfacemodelsspaceszparameter: src must be one of zYou are loading a model/Space with a different access token than the one you used to load a previous model/Space. This is not recommended, as it may cause unexpected behavior.)splitlenjoin
from_modelfrom_spaceslowerkeysr   r   warningswarn)r   r   r   r    r%   tokensfactory_methodsblockss           r&   r$   r$   K   s    {CKK!OOOf OOQixxqrr
## "	, ,O 			&&&@(<(<(>(>@@ 	'&& 'G,<,H,HM F   $0OC0xQQ&QQFMr(   c                v    dd d | di}|.|d         d         |d         d<   |d         d         |d         d<   |S )Ninputs)generated_responsespast_user_inputstextconversationr=   r>   r#   )r?   statepayloads      r&   chatbot_preprocessrC   q   sd    $DRVWWG 383H!4
/0 16n0E1
,- Nr(   c                    |                                  }t          t          |d         d         |d         d                             }||fS )Nr@   r>   r=   )jsonlistzip)responseresponse_jsonchatbot_values      r&   chatbot_postprocessrK      sT    MMOOM.)*<=.)*?@	
 	
 M -''r(   
model_namec           	     "   d|  }d|  t          d|            |dd| ini t          j        d          }|j        dk    sJ d	|  d
            |                                                    d          i dt          j        dddd          t          j        dd          d d ddt          j        dddd          t          j        dd          t          t          ddt          j        dddd          t          j        dd          t          d ddt          j        d          t          j        d          gt          j        d          t          j        d          gt          t          ddt          j        dd          t          j        dd          d d dd t          j        dd          t          j        d!d          d" d# dd$t          j        dd%d&          t          j        d!d          t          d' dd(t          j        d)d*d+          t          j        d,d          gt          j        d-d          t          j        d.d          gd/ d0 dd1t          j        dd          t          j        d2d          d3 d4 dd5t          j        dd          t          j        d!d          d6 d7 dd8t          j        dd          t          j        dd          d9 d: dd;t          j        dd          t          j        d<d          d= d> dd?t          j        dd          t          j        d@d          dA dB ddCt          j        dd          t          j        dDd          t          j        dEd          gt          j        d!d          dF dG ddHt          j        dIdJdK          t          j        d)dLdMdN          gt          j        d!d          dO dP ddQt          j        dd          t          j        dRd          dS t          ddTt          j        dd          t          j        dd          dU t          dt          j        dd          t          j        dd          dV dW dt          j        ddXd&          t          j        d,d          gt          j        dYd          dZ d[ dt          j        dd%d&          t          j        d,d          gt          j        dYd          d\ d] dt          j        dd%d&          t          j        d<d          t          d^ dd_}d`v rwt)          |           }t+          |          \  }}|r|ggnd }t          j        dadb|t-          |          dcfdd          t          j        dedfdggdh          t.          di |dj|<   |vrt1          dk           |         fdl}	|| |	_        n||	_        |	dm         dn         |                     do          dp}
t5          |
fi |}dk    |dq<   t7          j        dri |}|S )sNzhttps://huggingface.co/z,https://api-inference.huggingface.co/models/zFetching model from: AuthorizationBearer GETheaders   zCould not find model: z. If it is a private or gated model, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `api_key` parameter.pipeline_tagzaudio-classificationuploadfilepathInputF)sourcetypelabelrenderClass)rZ   r[   c                    t           S N)r   )is    r&   <lambda>zfrom_model.<locals>.<lambda>   s    I r(   c                X    t          d |                                 D                       S )Nc                ^    i | ]*}|d                               d          d         |d         +S rZ   z, r   scorer/   .0r_   s     r&   
<dictcomp>z0from_model.<locals>.<lambda>.<locals>.<dictcomp>   5    III17!!$''*AgJIIIr(   r   rE   rs    r&   r`   zfrom_model.<locals>.<lambda>   *    %6IIIII& & r(   )r<   outputs
preprocesspostprocesszaudio-to-audioOutputzautomatic-speech-recognitionc                6    |                                  d         S )Nr?   rE   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s    QVVXXf%5 r(   conversational)r[   zfeature-extractionc                
    d| iS Nr<   r#   xs    r&   r`   zfrom_model.<locals>.<lambda>   
    XqM r(   c                6    |                                  d         S )Nr   rs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s    QVVXXa[ r(   z	fill-maskClassificationc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>   ry   r(   c                X    t          d |                                 D                       S )Nc                ,    i | ]}|d          |d         S )	token_strrd   r#   rf   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>   s"    >>>;7>>>r(   rj   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s*    %6>>QVVXX>>>& & r(   zimage-classificationzInput Image)rY   rZ   r[   c                X    t          d |                                 D                       S )Nc                ^    i | ]*}|d                               d          d         |d         +S rc   re   rf   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>   ri   r(   rj   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   rm   r(   zquestion-answering   r   )linesrZ   r[   QuestionAnswerScorec                    d| |diS )Nr<   )contextquestionr#   )cqs     r&   r`   zfrom_model.<locals>.<lambda>   s    aQ2O2O'P r(   c                n    |                                  d         d|                                  d         ifS )NanswerrZ   rd   rs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s*    affhhx&87AFFHHWDU:V%W r(   summarizationSummaryc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>   ry   r(   c                B    |                                  d         d         S )Nr   summary_textrs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s    QVVXXa[%@ r(   ztext-classificationc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>   ry   r(   c                d    t          d |                                 d         D                       S )Nc                ^    i | ]*}|d                               d          d         |d         +S rc   re   rf   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>   s5    LLL17!!$''*AgJLLLr(   r   rj   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   s.    %6LLLLL& & r(   ztext-generationc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>   ry   r(   c                B    |                                  d         d         S Nr   generated_textrs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>       QVVXXa[1A%B r(   ztext2text-generationzGenerated Textc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>   ry   r(   c                B    |                                  d         d         S r   rs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>   r   r(   translationTranslationc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>  ry   r(   c                B    |                                  d         d         S )Nr   translation_textrs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>  s    QVVXXa[1C%D r(   zzero-shot-classificationz&Possible class names (comma-separated)zAllow multiple true classesc                    | ||ddS )N)candidate_labelsmulti_class)r<   
parametersr#   )r_   r   ms      r&   r`   zfrom_model.<locals>.<lambda>  s    34QGG+ + r(   c           	          t           fdt          t                                           d                             D                       S )Nc                    i | ]A}                                 d          |                                          d         |         BS )labelsscoresrs   )rg   r_   rl   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>  sO        FFHHX&q)16688H+=a+@  r(   r   )r   ranger0   rE   rk   s   `r&   r`   zfrom_model.<locals>.<lambda>  sU    %6   "3qvvxx'9#:#:;;  & & r(   zsentence-similarityzThat is a happy personzSource Sentence)valuerZ   r[   z#Separate each sentence by a newlinezSentences to compare to)r   placeholderrZ   r[   c                H    d| d |                                 D             diS )Nr<   c                    g | ]
}|d k    |S ) r#   )rg   ss     r&   
<listcomp>z0from_model.<locals>.<lambda>.<locals>.<listcomp>/  s    !O!O!OqBww!wwwr(   )source_sentence	sentences)
splitlines)r   r   s     r&   r`   zfrom_model.<locals>.<lambda>,  s6    '*!O!OY-A-A-C-C!O!O!O 2 r(   c                r    t          d t          |                                           D                       S )Nc                     i | ]\  }}d | |S )z	sentence r#   )rg   r_   vs      r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>3  s$    DDD1Q!DDDr(   )r   	enumeraterE   rk   s    r&   r`   zfrom_model.<locals>.<lambda>2  s2    %6DD	!&&((0C0CDDD& & r(   ztext-to-speechAudioc                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>:  ry   r(   ztext-to-imagec                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>A  ry   r(   c                
    d| iS rv   r#   rw   s    r&   r`   zfrom_model.<locals>.<lambda>H  ry   r(   c                    | S r^   r#   rk   s    r&   r`   zfrom_model.<locals>.<lambda>I  s    Q r(   zInput DocumentLabelc                *    dt          |           |diS Nr<   )imager   r   imgr   s     r&   r`   zfrom_model.<locals>.<lambda>R  $    055 ! * r(   c                X    t          d |                                 D                       S )Nc                ,    i | ]}|d          |d         S r   rd   r#   rf   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>Y  "    ;;;Q8aj;;;r(   rj   rk   s    r&   r`   zfrom_model.<locals>.<lambda>X  *    %6;;!&&((;;;& & r(   c                *    dt          |           |diS r   r   r   s     r&   r`   zfrom_model.<locals>.<lambda>c  r   r(   c                X    t          d |                                 D                       S )Nc                ,    i | ]}|d          |d         S r   r#   rf   s     r&   rh   z0from_model.<locals>.<lambda>.<locals>.<dictcomp>j  r   r(   rj   rk   s    r&   r`   zfrom_model.<locals>.<lambda>i  r   r(   c                B    |                                  d         d         S r   rs   rk   s    r&   r`   zfrom_model.<locals>.<lambda>t  r   r(   )token-classificationzdocument-question-answeringzvisual-question-answeringzimage-to-text)ztabular-classificationztabular-regressionz
Input Rowspandasfixed)rZ   rY   rR   	col_countr[   Predictionsarray
prediction)rZ   rY   rR   r[   c                P    dgd t          j        | j                  D             dS )Nr   c                    g | ]}|gS r#   r#   )rg   preds     r&   r   z0from_model.<locals>.<lambda>.<locals>.<listcomp>  s    ???D$???r(   rR   data)rE   loadsr?   rk   s    r&   r`   zfrom_model.<locals>.<lambda>  s0    (>??DJqv,>,>???& & r(   )r<   rn   ro   rp   exampleszUnsupported pipeline type: c                     d         |  }t          |t                    r-|                    dddii           t          j        |          }t          j        d	
|          }|j        dk    r|                                }d\  }}|                    d	          rd
|                    d	           }|                    d          rd|                    d           }t          d|j         |z   |z             dk    r1|                                }| d         }t          j        ||          } d         |          }|S )Nro   optionswait_for_modelTPOSTr   rS   )r   r   errorz	, Error: r6   z, Warnings: z<Could not complete request to HuggingFace API, Status Code: r   r   rp   )
isinstancedictupdaterE   dumpsrequestsrequeststatus_codegetr   r
   format_ner_list)paramsr   rH   errors_jsonerrorswarns
ner_groupsinput_stringoutputapi_urlrR   ppipelines            r&   query_huggingface_apiz)from_model.<locals>.query_huggingface_api  sq   %x%v.$
 
 	$ KK%5t$<=>>>:d##D#FGW4PPP3&&"--//K"MFEw'' @?[__W%=%=??z** ED{z'B'BDDexOcee   '''!J!!9L,\:FFH(-(22r(   r<   rn   r   )fnr<   rn   titler   	_api_moder#   )printr   r   r   rE   r   r	   r   r   r   r   TextboxStateChatbotrC   rK   	DataframeImageCheckboxHighlightedTextr   r   r0   r   
ValueError__name__r   gradior   )rL   r   r    r%   	model_urlrH   	pipelinesexample_data	col_namesr   interface_info	interfacer   rR   r   r   s               @@@@r&   r2   r2      s	   6*66IIZIIG	
-)
-
-...9A9M 4( 4 455SUG w@@@H### U
  U  U  U 	$##N++A^ &j   "'geDDD-- 
!
 
!
^ 	 &j   "'huEEE#+
 
^* 	' &j   ")GGG#55)
 )
+^< 	!)7779IQV9W9W9WX"*%888*:JRW:X:X:XY,.	
 
=^H 	 (wuEEE!+(5III1100
 
I^V 	 (wuEEE!'.>uMMM11 	
 
W^f 	 &}U   "'.>uMMM# 
!
 
!
g^| 	 ")EJJJ"EBBB
 "%@@@ wu=== QPWW
 
}^V 	 (wuEEE!)	%HHH11@@
 
W^d 	 (wuEEE!'.>uMMM11  
  
e^v 	 (wuEEE!)GGG11BB
 
w^D 	 (wuEEE!)0@OOO11BB!
 !
E^R 	 (wuEEE!)eLLL11DD	
 
S^^ 	# "???"Ee   #*GPUVVV "'.>uMMM  %
 %
_^H 	 "2+   
 " E3 	   "'.>uMMM  - 
  
I^| 	 (wuEEE!'geDDD11+
 
}^J 	 (wuEEE!'huEEE11+
 
K^\ !(wuEEE!1OOO11&;!
 !
  j8HQVWWW"EBBB "'geDDD  (
 (
(  jeTTT"EBBB "'geDDD  &
 &
& !&}U   ")0@OOO#BB
 
k^ ^ ^I@ 	<<<+J77".|"<"<	<+7A''T !*"!y>>73   "+#'L>RW   '  %!
 
	!& 	yAY&&:q::;;;|H       < })3&&).& $8$I&LL,, N .++F++F
 //F; **6**Ir(   
space_namec                d   d|  }t          d|            i }|d| |d<   t          j        d|  d|                                                              d          }|t	          d	|  d
          t          j        ||          }t          j        d|j                  }	 t          j        |	                    d                    }	n%# t          $ r}
t	          d|            |
d }
~
ww xY wd|	v rt          | |	|||fi |S |rt          j        d           t          | |          S )Nzhttps://huggingface.co/spaces/zFetching Space from: rO   rN   z"https://huggingface.co/api/spaces/z/hostrQ   hostzCould not find Space: z. If it is a private or gated Space, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `api_key` parameter.z+window.gradio_config = (.*?);[\s]*</script>r+   zCould not load the Space: allow_flaggingzYou cannot override parameters for this Space by passing in kwargs. Instead, please load the Space as a function and use it to create a Blocks or Interface locally. You may find this Guide helpful: https://gradio.app/using_blocks_like_functions/)spacer   )r   r   r   rE   r  researchr?   r   groupAttributeErrorfrom_spaces_interfacer6   r7   from_spaces_blocks)r  r   r    r%   	space_urlrR   
iframe_urlrl   resultconfigaes              r&   r3   r3     s    >==I	
-)
-
-...G#7X#7#7  	BBBBG	
 	
 	
 
	V   ZZ  Z  Z  Z
 
 	
 	Z111AY6 FLFLLOO,, L L LBjBBCCKL6!!$x
 
?E
 
 	
  	MB   "
XFFFFs   )'C 
C3C..C3r  c                    t          | |          }d |j        D             }t          j                            |j        ||j                  S )N)r   c                    g | ]	}|j         
S r#   )_predict_resolve)rg   endpoints     r&   r   z&from_spaces_blocks.<locals>.<listcomp>  s    NNN8,NNNr(   )r   	endpointsr  r   from_configr  r   )r  r   clientpredict_fnss       r&   r  r    sH    EH---FNNV=MNNNK=$$V]KLLLr(   r  r   r  r   c                   	 t                    | dddi	|d| 	d<   	fd}||n| |_        |d<   t          fi |}d|d	<   t          j        d
i |}|S )Nz/api/predict/zContent-Typezapplication/jsonrO   rN   c                     t          j        d| i          } t          j        |           }t          j        |j                            d                    }d|v rd|d         v rt          d          	 |d         }n%# t          $ r}t          d|           |d }~ww xY wt          d                   d	k    r|d
         }t          d                   d	k    rt          |t                    r|d
         }|S )Nr   r   zutf-8r   429z)Too many requests to the Hugging Face APIzNCould not find 'data' key in response from external Space. Response received: rn   r+   r   )rE   r   r   postr   contentdecoder   KeyErrorr0   r   rF   )r   rH   r  r   ker   r  rR   s        r&   r   z!from_spaces_interface.<locals>.fn  s#   z64.))='EEEH,33G<<==f&/!9!9&'RSSS	F^FF 	 	 	iagii 	
 y!""a''AYFvi !!Q&&:D,
 ,
& AYFs   9B 
B$BB$r   Tr   r#   )r   r  r   r  r   )
rL   r  r    r   r  r%   r   r
  r   rR   s
    `      @@r&   r  r    s     )00F***G12G#7X#7#7       , "-%%JBKF4L&##F##FF; **6**Ir(   )NNNN)r   r   r   r   r   r   r   r   r    r   r!   r   )NNN)
r   r   r   r   r   r   r    r   r!   r   )rL   r   r   r   r    r   )r  r   r   r   r    r   r!   r   )r  r   r   r   r!   r   )rL   r   r  r   r    r   r   r   r  r   r!   r   )/__doc__
__future__r   rE   r  r6   typingr   r   r   gradio_clientr   gradio_client.documentationr   r   r  r	   r
   gradio.contextr   gradio.deprecationr   gradio.exceptionsr   r   gradio.external_utilsr   r   r   r   r   r   gradio.processing_utilsr   r   gradio.blocksr   gradio.interfacer   r'   r$   rC   rK   r2   r3   r  r  r#   r(   r&   <module>r7     su  8 8 # " " " " "  				  * * * * * * * *              I I I I I I I I  $ $ $ $ $ $ $ $ " " " " " " / / / / / / 9 9 9 9 9 9 9 9                C B B B B B B B +$$$$$$******  	 " " " 
 ! ! ! ! !L 	# # # # #L  ( ( (} } } }@
-G -G -G -G`M M M M+ + + + + +r(   