repo_uploader / app.py
Efreak
try to adapt for uploading files...
aa692d6 unverified
raw
history blame
2.21 kB
import gradio as gr
import requests
import subprocess
import os
from huggingface_hub import whoami
from huggingface_hub import HfApi
from huggingface_hub import login
api=HfApi()
REPO_TYPES = ["model", "dataset", "space"]
def duplicate(source_url, dst_repo, token, file_name, dest,repo_type):
try:
_ = whoami(token)
# ^ this will throw if token is invalid
login(token=token)
subprocess.check_call([r"mkdir","-p","downloads"])
os.chdir('downloads')
subprocess.check_call([r"aria2c","-x16","--split=16",source_url])
api.upload_file(
path_or_fileobj=file_name,
path_in_repo=dest,
repo_id=dst_repo,
repo_type=repo_type
)
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 URL (e.g. civitai.com/api/download/models/4324322534)"),
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
gr.Textbox(placeholder="Write access token", type="password"),
gr.Textbox(placeholder="Post-download name of your file, cuz I'm lazy (e.g. stupidmodel.safetensors)"),
gr.Textbox(placeholder="Destination for your file (e.g. /models/Stable-diffusion/stupidmodel.safetensors)"),
gr.Dropdown(choices=REPO_TYPES, value="model"),
],
outputs=[
gr.Markdown(label="output"),
gr.Image(show_label=False),
],
title="Download a file to your repo!",
description="Download a file to your 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'>token settings</a></p>",
allow_flagging="never",
live=False,
)
interface.launch(enable_queue=True)