
    ;3h                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZ ddlmZmZ ddlZddl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ZddlmZ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* er$ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6  e       	 	 	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 d%d       Z7	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 d'dZ8	 d(	 	 	 	 	 	 	 	 	 d)dZ9	 d(	 	 	 	 	 	 	 	 	 d*dZ:d+dZ;	 	 	 	 	 	 	 	 	 	 	 	 d,dZ<dZ=dZ>	 	 	 	 	 	 d-dZ? e       	 d(d dd!d"	 	 	 	 	 	 	 	 	 	 	 	 	 d.d#       Z@y)/zThis module should not be used directly as its API is subject to change. Instead,
use the `gr.Blocks.load()` or `gr.load()` functions.    )annotationsN)Callable	Generator)Path)TYPE_CHECKINGLiteral)Client)Endpoint)document)encode_url_or_file_to_base64)version)
componentsexternal_utilsutils)MultimodalValue)Context)GradioVersionIncompatibleErrorTooManyRequestsError)save_base64_to_cache	to_binary)
PROVIDER_T)BlocksChatInterface)MessageDict)LoginButton)	Interfacec                    |||}t        j                  d       C j                  d      }t        |      dk  rt	        d      |d   dj                  |dd        J t        t              sdvrt	        d      |Bd	v r>t        j                  j                  d
      t        j                  j                  d
      }t        t              r
  |fi S |st        d |dS t        |t        j                        rjt        j                  d      5 }|j                  s|j!                          t        j                   |j"                  g      d fd       }	ddd       |S t        j                  d      5 }t        j$                  d      5 }
t        j&                  ddd      }t        j(                  ddd      }t        j*                         }ddd       t        j,                  j.                  g
      d        }t        j,                  |j.                  j0                  g||g|g      d        }t        j,                  |j"                  |g||g      d        }t        j                   |g|j.                  g       fd       }ddd       |S # 1 sw Y   S xY w# 1 sw Y   xY w# 1 sw Y   S xY w)a	  
    Constructs a Gradio app automatically from a Hugging Face model/Space repo name or a 3rd-party API provider. Note that if a Space repo is loaded, certain high-level attributes of the Blocks (e.g. custom `css`, `js`, and `head` attributes) will not be loaded.
    Parameters:
        name: the name of the model (e.g. "google/vit-base-patch16-224") or Space (e.g. "flax-community/spanish-gpt2"). This is the first parameter passed into the `src` function. Can also be formatted as {src}/{repo name} (e.g. "models/google/vit-base-patch16-224") if `src` is not provided.
        src: function that accepts a string model `name` and a string or None `token` and returns a Gradio app. Alternatively, this parameter takes one of two strings for convenience: "models" (for loading a Hugging Face model through the Inference API) or "spaces" (for loading a Hugging Face Space). If None, uses the prefix of the `name` parameter to determine `src`.
        token: optional token that is passed as the second parameter to the `src` function. If not explicitly provided, will use the HF_TOKEN environment variable or fallback to the locally-saved HF token when loading models but not Spaces (when loading Spaces, only provide a token if you are loading a trusted private Space as the token can be read by the Space you are loading). Find your HF tokens here: https://huggingface.co/settings/tokens.
        accept_token: if True, a Textbox component is first rendered to allow the user to provide a token, which will be used instead of the `token` parameter when calling the loaded model or Space. Can also provide an instance of a gr.LoginButton in the same Blocks scope, which allows the user to login with a Hugging Face account whose token will be used instead of the `token` parameter when calling the loaded model or Space.
        kwargs: additional keyword parameters to pass into the `src` function. If `src` is "models" or "Spaces", these parameters are passed into the `gr.Interface` or `gr.ChatInterface` constructor.
        provider: the name of the third-party (non-Hugging Face) providers to use for model inference (e.g. "replicate", "sambanova", "fal-ai", etc). Should be one of the providers supported by `huggingface_hub.InferenceClient`. This parameter is only used when `src` is "models"
    Returns:
        a Gradio Blocks app for the given model
    Example:
        import gradio as gr
        demo = gr.load("gradio/question-answering", src="spaces")
        demo.launch()
    Nz\The `hf_token` parameter is deprecated. Please use the equivalent `token` parameter instead./   zYEither `src` parameter must be provided, or `name` must be formatted as {src}/{repo name}r   )modelsspaceshuggingfacezThe `src` parameter must be one of 'huggingface', 'models', 'spaces', or a function that accepts a model name (and optionally, a token), and returns a Gradio app.)r!   r#   HF_TOKENnamesrchf_tokenproviderT)fill_height)triggersc                D    | d n| j                   }t        d|dS Nr%    )tokenload_blocks_from_huggingface)oauth_tokentoken_valuekwargsr&   r)   r'   s     O/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/external.pycreate_blockszload.<locals>.create_blocksm   s=    &1&9d{?P?P3 (%	
      z Enter your token and press enterpasswordF)type
