Delete scripts/model-downloader-old.py
Browse files- scripts/model-downloader-old.py +0 -369
scripts/model-downloader-old.py
DELETED
@@ -1,369 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
import argparse
|
4 |
-
import subprocess
|
5 |
-
import gradio as gr
|
6 |
-
from urllib.parse import urlparse
|
7 |
-
from modules import scripts, script_callbacks
|
8 |
-
try:
|
9 |
-
from modules.paths_internal import data_path, models_path, extensions_dir
|
10 |
-
except ImportError:
|
11 |
-
from modules.paths import data_path, models_path
|
12 |
-
extensions_dir = os.path.join(data_path, 'extensions')
|
13 |
-
|
14 |
-
sd_path = os.getcwd()
|
15 |
-
ext = '/extensions'
|
16 |
-
no_prev = None
|
17 |
-
addnet_path = None
|
18 |
-
md_path = scripts.basedir()
|
19 |
-
|
20 |
-
parser = argparse.ArgumentParser()
|
21 |
-
parser.add_argument('--ckpt-dir', type=str, default=os.path.join(models_path, 'Stable-diffusion'))
|
22 |
-
parser.add_argument('--vae-dir', type=str, default=os.path.join(models_path, 'VAE'))
|
23 |
-
parser.add_argument('--embeddings-dir', type=str, default=os.path.join(data_path, 'embeddings'))
|
24 |
-
parser.add_argument('--hypernetwork-dir', type=str, default=os.path.join(models_path, 'hypernetworks'))
|
25 |
-
parser.add_argument('--lora-dir', type=str, default=os.path.join(models_path, 'Lora'))
|
26 |
-
parser.add_argument('--lyco-dir', type=str, default=os.path.join(models_path, 'LyCORIS'))
|
27 |
-
args, _ = parser.parse_known_args()
|
28 |
-
|
29 |
-
if not os.path.exists(os.path.join(sd_path, 'html', 'card-no-preview.png')):
|
30 |
-
try:
|
31 |
-
no_prev = os.path.join(md_path, 'images', 'card-no-prev.png')
|
32 |
-
except:
|
33 |
-
pass
|
34 |
-
else:
|
35 |
-
no_prev = os.path.join(sd_path, 'html', 'card-no-preview.png')
|
36 |
-
if not os.path.exists(os.path.join(sd_path, ext, 'sd-webui-additional-networks')):
|
37 |
-
addnet_path = os.path.join(extensions_dir, 'sd-webui-additional-networks')
|
38 |
-
else:
|
39 |
-
addnet_path = os.path.join(sd_path, ext, 'sd-webui-additional-networks')
|
40 |
-
|
41 |
-
checkpoint_path = args.ckpt_dir
|
42 |
-
vae_path = args.vae_dir
|
43 |
-
embedding_path = args.embeddings_dir
|
44 |
-
hypernetwork_path = args.hypernetwork_dir
|
45 |
-
lora_path = args.lora_dir
|
46 |
-
lycoris_path = args.lyco_dir
|
47 |
-
controlnet_path = os.path.join(extensions_dir, 'sd-webui-controlnet')
|
48 |
-
controlnet_model_path = os.path.join(controlnet_path, 'models')
|
49 |
-
|
50 |
-
print(f'Model Downloader v1.0.8 Pre')
|
51 |
-
print('Checking Directories...')
|
52 |
-
if not os.path.exists(checkpoint_path):
|
53 |
-
os.makedirs(checkpoint_path)
|
54 |
-
print ('Creating Checkpoint Folder')
|
55 |
-
if not os.path.exists(hypernetwork_path):
|
56 |
-
os.makedirs(hypernetwork_path)
|
57 |
-
print ('Creating Hypernetwork Folder')
|
58 |
-
if not os.path.exists(embedding_path):
|
59 |
-
os.makedirs(embedding_path)
|
60 |
-
print ('Creating TextualInversion/Embeddings Folder')
|
61 |
-
if not os.path.exists(vae_path):
|
62 |
-
os.makedirs(vae_path)
|
63 |
-
print ('Creating VAE Folder')
|
64 |
-
if not os.path.exists(lora_path):
|
65 |
-
os.makedirs(lora_path)
|
66 |
-
print ('Creating LoRA Folder')
|
67 |
-
if not os.path.exists(lycoris_path):
|
68 |
-
os.makedirs(lycoris_path)
|
69 |
-
print ('Creating LyCORIS Folder')
|
70 |
-
else:
|
71 |
-
pass
|
72 |
-
print('all Directories already Created.')
|
73 |
-
|
74 |
-
def folder(content_type):
|
75 |
-
if content_type == 'Checkpoint':
|
76 |
-
downloadpath = checkpoint_path
|
77 |
-
elif content_type == 'Hypernetwork':
|
78 |
-
downloadpath = hypernetwork_path
|
79 |
-
elif content_type == 'TextualInversion/Embedding':
|
80 |
-
downloadpath = embedding_path
|
81 |
-
elif content_type == 'VAE':
|
82 |
-
downloadpath = vae_path
|
83 |
-
elif content_type == 'LoRA':
|
84 |
-
downloadpath = lora_path
|
85 |
-
elif content_type == 'LyCORIS(LoCon/LoHA)':
|
86 |
-
downloadpath = lycoris_path
|
87 |
-
elif content_type == 'ControlNet Model':
|
88 |
-
downloadpath = controlnet_model_path
|
89 |
-
else:
|
90 |
-
downloadpath = 'Unset, Please Choose your Content Type'
|
91 |
-
return downloadpath
|
92 |
-
|
93 |
-
def get_filename_from_url(url):
|
94 |
-
if url.find('https://civitai.com/')!=-1:
|
95 |
-
convert = '' + url.replace('download/models', 'v1/model-versions')
|
96 |
-
req = requests.get(convert, stream=True)
|
97 |
-
basename, extension = os.path.splitext(req.json()['files'][0]['name'].replace(' ', '_'))
|
98 |
-
else:
|
99 |
-
parse = urlparse(url).path
|
100 |
-
req = parse[parse.rfind('/') + 1:].replace(' ', '_')
|
101 |
-
basename, extension = os.path.splitext(req)
|
102 |
-
return basename, extension
|
103 |
-
|
104 |
-
def get_image_from_url(url):
|
105 |
-
if url.find('https://civitai.com/')!=-1:
|
106 |
-
convert = '' + url.replace('download/models', 'v1/model-versions')
|
107 |
-
req = requests.get(convert, stream=True)
|
108 |
-
imgurl = req.json()['images'][0]['url']
|
109 |
-
else:
|
110 |
-
imgurl = no_prev
|
111 |
-
return imgurl
|
112 |
-
|
113 |
-
def change_name(changename):
|
114 |
-
if changename:
|
115 |
-
filename = gr.Textbox.update(visible=True)
|
116 |
-
else:
|
117 |
-
filename = gr.Textbox.update(visible=False)
|
118 |
-
return filename
|
119 |
-
|
120 |
-
def custom_download_path(custompath):
|
121 |
-
if custompath:
|
122 |
-
downloadpath = gr.Textbox.update(visible=True)
|
123 |
-
else:
|
124 |
-
downloadpath = gr.Textbox.update(visible=False)
|
125 |
-
return downloadpath
|
126 |
-
|
127 |
-
def get_data_from_url(url, downloadpath):
|
128 |
-
try:
|
129 |
-
imgurl = get_image_from_url(url)
|
130 |
-
basename, extension = get_filename_from_url(url)
|
131 |
-
markdown2 = f'''
|
132 |
-
<font size=2>
|
133 |
-
<center><b>Model Information</b><br></center>
|
134 |
-
<b>URL :</b> {url}<br>
|
135 |
-
<b>Folder Path :</b> {downloadpath}<br>
|
136 |
-
<b>File Name :</b> {basename}{extension}<br>
|
137 |
-
'''
|
138 |
-
except:
|
139 |
-
imgurl = no_prev
|
140 |
-
markdown2 = f'''
|
141 |
-
<font size=2>
|
142 |
-
<center><b>Model Information</b><br></center>
|
143 |
-
<b>URL :</b> {url}<br>
|
144 |
-
<b>Folder Path :</b> {downloadpath}<br>
|
145 |
-
<b>File Name :</b> ???
|
146 |
-
'''
|
147 |
-
filename = gr.Textbox.update(basename)
|
148 |
-
image = gr.Image.update(imgurl)
|
149 |
-
download_btn = gr.Button.update(visible=True, variant='primary')
|
150 |
-
out_text = gr.Textbox.update('Ready', visible=True)
|
151 |
-
info = gr.Markdown.update(markdown2)
|
152 |
-
return filename, image, download_btn, out_text, info
|
153 |
-
|
154 |
-
def start_downloading(downloader_type, download_btn, url, downloadpath, filename, addnet, logging, new_folder, preview):
|
155 |
-
complete1 = f'SUCCESS: Download Completed, Saved to\n'
|
156 |
-
complete2 = f'ERROR: File Already Exist in\n'
|
157 |
-
complete3 = 'ERROR: Something went wrong, please try again later'
|
158 |
-
path, extension = get_filename_from_url(url)
|
159 |
-
imgname = f'{filename}.preview.png'
|
160 |
-
if new_folder:
|
161 |
-
target1 = os.path.join(downloadpath, filename)
|
162 |
-
target2 = os.path.join(addnet_path, 'models', 'lora', filename)
|
163 |
-
else:
|
164 |
-
target1 = os.path.join(downloadpath)
|
165 |
-
target2 = os.path.join(addnet_path, 'models', 'lora')
|
166 |
-
final_target = None
|
167 |
-
if addnet:
|
168 |
-
final_target = target2
|
169 |
-
else:
|
170 |
-
final_target = target1
|
171 |
-
back(download_btn)
|
172 |
-
if not os.path.exists(os.path.join(final_target, f'{filename}{extension}')):
|
173 |
-
try:
|
174 |
-
if downloader_type == 'aria2':
|
175 |
-
command = f'aria2c -c -x 16 -s 16 -k 1M --input-file model.txt -d {final_target}'
|
176 |
-
with open('model.txt', 'w') as w:
|
177 |
-
if not url.find('https://civitai.com/')!=-1:
|
178 |
-
w.write(f'{url}\n out={filename}{extension}')
|
179 |
-
else:
|
180 |
-
if preview:
|
181 |
-
imgurl = get_image_from_url(url)
|
182 |
-
w.write(f'{url}\n out={filename}{extension}\n{imgurl}\n out={imgname}')
|
183 |
-
else:
|
184 |
-
w.write(f'{url}\n out={filename}{extension}')
|
185 |
-
if logging:
|
186 |
-
line = subprocess.getoutput(command)
|
187 |
-
yield line
|
188 |
-
print(line)
|
189 |
-
else:
|
190 |
-
line = os.popen(command)
|
191 |
-
for l in line:
|
192 |
-
l = l.rstrip()
|
193 |
-
yield f'{complete1}{final_target}'
|
194 |
-
print(f'{complete1}{final_target}')
|
195 |
-
elif downloader_type == 'requests':
|
196 |
-
if new_folder:
|
197 |
-
os.makedirs(final_target, exist_ok=True)
|
198 |
-
else:
|
199 |
-
pass
|
200 |
-
download = requests.get(url, allow_redirects=True)
|
201 |
-
if not url.find('https://civitai.com/')!=-1:
|
202 |
-
with open(os.path.join(final_target, f'{filename}{extension}'), 'wb') as f:
|
203 |
-
f.write(download.content)
|
204 |
-
else:
|
205 |
-
if preview:
|
206 |
-
imgurl = get_image_from_url(url)
|
207 |
-
img_download = requests.get(str(imgurl), allow_redirects=True)
|
208 |
-
with open(os.path.join(final_target, f'{filename}{extension}'), 'wb') as f:
|
209 |
-
f.write(download.content)
|
210 |
-
with open(os.path.join(final_target, imgname), 'wb') as img:
|
211 |
-
img.write(img_download.content)
|
212 |
-
else:
|
213 |
-
with open(os.path.join(final_target, f'{filename}{extension}'), 'wb') as f:
|
214 |
-
f.write(download.content)
|
215 |
-
yield f'{complete1}{final_target}'
|
216 |
-
print(f'{complete1}{final_target}')
|
217 |
-
except Exception as e:
|
218 |
-
yield f'{e}\n{complete3}'
|
219 |
-
print(f'{e}\n{complete3}')
|
220 |
-
else:
|
221 |
-
yield f'{complete2}{final_target}'
|
222 |
-
print(f'{complete2}{final_target}')
|
223 |
-
|
224 |
-
def back(download_btn):
|
225 |
-
return gr.Button.update(visible=True, variant='secondary')
|
226 |
-
|
227 |
-
def on_ui_tabs():
|
228 |
-
with gr.Blocks() as downloader:
|
229 |
-
with gr.Row():
|
230 |
-
with gr.Column():
|
231 |
-
downloader_type = gr.Radio(
|
232 |
-
label='Downloader Type',
|
233 |
-
choices=[
|
234 |
-
'aria2',
|
235 |
-
'requests'
|
236 |
-
],
|
237 |
-
value='aria2',
|
238 |
-
type='value'
|
239 |
-
)
|
240 |
-
with gr.Row():
|
241 |
-
content_type = gr.Radio(
|
242 |
-
label='1. Choose Content Type',
|
243 |
-
choices=[
|
244 |
-
'Checkpoint',
|
245 |
-
'Hypernetwork',
|
246 |
-
'TextualInversion/Embedding',
|
247 |
-
'VAE',
|
248 |
-
'LoRA',
|
249 |
-
'LyCORIS(LoCon/LoHA)',
|
250 |
-
'ControlNet Model'
|
251 |
-
]
|
252 |
-
)
|
253 |
-
addnet = None
|
254 |
-
if os.path.exists(addnet_path):
|
255 |
-
addnet = gr.Checkbox(label='save to Additional Networks', value=False, visible=True)
|
256 |
-
print('Model Downloader detects Additional Networks, creating checkbox for AddNet.')
|
257 |
-
else:
|
258 |
-
addnet = gr.Checkbox(value=False, visible=False)
|
259 |
-
with gr.Row():
|
260 |
-
with gr.Column():
|
261 |
-
url = gr.Textbox(
|
262 |
-
label='2. Put Link Download Below',
|
263 |
-
max_lines=1, placeholder='Type/Paste URL Here'
|
264 |
-
)
|
265 |
-
downloadpath = gr.Textbox(
|
266 |
-
value='Unset, Please Choose your Content Type',
|
267 |
-
label='Custom Download Path',
|
268 |
-
placeholder='Paste Folder Path Here',
|
269 |
-
visible=False
|
270 |
-
)
|
271 |
-
filename = gr.Textbox(
|
272 |
-
label='Change Filename',
|
273 |
-
placeholder='Filename',
|
274 |
-
visible=False
|
275 |
-
)
|
276 |
-
with gr.Row():
|
277 |
-
logging = gr.Checkbox(label='turn on log', value=False)
|
278 |
-
changename = gr.Checkbox(label='Change Filename', value=False)
|
279 |
-
custompath = gr.Checkbox(label='Custom Download Path', value=False)
|
280 |
-
preview = gr.Checkbox(label='Download Preview', value=True)
|
281 |
-
new_folder = gr.Checkbox(label='Create New Folder', value=False)
|
282 |
-
with gr.Row():
|
283 |
-
with gr.Column():
|
284 |
-
download_btn = gr.Button(
|
285 |
-
'Start Download',
|
286 |
-
visible=False,
|
287 |
-
variant='secondary'
|
288 |
-
)
|
289 |
-
out_text = gr.Textbox(
|
290 |
-
label='Download Result',
|
291 |
-
placeholder='Result',
|
292 |
-
visible=False,
|
293 |
-
)
|
294 |
-
with gr.Row():
|
295 |
-
with gr.Column():
|
296 |
-
info = gr.Markdown(
|
297 |
-
'''
|
298 |
-
<font size=2>
|
299 |
-
<center><b>Model Information</b><br></center>
|
300 |
-
<b>URL :</b><br>
|
301 |
-
<b>Folder Path :</b><br>
|
302 |
-
<b>File Name :</b>
|
303 |
-
'''
|
304 |
-
)
|
305 |
-
with gr.Column():
|
306 |
-
prev_markdown = gr.Markdown('''<font size=2><b>Preview Model :</b>''')
|
307 |
-
image = gr.Image(value=no_prev, show_label=False)
|
308 |
-
image.style(width=156, height=234)
|
309 |
-
with gr.Row():
|
310 |
-
github = gr.Markdown(
|
311 |
-
'''
|
312 |
-
<center><font size=2>Having Issue? |
|
313 |
-
<a href=https://github.com/Iyashinouta/sd-model-downloader/issues>
|
314 |
-
Report Here</a><br>
|
315 |
-
<center><font size=1>Model Downloader v1.0.8 Pre
|
316 |
-
'''
|
317 |
-
)
|
318 |
-
content_type.change(folder, content_type, downloadpath)
|
319 |
-
changename.change(change_name, changename, filename)
|
320 |
-
custompath.change(custom_download_path, custompath, downloadpath)
|
321 |
-
url.change(
|
322 |
-
get_data_from_url,
|
323 |
-
[
|
324 |
-
url,
|
325 |
-
downloadpath
|
326 |
-
],
|
327 |
-
[
|
328 |
-
filename,
|
329 |
-
image,
|
330 |
-
download_btn,
|
331 |
-
out_text,
|
332 |
-
info
|
333 |
-
]
|
334 |
-
)
|
335 |
-
download_btn.click(
|
336 |
-
start_downloading,
|
337 |
-
[
|
338 |
-
downloader_type,
|
339 |
-
download_btn,
|
340 |
-
url,
|
341 |
-
downloadpath,
|
342 |
-
filename,
|
343 |
-
addnet,
|
344 |
-
logging,
|
345 |
-
new_folder,
|
346 |
-
preview
|
347 |
-
],
|
348 |
-
out_text
|
349 |
-
)
|
350 |
-
url.submit(
|
351 |
-
start_downloading,
|
352 |
-
[
|
353 |
-
downloader_type,
|
354 |
-
download_btn,
|
355 |
-
url,
|
356 |
-
downloadpath,
|
357 |
-
filename,
|
358 |
-
addnet,
|
359 |
-
logging,
|
360 |
-
new_folder,
|
361 |
-
preview
|
362 |
-
],
|
363 |
-
out_text
|
364 |
-
)
|
365 |
-
download_btn.click(back, download_btn, download_btn)
|
366 |
-
url.submit(back, url, download_btn)
|
367 |
-
return (downloader, 'Model Downloader', 'downloader'),
|
368 |
-
|
369 |
-
script_callbacks.on_ui_tabs(on_ui_tabs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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