File size: 2,356 Bytes
83d67af
 
a8cd886
1c179c5
 
83d67af
 
a8cd886
1c179c5
 
 
 
83d67af
1c179c5
 
83d67af
1c179c5
 
83d67af
1c179c5
83d67af
1c179c5
 
 
 
 
83d67af
1c179c5
83d67af
1c179c5
 
 
 
 
83d67af
1c179c5
83d67af
7e9cddf
 
 
daf4367
 
83d67af
 
 
 
 
 
 
 
 
58a697f
daf4367
83d67af
 
 
 
 
 
 
 
 
 
7e9cddf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import tempfile

import gradio as gr
from huggingface_hub import create_repo, whoami, repo_exists, upload_folder, snapshot_download, logging
from huggingface_hub.utils import disable_progress_bars
import os


disable_progress_bars()
logging.set_verbosity(51)


def submit(source_repo, token):
    try:
        hfa = os.getenv('HFA_TOKEN')

        org = "huggingface-assignments"
        username = whoami(token)["name"]

        target_repo = f"{org}/{username}-submission-0"

        while repo_exists(target_repo, token=hfa):
            dst_split = target_repo.split('-')
            dst_index = int(dst_split[-1])
            dst_split[-1] = str(dst_index + 1)
            target_repo = '-'.join(dst_split)

        create_repo(target_repo, token=hfa, private=True)

        with tempfile.TemporaryDirectory() as tmp:
            snapshot_download(source_repo, local_dir=tmp, local_dir_use_symlinks=False, token=token)
            upload_folder(repo_id=target_repo, folder_path=tmp, token=hfa)
    except Exception:
        return "There was an error. Contact lysandre@huggingface.co for help."

    return "Success! Your submission has been entered."


description = "<h3>Submit your Hugging Face assignment.</h3> \nEnter the source repository (that should be private " \
              "and only " \
              "accessible to you!) and your fine-grained (or read) token. \n\nWe'll duplicate it privately so that we may take a look. " \
              "We do not save your token."

article = "<p>Find your read token at <a href='https://huggingface.co/settings/token' target='_blank'>token settings" \
          "</a></p>"

with gr.Blocks(title="Submit your assignment!") as demo:
    with gr.Column():
        gr.Markdown(description)

        with gr.Column():
            source = gr.Textbox(label='Source repository', placeholder="Source repository (e.g. lysandre/submission)")
            token = gr.Textbox(label='Fine-grained token/Read token with access to the repository', placeholder="Read access token", type="password")
            with gr.Row():
                button = gr.Button("Submit", variant="primary")

        with gr.Column():
            output = gr.Markdown()

        gr.Markdown(article)

    button.click(submit, inputs=[source, token], outputs=output, concurrency_limit=1)

demo.queue(max_size=10).launch(show_api=True)