TheLastBen commited on
Commit
eebb9e6
1 Parent(s): c5827ea

Create mainpaperspaceA1111.py

Browse files
Files changed (1) hide show
  1. mainpaperspaceA1111.py +173 -0
mainpaperspaceA1111.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from IPython.display import clear_output
3
+ from subprocess import call, getoutput
4
+ import time
5
+ import sys
6
+ import fileinput
7
+ import ipywidgets as widgets
8
+
9
+
10
+
11
+ def Deps(force_reinstall):
12
+
13
+ if not force_reinstall and os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
14
+ print('Dependencies already installed')
15
+ else:
16
+ print('Installing the dependencies...')
17
+ call("pip install --root-user-action=ignore --no-deps -q accelerate==0.12.0", shell=True, stdout=open('/dev/null', 'w'))
18
+ if not os.path.exists('/usr/local/lib/python3.9/dist-packages/safetensors'):
19
+ os.chdir('/usr/local/lib/python3.9/dist-packages')
20
+ call("rm -r torch torch-1.12.0+cu116.dist-info torchaudio* torchvision* PIL Pillow* transformers* numpy* gdown*", shell=True, stdout=open('/dev/null', 'w'))
21
+ os.chdir('/notebooks')
22
+ if not os.path.exists('/models'):
23
+ call('mkdir /models', shell=True)
24
+ if not os.path.exists('/notebooks/models'):
25
+ call('ln -s /models /notebooks', shell=True)
26
+ if os.path.exists('/deps'):
27
+ call("rm -r /deps", shell=True)
28
+ call('mkdir /deps', shell=True)
29
+ if not os.path.exists('cache'):
30
+ call('mkdir cache', shell=True)
31
+ os.chdir('/deps')
32
+ call('wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/aptdeps.txt', shell=True)
33
+ call('dpkg -i *.deb', shell=True, stdout=open('/dev/null', 'w'))
34
+ call('wget -q https://huggingface.co/TheLastBen/dependencies/resolve/main/pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
35
+ call('tar -C / --zstd -xf pps.tar.zst', shell=True, stdout=open('/dev/null', 'w'))
36
+ call("sed -i 's@~/.cache@/notebooks/cache@' /usr/local/lib/python3.9/dist-packages/transformers/utils/hub.py", shell=True)
37
+ os.chdir('/notebooks')
38
+ call("git clone --depth 1 -q --branch updt https://github.com/TheLastBen/diffusers /diffusers", shell=True, stdout=open('/dev/null', 'w'))
39
+ if not os.path.exists('/notebooks/diffusers'):
40
+ call('ln -s /diffusers /notebooks', shell=True)
41
+ call("rm -r /deps", shell=True)
42
+ os.chdir('/notebooks')
43
+ clear_output()
44
+
45
+ done()
46
+
47
+
48
+ def repo():
49
+
50
+ print('Installing/Updating the repo...')
51
+ os.chdir('/notebooks')
52
+ if not os.path.exists('/notebooks/sd/stablediffusion'):
53
+ call('wget -q -O sd_kg.tar.zst https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_kg.tar.zst', shell=True)
54
+ call('tar --zstd -xf sd_kg.tar.zst', shell=True)
55
+ call('rm sd_kg.tar.zst', shell=True)
56
+
57
+ os.chdir('/notebooks/sd')
58
+ if not os.path.exists('stable-diffusion-webui'):
59
+ call('git clone -q --depth 1 --branch master https://github.com/AUTOMATIC1111/stable-diffusion-webui', shell=True)
60
+
61
+ os.chdir('/notebooks/sd/stable-diffusion-webui/')
62
+ call('git reset --hard', shell=True, stdout=open('/dev/null', 'w'))
63
+ print('')
64
+ call('git pull', shell=True, stdout=open('/dev/null', 'w'))
65
+ os.chdir('/notebooks')
66
+ clear_output()
67
+ done()
68
+
69
+
70
+ def mdl(Original_Model_Version, Path_to_MODEL, MODEL_LINK, safetensors, Temporary_Storage):
71
+ import gdown
72
+ if Path_to_MODEL !='':
73
+ if os.path.exists(str(Path_to_MODEL)):
74
+ print('Using the trained model.')
75
+ model=Path_to_MODEL
76
+ else:
77
+ print('Wrong path, check that the path to the model is correct')
78
+
79
+ elif MODEL_LINK != "":
80
+ modelname="model.safetensors" if safetensors else "model.ckpt"
81
+ if Temporary_Storage:
82
+ model=f'/models/{modelname}'
83
+ else:
84
+ model=f'/notebooks/sd/stable-diffusion-webui/models/Stable-diffusion/{modelname}'
85
+ if os.path.exists(model):
86
+ call('rm '+model, shell=True)
87
+ gdown.download(url=MODEL_LINK, output=model, quiet=False, fuzzy=True)
88
+
89
+ if os.path.exists(model) and os.path.getsize(model) > 1810671599:
90
+ clear_output()
91
+ print('Model downloaded, using the trained model.')
92
+ else:
93
+ print('Wrong link, check that the link is valid')
94
+
95
+ else:
96
+ if Original_Model_Version == "v1.5":
97
+ model="/datasets/stable-diffusion-classic/v1-5-pruned-emaonly.ckpt"
98
+ print('Using the original V1.5 model')
99
+ elif Original_Model_Version == "v2-512":
100
+ model="dataset"
101
+ print('Using the original V2-512 model')
102
+ elif Original_Model_Version == "v2-768":
103
+ model="dataset"
104
+ print('Using the original V2-768 model')
105
+ else:
106
+ model=""
107
+ print('Wrong model version')
108
+
109
+ return model
110
+
111
+
112
+ def sd(User, Password, Use_localtunnel):
113
+
114
+ auth=f"--gradio-auth {User}:{Password}"
115
+ if User =="" or Password=="":
116
+ auth=""
117
+
118
+ if not os.path.exists('/usr/lib/node_modules/localtunnel'):
119
+ call('npm install -g localtunnel --silent', shell=True, stdout=open('/dev/null', 'w'))
120
+ clear_output()
121
+
122
+
123
+ share=''
124
+ call('wget -q -O /usr/local/lib/python3.9/dist-packages/gradio/blocks.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/blocks.py', shell=True)
125
+
126
+ if not Use_localtunnel:
127
+ share='--share'
128
+
129
+ else:
130
+ share=''
131
+ os.chdir('/notebooks')
132
+ call('nohup lt --port 7860 > srv.txt 2>&1 &', shell=True)
133
+ time.sleep(2)
134
+ call("grep -o 'https[^ ]*' /notebooks/srv.txt >srvr.txt", shell=True)
135
+ time.sleep(2)
136
+ srv= getoutput('cat /notebooks/srvr.txt')
137
+
138
+ for line in fileinput.input('/usr/local/lib/python3.9/dist-packages/gradio/blocks.py', inplace=True):
139
+ if line.strip().startswith('self.server_name ='):
140
+ line = f' self.server_name = "{srv[8:]}"\n'
141
+ if line.strip().startswith('self.server_port ='):
142
+ line = ' self.server_port = 443\n'
143
+ if line.strip().startswith('self.protocol = "https"'):
144
+ line = ' self.protocol = "https"\n'
145
+ if line.strip().startswith('if self.local_url.startswith("https") or self.is_colab'):
146
+ line = ''
147
+ if line.strip().startswith('else "http"'):
148
+ line = ''
149
+ sys.stdout.write(line)
150
+
151
+ call('rm /notebooks/srv.txt', shell=True)
152
+ call('rm /notebooks/srvr.txt', shell=True)
153
+
154
+ os.chdir('/notebooks/sd/stable-diffusion-webui/modules')
155
+ call('wget -q -O paths.py https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/AUTOMATIC1111_files/paths.py', shell=True)
156
+ call("sed -i 's@/content/gdrive/MyDrive/sd/stablediffusion@/notebooks/sd/stablediffusion@' /notebooks/sd/stable-diffusion-webui/modules/paths.py", shell=True)
157
+ os.chdir('/notebooks/sd/stable-diffusion-webui')
158
+ clear_output()
159
+
160
+ configf="--disable-console-progressbars --no-half-vae --disable-safe-unpickle --api --xformers --medvram --skip-version-check"
161
+
162
+ return configf, auth, share
163
+
164
+
165
+ def done():
166
+ done = widgets.Button(
167
+ description='Done!',
168
+ disabled=True,
169
+ button_style='success',
170
+ tooltip='',
171
+ icon='check'
172
+ )
173
+ display(done)