julien-c HF staff commited on
Commit
bdea1c9
1 Parent(s): aaabecf

Revamp app.py to use exposed API

Browse files
Files changed (1) hide show
  1. app.py +60 -55
app.py CHANGED
@@ -1,61 +1,63 @@
1
  import gradio as gr
2
- from huggingface_hub import create_repo, upload_file, Repository, whoami
3
- import subprocess
4
- import os, shutil
 
 
 
 
 
 
 
5
 
6
  def duplicate(source_repo, dst_repo, token, repo_type):
7
- # Creating repos has inconsistent API (https://github.com/huggingface/huggingface_hub/issues/47)
8
- repo_namespace, dst_id = dst_repo.split("/")
9
- username = whoami(token)["name"]
10
- org = None
11
- if repo_namespace != username:
12
- org = repo_namespace
13
 
14
- # Create the destination repo
15
- if repo_type in ["space", "dataset"]:
16
- # For some reason create_repo does not allow repo_type="model"..., even if documentation says
17
- # that's the default.
18
- url = create_repo(dst_id, token=token, organization=org, repo_type=repo_type, space_sdk="gradio", private=False)
19
- else:
20
- url = create_repo(dst_id, token=token, organization=org, private=False)
21
-
22
- # Clone source repo
23
- endpoint = "huggingface.co/"
24
- if repo_type in ["space", "dataset"]:
25
- endpoint += repo_type + "/"
26
- full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
27
- local_dir = "hub/" + source_repo
28
-
29
- if repo_type in ["space", "dataset"]:
30
- # Same as above
31
- repo = Repository(local_dir=local_dir, clone_from=full_path, repo_type=repo_type)
32
- else:
33
- repo = Repository(local_dir=local_dir, clone_from=full_path)
34
-
35
- for root, dirs, files in os.walk(local_dir):
36
- if not root.startswith("."):
37
- if repo_type == "model":
38
- repo_type = None
39
- for f in files:
40
- if not f.startswith("."):
41
- if ".git" not in root:
42
- # remove hub/namespace/reponame
43
- directory_path_in_repo = "/".join(root.split("/")[3:])
44
- path_in_repo = os.path.join(directory_path_in_repo, f)
45
- local_file_path = os.path.join(local_dir, path_in_repo)
46
- print("From: ", local_file_path, " to: ", path_in_repo)
47
- upload_file(path_or_fileobj=local_file_path, path_in_repo=path_in_repo, repo_id=dst_repo, token=token, repo_type=repo_type)
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # Clean up to be nice with the environment
51
- for filename in os.listdir(local_dir):
52
- file_path = os.path.join(local_dir, filename)
53
- if os.path.isfile(file_path) or os.path.islink(file_path):
54
- os.unlink(file_path)
55
- elif os.path.isdir(file_path):
56
- shutil.rmtree(file_path)
57
-
58
- return f"Find your repo <a href='{url}' target=\"_blank\" style=\"text-decoration:underline\">here</a>", "sp.jpg"
59
 
60
  interface = gr.Interface(
61
  fn=duplicate,
@@ -63,13 +65,16 @@ interface = gr.Interface(
63
  gr.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
64
  gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
65
  gr.Textbox(placeholder="Write access token"),
66
- gr.Dropdown(choices=["model", "dataset", "space"])
 
 
 
 
67
  ],
68
- outputs=["html", "image"] ,
69
  title="Duplicate your repo!",
70
  description="Duplicate a Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.",
71
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>tokens settings</a></p>",
72
  allow_flagging="never",
73
  live=False,
74
  )
75
- interface.launch(enable_queue=True)
 
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"
9
+
10
+ REPO_TYPES = ["model", "dataset", "space"]
11
+
12
 
13
  def duplicate(source_repo, dst_repo, token, repo_type):
14
+ try:
15
+ if not repo_type in REPO_TYPES:
16
+ raise ValueError("need to select valid repo type")
17
+ _ = whoami(token)
18
+ # ^ this will throw if token is invalid
 
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
+
46
+ return (
47
+ f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
48
+ "sp.jpg",
49
+ )
50
+
51
+ except Exception as e:
52
+ return (
53
+ f"""
54
+ ### Error 😢😢😢
55
+
56
+ {e}
57
+ """,
58
+ None,
59
+ )
60
 
 
 
 
 
 
 
 
 
 
61
 
62
  interface = gr.Interface(
63
  fn=duplicate,
 
65
  gr.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
66
  gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
67
  gr.Textbox(placeholder="Write access token"),
68
+ gr.Dropdown(choices=REPO_TYPES, value="model"),
69
+ ],
70
+ outputs=[
71
+ gr.Markdown(label="output"),
72
+ gr.Image(show_label=False),
73
  ],
 
74
  title="Duplicate your repo!",
75
  description="Duplicate a Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.",
76
  article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>tokens settings</a></p>",
77
  allow_flagging="never",
78
  live=False,
79
  )
80
+ interface.launch(enable_queue=True)