Carlos2312 commited on
Commit
52ebbfc
·
verified ·
1 Parent(s): 9cdcc98

Upload Forge.py

Browse files
Files changed (1) hide show
  1. Forge.py +262 -0
Forge.py ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from IPython.display import display, HTML, clear_output, Image
2
+ from ipywidgets import widgets
3
+ from IPython import get_ipython
4
+ from pathlib import Path
5
+ import subprocess, time, os, shlex, json, shutil
6
+ from nenen88 import pull, say, download, clone, tempe
7
+
8
+ repo = f"git clone -q https://github.com/Panchovix/stable-diffusion-webui-reForge forge"
9
+
10
+ home = Path.home()
11
+ src = home / '.gutris1'
12
+ css = src / 'multi.css'
13
+ img = src / 'loading.png'
14
+ mark = src / 'marking.py'
15
+ multi = home / '.conda/multi.py'
16
+
17
+ tmp = Path('/tmp')
18
+ vnv = tmp / 'venv'
19
+
20
+ webui = home / 'forge'
21
+
22
+ os.chdir(home)
23
+
24
+ if webui.exists():
25
+ git_dir = webui / '.git'
26
+ if git_dir.exists():
27
+ os.chdir(webui)
28
+ commit_hash = os.popen('git rev-parse HEAD').read().strip()
29
+
30
+ get_ipython().system("git pull origin main")
31
+ get_ipython().system("git fetch --tags")
32
+
33
+ x = [
34
+ f"https://github.com/gutris1/segsmaker/raw/main/script/controlnet/controlnet.py {webui}/asd",
35
+ f"https://github.com/gutris1/segsmaker/raw/main/script/zrok.py {webui}",
36
+ f"https://github.com/gutris1/segsmaker/raw/main/script/pinggy.py {webui}",
37
+ f"https://github.com/gutris1/segsmaker/raw/main/script/ngrokk.py {webui}",
38
+ f"https://github.com/gutris1/segsmaker/raw/main/script/venv.py {webui}",
39
+ f"https://github.com/gutris1/segsmaker/raw/main/script/multi/segsmaker.py {webui}"]
40
+
41
+ for y in x:
42
+ download(y)
43
+
44
+ else:
45
+ devnull = {"stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
46
+
47
+ loading = widgets.Output()
48
+ sd_setup = widgets.Output()
49
+
50
+ button1 = widgets.Button(description='SD 1.5')
51
+ button2 = widgets.Button(description='SD XL')
52
+ back_button = widgets.Button(description='⬅️')
53
+
54
+ panel = widgets.HBox([button1, back_button, button2], layout=widgets.Layout(
55
+ width='500px',
56
+ height='100%',
57
+ display='flex',
58
+ flex_flow='row',
59
+ align_items='center',
60
+ justify_content='space-between',
61
+ padding='20px'))
62
+
63
+ button1.add_class("buttons")
64
+ button2.add_class("buttons")
65
+ back_button.add_class("back-b")
66
+ panel.add_class("main-panel")
67
+
68
+ def load_css(css):
69
+ with css.open("r") as file:
70
+ data = file.read()
71
+
72
+ display(HTML(f"<style>{data}</style>"))
73
+
74
+ def tmp_cleaning():
75
+ for item in tmp.iterdir():
76
+ if item.is_dir() and item != vnv:
77
+ shutil.rmtree(item)
78
+ elif item.is_file() and item != vnv:
79
+ item.unlink()
80
+
81
+ def venv_install():
82
+ url = 'https://huggingface.co/pantat88/back_up/resolve/main/venv.tar.lz4'
83
+ fn = Path(url).name
84
+
85
+ def check(folder):
86
+ du = get_ipython().getoutput(f'du -s -b {folder}')
87
+ if du:
88
+ size = int(du[0].split()[0])
89
+ return size
90
+ else:
91
+ return 0
92
+
93
+ def venv():
94
+ if vnv.exists() and check(vnv) > 7 * 1024**3:
95
+ return
96
+ else:
97
+ os.chdir(tmp)
98
+ get_ipython().system(f'rm -rf {vnv}')
99
+
100
+ say("<br><b>【{red} Installing VENV{d} 】{red}</b>")
101
+ download(url)
102
+
103
+ get_ipython().system(f'pv {fn} | lz4 -d | tar xf -')
104
+ Path(fn).unlink()
105
+
106
+ get_ipython().system(f'rm -rf {vnv / "bin" / "pip*"}')
107
+ get_ipython().system(f'rm -rf {vnv / "bin" / "python*"}')
108
+ os.system(f'python -m venv {vnv}')
109
+ os.system('/tmp/venv/bin/python3 -m pip install -q --upgrade pip')
110
+
111
+ venv()
112
+ os.chdir(home)
113
+
114
+ def req_list(home, webui):
115
+ return [
116
+ f"rm -rf {home}/tmp {home}/.cache/*",
117
+ f"rm -rf {webui}/models/Stable-diffusion/tmp_ckpt {webui}/models/Lora/tmp_lora {webui}/models/ControlNet",
118
+ f"rm -rf {webui}/models/svd {webui}/models/z123",
119
+ f"mkdir -p {webui}/models/Lora",
120
+ f"mkdir -p {webui}/models/ESRGAN",
121
+ f"ln -vs /tmp {home}/tmp",
122
+ f"ln -vs /tmp/ckpt {webui}/models/Stable-diffusion/tmp_ckpt",
123
+ f"ln -vs /tmp/lora {webui}/models/Lora/tmp_lora",
124
+ f"ln -vs /tmp/controlnet {webui}/models/ControlNet",
125
+ f"ln -vs /tmp/z123 {webui}/models/z123",
126
+ f"ln -vs /tmp/svd {webui}/models/svd"]
127
+
128
+ def sd_clone(home, webui, devnull):
129
+ time.sleep(1)
130
+ pull(f"https://github.com/gutris1/segsmaker forge {webui}")
131
+
132
+ tmp_cleaning()
133
+
134
+ os.chdir(webui)
135
+ req = req_list(home, webui)
136
+
137
+ for lines in req:
138
+ subprocess.run(shlex.split(lines), **devnull)
139
+
140
+ scripts = [
141
+ f"https://github.com/gutris1/segsmaker/raw/main/script/controlnet/controlnet.py {webui}/asd",
142
+ f"https://github.com/gutris1/segsmaker/raw/main/script/zrok.py {webui}",
143
+ f"https://github.com/gutris1/segsmaker/raw/main/script/pinggy.py {webui}",
144
+ f"https://github.com/gutris1/segsmaker/raw/main/script/ngrokk.py {webui}",
145
+ f"https://github.com/gutris1/segsmaker/raw/main/script/venv.py {webui}",
146
+ f"https://github.com/gutris1/segsmaker/raw/main/script/multi/segsmaker.py {webui}"]
147
+
148
+ upscalers = [
149
+ f"https://huggingface.co/pantat88/ui/resolve/main/4x-UltraSharp.pth {webui}/models/ESRGAN",
150
+ f"https://huggingface.co/pantat88/ui/resolve/main/4x-AnimeSharp.pth {webui}/models/ESRGAN",
151
+ f"https://huggingface.co/pantat88/ui/resolve/main/4x_NMKD-Superscale-SP_178000_G.pth {webui}/models/ESRGAN",
152
+ f"https://huggingface.co/pantat88/ui/resolve/main/4x_RealisticRescaler_100000_G.pth {webui}/models/ESRGAN",
153
+ f"https://huggingface.co/pantat88/ui/resolve/main/8x_RealESRGAN.pth {webui}/models/ESRGAN",
154
+ f"https://huggingface.co/pantat88/ui/resolve/main/4x_foolhardy_Remacri.pth {webui}/models/ESRGAN"]
155
+
156
+ line = scripts + upscalers
157
+ for item in line:
158
+ download(item)
159
+
160
+ tempe()
161
+
162
+ def extensions(webui):
163
+ say("<br><b>【{red} Installing Extensions{d} 】{red}</b>")
164
+ os.chdir(webui / "extensions")
165
+ clone(str(webui / "asd/extension.txt"))
166
+
167
+ def sd_1_5(home, webui, devnull):
168
+ sd_clone(home, webui, devnull)
169
+
170
+ extras = [
171
+ f"https://huggingface.co/pantat88/ui/resolve/main/embeddings.zip {webui}",
172
+ f"https://civitai.com/api/download/models/150491 {webui}/embeddings edgQuality.pt",
173
+ f"https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors {webui}/models/VAE"]
174
+
175
+ for items in extras:
176
+ download(items)
177
+
178
+ get_ipython().system(f"unzip -qo {webui}/embeddings.zip -d {webui}/embeddings && rm {webui}/embeddings.zip")
179
+
180
+ extensions(webui)
181
+
182
+ def sd_xl(home, webui, devnull):
183
+ sd_clone(home, webui, devnull)
184
+
185
+ extras = [
186
+ f"https://civitai.com/api/download/models/182974 {webui}/embeddings",
187
+ f"https://civitai.com/api/download/models/159385 {webui}/embeddings",
188
+ f"https://civitai.com/api/download/models/159184 {webui}/embeddings",
189
+ f"https://civitai.com/api/download/models/264491 {webui}/models/VAE XL_VAE_F1.safetensors"]
190
+
191
+ for items in extras:
192
+ download(items)
193
+
194
+ extensions(webui)
195
+
196
+ def marking(path, fn, ui):
197
+ txt = path / fn
198
+ values = {
199
+ 'ui': ui,
200
+ 'launch_args1': '',
201
+ 'launch_args2': '',
202
+ 'zrok_token': '',
203
+ 'ngrok_token': '',
204
+ 'tunnel': ''
205
+ }
206
+
207
+ if not txt.exists():
208
+ with open(txt, 'w') as file:
209
+ json.dump(values, file, indent=4)
210
+
211
+ with open(txt, 'r') as file:
212
+ data = json.load(file)
213
+
214
+ data.update({
215
+ 'ui': ui,
216
+ 'launch_args1': '',
217
+ 'launch_args2': '',
218
+ 'tunnel': ''
219
+ })
220
+
221
+ with open(txt, 'w') as file:
222
+ json.dump(data, file, indent=4)
223
+
224
+ def sd_install(b):
225
+ panel.close()
226
+ clear_output()
227
+
228
+ with loading:
229
+ display(Image(filename=str(img)))
230
+
231
+ with sd_setup:
232
+ sd_setup.clear_output()
233
+ say("<b>【{red} Installing Stable Diffusion Forge{d} 】{red}</b>")
234
+ get_ipython().system(f"{repo}")
235
+
236
+ if b.description == 'SD 1.5':
237
+ sd_1_5(home, webui, devnull)
238
+ elif b.description == 'SD XL':
239
+ sd_xl(home, webui, devnull)
240
+
241
+ venv_install()
242
+
243
+ marking(src, 'marking.json', 'Forge')
244
+ get_ipython().magic(f"run {mark}")
245
+
246
+ with loading:
247
+ loading.clear_output(wait=True)
248
+ say("<b>【{red} Done{d} 】{red}</b>")
249
+
250
+ def go_back(b):
251
+ panel.close()
252
+ clear_output()
253
+
254
+ with sd_setup:
255
+ get_ipython().magic(f"run {multi}")
256
+
257
+ load_css(css)
258
+ display(panel, sd_setup, loading)
259
+
260
+ button1.on_click(sd_install)
261
+ button2.on_click(sd_install)
262
+ back_button.on_click(go_back)