Revamp to use "official" Hub API (#3)
Browse files- Create .gitignore (048dc6e76de70fe5d32a9b56d3b07017240fe0ff)
- Upgrade Gradio to current version? (7f68ab29afe3d4497cb7a1e0775b54f6ffd0af42)
- update url to tokens page (6e29aba0e2728f362c220f35832719014169e76c)
- Update minor deprecated stuff (574ae04fa2786354f0efef9b40c3193cc0409cc8)
- .vscode autoformatting (aaabecfbdf00aae7316d477bfa0107f3f9dbfa63)
- Revamp app.py to use exposed API (bdea1c99f14c68016d314cd5b74f846c3ccac871)
- tiny tweak (e636c5d20aa7869d89817bc371cf7055f0d8ac63)
- Actually build_hf_headers, hf_raise_for_status are already exposed (f432669956f0e1f17b41f5f3dbaa1a1d0cad2d2e)
- .gitignore +1 -0
- .vscode/settings.json +4 -0
- README.md +1 -1
- 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.
|
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 |
-
|
3 |
-
import
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def duplicate(source_repo, dst_repo, token, repo_type):
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
org = repo_namespace
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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.
|
64 |
-
gr.
|
65 |
-
gr.
|
66 |
-
gr.
|
|
|
|
|
|
|
|
|
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/
|
71 |
-
article="<p>Find your write token at <a href='https://huggingface.co/settings/
|
72 |
-
allow_flagging=
|
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)
|