show_label	containerRemember me on this device)labelvaluer:   )outputsc                 0    t        j                  dd      S )NzToken settingsF)open)gr	Accordionr.   r6   r4   hide_accordionzload.<locals>.hide_accordion   s    ||$45AAr6   )inputsr>   c                l    |r| r| t        j                  dd      fS dt        j                  d      fS )Nz#Remember me on this device (saved!)T)r<   r=    r;   r<   )rA   Checkbox)r2   remember_token_values     r4   
save_tokenzload.<locals>.save_token   s=     (K&C4)   r{{1MNNNr6   c                    | r| dfS y)NT)rF   Fr.   )r2   s    r4   
load_tokenzload.<locals>.load_token   s    &,,$r6   )rD   r+   c                $    t        d| dS r-   )r0   )r2   r3   r&   r)   r'   s    r4   createzload.<locals>.create   s+    3 (%	
  r6   r.   )r1   zgr.OAuthToken | None)warningswarnsplitlen
ValueErrorjoin
isinstancer   osenvirongetr0   rA   r   r   is_renderedrenderloadrB   TextboxrH   BrowserStateonsubmitchange)r&   r'   r/   r(   accept_tokenr)   r3   partsdemor5   	accordiontextboxremember_tokenbrowser_staterC   rJ   rL   rN   s   ``   ``           r4   r[   r[   +   s   8 j	
 {

3u:?k  Ahxxab	"??c8$4W)W q
 	
 	,,JJNN:&2

z*#x 4)&))+ 
3
EK
 	
 
L"..	1YY4( 	D++##%YY, -	 YY4( ,	D@A 	2Y**#$#
 "$6eu" !# 1	2 UUGNN#Y7B 8B UU!6!670&7
O
O UU499m_w>WX% Y% YYwi7>>2BC DK,	\ 	 	2 	2,	\ s3   (A
J8K(AK-CK8KK	
KKc                    |Ct         j                  (t         j                  |k7  rt        j                  d       |t         _        |dk(  r|dn|}t	        | f|||d|}|S t        | f|||d|}|S )zNCreates and returns a Blocks instance from a Hugging Face model or Space repo.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.r"   F)r(   aliasr)   )r   r(   rO   rP   from_spaces
from_model)r&   r'   r(   ri   r)   r3   r/   blockss           r4   r0   r0      s     'G,<,<,HMM F $
h!)x
 
DJ
 M 
#58
GM
 Mr6   c                   ddit        j                  | ||      }t        j                  | |      \  }}d|  t        j
                  j                  d      xs) t        t        t        j                               dz        fd}d d d }	|dk(  rNt        j                  d	d
      }
t        j                  d      }t        j                  dg}	|j                  n|dk(  r4t        j                  d	d
      }
t        j                  d      }dg}	|nl|dk(  rAt        j                  d	d
      }
t        j                   d      }dg}	|j"                  d n&|dk(  rJt        j                   d
      }
