julien-c HF staff Wauplin HF staff commited on
Commit
f432669
1 Parent(s): e636c5d

Actually build_hf_headers, hf_raise_for_status are already exposed

Browse files

Co-authored-by: Lucain <[email protected]>

Files changed (1) hide show
  1. app.py +5 -23
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import gradio as gr
2
- from huggingface_hub import whoami
3
  import requests
4
- from requests import HTTPError, JSONDecodeError
5
-
6
 
7
  ENDPOINT = "https://huggingface.co"
8
  # ENDPOINT = "http://localhost:5564"
@@ -19,27 +18,10 @@ def duplicate(source_repo, dst_repo, token, repo_type):
19
 
20
  r = requests.post(
21
  f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
22
- headers={"authorization": f"Bearer {token}"},
23
- json={
24
- "repository": dst_repo,
25
- },
26
  )
27
-
28
- ## Following lines:
29
- ## TODO(wauplin)
30
- ## Expose `hf_raise_for_status` from huggingface_hub
31
- ## or even provide a helper to do any API request to hf.co passing just the path and json payload?
32
- try:
33
- r.raise_for_status()
34
- except HTTPError as e:
35
- try:
36
- server_data = r.json()
37
- except JSONDecodeError:
38
- server_data = {}
39
- server_message_from_body = server_data.get("error")
40
- if server_message_from_body:
41
- raise ValueError(server_message_from_body)
42
- raise e
43
 
44
  repo_url = r.json().get("url")
45
 
 
1
  import gradio as gr
 
2
  import requests
3
+ from huggingface_hub import whoami
4
+ from huggingface_hub.utils import build_hf_headers, hf_raise_for_status
5
 
6
  ENDPOINT = "https://huggingface.co"
7
  # ENDPOINT = "http://localhost:5564"
 
18
 
19
  r = requests.post(
20
  f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate",
21
+ headers=build_hf_headers(token=token),
22
+ json={"repository": dst_repo},
 
 
23
  )
24
+ hf_raise_for_status(r)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  repo_url = r.json().get("url")
27