Spaces:
Runtime error
Runtime error
File size: 3,437 Bytes
773d305 45d62c0 2e046cf 773d305 2e046cf b99b7f1 2e046cf 0dcf350 2e046cf b99b7f1 2e046cf 4e6838c b99b7f1 4e6838c 2e046cf b99b7f1 0dcf350 2e046cf d491d15 2e046cf d491d15 2e046cf 0dcf350 2e046cf 2f5a34e 2e046cf 773d305 a0ab1c8 2e046cf b99b7f1 773d305 28323ea 2f5a34e 0ca59b4 09488af 773d305 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import gradio as gr
from huggingface_hub import create_repo, whoami, Repository
import subprocess
import os, shutil
def fork(source_repo, dst_repo, token, repo_type):
# Creating repos has inconsistent API (https://github.com/huggingface/huggingface_hub/issues/47)
repo_namespace, dst_id = dst_repo.split("/")
username = whoami(token)
org = None
if repo_namespace != username:
org = repo_namespace
# Create the destination repo
if repo_type in ["space", "dataset"]:
# For some reason create_repo does not allow repo_type="model"..., even if documentation says
# that's the default.
create_repo(dst_id, token=token, organization=org, repo_type=repo_type, space_sdk="gradio")
else:
create_repo(dst_id, token=token, organization=org)
# Clone source repo
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"]:
# Same as above
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)
# Change remote origin
command = f"git remote set-url origin https://user:{token}@huggingface.co/"
if repo_type in ["space", "dataset"]:
# Can we not have to add the s here? Why do we use singular and plural inconsistently?
command += repo_type +"s/"
command += dst_repo
subprocess.run(
command.split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=local_dir,
)
subprocess.run(
"git lfs install --force --local".split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=local_dir,
)
# Push!
subprocess.run(
"git lfs push --force origin main".split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding="utf-8",
check=True,
cwd=local_dir,
)
# Clean up to be nice with the environment
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)
# Same as above...
if repo_type in ["space", "dataset"]:
endpoint += "s"
return endpoint + "/" + dst_repo
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="Fork your repo!",
description="Fork 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) |