Spaces:
Runtime error
Runtime error
Efreak
commited on
Updates
Browse filesUse a separate directory for each file. Allows for 'download.zip'. Also allows saving filename to work, since I need to get it from *somewhere*.
Add errors for missing inputs.
Also give a stupid excuse when there's an error. Mostly we blame the user.
app.py
CHANGED
@@ -5,27 +5,47 @@ import os
|
|
5 |
from huggingface_hub import whoami
|
6 |
from huggingface_hub import HfApi
|
7 |
from huggingface_hub import login
|
|
|
|
|
8 |
|
9 |
api=HfApi()
|
10 |
REPO_TYPES = ["model", "dataset", "space"]
|
11 |
|
12 |
-
def duplicate(source_url, dst_repo, token,
|
13 |
try:
|
14 |
_ = whoami(token)
|
15 |
# ^ this will throw if token is invalid
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
login(token=token)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
subprocess.check_call([r"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
api.upload_file(
|
24 |
-
path_or_fileobj=
|
25 |
-
path_in_repo=
|
26 |
repo_id=dst_repo,
|
27 |
repo_type=repo_type
|
28 |
)
|
|
|
|
|
|
|
|
|
29 |
|
30 |
return (
|
31 |
f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
|
@@ -33,12 +53,20 @@ def duplicate(source_url, dst_repo, token, file_name, dest,repo_type):
|
|
33 |
)
|
34 |
|
35 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
36 |
return (
|
37 |
f"""
|
38 |
### Error 😢😢😢
|
39 |
|
40 |
{e}
|
41 |
-
|
|
|
|
|
|
|
42 |
None,
|
43 |
)
|
44 |
|
@@ -49,8 +77,8 @@ interface = gr.Interface(
|
|
49 |
gr.Textbox(placeholder="Source URL (e.g. civitai.com/api/download/models/4324322534)"),
|
50 |
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
|
51 |
gr.Textbox(placeholder="Write access token", type="password"),
|
52 |
-
gr.Textbox(placeholder="Post-download name of your file,
|
53 |
-
gr.Textbox(placeholder="Destination for your file (e.g. /models/Stable-diffusion/
|
54 |
gr.Dropdown(choices=REPO_TYPES, value="model"),
|
55 |
],
|
56 |
outputs=[
|
|
|
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/"+int(time.time())+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(filetoup)
|
48 |
+
os.rmdir(dir)
|
49 |
|
50 |
return (
|
51 |
f'Find your repo <a href=\'{repo_url}\' target="_blank" style="text-decoration:underline">here</a>',
|
|
|
53 |
)
|
54 |
|
55 |
except Exception as e:
|
56 |
+
blames=["grandma","my boss","your boss","God","you","you. It's *all* your fault.","the pope"]
|
57 |
+
blameweights=(1,1,1,1,4,2,1)
|
58 |
+
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!"]
|
59 |
+
excusesweights=(12,1,1,2,3)
|
60 |
+
excuse=random.choices(excuses,weights=excusesweights)[0]
|
61 |
return (
|
62 |
f"""
|
63 |
### Error 😢😢😢
|
64 |
|
65 |
{e}
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
""" + excuse,
|
70 |
None,
|
71 |
)
|
72 |
|
|
|
77 |
gr.Textbox(placeholder="Source URL (e.g. civitai.com/api/download/models/4324322534)"),
|
78 |
gr.Textbox(placeholder="Destination repository (e.g. osanseviero/dst)"),
|
79 |
gr.Textbox(placeholder="Write access token", type="password"),
|
80 |
+
gr.Textbox(placeholder="Post-download name of your file, if you want it changed (e.g. stupidmodel.safetensors)"),
|
81 |
+
gr.Textbox(placeholder="Destination for your file within your repo. Don't include the filename, end path with a / (e.g. /models/Stable-diffusion/)"),
|
82 |
gr.Dropdown(choices=REPO_TYPES, value="model"),
|
83 |
],
|
84 |
outputs=[
|