|
import gradio as gr |
|
from huggingface_hub import whoami |
|
import requests |
|
from requests import HTTPError, JSONDecodeError |
|
|
|
|
|
ENDPOINT = "https://huggingface.co" |
|
|
|
|
|
REPO_TYPES = ["model", "dataset", "space"] |
|
|
|
|
|
def duplicate(source_repo, dst_repo, token, repo_type): |
|
try: |
|
if not repo_type in REPO_TYPES: |
|
raise ValueError("need to select valid repo type") |
|
_ = whoami(token) |
|
|
|
|
|
r = requests.post( |
|
f"{ENDPOINT}/api/{repo_type}s/{source_repo}/duplicate", |
|
headers={"authorization": f"Bearer {token}"}, |
|
json={ |
|
"repository": dst_repo, |
|
}, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
r.raise_for_status() |
|
except HTTPError as e: |
|
try: |
|
server_data = r.json() |
|
except JSONDecodeError: |
|
server_data = {} |
|
server_message_from_body = server_data.get("error") |
|
if server_message_from_body: |
|
raise ValueError(server_message_from_body) |
|
raise e |
|
|
|
repo_url = r.json().get("url") |
|
|
|
return ( |
|
f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>', |
|
"sp.jpg", |
|
) |
|
|
|
except Exception as e: |
|
return ( |
|
f""" |
|
### Error π’π’π’ |
|
|
|
{e} |
|
""", |
|
None, |
|
) |
|
|
|
|
|
interface = gr.Interface( |
|
fn=duplicate, |
|
inputs=[ |
|
gr.Textbox(placeholder="Source repository (e.g. osanseviero/src)"), |
|
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"), |
|
gr.Textbox(placeholder="Write access token"), |
|
gr.Dropdown(choices=REPO_TYPES, value="model"), |
|
], |
|
outputs=[ |
|
gr.Markdown(label="output"), |
|
gr.Image(show_label=False), |
|
], |
|
title="Duplicate your repo!", |
|
description="Duplicate a Hugging Face repository! You need to specify a write token obtained in https://hf.co/settings/tokens. This Space is a an experimental demo.", |
|
article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>tokens settings</a></p>", |
|
allow_flagging="never", |
|
live=False, |
|
) |
|
interface.launch(enable_queue=True) |
|
|