wL i= UdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZmZddlmZddlmZmZmZmZmZmZdd l m!Z!m"Z"m#Z#m$Z$d d l%m&Z&m'Z'e rdd l(m)Z)m*Z*dd l+m,Z,e-Z.ee-e fZ/ee0ee/e.de1e2fZ3ddhZ4ejje6Z7eGddZ8Gdde0Z9dZ:dZ;dZde3de-de-fdZ?d e-ddfd!Z@de0defd"ZAde0defd#ZBde0ddfd$ZCd%ee0efdefd&ZDd'ee0d(eEdeee-ee'ffd)ZFd'e e0d(eEdee e-e e'ffd*ZGd+e0d(eEdeee-e'ffd,ZHd-ee0dee&fd.ZId-e e0de e&fd/ZJd+e0dee&fd0ZKd1d2d%d3de e0fd4ZLiZMeee-ee-feNd5<d6ee-d7ee-ddfd8ZOd6ee-dee-fd9ZPd:edefd;ZQdZRy)?zEContains utilities used by both the sync and async inference clients.N) dataclass)Path) TYPE_CHECKINGAny AsyncIterableBinaryIODictIterableListLiteralNoReturnOptionalUnionoverload) HTTPError)GenerationErrorIncompleteGenerationErrorOverloadedErrorTextGenerationError UnknownErrorValidationError) get_sessionis_aiohttp_availableis_numpy_availableis_pillow_available)ChatCompletionStreamOutputTextGenerationStreamOutput)ClientResponse ClientSessionImager#z text-to-imagezimage-to-imagecreZdZUeed<eed<eeed<eeeeefed<ee ed<eee fed<y)RequestParametersurltaskmodeljsondataheadersN) __name__ __module__ __qualname__str__annotations__rrr r bytesrg/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/inference/_common.pyr%r%EsD H I C= 5dD) ** 5/ #s(^r3r%cFeZdZUdZeeed<ddedeeffd ZxZ S) MimeBytesa& A bytes object with a mime type. To be returned by `_prepare_payload_open_as_mime_bytes` in subclasses. Example: ```python >>> b = MimeBytes(b"hello", "text/plain") >>> isinstance(b, bytes) True >>> b.mime_type 'text/plain' ``` mime_typer*c|t|||}||_t|tr||j|_|SN)super__new__r7 isinstancer6)clsr*r7obj __class__s r4r;zMimeBytes.__new__`s9goc4(! dI &9+< NNCM r3r9) r,r-r.__doc__rr/r0r1r; __classcell__)r?s@r4r6r6Os0 }5Xc]r3r6c8ts tdddl}|S)NzMPlease install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).r)r ImportErroraiohttp)rDs r4_import_aiohttprEks  !ijj Nr3c8ts tdddl}|S)z.Make sure `numpy` is installed on the machine.zGPlease install numpy to use deal with embeddings (`pip install numpy`).rN)rrCnumpy)rGs r4 _import_numpyrHts  cdd Lr3c<ts tdddlm}|S)z,Make sure `PIL` is installed on the machine.zPlease install Pillow to use deal with images (`pip install Pillow`). If you don't want the image to be post-processed, use `client.post(...)` and get the raw response from the server.rr")rrCPILr#r"s r4_import_pil_imagerK}s%   `   Lr3contentreturncyr9r2rLs r4_open_as_mime_bytesrPs9 ?"}((1H ((,,^BR:ST T  $T']O4u u r3c^t|}tj|jS)z\Encode a raw file (image, audio) into base64. Can be bytes, an opened file, a path or a URL.)rPbase64 b64encodedecode)rL raw_bytess r4 _b64_encoderss&#G,I   I & - - //r3default_mime_typect|tr|jdr|St|}|jxs|}t j |j}d|d|S)N)rVrUdata:rvz;base64,)r<r/rarPr7rorprq)rLrtrrr7 encoded_datas r4_as_urlrxsn'3G$6$67W$X$G,I##8'8I##I.557L9+Xl^ 44r3 encoded_imagect}|jtjt j |S)z/Parse a base64-encoded string into a PIL Image.)rKopenrfrgro b64decode)ryr#s r4 _b64_to_imager}s/  E 5::bjj!1!1-!@A BBr3cHtj|jS)acParse bytes from a Response object into a Python list. Expects the response body to be JSON-encoded data. NOTE: This is exactly the same implementation as `_bytes_to_dict` and will not complain if the returned data is a dictionary. The only advantage of having both is to help the user (and mypy) understand what kind of data to expect. r)loadsrqrOs r4_bytes_to_listr ::gnn& ''r3cHtj|jS)acParse bytes from a Response object into a Python dictionary. Expects the response body to be JSON-encoded data. NOTE: This is exactly the same implementation as `_bytes_to_list` and will not complain if the returned data is a list. The only advantage of having both is to help the user (and mypy) understand what kind of data to expect. rrOs r4_bytes_to_dictrrr3c`t}|jtj|S)zParse bytes from a Response object into a PIL Image. Expects the response body to be raw bytes. To deal with b64 encoded images, use `_b64_to_image` instead. )rKr{rfrg)rLr#s r4_bytes_to_imager s&  E 5::bjj) **r3rlcPt|trtj|S|Sr9)r<r1r)r)rls r4_as_dictrs#-h#>4::h LHLr3bytes_output_as_linesdetailsc#bK|D]} t||}||y#t$rYywxYww)z*Used in `InferenceClient.text_generation`.N%_format_text_generation_stream_output StopIterationrr byte_payloadoutputs r4 _stream_text_generation_responsersH .  :<QF  L    s / // ,/,/ctK|23d{} t||}||7#t$rYywxYw6yw)z/Used in `AsyncInferenceClient.text_generation`.Nrrs r4&_async_stream_text_generation_responser'sS 4l :<QF  L    4s486$68 &886 3838rc|jdsy|jdk(r td|jd}t j |j djd}|jdt|d|jdtj|}|s|jjS|S Nsdata:s data: [DONE]z[DONE] signal received.zutf-8rvz/nerror error_type)rastriprrqr)rlstriprstriprb_parse_text_generation_errorrparse_obj_as_instancetokentext)rrpayload json_payloadrs r4rr5s  " "8 ,.566!!'*G::gnnW5<EcI\I\IaIaIc G$[[. 0 C Z'  s A A A,rrc|dk(r t|S|dk(r t|S|dk(r t|S|dk(r t|St |S)N generationincomplete_generation overloaded validation)rrrrr)rrs r4rrsZ\!u%%,,(//\!u%%\!u%%  r3)Sr@rorfr)loggingr^ dataclassesrpathlibrtypingrrrrr r r r r rrrrequestsrhuggingface_hub.errorsrrrrrrutilsrrrr_generated.typesrrrDr r! PIL.Imager#r/UrlTPathTr1rYrZContentTTASKS_EXPECTING_IMAGES getLoggerr,r\r%r6rErHrKrPrsrxr}rrrrboolrrrrrrrrr0rrrrr2r3r4rsL !    _^T5  c4i %w :M N*+;<   8 $  8  < >?  (/ :>  = }-GH HI 88"&8 eC33458, %  ()  u% -. JJ ()J*O?OTabgTh6GI#T(3-c*B%CHY8C=YVZ[^V_YdhY>8C=>T#Y>I(6  8C= Uh r3