t        j$                  d      }|j&                  t(        j*                  n|dk(  rMt        j                   d
      }
t        j                  d      }dg}	t        j,                  |j.                  n|dk(  rNt        j0                  d	d      }
t        j                  d      }t        j                  dg}	|j2                  n2|dk(  r|t        j                   d      t        j                   dd      g}
t        j                   d      t        j                  d       g}d!d"gg}	t        j4                  |j6                  n|d#k(  rAt        j                   d
      }
t        j                   d$      }d%gg}	d& |j8                  nk|d'k(  rMt        j                   d
      }
t        j                  d      }d(g}	t        j                  |j:                  n|d)k(  r|rPd*|v rLd+d,lm} d+d-l m!} t        jD                  |      g d.}	d/|	i}tG        |fi |} |d0d1d23      } |f|d1d4|S t        j                   d5      }
|
}d6g}	t        jH                  |      n|d7k(  r=t        j                   d
      }
t        j                   d8      }d9g}	|jJ                  nN|d:k(  r@t        j                   d
      }
t        j                   d;      }d< d=g}	|jL                  n	|d>k(  rt        j                   d
      t        j                   d?      t        jN                  d@      g}
t        j                  d      }t        j                  g dAg}	t        jP                  |      n|dBk(  rat        j                   dCdDE      t        j                   ddFdGH      g}
t        jR                  dI      }dJdKgg}	t        jT                  |      n|dLk(  r=t        j                   d
      }
t        j                  dM      }d=g}	|jV                  n|dNk(  r=t        j                   d
      }
t        j0                  d      }dOg}	|jX                  n|dPk(  rFt        j                   d
      }
t        jZ                  d      }dQg}	t        j\                  |      nL|dRk(  rat        j0                  d	dS      t        j                   d      g}
t        j                  t        j                  dT      }|j^                  n|dUk(  rft        j0                  d	d      t        j                   d      g}
t        j                  dT      }t        j`                  ddVgg}	|jb                  n{|dWk(  r>t        j0                  d	d      }
t        j                   d8      }dg}	|jd                  n8|dXv rt        jf                  |       }	t        jh                  |	      \  }}	|	r|	ggnd }	t        j$                  dYdZ|tk        |      d[fd\]      }
t        j$                  d^d_d`ga      }t        jl                  n|dbk(  rCt        j0                  d	d      }
t        jn                  dc      }t        jp                  |      nh|ddk(  rUt        j0                  d	d      t        j                   d
      g}
t        j0                  d      }ddegg}	|jr                  ntu        df|       fdg}|xs | |_;        ||
|| |	d\dh}tG        |fi |}ty        jz                  dii |}|S )jNzX-Wait-For-Modeltrue)modelheadersr/   r)   z,https://api-inference.huggingface.co/models/GRADIO_TEMP_DIRgradioc                    t        d| i      } t        j                  d|       }t        t	        j
                  |            S )NpathPOST)rp   content)	cache_dir)r   httpxrequestr   r   encode_to_base64)dataresponseGRADIO_CACHEapi_urlrp   s     r4   custom_post_binaryz&from_model.<locals>.custom_post_binary   sB    &$(=='4P#++H5
 	
r6   zaudio-classificationfilepathInput)r8   r<   ClassrG   zBhttps://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wavzaudio-to-audioOutputzautomatic-speech-recognitionc                    | j                   S N)textxs    r4   <lambda>zfrom_model.<locals>.<lambda>   s
     r6   zfeature-extractionz	fill-maskClassificationzIHugging Face is the AI community, working together, to [MASK] the future.zimage-classificationzInput ImagezAhttps://gradio-builds.s3.amazonaws.com/demo-files/cheetah-002.jpgzquestion-answeringQuestion   r   )linesr<   AnswerScorez3What entity was responsible for the Apollo program?zThe Apollo program, also known as Project Apollo, was the third United States human spaceflight program carried out by the National Aeronautics and Space Administration (NASA), which accomplished landing the first humans on the Moon from 1969 to 1972.summarizationSummarya  The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.c                    | j                   S r   )summary_textr   s    r4   r   zfrom_model.<locals>.<lambda>3  s
     r6   ztext-classificationI feel greatztext-generationconversationalr   r   Chatbot)z What is the capital of Pakistan?zTell me a joke about calculus.z Explain gravity to a 5-year-old.z)What were the main causes of World War I?examplesr    messagesT)scaler8   
