Update main22.py
Browse files
main22.py
CHANGED
|
@@ -5,10 +5,13 @@ import shutil
|
|
| 5 |
import os
|
| 6 |
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
import sys
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
params = {}
|
|
|
|
| 12 |
for arg in sys.argv[1:]:
|
| 13 |
if arg.startswith('--'):
|
| 14 |
key_value = arg[len('--'):].split('=')
|
|
@@ -18,6 +21,10 @@ for arg in sys.argv[1:]:
|
|
| 18 |
|
| 19 |
check_dir = f"{params['dir']}/models/Stable-diffusion"
|
| 20 |
lora_dir = f"{params['dir']}/models/Lora"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def swap(arr, l, r):
|
| 22 |
temp = arr[l]
|
| 23 |
arr[l] = arr[r]
|
|
@@ -231,53 +238,68 @@ def move_model(source_path, target_path):
|
|
| 231 |
|
| 232 |
def download_file(item):
|
| 233 |
# for item in content:
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
# 判断是civitai还是hugging face还是自定义
|
| 246 |
-
# civitai
|
| 247 |
-
if '.' not in file_name:
|
| 248 |
-
file_name = get_civitai_file(result.stdout)
|
| 249 |
-
# 如果定义了名字则重命名
|
| 250 |
-
if item['name'] != '未命名':
|
| 251 |
-
# 需要进行文件名的兼容处理
|
| 252 |
-
temp_name = sanitize_filename(item['name']) + os.path.splitext(file_name)[1]
|
| 253 |
-
os.rename(f"{lora_dir}/{file_name}", f"{lora_dir}/{temp_name}")
|
| 254 |
-
final_name = temp_name
|
| 255 |
-
else:
|
| 256 |
-
final_name = file_name
|
| 257 |
-
source_path = f"{lora_dir}/{final_name}"
|
| 258 |
-
file_size = get_file_size(source_path)
|
| 259 |
-
# 如果path定义了则进行移动
|
| 260 |
-
if item.get('path') and item['path'] != "":
|
| 261 |
-
target_path = item['path']
|
| 262 |
-
move_model(source_path, target_path)
|
| 263 |
-
item['path'] = target_path
|
| 264 |
-
print(file_name + '已下载,重命名为:' + final_name)
|
| 265 |
-
print('移动--', final_name, f'到{target_path}')
|
| 266 |
-
# 如果为checkpoint则进行移动
|
| 267 |
-
elif file_size and 'GB' in file_size:
|
| 268 |
-
move_model(source_path, check_dir)
|
| 269 |
-
item['path'] = check_dir
|
| 270 |
-
print(file_name + '已下载,重命名为:' + final_name)
|
| 271 |
-
print('移动checkpoint--', final_name, f'到{check_dir}文件夹')
|
| 272 |
-
# 如果为lora则直接调用source_path
|
| 273 |
else:
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
|
| 283 |
# download_file(item)
|
|
|
|
| 5 |
import os
|
| 6 |
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
import sys
|
| 8 |
+
import traceback
|
| 9 |
+
from urllib.parse import urlparse
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
params = {}
|
| 14 |
+
cookie=''
|
| 15 |
for arg in sys.argv[1:]:
|
| 16 |
if arg.startswith('--'):
|
| 17 |
key_value = arg[len('--'):].split('=')
|
|
|
|
| 21 |
|
| 22 |
check_dir = f"{params['dir']}/models/Stable-diffusion"
|
| 23 |
lora_dir = f"{params['dir']}/models/Lora"
|
| 24 |
+
|
| 25 |
+
with open(f'/content/cookie.txt', 'r') as readFile:
|
| 26 |
+
cookie=readFile.read()
|
| 27 |
+
|
| 28 |
def swap(arr, l, r):
|
| 29 |
temp = arr[l]
|
| 30 |
arr[l] = arr[r]
|
|
|
|
| 238 |
|
| 239 |
def download_file(item):
|
| 240 |
# for item in content:
|
| 241 |
+
try:
|
| 242 |
+
if selected_mods and item['name'] not in selected_mods:
|
| 243 |
+
return
|
| 244 |
+
download_url = item['downloadLink']
|
| 245 |
+
# huggingface
|
| 246 |
+
match = re.search(r'/([^/]*)$', download_url)
|
| 247 |
+
file_name = match.group(1)
|
| 248 |
+
# 开始下载
|
| 249 |
+
if '.' not in match.group(1):
|
| 250 |
+
if len(cookie)>0:
|
| 251 |
+
cmd = ['aria2c', '--header', f'Cookie:{cookie}', '--console-log-level=error', '-c', '-x', '16', '-s', '16', '-k', '1M', download_url, '-d', lora_dir]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
else:
|
| 253 |
+
cmd = f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir}"
|
| 254 |
+
else:
|
| 255 |
+
cmd = f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {download_url} -d {lora_dir} -o {file_name}"
|
| 256 |
+
result = subprocess.run(cmd,stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
| 257 |
+
if result.returncode == 0:
|
| 258 |
+
# 判断是civitai还是hugging face还是自定义
|
| 259 |
+
# civitai
|
| 260 |
+
if '.' not in file_name:
|
| 261 |
+
file_name = get_civitai_file(result.stdout)
|
| 262 |
+
# 如果定义了名字则重命名
|
| 263 |
+
if item['name'] != '未命名':
|
| 264 |
+
# 需要进行文件名的兼容处理
|
| 265 |
+
temp_name = sanitize_filename(item['name']) + os.path.splitext(file_name)[1]
|
| 266 |
+
os.rename(f"{lora_dir}/{file_name}", f"{lora_dir}/{temp_name}")
|
| 267 |
+
final_name = temp_name
|
| 268 |
+
else:
|
| 269 |
+
final_name = file_name
|
| 270 |
+
source_path = f"{lora_dir}/{final_name}"
|
| 271 |
+
file_size = get_file_size(source_path)
|
| 272 |
+
# 如果path定义了则进行移动
|
| 273 |
+
if item.get('path') and item['path'] != "":
|
| 274 |
+
target_path = item['path']
|
| 275 |
+
move_model(source_path, target_path)
|
| 276 |
+
item['path'] = target_path
|
| 277 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 278 |
+
print('移动--', final_name, f'到{target_path}')
|
| 279 |
+
# 如果为checkpoint则进行移动
|
| 280 |
+
elif file_size and 'GB' in file_size:
|
| 281 |
+
move_model(source_path, check_dir)
|
| 282 |
+
item['path'] = check_dir
|
| 283 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 284 |
+
print('移动checkpoint--', final_name, f'到{check_dir}文件夹')
|
| 285 |
+
# 如果为lora则直接调用source_path
|
| 286 |
+
else:
|
| 287 |
+
item['path'] = lora_dir
|
| 288 |
+
print(file_name + '已下载,重命名为:' + final_name)
|
| 289 |
+
# 改变item['name']用于最小化更新的重命名判断
|
| 290 |
+
item['name'] = final_name
|
| 291 |
+
oldCo.append(item)
|
| 292 |
+
else:
|
| 293 |
+
if "login" in get_civitai_file(result.stdout):
|
| 294 |
+
print("===============================================================================\n ")
|
| 295 |
+
print(f"{item['name']}下载失败,需要登录civitai进行下载,请按照如下步骤获取cookie填入")
|
| 296 |
+
print("1.ctrl+t打开空白页,按f12选择\"网络\"")
|
| 297 |
+
print(f"2.粘贴{item['downloadLink']}到链接输入栏进行跳转同时观察包含http s://civitai.com/api/download 的这个请求")
|
| 298 |
+
print(f"3.将请求头的cookie复制下来在cookie的输入框中填入,重新执行2.2进行下载\n ")
|
| 299 |
+
print("===============================================================================")
|
| 300 |
+
print(f"{item['name']}下载失败,请检查{item['downloadLink']}")
|
| 301 |
+
except Exception as e:
|
| 302 |
+
traceback.print_exc()
|
| 303 |
|
| 304 |
|
| 305 |
# download_file(item)
|