submit / app.py
lysandre's picture
lysandre HF staff
Initial commit
83d67af
raw history blame
No virus
1.88 kB
import tempfile
import gradio as gr
from huggingface_hub import create_repo, whoami, repo_exists, upload_folder, snapshot_download
import os
def submit(source_repo, token):
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)
upload_folder(repo_id=target_repo, folder_path=tmp, token=hfa)
return "success"
description = "<h3>Submit your Hugging Face assignment.</h3> \nEnter the source repository (that should be private and only " \
"accessible to you!) and your read token. \n\nWe'll duplicate it privately so that we may take a look. " \
"We do not save your read 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(placeholder="Source repository (e.g. lysandre/submission)")
token = gr.Textbox(placeholder="Read access token")
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)