wL icUdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z m Z mZmZmZmZmZddlZddlmZmZddlmZddlmZdd lmZd d lmZd d lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%d dlm&Z&d dl'm(Z(d dl)m*Z*d dl+m,Z,e&jZe.Z/dZ0dZ1ejddejfZ4GddeZ5GddeZ6dejnfdZ8e gejnfZ9e8a:e9e;d<e8fde9ddfdZed!e?d"e?dejnfd#Z@d$d d%ejejejjfd&d'd(e,d)eEd*e?d+eFd,eFd-eeeGeeeGd.ffd/ee?ee?d.ffdefd0ZHd)eEd1eeEdeEfd2ZId@d3ed4eeEddfd5ZJd6ee#d7eEd3ede#fd8ZKd9ej2deEfd:ZLejdd;ejZNdZOy)Az>Contains utilities to handle HTTP requests in Huggingface Hub.N) lru_cache)quote)AnyCallableListOptionalTupleTypeUnion) HTTPErrorResponse) HTTPAdapter)PreparedRequest)OfflineModeIsEnabled) constants)BadRequestErrorDisabledRepoErrorEntryNotFoundErrorGatedRepoErrorHfHubHTTPErrorRepositoryNotFoundErrorRevisionNotFoundError)logging)JSONDecodeError) SliceFileObj) HTTP_METHOD_TX-Amzn-Trace-Idz x-request-ida  # staging or production endpoint ^https://[^/]+ ( # on /api/repo_type/repo_id /api/(models|datasets|spaces)/(.+) | # or /repo_id/resolve/revision/... /(.+)/resolve/(.+) ) )flagsc6eZdZdZfdZdedeffd ZxZS)UniqueRequestIdAdapterrc t||fi|t|jvrO|jj t xst tj|jt<tt |jj dddkD}tjd|jtd|jd|jd|d y) N authorizationrzRequest z:  z (authenticated: ))super add_headersX_AMZN_TRACE_IDheadersget X_REQUEST_IDstruuiduuid4lenloggerdebugmethodurl)selfrequestkwargs has_token __class__s a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/utils/_http.pyr)z"UniqueRequestIdAdapter.add_headersMs G.v. '// 1/6/B/B

