File size: 13,668 Bytes
199c9f1
 
 
 
 
 
3c1994d
199c9f1
 
67a9379
c064f6d
c0b5253
 
 
bc81231
cb7f1f8
c0b5253
a44c49e
 
 
 
 
 
 
 
 
8229ac1
02318b6
a44c49e
 
 
 
9c62b8a
bc81231
bad97bf
 
ae48e35
eaf90c5
193ae46
bad97bf
 
 
eaf90c5
 
bad97bf
 
c0b5253
 
 
 
4ec02fc
c0b5253
 
 
4ec02fc
c0b5253
 
 
 
4ec02fc
c0b5253
f63f482
 
 
 
3c1994d
 
 
c0b5253
f63f482
bc81231
f63f482
7cf27f7
 
b9dc260
7cf27f7
 
b9dc260
7cf27f7
 
b9dc260
7cf27f7
 
b9dc260
7cf27f7
 
c0b5253
3c1994d
bc81231
 
472eb28
 
 
 
 
 
6a9db3c
 
 
 
 
 
 
 
a9e51aa
 
 
 
 
 
 
 
bc81231
 
 
 
 
cb7f1f8
bc81231
 
 
cb7f1f8
 
bc81231
 
 
 
 
 
 
 
3de9df6
bc81231
 
 
 
 
 
3de9df6
 
 
bc81231
 
 
3de9df6
cb7f1f8
 
 
 
 
67a9379
cb7f1f8
 
752a58a
 
 
 
 
 
 
 
 
 
4765698
752a58a
 
c0b5253
30ac3d6
 
 
c064f6d
c0b5253
 
b175493
d07c1be
4a84e19
 
b175493
 
5d13632
4a84e19
c951390
 
 
 
5f6a1d7
e0ac88f
40e81d5
c951390
 
 
 
 
 
bb6880b
c951390
 
 
 
 
5d13632
 
5f6a1d7
e0ac88f
40e81d5
d07c1be
aad18e4
5d13632
c0b5253
02f5c07
d07c1be
c0b5253
bb6880b
5d13632
c0b5253
 
3245f8f
c0b5253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3245f8f
c0b5253
 
 
 
 
 
 
3245f8f
c0b5253
 
 
 
 
 
 
3245f8f
c0b5253
 
 
 
 
 
 
bad97bf
 
 
 
 
 
 
 
ffe88fb
bad97bf
 
 
 
 
 
 
ffe88fb
bad97bf
ffe88fb
bad97bf
 
ffe88fb
bad97bf
 
 
 
 
 
 
ffe88fb
bad97bf
ffe88fb
bad97bf
c0b5253
bad97bf
 
1d279cc
c0b5253
 
 
 
 
bad97bf
c0b5253
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# SD 插件路径 /tmp/code/sd/stable-diffusion-webui/extensions
# SD embeddings路径 /tmp/code/sd/stable-diffusion-webui/embeddings
# SD VAE路径 /tmp/code/sd/stable-diffusion-webui/models/VAE
# SD Lora路径 /tmp/code/sd/stable-diffusion-webui/models/Lora
# SD 模型路径 /tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion

# comfyui 模型、VAE、Lora由SD文件路径共享
# comfyui 插件路径 /tmp/code/sd/ComfyUI/custom_nodes

## 为更改文件内容后就能被执行,本py文件会每30秒调用执行一次,做好判断
print('--------------远程执行PY---------------------')
 
import os
import subprocess
import configparser
import shutil

