lysandre HF staff commited on
Commit
83d67af
β€’
1 Parent(s): 707bc6c

Initial commit

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. README.md +3 -6
  3. app.py +51 -72
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md CHANGED
@@ -1,13 +1,10 @@
1
  ---
2
- title: Repo_duplicator
3
  emoji: 😻
4
- colorFrom: gray
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 3.0.10
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
 
1
  ---
2
+ title: Submit
3
  emoji: 😻
4
+ colorFrom: purple
5
  colorTo: blue
6
  sdk: gradio
 
7
  app_file: app.py
8
  pinned: false
9
  license: mit
10
+ ---
 
 
app.py CHANGED
@@ -1,75 +1,54 @@
 
 
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>", "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)
 
1
+ import tempfile
2
+
3
  import gradio as gr
4
+ from huggingface_hub import create_repo, whoami, repo_exists, upload_folder, snapshot_download
5
+ import os
6
+
7
 
8
+ def submit(source_repo, token):
9
+ hfa = os.getenv('HFA_TOKEN')
10
+
11
+ org = "huggingface-assignments"
12
  username = whoami(token)["name"]
13
+
14
+ target_repo = f"{org}/{username}-submission-0"
15
+
16
+ while repo_exists(target_repo, token=hfa):
17
+ dst_split = target_repo.split('-')
18
+ dst_index = int(dst_split[-1])
19
+ dst_split[-1] = str(dst_index + 1)
20
+ target_repo = '-'.join(dst_split)
21
+
22
+ create_repo(target_repo, token=hfa, private=True)
23
+
24
+ with tempfile.TemporaryDirectory() as tmp:
25
+ snapshot_download(source_repo, local_dir=tmp, local_dir_use_symlinks=False)
26
+ upload_folder(repo_id=target_repo, folder_path=tmp, token=hfa)
27
+
28
+ return "success"
29
+
30
+ description = "<h3>Submit your Hugging Face assignment.</h3> \nEnter the source repository (that should be private and only " \
31
+ "accessible to you!) and your read token. \n\nWe'll duplicate it privately so that we may take a look. " \
32
+ "We do not save your read token."
33
+
34
+ article = "<p>Find your read token at <a href='https://huggingface.co/settings/token' target='_blank'>token settings" \
35
+ "</a></p>"
36
+
37
+ with gr.Blocks(title="Submit your assignment!") as demo:
38
+ with gr.Column():
39
+ gr.Markdown(description)
40
+
41
+ with gr.Column():
42
+ source = gr.Textbox(placeholder="Source repository (e.g. lysandre/submission)")
43
+ token = gr.Textbox(placeholder="Read access token")
44
+ with gr.Row():
45
+ button = gr.Button("Submit", variant="primary")
46
+
47
+ with gr.Column():
48
+ output = gr.Markdown()
49
+
50
+ gr.Markdown(article)
51
+
52
+ button.click(submit, inputs=[source, token], outputs=output, concurrency_limit=1)
53
+
54
+ demo.queue(max_size=10).launch(show_api=True)