>:J!GKK=Xijsittu v r7returnc<tjr!tjdt | t ||g|i|S#tj$rA}|jjt}|g|jd|d|_ d}~wwxYw)zSCatch any RequestException to append request id to the error message for debugging.zSend: Nz (Request ID: r') rHF_DEBUGr2r3_curlifyr(sendrequestsRequestExceptionr+r,r*args)r6r7rDr8e request_idr:s r;rAzUniqueRequestIdAdapter.sendZs    LL6(7"3!45 6 7<9$9&9 9((  ,,_=J%A166A]:,a#@A   sAB requests.Session: session = requests.Session() session.proxies = {"http": "http://10.10.1.10:3128", "https": "https://10.10.1.11:1080"} return session # Set it as the default session factory configure_http_backend(backend_factory=backend_factory) # In practice, this is mostly done internally in `huggingface_hub` session = get_session() ``` N)rUreset_sessions)rVs r;configure_http_backendrY~s@.r<cdttjtjS)a Get a `requests.Session` object, using the session factory from the user. Use [`get_session`] to get a configured Session. Since `requests.Session` is not guaranteed to be thread-safe, `huggingface_hub` creates 1 Session instance per thread. They are all instantiated using the same `backend_factory` set in [`configure_http_backend`]. A LRU cache is used to cache the created sessions (and connections) between calls. Max size is 128 to avoid memory leaks if thousands of threads are spawned. See [this issue](https://github.com/psf/requests/issues/2766) to know more about thread-safety in `requests`. Example: ```py import requests from huggingface_hub import configure_http_backend, get_session # Create a factory function that returns a Session with configured proxies def backend_factory() -> requests.Session: session = requests.Session() session.proxies = {"http": "http://10.10.1.10:3128", "https": "https://10.10.1.11:1080"} return session # Set it as the default session factory configure_http_backend(backend_factory=backend_factory) # In practice, this is mostly done internally in `huggingface_hub` session = get_session() ```  process_id thread_id)_get_session_from_cacheosgetpid threading get_identrNr<r; get_sessionrcs : #biikYEXEXEZ [[r<c,tjy)zReset the cache of sessions. Mostly used internally when sessions are reconfigured or an SSLError is raised. See [`configure_http_backend`] for more details. N)r^ cache_clearrNr<r;rXrXs '')r<r\r]ctS)z Create a new session per thread using global factory. Using LRU cache (maxsize 128) to avoid memory leaks when using thousands of threads. Cache is cleared when `configure_http_backend` is called. )rUr[s r;r^r^s # $$r<)iiii) max_retriesbase_wait_time max_wait_timeretry_on_exceptionsretry_on_status_codesr4r5rirjrkrl.rmc $t|tr|f}t|tr|f}d}|} d} d|vr6t|dtjt fr|dj } t} |dz } | |dj| | jd||d|} | j|vr| Stjd| jd|d|||kDr| j| S tjd | d |d |dt#j$| t'|| dz} #|$rS} tjd | d |d|t| tjr t!||kDr| Yd} ~ d} ~ wwxYw)a7 Wrapper around requests to retry calls on an endpoint, with exponential backoff. Endpoint call is retried on exceptions (ex: connection timeout, proxy error,...) and/or on specific status codes (ex: service unavailable). If the call failed more than `max_retries`, the exception is thrown or `raise_for_status` is called on the response object. Re-implement mechanisms from the `backoff` library to avoid adding an external dependencies to `hugging_face_hub`. See https://github.com/litl/backoff. Args: method (`Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"]`): HTTP method to perform. url (`str`): The URL of the resource to fetch. max_retries (`int`, *optional*, defaults to `5`): Maximum number of retries, defaults to 5 (no retries). base_wait_time (`float`, *optional*, defaults to `1`): Duration (in seconds) to wait before retrying the first time. Wait time between retries then grows exponentially, capped by `max_wait_time`. max_wait_time (`float`, *optional*, defaults to `8`): Maximum duration (in seconds) to wait before retrying. retry_on_exceptions (`Type[Exception]` or `Tuple[Type[Exception]]`, *optional*): Define which exceptions must be caught to retry the request. Can be a single type or a tuple of types. By default, retry on `requests.Timeout`, `requests.ConnectionError` and `requests.exceptions.ChunkedEncodingError`. retry_on_status_codes (`int` or `Tuple[int]`, *optional*, defaults to `(500, 502, 503, 504)`): Define on which status codes the request must be retried. By default, 5xx errors are retried. **kwargs (`dict`, *optional*): kwargs to pass to `requests.request`. Example: ``` >>> from huggingface_hub.utils import http_backoff # Same usage as "requests.request". >>> response = http_backoff("GET", "https://www.google.com") >>> response.raise_for_status() # If you expect a Gateway Timeout from time to time >>> http_backoff("PUT", upload_url, data=data, retry_on_status_codes=504) >>> response.raise_for_status() ``` > [!WARNING] > When using `requests` it is possible to stream data by passing an iterator to the > `data` argument. On http backoff this is a problem as the iterator is not reset > after a failed call. This issue is mitigated for file objects or any IO streams > by saving the initial position of the cursor (with `data.tell()`) and resetting the > cursor between each call (with `data.seek()`). For arbitrary iterators, http backoff > will fail. If this is a hard constraint for you, please let us know by opening an > issue on [Github](https://github.com/huggingface/huggingface_hub). rNdatar)r4r5z HTTP Error z thrown while requesting r&'z' thrown while requesting z Retrying in z s [Retry /z].rrN) isinstancetypeintioIOBasertellrcseekr7 status_coder2warningraise_for_statusrBConnectionErrorrXtimesleepmin)r4r5rirjrkrlrmr8nb_tries sleep_timeio_obj_initial_posrSresponseerrs r; http_backoffrsH%t,24'-!6 8HJ  Jvf~ <7PQ#F^002mG A  "-v##$67'wHf#HHH##+@@ NN[)=)=(>>WX^W__`ad`ef g+%))+  & j\8*Ak]RTUV : Q7 G *#  NNQse#=fXQseL M#x778 +% &  s8:D73>D77F [!WARNING] > Raises when the request has failed: > > - [`~utils.RepositoryNotFoundError`] > If the repository to download from cannot be found. This may be because it > doesn't exist, because `repo_type` is not set correctly, or because the repo > is `private` and you do not have access. > - [`~utils.GatedRepoError`] > If the repository exists but is gated and the user is not on the authorized > list. > - [`~utils.RevisionNotFoundError`] > If the repository exists but the revision couldn't be find. > - [`~utils.EntryNotFoundError`] > If the repository exists but the entry (e.g. the requested file) couldn't be > find. > - [`~utils.BadRequestError`] > If request failed with a HTTP 400 BadRequest error. > - [`~utils.HfHubHTTPError`] > If request failed for a reason not listed above. z X-Error-CodeX-Error-MessageRevisionNotFoundz Client Error. zRevision Not Found for url: . EntryNotFoundzEntry Not Found for url: GatedRepoz!Cannot access gated repo for url z$Access to this resource is disabled.z!Cannot access repository for url  RepoNotFoundiz+Invalid credentials in Authorization headerNzRepository Not Found for url: z Please make sure you specified the correct `repo_id` and `repo_type`. If you are trying to access a private or gated repo, make sure you are authenticated. For more details, see https://huggingface.co/docs/huggingface_hub/authenticationiz Bad request for z endpoint:z Bad request:iz Forbidden: z Cannot access content at: z2 Make sure your token has the correct permissions.iRangez. Requested range: z. Content-Range: z Content-Range)r{r r+r,ryr5_formatrrrrr7REPO_API_REGEXsearchrrrr.)rrrE error_code error_messagemessage range_headers r;hf_raise_for_statusr\sfjJ?!!# H?%%)).9  ((,,->? + +!--.n=FKghphthtguuvIwwG/(C J ? *!--.n=FKdemeqeqdrrsIttG,gx@a G ; &''(7&@EfgogsgsfttuCvv .'8 )  C '!NN  ,  $$0%%h&6&6&:&:;G''(728<<.BCNN 17HE1 L  ! !S (DQD]&}oZ@cu /7H=1 D  ! !S (x++,LqI0a@AGH  .'8QH?s KJ8KK error_typecustom_messagec0g}|jjd}||j| |j}|jd}|3t |t r|j |n|j||jd}| |D]}d|vs|j|d|D cgc]5} t| jst| j7}} t tj|}dj|} |} | r5| j|jvrd |vr | d| zz } n| d | zz } t|jjtd} | rd | d } n1t|jjt d} | rd | d } | rI| j| jvr)d| vr | j#d}| d| z| |dz} n|  z } || j|| xsd S#t$rY|jjdd}|jr-d|jvr|j|jYwxYwcc} w)Nrerrorerrorsrz Content-Typer%htmlrrz (Request ID: r'z (Amzn Trace ID: )rserver_message)r+r,appendjsonrrlistextendrtextlowerr.stripdictfromkeysjoinr-r*index)rrr server_errors from_headersrorr content_typelinerfinal_error_messagerFrequest_id_message newline_indexs r;rrsM##''(9:L\*0}}!  %&$$U+$$U+(#   ;%!((y)9: ;4AV4CIOODUSY__&VMV}56MYY}-N)...08L8L8NN ^ # 4.#8 8  6N#: : X%%)),;\}}|r|jt||s%|jt|@dj!|S)zConvert a `requests.PreparedRequest` into a curl command (str). Used for debug purposes only. Implementation vendored from https://github.com/ofw/curlify/blob/master/curlify.py. MIT License Copyright (c) 2016 Egor. )curlNz-Xr$zz-Hz{0}: {1}zutf-8ignore)rreadziNz ... [truncated]z-drr%r&)r4sortedr+itemsrformatbodyrrbytesdecodehasattrr1rr5rrr)r7partskvr flat_partss r;r@r@(sS  w~~$E w,,./31 779 'A 4**1a01223 |||| dE ";;wx;8D T6 "'D t9t ;!33D 4dB/011 tW[[! ""EJ(1   eAh '   eAh ' ( 88J r<z%^\s*bytes\s*=\s*(\d*)\s*-\s*(\d*)\s*$original_range resume_sizec|sd|dSd|vrtd|dtj|}|std|d|j \}}|s:|std|dt ||z }d|}|d krtd |d|St |}||z}|r)t |}d|d|}||kDrtd |d|Sd|dS) zB Adjust HTTP Range header to account for resume position. zbytes=-,zMultiple ranges detected - z, not supported yet.zInvalid range format - rzbytes=-rzEmpty new range - ) ValueError RANGE_REGEXmatch RuntimeErrorgroupsrt)rrrstartend new_suffix new_range new_starts r;_adjust_range_headerrTs'  }A&& n6~6HH\]^^   n -E 4^4FaHIIJE3 !88J!LM MX + j\* ?!3I=BC C JE #I #hYKq. s?!3I=BC C I;a  r<)r=N)N)P__doc__rur_rerar}r/ functoolsrshlexrtypingrrrrr r r rBr r requests.adaptersrrequests.modelsrhuggingface_hub.errorsrr%rrrrrrrrrr_fixesr_lfsr_typingr get_loggerrGr2r*r-compileVERBOSErr"rLrPrTBACKEND_FACTORY_TrU__annotations__rYrcrXrtr^Timeoutr| exceptionsChunkedEncodingErrorr.float Exceptionrrrrr@ IGNORECASErrrNr<r;rsE  DDD()+7#"   H % $   **  [< [ ("2"2R!1!112-E*EAY!,=!]a!H\X%%\@* %%%@P@P% %  00P :Nx8 x8 x8 x8  x8  x8tId9os6J0KKLx8!eCHo!56x8x8v  x}  ?(?8C=?TX?DGm^,GmcGmXGmZhGmT% h..% 3% RbjjA2==Q "!#"!S"!XVY]"!r<