NotFungibleIO commited on
Commit
bafe010
1 Parent(s): 5f360b0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
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 = "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(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)