Spaces:
Runtime error
Runtime error
Commit
·
ab6c524
0
Parent(s):
Duplicate from anonderpling/repo_uploader
Browse files- .gitignore +1 -0
- README.md +15 -0
- app.py +100 -0
- packages.txt +1 -0
- requirements.txt +1 -0
- sp.jpg +0 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env/
|
README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Repo Uploader
|
| 3 |
+
emoji: 😈
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.12.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
duplicated_from: anonderpling/repo_uploader
|
| 12 |
+
python_version: 3.11.2
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import subprocess
|
| 4 |
+
import os
|
| 5 |
+
from huggingface_hub import whoami
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
import random
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
api=HfApi()
|
| 12 |
+
REPO_TYPES = ["model", "dataset", "space"]
|
| 13 |
+
|
| 14 |
+
def duplicate(source_url, dst_repo, token, new_name, dst_repo_path, repo_type):
|
| 15 |
+
try:
|
| 16 |
+
_ = whoami(token)
|
| 17 |
+
# ^ this will throw if token is invalid
|
| 18 |
+
|
| 19 |
+
# make sure the user fills out the other required paths.
|
| 20 |
+
if not dst_repo_path[len(dst_repo_path)-1] == '/':
|
| 21 |
+
raise Exception("Your destination path *must* end with a /")
|
| 22 |
+
if not source_url:
|
| 23 |
+
raise Exception("You haven't chosen a file to download!")
|
| 24 |
+
if not dst_repo:
|
| 25 |
+
raise Exception("You haven't chosen a repo to download to")
|
| 26 |
+
login(token=token)
|
| 27 |
+
|
| 28 |
+
# keep things separate, partly in case people download different files with same name (`download.zip`). Especially, it also allows saving filename to work
|
| 29 |
+
dir="/home/user/apps/downloads/"+str(int(time.time()))+str(random.getrandbits(8))+"/"
|
| 30 |
+
subprocess.check_call([r"mkdir","-p",dir])
|
| 31 |
+
subprocess.check_call([r"aria2c","-x16","--split=16",source_url,"--dir="+dir])
|
| 32 |
+
files=os.listdir(dir)
|
| 33 |
+
|
| 34 |
+
if new_name:
|
| 35 |
+
dst_repo_path=dst_repo_path+new_name
|
| 36 |
+
else:
|
| 37 |
+
dst_repo_path=dst_repo_path+files[0]
|
| 38 |
+
|
| 39 |
+
api.upload_file(
|
| 40 |
+
path_or_fileobj=dir+files[0],
|
| 41 |
+
path_in_repo=dst_repo_path,
|
| 42 |
+
repo_id=dst_repo,
|
| 43 |
+
repo_type=repo_type
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# now clean up
|
| 47 |
+
os.remove(dir+files[0])
|
| 48 |
+
os.rmdir(dir)
|
| 49 |
+
match repo_type:
|
| 50 |
+
case "space":
|
| 51 |
+
repo_url=f"https://hf.co/spaces/{dst_repo}"
|
| 52 |
+
case "dataset":
|
| 53 |
+
repo_url=f"https://hf.co/datasets/{dst_repo}"
|
| 54 |
+
case "model":
|
| 55 |
+
repo_url=f"https://hf.co/{dst_repo}"
|
| 56 |
+
return (
|
| 57 |
+
f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
|
| 58 |
+
"sp.jpg",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
blames=["grandma","my boss","your boss","God","you","you. It's *all* your fault.","the pope"]
|
| 63 |
+
blameweights=(1,1,1,1,4,2,1)
|
| 64 |
+
excuses=["I blame it all on "+random.choices(blames,weights=blameweights)[0],"It's my fault, sorry.","I did it on purpose.","That file doesn't want to be downloaded.","You nincompoop!"]
|
| 65 |
+
excusesweights=(12,1,1,2,3)
|
| 66 |
+
excuse=random.choices(excuses,weights=excusesweights)[0]
|
| 67 |
+
return (
|
| 68 |
+
f"""
|
| 69 |
+
### Error 😢😢😢
|
| 70 |
+
|
| 71 |
+
{e}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
<i>""" + excuse+"</i>",
|
| 76 |
+
None,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
interface = gr.Interface(
|
| 81 |
+
fn=duplicate,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Textbox(placeholder="Source URL (e.g. civitai.com/api/download/models/4324322534)"),
|
| 84 |
+
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
|
| 85 |
+
gr.Textbox(placeholder="Write access token", type="password"),
|
| 86 |
+
gr.Textbox(placeholder="Post-download name of your file, if you want it changed (e.g. stupidmodel.safetensors)"),
|
| 87 |
+
gr.Textbox(placeholder="Destination for your file within your repo. Don't include the filename, end path with a / (e.g. /models/Stable-diffusion/)"),
|
| 88 |
+
gr.Dropdown(choices=REPO_TYPES, value="model"),
|
| 89 |
+
],
|
| 90 |
+
outputs=[
|
| 91 |
+
gr.Markdown(label="output"),
|
| 92 |
+
gr.Image(show_label=False),
|
| 93 |
+
],
|
| 94 |
+
title="Download a file to your repo!",
|
| 95 |
+
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.",
|
| 96 |
+
article="<p>Find your write token at <a href='https://huggingface.co/settings/tokens' target='_blank'>token settings</a></p>",
|
| 97 |
+
allow_flagging="never",
|
| 98 |
+
live=False, # since i keep wondering, this prevents it from running again automatically when an input changes
|
| 99 |
+
)
|
| 100 |
+
interface.launch(enable_queue=True)
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
aria2
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.11.0
|
sp.jpg
ADDED
|