allow_tags)chatbotr8   TextzOnce upon a timeztext2text-generationzGenerated Textz)Translate English to Arabic: How are you?translationTranslationc                    | j                   S r   )translation_textr   s    r4   r   zfrom_model.<locals>.<lambda>^  s     2 2 r6   zHello, how are you?zzero-shot-classificationz&Possible class names (comma-separated)zAllow multiple true classes)r   z
happy, sadFzsentence-similarityzSource SentencezEnter an original sentence)r<   placeholderz>Sentences to compare to -- separate each sentence by a newlinezSentences to compare to)r   r   r<   zSimilarity scoreszThat is a happy personzThat person is very happyztext-to-speechAudioztext-to-imagezA beautiful sunsetztoken-classificationzXHugging Face is a company based in Paris and New York City that acquired Gradio in 2021.zdocument-question-answeringzInput DocumentLabelzvisual-question-answeringzWhat animal is in the image?zimage-to-text)ztabular-classificationztabular-regressionz
Input RowspandasfixedF)r<   r8   rp   	col_countrZ   Predictionsarray
prediction)r<   r8   rp   zobject-detectionAnnotationszimage-to-imagez"Photo of a cheetah with green eyeszUnsupported pipeline type: c                      |  } 	  |  }  |       } | S # t         $ r}t        j                  |       Y d }~/d }~ww xY wr   )	Exceptionr   handle_hf_error)r{   efnpostprocess
preprocesss     r4   %query_huggingface_inference_endpointsz9from_model.<locals>.query_huggingface_inference_endpoints  s[    !t$D	.t9D "t$D  	.**1--	.s    	A>A)r   rD   r>   titler   cache_examplesr.   )>huggingface_hubInferenceClientr   get_model_inforV   rW   rX   strr   tempfile
gettempdirr   r   r   postprocess_labelaudio_classificationr\   automatic_speech_recognition	Dataframefeature_extractionr   resolve_singletonpostprocess_mask_tokens	fill_maskImageimage_classificationpostprocess_question_answeringquestion_answeringr   text_classificationrr   r   gradio.componentsr   conversational_wrapperdicttext_generation_wrappertext_generationr   rH    zero_shot_classification_wrapperJSONsentence_similarity_wrappertext_to_speechtext_to_imageHighlightedTexttoken_classification_wrapperdocument_question_answering%postprocess_visual_question_answeringvisual_question_answeringimage_to_textget_tabular_examplescols_to_rowsrR   tabular_wrapperAnnotatedImageobject_detection_wrapperimage_to_imagerS   __name__rA   r   )
model_namer(   ri   r)   r3   clientptagsr   r   rD   r>   r   r   chat_interface_kwargsr   	col_namesr   interface_info	interfacer}   r~   r   rp   r   r   s                       @@@@@@r4   rk   rk      s    "6*G,,'HF ++JAGAt =ZLIG::>>"34 X  "#h.9L
 JKH 	""!!zA""1$66P
 ((	
	!!zA""2P
  	
,	,!!zA$$84P
 00&	
"	"##'2&&X6&&--	
k	##'2"")9:W
 %<<	
$	$!!zG"")9:$66WX((	
"	"Z0Qi8

 X.7+
 FK
 %CC&&	
o	##'2$$95 z

 /!!	
#	###'2"")9:"#$66''	
	$,,166v>BH H%! /:6:FAJ4HG PW:PPP##&1&'33F;	
$	$##'2$$+;<?@##	
m	##'2$$=92)*	
(	(W-%MN&CD

 "")9:$669:<<VD	
#	#'8 \/

 //(;<-/JKL77?	
	##'2""1)*""	
o	##'2""2()!!	
$	$##'2,,8<f
 88@	
+	+*4DEZ0
 %66""1//	
)	)*MBZ0
 ""1$JJ T.
 --	
o	!!zG$$+;<WX!!	
>	>!66zB,99(C	8#+XJ<%%9~w/
 &&g~
 ++	
 	 !!zG++-@44V<	
	*MBW-
 ""2 T4
 ""6qc:;;
 6;5Hj)2 4N .+F+F&v&Ir6   c                   |t        j                  d       d|  }t        d|        i }|dvrd| |d<   t        j                  d|  d|	      j                         j	                  d
      }|t        d|  d      t        j                  |dz   |	      }|j                  dk(  r]t        j                  ||	      }	t        j                  d|	j                        }
	 t        j                  |
