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 = "

Submit your Hugging Face assignment.

\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 = "

Find your read token at token settings" \ "

" 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)