Revamp to use "official" Hub API

#3
by julien-c HF staff - opened
Files changed (4) hide show
  1. .gitignore +1 -0
  2. .vscode/settings.json +4 -0
  3. README.md +1 -1
  4. app.py +48 -61
.gitignore ADDED
@@ -0,0 +1 @@
 
1
+ .env/
.vscode/settings.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "python.formatting.provider": "black"
4
+ }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 😻
4
  colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.0.10
8
  app_file: app.py
9
  pinned: false
10
  license: mit
4
  colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.9.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -1,75 +1,62 @@
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,
62
  inputs=[
63
- gr.inputs.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
64
- gr.inputs.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
65
- gr.inputs.Textbox(placeholder="Write access token"),
66
- gr.inputs.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/token. This Space is a an experimental demo.",
71
- article="<p>Find your write token at <a href='https://huggingface.co/settings/token' target='_blank'>token settings</a></p>",
72
- allow_flagging=False,
73
  live=False,
74
  )
75
- interface.launch(enable_queue=True)
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"
8
+
9
+ REPO_TYPES = ["model", "dataset", "space"]
10
+
11
 
12
  def duplicate(source_repo, dst_repo, token, repo_type):
13
+ try:
14
+ if not repo_type in REPO_TYPES:
15
+ raise ValueError("need to select valid repo type")
16
+ _ = whoami(token)
17
+ # ^ this will throw if token is invalid
 
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
+
28
+ return (
29
+ f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
30
+ "sp.jpg",
31
+ )
32
+
33
+ except Exception as e:
34
+ return (
35
+ f"""
36
+ ### Error 😒😒😒
37
+
38
+ {e}
39
+ """,
40
+ None,
41
+ )
42
 
 
 
 
 
 
 
 
 
 
43
 
44
  interface = gr.Interface(
45
  fn=duplicate,
46
  inputs=[
47
+ gr.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
48
+ gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
49
+ gr.Textbox(placeholder="Write access token"),
50
+ gr.Dropdown(choices=REPO_TYPES, value="model"),
51
+ ],
52
+ outputs=[
53
+ gr.Markdown(label="output"),
54
+ gr.Image(show_label=False),
55
  ],
 
56
  title="Duplicate your repo!",
57
+ 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.",
58
+ article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
59
+ allow_flagging="never",
60
  live=False,
61
  )
62
+ interface.launch(enable_queue=True)