j                  d            }n/|j                  dk(  r|j                         }nt        d|  d      d|v rt        | ||||fi |S |rt        j                  d       t        | |      S # t        $ r}t        d|        |d }~ww xY w)NzRThe `provider` parameter is not supported when loading Spaces. It will be ignored.zhttps://huggingface.co/spaces/zFetching Space from: FNBearer Authorizationz"https://huggingface.co/api/spaces/z/host)rp   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 `hf_token` parameter.z/configi  z+window.gradio_config = (.*?);[\s]*</script>r    zCould not load the Space:    z) because the config could not be fetched.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(   )rO   rP   printrx   rX   jsonrS   status_coderesearchr   loadsgroupAttributeErrorfrom_spaces_interfacefrom_spaces_blocks)
space_namer(   ri   r)   r3   	space_urlrp   
iframe_urlconfig_requestrresultconfigaes                r4   rj   rj     s    `	
 1=I	!)
-.G}$%,XJ#7 		0EBG	
 
	V  $ZL  1Z  [
 	
 YYzI5wGN!!S(IIj'2:AFF
	PZZQ0F 
	#	#s	*$$&(4]^
 	
 6!$x
?E
 	
 MMB "
XFF)  	P9*FGRO	Ps   #$E/ /	F8FFc                4   t        | |dd      }|j                  t        j                  d      k  rt	        d|j                   d      g }|j
                  j                         D ]{  \  }}t        |t              st        dt        |             |j                  |      }|j                  r!|j                  |j                  |             k|j                  d        } t        j                   j#                  |j$                  ||j&                        S )NF)r(   download_files_skip_componentsz4.0.0b14zCGradio version 4.x cannot load spaces with versions less than 4.x (z3).Please downgrade to version 3 to load this space.z-Expected endpoint to be an Endpoint, but got )r	   app_versionr   Versionr   	endpointsitemsrU   r
   	TypeErrorr8   
new_helper
backend_fnappendmake_end_to_end_fnrA   r   from_configr   r'   )r   r(   r   predict_fnsfn_indexendpointhelpers          r4   r   r   0  s   	F GOOJ77,QRXRdRdQe f@ @
 	
 K$..446 	%((H-?X?OP  ""8,x::6BCt$	% 99  VZZHHr6   c                   	 t        j                        | dddi	|dvrd| 	d<   	fd}||n| |_        |d<   t        fi |}d	|d
<   t	        j
                  di |}|S )Nz/api/predict/zContent-Typezapplication/jsonr   r   r   c                    t        j                  d| i      } t        j                  |       }t        j                  |j
                  j                  d            }d|v rd|d   v rt        d      	 |d   }t        d         d	k(  r|d
   }t        d         d	k(  rt        |t              r|d
   }|S # t        $ r}t        d|       |d }~ww xY w)Nr{   )rp   r{   zutf-8error429z)Too many requests to the Hugging Face APIzNCould not find 'data' key in response from external Space. Response received: r>   r    r   )r   dumpsrx   postr   rv   decoder   KeyErrorrR   rU   list)r{   r|   r   outputker~   r   rp   s        r4   r   z!from_spaces_interface.<locals>.fn^  s    zz64.)::gwTBH,,33G<=f&/!9&'RSS	F^F y!"a'AYFy!"a'Jvt,DAYF  	`ag`hi	s   6B9 9	CCCr   T	_api_moder.   )r   streamline_spaces_interfacer   r   rA   r   )
r   r   ri   r(   r   r3   r   r   r~   rp   s
    `      @@r4   r   r   O  s     77?FM*G12G}$%,XJ#7 , "-%JBKF4L&#F#FF;&v&Ir6   )=z.docz.docxz.rtfz.epubz.odtz.odpz.pptxz.txtz.mdz.pyz.ipynbz.jsz.jsxz.htmlz.cssz.javaz.csz.phpz.cz.ccz.cppz.cxxz.ctsz.hz.hhz.hppz.rsz.Rz.Rmdz.swiftz.goz.rbz.ktz.ktsz.tsz.tsxz.mz.mmz.mtsz.scalaz.dartz.luaz.plz.pmz.tz.shz.bashz.zshz.batz.coffeez.csvz.logz.iniz.cfgz.configz.jsonz.protoz.yamlz.ymlz.tomlz.sql)z.pngz.jpgz.jpegz.gifz.webpc                t   g }| D ]  }t        |d   t              r|j                  |d   |d   d       1t        |d   t              r/|d   ddt	        |d   d         idgd}|j                  |       st        d|d    d	       t        |t              r|}g }n$|j                  d
