Spaces:
Sleeping
Sleeping
csukuangfj
commited on
Commit
•
5d41aef
1
Parent(s):
d8c7865
small fixes
Browse files
app.py
CHANGED
@@ -3,40 +3,49 @@ from huggingface_hub import create_repo, whoami, Repository
|
|
3 |
import subprocess
|
4 |
import os, shutil
|
5 |
|
|
|
6 |
def fork(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)
|
10 |
org = None
|
11 |
if repo_namespace != username:
|
12 |
-
|
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 |
-
create_repo(
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
else:
|
20 |
-
create_repo(dst_id, token=token
|
21 |
-
|
22 |
# Clone source repo
|
23 |
endpoint = "https://huggingface.co/"
|
24 |
if repo_type in ["space", "dataset"]:
|
25 |
endpoint += repo_type
|
26 |
full_path = endpoint + "/" + source_repo
|
27 |
local_dir = "hub/" + source_repo
|
28 |
-
|
29 |
if repo_type in ["space", "dataset"]:
|
30 |
# Same as above
|
31 |
-
repo = Repository(
|
|
|
|
|
32 |
else:
|
33 |
repo = Repository(local_dir=local_dir, clone_from=full_path)
|
34 |
-
|
35 |
# Change remote origin
|
36 |
command = f"git remote set-url origin https://user:{token}@huggingface.co/"
|
37 |
if repo_type in ["space", "dataset"]:
|
38 |
# Can we not have to add the s here? Why do we use singular and plural inconsistently?
|
39 |
-
command += repo_type +"s/"
|
40 |
|
41 |
command += dst_repo
|
42 |
subprocess.run(
|
@@ -47,7 +56,7 @@ def fork(source_repo, dst_repo, token, repo_type):
|
|
47 |
check=True,
|
48 |
cwd=local_dir,
|
49 |
)
|
50 |
-
|
51 |
subprocess.run(
|
52 |
"git lfs install --force --local".split(),
|
53 |
stderr=subprocess.PIPE,
|
@@ -56,7 +65,7 @@ def fork(source_repo, dst_repo, token, repo_type):
|
|
56 |
check=True,
|
57 |
cwd=local_dir,
|
58 |
)
|
59 |
-
|
60 |
# Push!
|
61 |
subprocess.run(
|
62 |
"git push --force origin main".split(),
|
@@ -69,24 +78,25 @@ def fork(source_repo, dst_repo, token, repo_type):
|
|
69 |
|
70 |
# Clean up to be nice with the environment
|
71 |
for filename in os.listdir(local_dir):
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
# Same as above...
|
79 |
if repo_type in ["space", "dataset"]:
|
80 |
endpoint += "s"
|
81 |
return endpoint + "/" + dst_repo
|
82 |
|
|
|
83 |
interface = gr.Interface(
|
84 |
fn=fork,
|
85 |
inputs=[
|
86 |
-
gr.
|
87 |
-
gr.
|
88 |
-
gr.
|
89 |
-
gr.
|
90 |
],
|
91 |
outputs=["textbox"],
|
92 |
title="Fork your repo!",
|
@@ -95,4 +105,4 @@ interface = gr.Interface(
|
|
95 |
allow_flagging=False,
|
96 |
live=False,
|
97 |
)
|
98 |
-
interface.launch(
|
|
|
3 |
import subprocess
|
4 |
import os, shutil
|
5 |
|
6 |
+
|
7 |
def fork(source_repo, dst_repo, token, repo_type):
|
8 |
# Creating repos has inconsistent API (https://github.com/huggingface/huggingface_hub/issues/47)
|
9 |
repo_namespace, dst_id = dst_repo.split("/")
|
10 |
username = whoami(token)
|
11 |
org = None
|
12 |
if repo_namespace != username:
|
13 |
+
org = repo_namespace
|
14 |
|
15 |
# Create the destination repo
|
16 |
if repo_type in ["space", "dataset"]:
|
17 |
# For some reason create_repo does not allow repo_type="model"..., even if documentation says
|
18 |
# that's the default.
|
19 |
+
create_repo(
|
20 |
+
dst_id,
|
21 |
+
token=token,
|
22 |
+
organization=org,
|
23 |
+
repo_type=repo_type,
|
24 |
+
space_sdk="gradio",
|
25 |
+
)
|
26 |
else:
|
27 |
+
create_repo(dst_id, token=token)
|
28 |
+
|
29 |
# Clone source repo
|
30 |
endpoint = "https://huggingface.co/"
|
31 |
if repo_type in ["space", "dataset"]:
|
32 |
endpoint += repo_type
|
33 |
full_path = endpoint + "/" + source_repo
|
34 |
local_dir = "hub/" + source_repo
|
35 |
+
|
36 |
if repo_type in ["space", "dataset"]:
|
37 |
# Same as above
|
38 |
+
repo = Repository(
|
39 |
+
local_dir=local_dir, clone_from=full_path, repo_type=repo_type
|
40 |
+
)
|
41 |
else:
|
42 |
repo = Repository(local_dir=local_dir, clone_from=full_path)
|
43 |
+
|
44 |
# Change remote origin
|
45 |
command = f"git remote set-url origin https://user:{token}@huggingface.co/"
|
46 |
if repo_type in ["space", "dataset"]:
|
47 |
# Can we not have to add the s here? Why do we use singular and plural inconsistently?
|
48 |
+
command += repo_type + "s/"
|
49 |
|
50 |
command += dst_repo
|
51 |
subprocess.run(
|
|
|
56 |
check=True,
|
57 |
cwd=local_dir,
|
58 |
)
|
59 |
+
|
60 |
subprocess.run(
|
61 |
"git lfs install --force --local".split(),
|
62 |
stderr=subprocess.PIPE,
|
|
|
65 |
check=True,
|
66 |
cwd=local_dir,
|
67 |
)
|
68 |
+
|
69 |
# Push!
|
70 |
subprocess.run(
|
71 |
"git push --force origin main".split(),
|
|
|
78 |
|
79 |
# Clean up to be nice with the environment
|
80 |
for filename in os.listdir(local_dir):
|
81 |
+
file_path = os.path.join(local_dir, filename)
|
82 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
83 |
+
os.unlink(file_path)
|
84 |
+
elif os.path.isdir(file_path):
|
85 |
+
shutil.rmtree(file_path)
|
86 |
+
|
87 |
# Same as above...
|
88 |
if repo_type in ["space", "dataset"]:
|
89 |
endpoint += "s"
|
90 |
return endpoint + "/" + dst_repo
|
91 |
|
92 |
+
|
93 |
interface = gr.Interface(
|
94 |
fn=fork,
|
95 |
inputs=[
|
96 |
+
gr.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
|
97 |
+
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
|
98 |
+
gr.Textbox(placeholder="Write access token"),
|
99 |
+
gr.Dropdown(choices=["model", "dataset", "space"]),
|
100 |
],
|
101 |
outputs=["textbox"],
|
102 |
title="Fork your repo!",
|
|
|
105 |
allow_flagging=False,
|
106 |
live=False,
|
107 |
)
|
108 |
+
interface.launch()
|