|
import gradio as gr |
|
from huggingface_hub import create_repo, upload_file, Repository |
|
import subprocess |
|
import os, shutil |
|
|
|
def duplicate(source_repo, dst_repo, token, repo_type): |
|
|
|
repo_namespace, dst_id = dst_repo.split("/") |
|
username = whoami(token) |
|
org = None |
|
if repo_namespace != username: |
|
org = repo_namespace |
|
|
|
|
|
if repo_type in ["space", "dataset"]: |
|
|
|
|
|
url = create_repo(dst_id, token=token, organization=org, repo_type=repo_type, space_sdk="gradio") |
|
else: |
|
url = create_repo(dst_id, token=token, organization=org) |
|
|
|
|
|
endpoint = "https://huggingface.co/" |
|
if repo_type in ["space", "dataset"]: |
|
endpoint += repo_type |
|
full_path = endpoint + "/" + source_repo |
|
local_dir = "hub/" + source_repo |
|
|
|
if repo_type in ["space", "dataset"]: |
|
|
|
repo = Repository(local_dir=local_dir, clone_from=full_path, repo_type=repo_type) |
|
else: |
|
repo = Repository(local_dir=local_dir, clone_from=full_path) |
|
|
|
files = listdir(local_dir) |
|
for f in files: |
|
if not f.startswith("."): |
|
upload_file(os.path.join(local_dir, f), f, dst_repo, token=token, repo_type=repo_type) |
|
|
|
|
|
for filename in os.listdir(local_dir): |
|
file_path = os.path.join(local_dir, filename) |
|
if os.path.isfile(file_path) or os.path.islink(file_path): |
|
os.unlink(file_path) |
|
elif os.path.isdir(file_path): |
|
shutil.rmtree(file_path) |
|
|
|
return "done.jpg" |
|
|
|
interface = gr.Interface( |
|
fn=fork, |
|
inputs=[ |
|
gr.inputs.Textbox(placeholder="Source repository (e.g. osanseviero/src)"), |
|
gr.inputs.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"), |
|
gr.inputs.Textbox(placeholder="Write access token"), |
|
gr.inputs.Dropdown(choices=["model", "dataset", "space"]) |
|
], |
|
outputs=["textbox"], |
|
title="Duplicate your repo!", |
|
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.", |
|
article="<p>Find your write token at <a href='https://huggingface.co/settings/token' target='_blank'>token settings</a></p>", |
|
allow_flagging=False, |
|
live=False, |
|
) |
|
interface.launch(enable_queue=True) |