d       }|j                  dg       }g g }}|D ]H  }	|	j                         j                  t              r|j                  |	       8|j                  |	       J |D ]%  }
|j                  dddt	        |
      idgd       ' |s|rp|xs d}|dj                  |D 	cg c]4  }	dt        |	      j                   dt        |	      j                          6 c}	      z  }|j                  dd
|dgd       |S c c}	w )Nrv   roler!  rv   	image_urlurlr   )r8   r#  zInvalid message format: z,. Messages must be either strings or tuples.r   filesuserrF   
z
## )r8   r   )rU   r   r  tupler   rS   rX   lowerendswithTEXT_FILE_EXTENSIONSrT   r   r&   	read_text)historynew_messageconversationmessageimage_messager   r%  image_filestext_encodedfileimages              r4   format_conversationr6    s    L gi(#. WY5GH 	*E2 !,!#?	@RST@U#V&
M .*79+=*>>jk ', +s#vt,, "BK %::<  !56%t$	%  
 !,&+-I%-P%Q
	

 |zr		 ) T
(4:+?+?+A*BC
 	
 	&$)G(HI	
 s   9F5
r3  T)
file_typessystem_message	streamingc                  	 ddl m} ddlm  |||       |rd|dgng t        j                  |      }dfd	}		 	 	 	 	 	 dfd
}
g }|D ].  }|dk(  r
|t        z  }|dk(  r
|t        z  }!t        d| d       d|vrddl
m}  |ddd      |d<    |r|
n|	fdt        |      |rt        j                  |      ndd|S # t        $ r}t        d      |d}~ww xY w)aN  
    Load a chat interface from an OpenAI API chat compatible endpoint.
    Parameters:
        base_url: The base URL of the endpoint, e.g. "http://localhost:11434/v1/"
        model: The name of the model you are loading, e.g. "llama3.2"
        token: The API token or a placeholder string if you are using a local model, e.g. "ollama"
        file_types: The file types allowed to be uploaded by the user. "text_encoded" allows uploading any text-encoded file (which is simply appended to the prompt), and "image" adds image upload support. Set to None to disable file uploads.
        system_message: The system message to use for the conversation, if any.
        streaming: Whether the response should be streamed.
        kwargs: Additional keyword arguments to pass into ChatInterface for customization.
    Example:
        import gradio as gr
        gr.load_chat(
            "http://localhost:11434/v1/",
            model="qwen2.5",
            token="***",
            file_types=["text_encoded", "image"],
            system_message="You are a silly assistant.",
        ).launch()
    r   )OpenAIznTo use OpenAI API Client, you must install the `openai` package. You can install it with `pip install openai`.Nr   )api_keybase_urlsystemr"  c                *   |xs }t        |      dkD  r+t        |d   t        t        f      r j                  |      }t        ||       }j                  j                  j                  |      j                  d   j                  j                  S )Nr   )ro   r   )rR   rU   r  r(  _tuples_to_messagesr6  chatcompletionsrN   choicesr0  rv   )r0  r-  r/  r   r   ro   start_messages      r4   open_apizload_chat.<locals>.open_api/  s    *]w<!
