jcplus commited on
Commit
80aa5c8
·
verified ·
1 Parent(s): e5b0723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -48
app.py CHANGED
@@ -1,75 +1,99 @@
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>"
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
  from huggingface_hub import create_repo, upload_file, Repository, whoami
3
+ import os
4
+ import shutil
5
 
6
  def duplicate(source_repo, dst_repo, token, repo_type):
7
+ # Get username from token
8
+ username = whoami(token=token)["name"]
9
  repo_namespace, dst_id = dst_repo.split("/")
10
+ org = repo_namespace if repo_namespace != username else None
 
 
 
11
 
12
  # Create the destination repo
13
  if repo_type in ["space", "dataset"]:
14
+ url = create_repo(
15
+ repo_id=dst_id,
16
+ token=token,
17
+ organization=org,
18
+ repo_type=repo_type,
19
+ space_sdk="gradio" if repo_type == "space" else None,
20
+ private=False
21
+ )
22
  else:
23
+ url = create_repo(
24
+ repo_id=dst_id,
25
+ token=token,
26
+ organization=org,
27
+ private=False
28
+ )
29
 
30
  # Clone source repo
31
  endpoint = "huggingface.co/"
32
  if repo_type in ["space", "dataset"]:
33
+ endpoint += f"{repo_type}/"
34
  full_path = f"https://{username}:{token}@{endpoint}{source_repo}"
35
+ local_dir = f"hub/{source_repo}"
36
 
37
+ # Clone repository
38
+ repo = Repository(
39
+ local_dir=local_dir,
40
+ clone_from=full_path,
41
+ repo_type=repo_type if repo_type in ["space", "dataset"] else None,
42
+ use_auth_token=token
43
+ )
44
 
45
+ # Upload files
46
+ for root, _, files in os.walk(local_dir):
47
+ if not root.startswith(".") and ".git" not in root:
48
+ for f in files:
49
+ if not f.startswith("."):
50
+ directory_path_in_repo = "/".join(root.split("/")[2:])
 
 
 
51
  path_in_repo = os.path.join(directory_path_in_repo, f)
52
  local_file_path = os.path.join(local_dir, path_in_repo)
53
+ upload_file(
54
+ path_or_fileobj=local_file_path,
55
+ path_in_repo=path_in_repo,
56
+ repo_id=dst_repo,
57
+ token=token,
58
+ repo_type=repo_type if repo_type != "model" else None
59
+ )
60
 
61
+ # Clean up
62
+ shutil.rmtree(local_dir, ignore_errors=True)
 
 
 
 
 
 
63
 
64
+ return f"Find your repo <a href='{url}' target='_blank' style='text-decoration:underline'>here</a>"
65
 
66
+ # Updated Gradio interface
67
  interface = gr.Interface(
68
  fn=duplicate,
69
  inputs=[
70
+ gr.Textbox(
71
+ label="Source repository",
72
+ placeholder="e.g. osanseviero/src"
73
+ ),
74
+ gr.Textbox(
75
+ label="Destination repository",
76
+ placeholder="e.g. osanseviero/dst"
77
+ ),
78
+ gr.Textbox(
79
+ label="Write access token",
80
+ type="password",
81
+ placeholder="Your HF token"
82
+ ),
83
+ gr.Dropdown(
84
+ choices=["model", "dataset", "space"],
85
+ label="Repository type",
86
+ value="model"
87
+ )
88
  ],
89
+ outputs=gr.HTML(),
90
  title="Duplicate your repo!",
91
+ description="Duplicate a Hugging Face repository! You need to specify a write token obtained from https://hf.co/settings/tokens. This Space is an experimental demo.",
92
+ article="""
93
+ <p>Find your write token at
94
+ <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>
95
+ """,
96
+ allow_flagging="never"
97
  )
98
+
99
+ interface.launch()