def install_aria2():
    try:
        # 检查 aria2c 是否已安装
        subprocess.run(['which', 'aria2c'], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        print("aria2c 已安装")
    except subprocess.CalledProcessError:
        # aria2c 未安装,尝试自动安装
        print("aria2c 未安装,正在尝试自动安装...")
        if 'ubuntu' in subprocess.check_output(['lsb_release', '-i']).decode().lower():
            os.system('apt install aria2 -y')
            print("aria2c 安装成功")
        else:
            print("无法自动检测您的操作系统,请手动安装 aria2。")
install_aria2()
print('--------------开始---------------------')


#SD插件下载
git_sd = [
    # 'https://github.com/2575044704/stable-diffusion-webui-localization-zh_CN2.git', #汉化
    'https://github.com/AlUlkesh/stable-diffusion-webui-images-browser', #图库浏览器
    'https://github.com/qilan2/sd-webui-lobe-theme.git',# lobe主题
]
#comfyui插件下载
git_comfyui = [
    'https://github.com/AIGODLIKE/AIGODLIKE-COMFYUI-TRANSLATION',#中文插件
    'https://github.com/talesofai/comfyui-browser'#查看器
]

# 包含文件名和下载 URL 的列表,格式为 ['文件名:下载URL', ...]
#Lora下载

Lora_url = [
    #'文件名:URL'
]
#embeddings下载
embeddings_url = [
    #'文件名:URL'
    
]
#VAE下载
VAE_url = [
    #'文件名:URL'
]
# 下载后空间数据可保存1个月
#国内环境 civitai 无法访问得走镜像  civitai下载得带上token  
#有时效的url也不行,因为12个号每个号运行只有2小时,要全部号环境下都有得过24小时,所有号才都下载了有时效的url会无法完成所有号的下载。
#但是如果下载过慢30-60分钟才下载一个那消耗的时间将不止24小时,2小时下载没完成在执行会断点延续继续下载。
#civitai 下载很慢 6G模型差不多下个40分钟

#模型下载 
mode_url = [
    # '动漫-NoobAI-XL_Epsilon-pred0.5-Version.safetensors:https://civitai.work/api/download/models/968495?token=51cb08c8db76338ef7ed18bc5f0954c0',
    # '动漫-NoobAIXLv1.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/c5f2b34d-2890-4f76-b7d4-475d27ee59a0/width=450/azuuru,%20%7Btorino%20aqua%7D,%20%7B%7B%7Balternate%20costume%20%7D%7D%7D,momoko_(momopoco)%20,mana_(remana)%20s-1818306228%20(6).jpeg',

    '动漫-NoobAI-XL_Epsilon-pred0.5-Version.safetensors:https://civitai.work/api/download/models/968495?token=51cb08c8db76338ef7ed18bc5f0954c0',
    '动漫-NoobAI-XL_Epsilon-pred0.5-Version.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/a0de8882-e895-4e39-acee-1c2f3c7bd2e5/original=true,quality=90/1234.jpeg',

    '动漫-NoobAI-XL_Epsilon-pred0.75-Version.safetensors:https://civitai.work/api/download/models/998979?token=51cb08c8db76338ef7ed18bc5f0954c0',
    '动漫-NoobAI-XL_Epsilon-pred0.75-Version.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/4131eb2e-b0e4-46be-a262-74b9ce29fa3a/original=true,quality=90/00056-3015447491.jpeg',

    '动漫-NoobAI-XL_Epsilon-pred.1.0-Version.safetensors:https://civitai.work/api/download/models/1022833?token=51cb08c8db76338ef7ed18bc5f0954c0',
    '动漫-NoobAI-XL_Epsilon-pred1.0-Version.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/c5f2b34d-2890-4f76-b7d4-475d27ee59a0/original=true,quality=90/azuuru,%20%7Btorino%20aqua%7D,%20%7B%7B%7Balternate%20costume%20%7D%7D%7D,momoko_(momopoco)%20,mana_(remana)%20s-1818306228%20(6).jpeg',

    '动漫-NoobAI-XL_V-Pred-0.5-Version.safetensors:https://civitai.work/api/download/models/1046043?token=51cb08c8db76338ef7ed18bc5f0954c0',
    '动漫-NoobAI-XL_V-Pred-0.5-Version.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/53a34e1f-deb1-4922-9d32-25b5f1ef1b49/original=true,quality=90/bc309540ec8002f2c9b1449bf0f854eb.jpeg',

    '动漫-NoobAI-XL_V-Pred-0.6-Version.safetensors:https://civitai.work/api/download/models/1070239?token=51cb08c8db76338ef7ed18bc5f0954c0',
    '动漫-NoobAI-XL_V-Pred-0.6-Version.jpeg:https://image.civitai.work/xG1nkqKTMzGDvpLrqFT7WA/b70000f1-f004-4f63-9302-c9a2bed40ab8/original=true,quality=90/e2eb9dd54f5c828c3d077d9f9616adc8.jpeg'
]

# openi 数据下载 数据集必须为公开数据集
openi_datasets = [
        # {
        #     "dataset_name": "qilan2/SD-QL",# 数据集路径
        #     "file_name": "SD模型3.zip",# 数据集名
        #     "save_path": "/tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion/",# 解压路径结尾带/
        #     "extract": True, # 是否解压文件 True  False
        #     "use_unzip": True # unzip解压 True 7z解压 False
        # },
    {
            "dataset_name": "qilan/sd",
            "file_name": "sd模型.zip",
            "save_path": "/tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion/",
            "extract": True,
            "use_unzip": False 
        }
    # ,
    # {
    #         "dataset_name": "qilan/sd",
    #         "file_name": "Noobai.zip",
    #         "save_path": "/tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion/",
    #         "extract": True,
    #         "use_unzip": True 
    #     }
    ]



def download_openi(datasets):
    for dataset in datasets:
        dataset_name = dataset["dataset_name"]
        file_name = dataset["file_name"]
        save_path = os.path.join(".", dataset["save_path"])
        extract = dataset.get("extract", True)
        use_unzip = dataset.get("use_unzip", True)

        if not os.path.exists(save_path):
            print(f"保存路径 {save_path} 不存在,跳过下载 {file_name}。")
            continue

        config = configparser.ConfigParser()
        config.read(os.path.join(save_path, 'time.ini'))

        if config.has_option('downloaded_files', file_name):
            print(f"配置文件中已存在 {file_name},跳过下载。")
            continue

        os.system(f'openi dataset download {dataset_name} {file_name}  --cluster NPU --save_path {save_path}')
        print(f"数据集 {file_name} 已下载到 {save_path} 目录。")

        if not config.has_section('downloaded_files'):
            config.add_section('downloaded_files')
        config.set('downloaded_files', file_name, 'True')
        with open(os.path.join(save_path, 'time.ini'), 'w') as configfile:
            config.write(configfile)

        # 解压文件
        if extract:
            extract_path = os.path.join(save_path, os.path.splitext(file_name)[0])
            if not os.path.exists(extract_path):
                os.makedirs(extract_path)
            if use_unzip:
                os.system(f'unzip -j {os.path.join(save_path, file_name)} -d {extract_path}')
            else:
                os.system(f'7z x {os.path.join(save_path, file_name)} -o{extract_path} -aoa')

            for root, dirs, files in os.walk(extract_path):
                for file in files:
                    src_path = os.path.join(root, file)
                    dst_path = os.path.join(save_path, file)
                    shutil.move(src_path, dst_path)
                for dir in dirs:
                    src_path = os.path.join(root, dir)
                    dst_path = os.path.join(save_path, dir)
                    shutil.move(src_path, dst_path)

            shutil.rmtree(extract_path)
            print(f"数据集 {file_name} 已解压到 {save_path} 目录。")

        os.remove(os.path.join(save_path, file_name))
        print(f"已删除压缩包文件 {file_name}。")


def download_aria2(file_name, download_dir, url):
    download_path = os.path.join(download_dir, file_name)
    aria2_path = download_path + ".aria2"
    
    # 检查文件是否已经下载完成
    if os.path.exists(download_path) and not os.path.exists(aria2_path):
        print(f'文件 {file_name} 已存在, URL: {url}')
        return
    
    # 如果存在 .aria2 文件, 继续断点下载
    if os.path.exists(aria2_path):
        print(f'文件 {file_name} 正在下载, 继续断点下载, URL: {url}')
        aria2_args = (
            f'aria2c --dir {download_dir} --out {file_name} '
            # '--continue=true'
            '--summary-interval=15 '#--console-log-level=error  
            '--header="Referer: https://www.liblib.art/modelinfo/0079e26dfd8a447fbb5553bda66758a8?from=search"'
            '-c -x 16 -s 16 '
            '{url}'
        )
        try:
            os.system(aria2_args.format(url=url))
            print(f'已成功下载: {file_name}')
            # os.system(f'rm -r {aria2_path}')
        except Exception as e:
            print(f'下载 {url} 失败: {e}')
        return
    
    # 如果没有 .aria2 文件且没有下载完成的文件, 则开始新的下载
    aria2_args = (
        f'aria2c --dir {download_dir} --out {file_name} '
        # '--continue=true'
        '--summary-interval=15 '#--console-log-level=error  
        '--header="Referer: https://www.liblib.art/modelinfo/0079e26dfd8a447fbb5553bda66758a8?from=search"'
        '-c -x 16 -s 16 '
        '{url}'
    )
    try:
        # print(f'aria2c --dir {download_dir} --out {file_name} --max-connection-per-server=10 --split=10 --min-split-size=1M --max-overall-download-limit=0 --max-download-limit=0 --continue=true {url}')
        os.system(aria2_args.format(url=url))
        print(f'已成功下载: {file_name}')
        # os.system(f'rm -r {aria2_path}')
    except Exception as e:
        print(f'下载 {url} 失败: {e}')

def download_Lora(file_urls, download_dir='/tmp/code/sd/stable-diffusion-webui/models/Lora'):
    """
    下载文件到指定目录,并支持替换文件名。如果文件已存在,则不进行下载。
    
    参数:
    file_urls (list): 包含文件名和下载 URL 的列表,格式为 ['文件名:下载URL', ...]
    download_dir (str): 下载文件的目标目录
    """
    # 创建目标目录
    os.makedirs(download_dir, exist_ok=True)
    # 下载文件
    for file_url in file_urls:
        try:
            file_name, url = file_url.split(':', 1)
            download_aria2(file_name, download_dir, url)
        except ValueError:
            print(f'无效的文件 URL 格式: {file_url}')
def download_embeddings(file_urls, download_dir='/tmp/code/sd/stable-diffusion-webui/embeddings'):
    os.makedirs(download_dir, exist_ok=True)
    for file_url in file_urls:
        try:
            file_name, url = file_url.split(':', 1)
            download_aria2(file_name, download_dir, url)
        except ValueError:
            print(f'无效的文件 URL 格式: {file_url}')
def download_VAE(file_urls, download_dir='/tmp/code/sd/stable-diffusion-webui/models/VAE'):
    os.makedirs(download_dir, exist_ok=True)
    for file_url in file_urls:
        try:
            file_name, url = file_url.split(':', 1)
            download_aria2(file_name, download_dir, url)
        except ValueError:
            print(f'无效的文件 URL 格式: {file_url}')
def download_mode(file_urls, download_dir='/tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion'):
    os.makedirs(download_dir, exist_ok=True)
    for file_url in file_urls:
        try:
            file_name, url = file_url.split(':', 1)
            download_aria2(file_name, download_dir, url)
        except ValueError:
            print(f'无效的文件 URL 格式: {file_url}')
def clone_repos_sd(repo_urls, clone_dir='/tmp/code/sd/stable-diffusion-webui/extensions'):
    """
    克隆 GitHub 仓库到指定目录
    参数:
    repo_urls (list): GitHub 仓库 URL 列表
    clone_dir (str): 克隆仓库的目标目录
    """
    # 创建目标目录
    os.makedirs(clone_dir, exist_ok=True, mode=0o755)
    for repo_url in repo_urls:
        try:
            repo_name = repo_url.rstrip('/').split('/')[-1]
            clone_path = os.path.join(clone_dir, repo_name)
            if os.path.exists(clone_path):
                print(f'仓库 {repo_name} 已存在, URL: {repo_url}')
                continue
            os.system(f'git clone {repo_url} {clone_path}')
            print(f'已成功克隆仓库: {repo_name}')
        except Exception as e:
            print(f'克隆 {repo_url} 失败: {e}')
def clone_repos_comfyui(repo_urls, clone_dir='/tmp/code/sd/ComfyUI/custom_nodes'):
    os.makedirs(clone_dir, exist_ok=True, mode=0o755)
    for repo_url in repo_urls:
        try:
            repo_name = repo_url.rstrip('/').split('/')[-1]
            clone_path = os.path.join(clone_dir, repo_name)
            if os.path.exists(clone_path):
                print(f'仓库 {repo_name} 已存在, URL: {repo_url}')
                continue
            os.system(f'git clone {repo_url} {clone_path}')
            print(f'已成功克隆仓库: {repo_name}')
        except Exception as e:
            print(f'克隆 {repo_url} 失败: {e}')

clone_repos_sd(git_sd)
clone_repos_comfyui(git_comfyui)
download_openi(openi_datasets)
download_Lora(Lora_url)
download_embeddings(embeddings_url)
download_VAE(VAE_url)
download_mode(mode_url)