71:e} E7m77@G*7G<KK##**% +  WQ	
 WWW	
r6   c              3    K   |xs 	}t        |      dkD  r+t        |d   t        t        f      r j                  |      }t        ||       }j                  j                  j                  |d      }d}|D ]P  }|j                  d   j                  j                  '||j                  d   j                  j                  z  }| R y w)Nr   T)ro   r   streamrF   )rR   rU   r  r(  r@  r6  rA  rB  rN   rC  deltarv   )
r0  r-  r/  rG  r|   chunkr   r   ro   rD  s
         r4   open_api_streamz"load_chat.<locals>.open_api_stream=  s      *]w<!
71:e} E7m77@G*7G<((//! 0 

  	E}}Q%%--9EMM!,22:::	s   BC"-Cr3  r5  zInvalid file type: z$. Must be 'text_encoded' or 'image'.r   r   r   r    T)r8   r   r   )r7  )r8   
multimodalre   )r0  str | MultimodalValuer-  list | Nonereturn
str | None)r0  rL  r-  rM  rN  zGenerator[str, None, None])openair;  ImportErrorgradio.chat_interfacer   r   none_or_singleton_to_listr+  IMAGE_FILE_EXTENSIONSrS   r   r   boolrA   MultimodalTextbox)r=  ro   r/   r7  r8  r9  r3   r;  r   rE  rJ  supported_extensions	file_typer   r   r   rD  s    `            @@@r4   	load_chatrY     sS   B!
 4EH5F;I(~	67r  00<J
 
&1<	# $  	& $88 '! $99 %i[0TU  -#1Ny$(
# $$0DE  y  |
	s   C 	C,C''C,)NNNFN)r&   r   r'   zHCallable[[str, str | None], Blocks] | Literal['models', 'spaces'] | Noner/   rO  r(   rO  ra   zbool | LoginButtonr)   PROVIDER_T | NonerN  r   )NNN)r&   r   r'   r   r(   rO  ri   rO  r)   rZ  rN  r   r   )
r   r   r(   rO  ri   rO  r)   rZ  rN  r   )
r   r   r(   str | None | Literal[False]ri   rO  r)   rZ  rN  r   )r   r   r(   r[  rN  r   )r   r   r   r   ri   rO  r(   r[  r   r   rN  r   )r-  zlist[MessageDict]r.  rL  rN  z
list[dict])r=  r   ro   r   r/   rO  r7  zPLiteral['text_encoded', 'image'] | list[Literal['text_encoded', 'image']] | Noner8  rO  r9  rU  rN  r   )A__doc__
__future__r   r   rV   r   r   rO   collections.abcr   r   pathlibr   typingr   r   rx   r   gradio_clientr	   gradio_client.clientr
   gradio_client.documentationr   gradio_client.utilsr   	packagingr   rr   rA   r   r   r   $gradio.components.multimodal_textboxr   gradio.contextr   gradio.exceptionsr   r   gradio.processing_utilsr   r   $huggingface_hub.inference._providersr   gradio.blocksr   rR  r   gradio.components.chatbotr   gradio.components.login_buttonr   gradio.interfacer   r[   r0   rk   rj   r   r   r+  rT  r6  rY  r.   r6   r4   <module>ro     s  8 #  	 	   /  )     ) 0 <   4 4 @ " D?$35:* 

 ',"&|
|
|
 | | %|  | | |D  "&
	  	
   B #'	eee e  	e eX	 #'	=G=G)=G =G  	=G =G@I>+++ + *	+
 + +\> ~ C >>-B>>B 
 f !%fff f
f f f f fr6   