osanseviero HF staff commited on
Commit
a8cd886
1 Parent(s): 9327e83

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import create_repo, upload_file, Repository
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")
19
+ else:
20
+ url = create_repo(dst_id, token=token, organization=org)
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
+ files = listdir(local_dir)
36
+ for f in files:
37
+ if not f.startswith("."):
38
+ upload_file(os.path.join(local_dir, f), f, dst_repo, token=token, repo_type=repo_type)
39
+
40
+ # Clean up to be nice with the environment
41
+ for filename in os.listdir(local_dir):
42
+ file_path = os.path.join(local_dir, filename)
43
+ if os.path.isfile(file_path) or os.path.islink(file_path):
44
+ os.unlink(file_path)
45
+ elif os.path.isdir(file_path):
46
+ shutil.rmtree(file_path)
47
+
48
+ return "done.jpg"
49
+
50
+ interface = gr.Interface(
51
+ fn=fork,
52
+ inputs=[
53
+ gr.inputs.Textbox(placeholder="Source repository (e.g. osanseviero/src)"),
54
+ gr.inputs.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
55
+ gr.inputs.Textbox(placeholder="Write access token"),
56
+ gr.inputs.Dropdown(choices=["model", "dataset", "space"])
57
+ ],
58
+ outputs=["textbox"],
59
+ title="Duplicate your repo!",
60
+ 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.",
61
+ article="<p>Find your write token at <a href='https://huggingface.co/settings/token' target='_blank'>token settings</a></p>",
62
+ allow_flagging=False,
63
+ live=False,
64
+ )
65
+ interface.launch(enable_queue=True)